I have below table information
Batch_ID | Items | Time | User |
---|---|---|---|
Batch_1 | 50 | 0.70 | Abc |
Batch_2 | 40 | 0.70 | Abc |
Batch_2 | 60 | 0.70 | Abc |
I want SQL code where I can calculate the SUM of Items and the SUM of Time but I want to DISTINCT the time value of the same Batch_ID.
The result that I should get is 150 for Items and 1.40 for time.
The results I got for the time value is 0.70.
Below example for the results I want to show:
User | Sum(Items) | Sum(Time) |
---|---|---|
Abc | 150 | 1.40 |
SELECT user_id, SUM(items), SUM(DISTINCT time) FROM items_p GROUP BY dayweek,user_id
2
Answers
Here is the query,
res1 query takes the distinct TIME from table grouped by batch_id,user and then returns both batch_id and user
res2 query takes sum of items grouped by user and then returns both user and sum of items.
Finally do a inner join for both using user.
I hope this will help ^^