0

Below is my SQL query which currently outputs this result:

enter image description here

SQL query:

SELECT 
    COUNT(*) AS noofuser,
    [DateTime], [Date], [hour],
    [Company]
FROM 
    LMT2_lmutilserverLicenseuser  

But, I need to add extra row called 'Total' based on a column 'DateTime' as shown in the below table.

enter image description here

6
  • total based on what?? Commented Dec 4, 2018 at 6:20
  • What are the types of your date and datetime columns? Commented Dec 4, 2018 at 6:22
  • @nikhilsugandh total of 'Count' Column based on 'DateTime' column Commented Dec 4, 2018 at 6:28
  • @TimBiegeleisen type of Date column is Date and 'DateTime' column is Varchar Commented Dec 4, 2018 at 6:29
  • @pankajbawdane, lookup for mysql rollup operator. Commented Dec 4, 2018 at 7:30

1 Answer 1

1

Combining the query with a group by should work, haven't been able to run but this should give the result. You can insert the result of your query in temp table (thus count(*) gets its own column) and then just do another group by on the temp table

SELECT cnt, date_time, date , hr ,company 
FROM LMT2_lmutilserverLicenseuserTemp 
UNION 
SELECT SUM(cnt), date_time, MAX(date) , MIN(hr) , 'tot' AS company 
FROM LMT2_lmutilserverLicenseuserTemp
GROUP BY date_time
ORDER BY date_time
Sign up to request clarification or add additional context in comments.

2 Comments

Error:Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
Yikes....yes you cannot do sum (count(*)) as the exception says, but the logic is still same, see updated ans

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.