I have a 3 fields ( ID, Price, Date )
and it looks like this..
|ID| |Price| | Date |
--------------------------------------------
|001| |150.00| | 2007-01-01 11:48:18.000 |
|002| |150.00| | 2007-01-01 15:57:19.000 |
|003| |150.00| | 2007-01-02 13:26:12.000 |
|004| |150.00| | 2007-01-03 10:31:14.000 |
etc etc
and I need to display the TOTAL AMOUNT of sales for EACH DAY for a certain period of time.
So when I put January 1 to January 6…
it should be
| Days | Total Sales |
-------------------------------
| January 1 | --some amount |
| January 2 | --some amount |
| January 3 | --some amount |
| January 4 | --some amount |
| January 5 | --some amount |
| January 6 | --some amount |
I just cant figure it out and Im stuck with this code 🙂 …
DECLARE @StartDate dateTime,@EndDate dateTime, @TotalSales integer
SET @StartDate = '2007-01-02 11:41:19.000'
SET @EndDate = '2007-01-02 11:46:06.000'
SET @TotalSales = 0
while ( @StartDate = '2007-01-02 11:41:19.000' )
BEGIN
--Some codes
END
thanks 🙂
You don’t need a loop, use set operations whenever possible:
SQL-Fiddle Demo