これが私が現在持っているもので、createdDateで何もフィルタリングしません。
SELECT [employeeID]
,employeeName
,isnull((SELECT SUM(case when clientID != 10 then timeSpent else 0 end)),0) as 'billable'
,isnull((SELECT SUM(case when clientID = 10 then timeSpent else 0 end)),0) as 'nonBillable'
FROM [myDB].[dbo].[myTable]
group by employeeID,employeeName
出力:
employeeID employeeName billable nonbillable
---------- ------------ -------- -----------
1 tom 5230 2302
2 dick 25 8439
3 harry 2430 9433
正確な出力は同様ですが、日付範囲内の値の合計のみが含まれます。
employeeID employeeName billable nonbillable
---------- ------------ -------- -----------
1 tom 35 5
2 dick 25 15
3 harry 2 48
[myTable]には次のフィールドがあります:employeeID、employeeName、clientID、timeSpent、createdDate
次のような各selectsumステートメントにフィルターを追加できるようにする必要があります。WHEREcreatedDateBETWEEN( '2012-10-01')AND( '2012-10-07')
次のバリエーションはすべて、SQLエラーまたは不正確な出力のいずれかで失敗します。
SELECT [employeeID]
,employeeName
,isnull((SELECT SUM(case when clientID != 10 then timeSpent else 0 end)),0) as 'billable'
,isnull((SELECT SUM(case when clientID = 10 then timeSpent else 0 end)),0) as 'nonBillable'
FROM [myDB].[dbo].[myTable]
WHERE createdDate BETWEEN ('2012-10-01') AND ('2012-10-07')
group by employeeID,employeeName
SELECT DISTINCT [employeeID]
,employeeName
,isnull((SELECT SUM(case when clientID != 10 then timeSpent else 0 end)),0) as 'billable'
,isnull((SELECT SUM(case when clientID = 10 then timeSpent else 0 end)),0) as 'nonBillable'
FROM [myDB].[dbo].[myTable]
WHERE createdDate BETWEEN ('2012-10-01') AND ('2012-10-07')
group by employeeID,employeeName
SELECT [employeeID]
,employeeName
,isnull((SELECT SUM(case when clientID != 10 then timeSpent else 0 end)WHERE createdDate BETWEEN ('2012-10-01') AND ('2012-10-07')),0) as 'billable'
,isnull((SELECT SUM(case when clientID = 10 then timeSpent else 0 end)WHERE createdDate BETWEEN ('2012-10-01') AND ('2012-10-07')),0) as 'nonBillable'
FROM [myDB].[dbo].[myTable]
group by employeeID,employeeName,createdDate
どんな助けでも大歓迎です。
前もって感謝します。