| Author |
Topic |
|
dhinasql
Posting Yak Master
195 Posts |
Posted - 2009-07-04 : 06:57:28
|
| Team,I want to display the monthly order datails, please find the sample table with valueTable Name : tblORDERSOrderId Price OrderDate1 100 2009-06-02 02:10:11.0012 200 2009-06-02 03:01:00.0003 500 2009-06-03 03:05:04.002I want to display the July month order details,SO i will pass month and Year in where condition,So for example i want to Display the July month record means i have to display all the 31 Days ( 1 to 31 ) and display the total sales for the each date, If the table does not contain any order for the date it should display the Total sales as 0please find my expected ResultDATE TOTALSALES 1 02 3003 5004 05 06 0.......31 0I am using the below Query but it is displaying only for the Available date in the orders tableDATE TOTALSALES 2 3003 500select day(orderdate) as DATE, sum(Price) as TotalSales from rr_order where month(orderdate)=6 and year(orderdate)=2009 group by day(orderdate) Please help me to get the expected outputthanks in advance |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-04 : 07:13:18
|
if you have a calendar table, use it. If not, use F_TABLE_DATE as a replacementdeclare @year int, @month int, @mth_st datetime, @mth_en datetimeselect @mth_st = dateadd(month, @month - 1, dateadd(year, @year - 1900, 0))select @mth_en = dateadd(month, @month, dateadd(year, @year - 1900, -1))select d.DAY_OF_MONTH, TotalSales = sum(isnull(o.Price, 0))from F_TABLE_DATE d left join tblORDERS o ON o.OrderDate >= d.[DATE] AND o.OrderDate < dateadd(day, 1, d.[DATE])group by d.DAY_OF_MONTH KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
dhinasql
Posting Yak Master
195 Posts |
Posted - 2009-07-04 : 07:39:35
|
| Khtan,Thank you so much for your Reply,I am working on SQL SERVER 2000I dont have Order Table and F_Table_Date ,I have tblOrders Table only, please let me know using this table alone can i able to get my expected output,Please guide me and help to get my expected outputThanks in Advance |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-07-05 : 02:53:05
|
| [code]DECLARE @Month int,@Year int,@StartDate datetime,@EndDate datetimeSELECT @Month=6,--passed on month value@Year=2009--passed on year valueSELECT @StartDate=DATEADD(mm,@Month-1,DATEADD(yy,@Year-1900,0)),@EndDate=DATEADD(mm,@Month,DATEADD(yy,@Year-1900,0))-1SELECT cal.DateVal AS [DATE],COALESCE(ord.TotalPrice,0) AS [TOTALSALES]FROM(SELECT DATEADD(dd,number,@StartDate) AS DateValFROM master..spt_valuesWHERE type='p'AND DATEADD(dd,number,@StartDate)<=@EndDate)calLEFT JOIN (SELECT DATEADD(dd,DATEDIFF(dd,0,OrderDate),0) AS DateVal,SUM(Price) AS TotalPriceFROM tblORDERS GROUP BY DATEADD(dd,DATEDIFF(dd,0,OrderDate),0))ordON ord.DateVal=cal.DateVal[/code] |
 |
|
|
|
|
|