1

I don't know if my query is correct. I want to group all the dates as one, but my query shows a null.

SELECT CustomerID,Cashier, OrderDate,SUM(Total) as totalprice
FROM SalesandReports
GROUP BY CustomerID, Cashier, OrderDate WITH ROLLUP

This is the output of my query:

This is the output of my query

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Makmak
  • 19
  • 2

4 Answers4

1

Try your original query without the WITH ROLLUP on the end - it sounds like you don't actually want rollup, just grouping

DancingFool
  • 1,247
  • 1
  • 8
  • 10
0

Maybe this can work for you:

SELECT
    CustomerID,Cashier,
    OrderDate,
    SUM(Total) as totalprice
FROM
    SalesandReports
GROUP BY
    OrderDate,
    CustomerID,
    Cashier
WITH ROLLUP

By making Orderdate first, the group by will be done first according to it and then on customerid and cashier.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rajan Mishra
  • 1,178
  • 2
  • 14
  • 30
0

You can do a trick here:

SELECT
   max(CustomerID) as CustomerId,
   max(Cashier) as Cashier,
   coalsce(OrderDate,'Other Dates'),
   SUM(Total) as totalprice
FROM
   SalesandReports
GROUP BY
   OrderDate WITH ROLLUP

By this GROUP BY, it will only be done by OrderDate.

Reference: the answers from Stack Overflow question Select multiple columns from a table, but group by one.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amit chauhan
  • 540
  • 9
  • 22
0

Try this my friend,

SELECT 
CustomerID
,Cashier
, OrderDate
,SUM(ISNULL(totalprice,0)) as totalprice
FROM SalesandReports
WHERE  OrderDate is not NULL  
GROUP BY CustomerID, Cashier, OrderDate 
Aakash Singh
  • 1,032
  • 8
  • 27