0
私のSales Tableには、1つのProperty内の特定のテナントの日々の売上高が表示されます。SQLクエリから月間毎月売上高
このコードを使用して、プロパティのグループ化に基づいて集計されたテナントの日次販売を一覧表示または選択することができました。
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だけを使用してください。
あなたの専門家の助言が必要です。
ありがとうございました!
これを試します。ありがとうございました! @quirrel – rickyProgrammer
working ..ありがとう! – rickyProgrammer
あなたは大歓迎です – Squirrel