0
在我的銷售表中,列出的是一個物業中特定租戶的每日銷售額。SQL查詢到每月逐項日銷售額
使用此代碼,我能夠列出或選擇租客的每日銷售額,根據Property的分組進行總結。
SELECT date , location , SUM(a.gsc) Sales
FROM SalesTable
WHERE (date BETWEEN '01/01/2015' AND '12/31/2015')
and (location = 'Property1')
GROUP BY location , date
ORDER BY location, date
而結果是這樣的
Date Location Sales
01/01/2015 Property1 10,000
01/02/2015 Property1 20,000
.
.
.
12/31/2015 Property1 15,000
我要完成作爲最終輸出的就是讓有天日期的第一行和月如的結果以表格形式列。 請參閱示例輸出以獲得進一步說明。
Date January February
Date Day Sales Date Day Sales
1 1-Jan Fri 10,000 1-Feb Mon 9,000
2 2-Jan Sat 20,000 2-Feb Tue 10,000
3 . . . . . .
4 . . . . . .
5 . . . . . .
6
7
8
.
.
.
.
.
31
這是我試過
SELECT date , location , SUM(a.gsc) Sales
INTO #TempTable1
FROM SalesTable
WHERE (date BETWEEN '01/01/2015' AND '12/31/2015')
and (location = 'Property1')
GROUP BY location , date
ORDER BY location, date
SELECT months.number, tenants.locationd , tenants.location
,(case when (t.DATE) = 1 then t.gsc end) as 'January'
,(case when (t.DATE) = 2 then t.gsc end)) as 'February'
(case when year(t.DATE) = 3 then t.gsc end) as 'March'
FROM
(
SELECT Number
FROM master..spt_values
WHERE Type = 'P' and Number between 1 and 31
) months
CROSS JOIN
( --12 Month Name will be cross joined sa selected tenant/s
SELECT DISTINCT locationd , location
FROM #AMRDSR1
) tenants
LEFT JOIN #AMRDSR1 t
ON months.number = datepart(day,t.date) and tenants.location = t.location
GROUP BY months.number, tenants.locationd , tenants.location , t.date, t.gsc
ORDER BY tenants.locationd , datepart(day,t.date)
我已建議使用支點,但是那是相當新的給我,似乎是很複雜的,如果有辦法只需使用TSQL就好了。
希望得到您的專家建議。
謝謝!
會試試這個。謝謝! @squirrel – rickyProgrammer
工作..謝謝! – rickyProgrammer
你是最受歡迎的 – Squirrel