Milena Lengauer
Milena Lengauer

Reputation: 23

SQL: Use Group BY two times

My table looks like this:

INSERT_DATE | WorkOrder_ID | Status

It is a log-table where every status change for one WorkOrder is inserted. So one WorkOrder_ID occurs multiple times with different statuses. What i want to do is: to select the 'Count' of Work-Orders per day and per WorkOrder_ID. So I need two group bys: one for the day and one for the WorkOrder_ID

Any idea? Thank you

Upvotes: 0

Views: 240

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133370

For workorder in a day you could use

 select INSERT_DATE , count(distinct WorkOrder_ID ) 
 from my_table 
 group by INSERT_DATE 

and for WorkOrder_ID and date

select INSERT_DATE ,  WorkOrder_ID , count(*) 
 from my_table 
 group by INSERT_DATE , WorkOrder_ID 

and for all WorkOrder_ID

 select  WorkOrder_ID , count(*) 
 from my_table 
 group by WorkOrder_ID 

Upvotes: 2

Related Questions