Group by with 3 columns - php

I have 3 columns to get data.
1- amount, 2- transaction_id and order_id
Made group by (transaction_id), sum(amount) and get all order_id.
id order_id transaction_id amount
1 1 3333 698.00
1 2 3333 758.00
1 3 3333 560.00
1 4 3333 360.00
1 5 5555 225.00
1 6 5555 102.00
1 7 2222 36.00
See image
TABLE
SELECT sum(amount) as sum, transaction_id, order_id FROM table WHERE ID='1' GROUP BY transaction_id
Got back:
The sum is OK.
The transaction id is OK.
The order id got back just the first of each transaction id.
I what to see order id like this transaction id 3333 - order id 1,2,3,4
Tried explode() but did not work
Help needed.

Assuming the db is mysql (please tag your db in the question), use group_concat for this like:
SELECT sum(amouNt) as sum, transaction_id, group_concat(order_id) orders FROM table WHERE ID='1' GROUP BY transaction_id
This will return orders column with comma separated values like 1,2,3,4

Related

Order by a columns count where another column has a static value

I have a table which stores user items, the two key columns which I would like to use in this query are user_id and item_id. The id field in the example is not needed but just added to show these aren't the only two columns in the table.
----------------------
id user_id item_id
----------------------
1 1 324
2 1 324
3 3 324
4 2 230
5 4 324
The query which I would like to construct should return the top 10 users who have the most items with a specific item id.
So for example if I wanted to run the query against the item ID 324 I should get the following result.
-------------------
user_id item_count
-------------------
1 2
3 1
4 1
2 0
try this
select user_id , count(*) as item_count from table
where item_id = 324 group by user_id order by item_count desc limit 10
limit 10 will show you the top 10 users and order by desc sort from high to low.
However, the above query will not give you the 0 count as per your question. If you really want the zero count you can try this: (assuming your table name is userlist)
SELECT distinct user_id,
(select
count(*) from `userlist`
where user_id=u.user_id and item_id=324
) as item_count FROM `userlist` u
order by item_count desc
I couldn't create the database in my local, but I think this will do the trick
SELECT user_id, COUNT(item_id) as item_count
FROM TABLE_NAME
WHERE item_id = 324
GROUP BY item_id
ORDER BY item_count;

IDs from count query

I have table Orders:
O_Id OrderDate OrderPrice Customer
1 2008/11/12 1000 Hansen
2 2008/10/23 1600 Nilsen
3 2008/09/02 700 Hansen
4 2008/09/03 300 Hansen
5 2008/08/30 2000 Jensen
6 2008/10/04 100 Nilsen
and query:
SELECT COUNT(Customer) AS CustomerNilsen FROM Orders
WHERE Customer='Nilsen'
but is possible add to this results IDs results?
I would like receive
count: 2
and
ids: 2 and 6
GROUP_CONCAT may help:
SELECT
COUNT(Customer) AS CustomerNilsen,
GROUP_CONCAT(O_Id) as IDS
FROM
Orders
WHERE
Customer='Nilsen'
Better to just fetch all the IDs and then use the appropriate row count function:
SELECT `O_Id` FROM `Orders`
WHERE `Customer` = 'Nilsen'
I don't really understand your question, but from what I can tell you just want to SELECT the o_id column in your query:
SELECT COUNT(Customer) AS CustomerNilsen, O_Id AS OrderID FROM Orders WHERE Customer='Nilsen'
You might check this
SELECT Customer,
COUNT(Customer) AS CustomerNilsen
FROM Orders
WHERE Customer = 'Nilsen'
GROUP BY Customer
HAVING CustomerNilsen = 1
Try this
SELECT O1.O_Id AS OrderID,Count(O1.O_Id) AS OrderCount FROM Orders O1
INNER JOIN Orders O2 ON O1.Customer = O2.Customer
WHERE O1.Customer='Nilsen'
GROUP BY O1.Customer,O1.O_Id

How can I get max sum in table with mysql

I have an SQL database with a table like this:
ObjID Rating
123 1
333 0
123 0
123 1
567 1
An Ojbect ID can show up in the table multiple times with different ratings, I need a way to select a "distinct" ObjID check all of its ratings and compare that to all other "distinct" ObjIDs and compare those ratings. In the end I should have the 1st - 3rd ObjIDs with the over all highest ratings, in this example 123, 567 and 333 would be printed out.
So the rating of each object is the total sum of all ratings for that object in the table?
SELECT ObjID, SUM(Rating) AS TotalRating
FROM ObjectTable
GROUP BY ObjID
ORDER BY SUM(Rating) DESC
LIMIT 3

Grab most recent values from table using php and mysql

Im having the below sql table structure and im trying to grab values from 2 tables using codeigniter.
table: salesman
id sales_name ref_id
1 kevin 174
2 mike 574
3 nick 777
table: sales_report
id salesman_id product purchased_date dispatched
1 2 BF0214 04-November-2011 Yes
2 2 CF0474 09-November-2011 No
3 2 BF0111 10-November-2011 No
4 3 BF0714 15-November-2011 Yes
5 3 BF0435 15-November-2011 Yes
6 2 BF0335 18-November-2011 Yes
7 1 BF0714 22-November-2011 Yes
8 1 BF0335 25-November-2011 Yes
im passing the salesman_id to the model to grab and display the values in my view.
my html table is as below
Ref ID | Salesman Name | Last product Sold | Sold Date | Dispatched Status
the problem that im having how can query to get the sales_name and ref_id from the salesman table and get the most recent product name, dispatched and purchase_date from from the sales_report table?
eg:
Ref ID | Salesman Name | Last product Sold | Sold Date | Dispatched Status
174 kevin BF0335 25-November-2011 Yes
574 mike BF0335 18-November-2011 Yes
777 nick BF0435 15-November-2011 Yes
Use
GROUP BY
ORDER BY
for getting this done
You need to use SELECT values from the sales_report, LEFT JOIN salesman to get in the sales_name and ref_id, ORDER BY the purchased_date (DESC to get the most recent one first instead of the oldest first), and GROUP BY the salesman.id to get it to only return one row per salesman.
SELECT * FROM sales_report LEFT JOIN salesman ON sales_report.salesman_id = salesman.id ORDER BY purchased_date DESC GROUP BY salesman.id
You should find last product by two criteras: its sold date and by its id; because some products may be sold in one day. So, try this one -
SELECT s.ref_id, s.sales_name, sr.product, sr.purchased_date, sr.dispatched FROM salesman s
JOIN (
SELECT salesman_id, product, purchased_date, dispatched FROM (
SELECT salesman_id, product, purchased_date, dispatched, IF(#salesman_id = salesman_id, #i := #i + 1, #i := 1) n, #salesman_id := salesman_id
FROM sales_report, (SELECT #i:= 0, #salesman_id = NULL) vars
ORDER BY salesman_id ASC, purchased_date DESC, id DESC) t
WHERE t.n = 1
) sr
ON s.id = sr.salesman_id;

problem with result order on distinct select query

i have a table like this
id name date group_id
1 n1 1 1
2 n2 1 1
3 n4 2 2
4 n5 2 2
i want ton write a query to return the group_id without duplicate ordered by date ASC
$query = " SELECT DISTINCT group_id FROM table ORDER BY date ASC";
this query will return 2 , 1 but this query is just going to consider date of the first row of each group_id to order the results
like if i have table like this
id name price date group_id
1 n1 2300 1 1
2 n2 3000 3 1
3 n4 4000 2 2
4 n5 2000 2 2
second row with with '1' as group_id has the biggest date so i should get 1,2 as result but query doesn't care about second row with the '1' group_id and still return 2,1
it only cares about the first row of each id for ordering the results
hopefully there is a easy way to solve this and i dont need to do something wird like putting everything in the 2d array and order that then deleting duplicates
Try this
select group_id,max(date) as SortDate
from table
group by group_id
order by SortDate
If I understand your problem, it sounds like you need to group by the date first.
SELECT group_id
FROM (
SELECT
group_id,
min(date) as min_date
FROM table
GROUP BY group_id
) as t
ORDER BY t.min_date;

Categories