SQL Multiple GROUP BY - mysql

I can't figure out the mysql query to extract the data I want from this table "timeEntry":
hours creationDate userId clientId projectId taskId
20 2012-02-18 1 1 1 1
40 2012-02-18 1 1 1 1
30 2012-02-21 2 1 1 1
20 2012-02-22 2 1 1 2
30 2012-02-22 2 1 1 2
80 2012-02-23 1 2 2 2
10 2012-02-23 3 2 2 2
15 2012-02-23 1 2 2 3
40 2012-02-23 1 2 4 1
And I would like to have this kind of result as another table, or csv/excel file or php array (where totalHours is the sum of the hours for a userId) for a given period of time, let say (between 2012-02-01 and 2012-02-25):
clientId projectId taskId userId totalHours
1 1 1 1 60
2 30
2 2 50
2 2 2 1 80
3 10
3 1 15
4 1 1 40
I guess I have to use multiple group by, I tried something like:
SELECT clientId, projectId, taskId, userId, sum(hours)
FROM `timeEntry`
WHERE date_creation >= "2012-02-01"
AND date_creation <= "2012-02-25"
GROUP BY clientId, projectId, taskId, userId;
But didn't work...
Thanks in advance.

Where needs to go before Group by.
If you want to filter by date before grouping, use the where clause you have but moved before the group by.
If you'd instead like to filter entire groups in or out, use a having:
...
Group by ...
Having max(date) <= someValue and min(date) >= someValue

SELECT clientId, projectId, taskId, userId sum(Hours) total_hours
FROM timeEntry
GROUP BY clientID, ProjectID, TaskID, userID;
You can use [with Rollup][1] to generate sub-aggregrates. if you want

Here is a simple solution. You need to group by all necessary fields
SELECT
t.clientId,
t.projectId,
t.taskId,
t.userId,
SUM(t.hours) AS Total
FROM test AS t
GROUP BY t.creationDate , t.userId , t.clientId , t.projectId , t.taskId
Fiddle Demo
OUTPUT:
clientId projectId taskId userId Total
____________________________________________________________
1 1 1 1 60
1 1 1 2 30
1 1 2 2 50
2 2 2 1 80
2 2 3 1 15
2 4 1 1 40
2 2 2 3 10

Related

Need help to group the available order details table to arrive at the distribution of count(order_line_item) , Qty and count(Qty)

I have a order table with the following information
Order ID, Product ID, Quantity ordered
OID PID Qty
1 10 1
1 20 2
2 10 2
2 40 4
3 50 1
3 20 3
4 30 1
4 90 2
4 90 5
5 10 2
5 20 2
5 70 5
5 60 1
6 80 2
If I run the following query
select `Qty`, count(`Qty`)
from `table`
group by `Qty`
I get the distribution of quantities in the table, which is
Qty count(`Qty`)
1 4
2 6
3 1
4 1
5 2
I want to find the distribution of quantity at order_line_item level too
That is how many orders which have one line item, had items with 1 quantity, 2 quantity and so one, something like
Count(Order_line_item) Qty Count(Qty)
1 2 1
2 1 2
2 2 2
2 3 1
2 4 1
3 1 1
3 2 1
3 5 1
4 1 1
4 2 2
4 5 1
What modification should i make in the above query to achieve this
Try this query
SELECT count_order_line_items, `Qty`, count(*)
FROM (
SELECT count(*) over (partition by `OID`) as count_order_line_items,
`Qty`
FROM Table1
) x
GROUP BY count_order_line_items, `Qty`
Demo: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=07dfb27a7d434eca1f0b9641aadd53c8
If your mysql version is less than 8 then try this one
SELECT count_order_line_items, `Qty`, count(*)
FROM Table1 t1
JOIN (
SELECT `OID`, count(*) as count_order_line_items
FROM Table1
GROUP BY `OID`
) t2 ON t1.`OID` = t2.`OID`
GROUP BY count_order_line_items, `Qty`
Demo: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=28c291a4693f31029a103f5c41a97d77

Merging Multiple Queries - Same Table

I have 1 table called itemmovement : It has Item Id , Quantity In , Quantity Out , Invoice Id, Date. I need to make in one query to show how many pieces are sold and beside the sold column there will be the current on hand quantity .
itemmovement
Id itemid qtyin qtyout invid purchasereturnid date
1 1 10 2019-01-04
2 2 8 2019-01-06
3 2 2 1 2019-01-08
4 1 3 2 2019-01-12
5 2 1 2019-02-04
6 3 4 2019-03-04
7 1 1 3 2019-04-04
8 1 1 1 2019-04-14
9 3 1 2 2019-04-24
I need the query to show this result
Id itemid Sold Quantity OnHandQty
1 1 4 5
2 2 2 7
3 3 0 3
I'm Trying to use this query but not working
SELECT *
FROM
(SELECT itmv.itemid,
sum(itmv.qtyout)-sum(itmv.qtyin)
FROM itemmovement itmv
WHERE (itmv.systemdate BETWEEN '2019-01-01' AND '2019-06-01')
AND invid>0
GROUP BY itmv.itemid) AS result1,
(SELECT sum(itmv2.qtyin)-sum(itmv2.qtyout)
FROM itemmovement itmv2
WHERE itmv.itemid=itmv2.itemid
GROUP BY itmv2.itemid) AS result2
ORDER BY sum(itmv.qtyin)-sum(itmv.qtyout)
I'm getting :
Unknown column 'itmv.itemid' in 'where clause it for this syntax :
where itmv.itemid = itmv2.itemid
Here's your query.
select itemid
, sum(case when COALESCE(invid,0) > 0 then qtyout else 0 end) as Sold_Qantity
, sum(qtyin)-sum(qtyout) as OnHandQty
from itemmovement
group by itemid

How to sort & group by sql query?

I have Data
id Date CatId itemid itemname price
1 10/5/2019 1 1 ABC 20
2 10/5/2019 1 2 XYZ 30
3 10/5/2019 2 1 ABC 20
4 10/5/2019 3 1 ABC 20
5 11/5/2019 1 2 XYZ 30
6 11/5/2019 2 1 ABC 20
7 11/5/2019 2 3 PQR 40
8 12/5/2019 3 1 ABC 20
9 12/5/2019 3 2 XYZ 30
10 12/5/2019 1 2 XYZ 30
11 12/5/2019 2 1 ABC 20
expected result data
date CatId toal
10/5/2019 1 50
10/5/2019 2 20
10/5/2019 3 20
11/5/2019 1 30
11/5/2019 2 60
12/5/2019 1 30
12/5/2019 2 20
12/5/2019 3 50
I want result order by date and after sum of price group by catid,
I have tried multiple query applied but get exact solution. I have spent so much time.
I have tried bellow queries
SELECT * FROM (SELECT * FROM `table` ORDER BY `date` DESC) as tbl
GROUP BY tbl.`catid`
SELECT *
FROM `table`
GROUP BY `date` ORDER BY `date` DESC
SELECT *
FROM (
SELECT * FROM `table`
ORDER BY `date` DESC
) AS sub
GROUP BY `catid` ORDER BY `date` DESC
the columns you can use in order by and group by can be different
You could use group by and sum directly
select date, catid, sum(price)
from my_table
group by date, catid
order by date, sum(price), catid

MySQL Group By 2 adjacent Dates

I have one table named viewlist as follows
Id article_id viewdate
--------------------------------------------------
1 1 2015-07-01
2 1 2015-07-01
3 1 2015-07-01
4 2 2015-07-01
5 2 2015-07-01
6 1 2015-07-02
7 2 2015-07-02
8 1 2015-07-03
9 2 2015-07-03
10 1 2015-07-05
11 1 2015-07-05
----------------------------------------------------
i need to write a MySQL query to get count and article_id by grouping adjacent viewdate field
wanted result as follows
article_id count date_period
-----------------------------------------------------------------
1 4 2015-07-01 - 2015-07-02
2 3 2015-07-01 - 2015-07-02
1 1 2015-07-03 - 2015-07-04
2 1 2015-07-03 - 2015-07-04
1 2 2015-07-05 - 2015-07-06
Is there any simple query to get this type of result?
you may use something on these lines
SELECT v.article_id, COUNT(*), CONCAT(b.dt, ' - ', b.dt2) as 'date_period'
FROM viewlist v INNER JOIN
(SELECT a.dt, DATE_ADD(a.dt, INTERVAL 1 DAY) AS dt2 FROM (SELECT DISTINCT(viewdate) AS dt FROM viewlist WHERE (DAY(viewdate) MOD 2) = 1) a) b ON v.viewdate BETWEEN b.dt AND b.dt2
GROUP BY 1, 3
ORDER BY b.dt

HIVE how to limit number of entries in group

I'm learning HIVE these days and meet some problems...
I have a table called SAMPLE:
USER_ID PRODUCT_ID NUMBER
1 3 20
1 4 30
1 2 25
1 6 50
1 5 40
2 1 10
2 3 15
2 2 40
2 5 30
2 3 35
How can I use HIVE to group table by user_id and in each group order the records by DESC order of NUMBER and in each group I want to keep up to 3 records.
The result I want to have is like:
USER_ID PRODUCT_ID NUMBER(optional column)
1 6 50
1 5 40
1 4 30
2 2 40
2 3 35
2 5 30
or
USER_ID PRODUCT_IDs
1 [6,5,4]
2 [2,3,5]
Could someone help me ?..
Thanks very much!!!!!!!!!!!!!!!!
try this,
select user_id,product_id,number
from(
select user_id,product_id,number, ROW_NUMBER() over (Partition BY user_id) as RNUM
from (
select user_id, number,product_id
from SAMPLE
order by number desc
) t) t2
where RNUM <=3
output
1 6 50
1 5 40
1 4 30
2 2 40
2 3 35
2 5 30
hive version should be 0.11 or greater, may I know if your version is lower