簡體   English   中英

多行總和和計算字段

[英]Multi Row Sum and calculated field

我正在嘗試找到一種方法,該方法將計算的行插入到SQL查詢中,該行基於從單獨的總計中減去的總和

例如:

Name | Title | eID | Spend_Category |Jan|Feb|...|Nov|Dec|Sum of Months|
Jay  |  CEO  | 1   | Lodging        |10 |20 |...| 0 |30 | 60          |
Jay  |  CEO  | 1   | Airlines       |20 |40 |...| 0 |60 | 120         |
Jay  |  CEO  | 1   | Auto           |10 |20 |...| 0 |30 | 60          |
Paul |  VP   | 2   | Lodging        |10 |20 |...| 0 |30 | 60          |

在另一張桌子上,我有每個員工ID的總支出。 我想做的是為每個用戶創建一個新行,並為每個用戶(eID)設置一個新的“其他”的Spend_Category,將所有該用戶的所有不同支出相加並從總數中減去。 因此,如果傑伊的總支出為500美元

例如:

Name | Title | eID | Spend_Category |Jan|Feb|...|Nov|Dec|Sum of Months|
Jay  |  CEO  | 1   | Lodging        |10 |20 |...| 0 |30 | 60          |
Jay  |  CEO  | 1   | Airlines       |20 |40 |...| 0 |60 | 120         |
Jay  |  CEO  | 1   | Auto           |10 |20 |...| 0 |30 | 60          |
Jay  |  CEO  | 1   | Other          |...................|$500-240=$260|
Paul |  VP   | 2   | Lodging        |10 |20 |...| 0 |30 | 60          |
...etc...

我當然樂於接受想法,並且對SQL非常陌生。 這是我到目前為止的工作,將不勝感激該小組的專業知識。

SELECT 
SC.Full_Name, 
TS.Job_Title,
TS.Card_Type,
SC.Employee_ID, 
SC.Genesis_Industry,
SC.May,
SC.June, 
SC.July, 
SC.August,
SC.September,
SC.October,
SC.November,
SC.December,
SC.January,
SC.February,
SC.March,
SC.April, 
(NZ(SC.May,0)+NZ(SC.June,0)+ NZ(SC.July,0)+ NZ(SC.August,0)+NZ(SC.September,0)+NZ(SC.October,0)+NZ(SC.November,0)+NZ(SC.December,0)+NZ(SC.January,0)+NZ(SC.February,0)+NZ(SC.March,0)+NZ(SC.April,0)) AS Category_Spend,
TS.Total_Spend
FROM Spend_Category SC
left join Top_Spender TS
on SC.Employee_ID=TS.Employee_ID

我創建了自己的快速表格,並寫了一個簡短的示例說明如何完成此操作。 但是,在完成此工作時,我注意到用這種方式編寫時,您必須知道想要哪些類別作為自己的訂單項,然后將其他類別放入“其他”字段。

create table #test (firstname varchar(max), 
                category varchar(max), 
                jan int, feb int, mar int, apr int, may int, jun int, jul int, aug int, sep int, oct int, nov int, dec int)

insert into #test values
('Joe','Lodging',100,50,874,7852,50,50,6,0,0,0,0,0),
('Joe','Airlines',100,510,874,60,20,50,6,0,0,0,0,0),
('Joe','Auto',100,50,874,60,50,50,6,0,0,0,0,0),
('Joe','Lunch',500,50,874,60,50,50,6,0,0,500,0,0),
('Joe','Insurance',100,50,874,4111,50,50,6,0,0,0,0,0)

select 
    firstname, 
    Other = sum(case when category not in ('lodging','airlines','auto') 
                     then yearly else 0 end)
into #tbl
from #test t
cross apply (select yearly = isnull(jan,0) + isnull(feb,0) + 
    isnull(mar,0) + isnull(apr,0) + isnull(may,0) + isnull(jun,0) + 
    isnull(jul,0) + isnull(aug,0) + isnull(sep,0) + isnull(oct,0) + 
    isnull(nov,0) + isnull(dec,0)) y
group by firstname

select 
    t.firstname,
    category, 
    yearlyTotal

from #test t
cross apply (select yearlyTotal = isnull(jan,0) + isnull(feb,0) + 
    isnull(mar,0) + isnull(apr,0) + isnull(may,0) + isnull(jun,0) + 
    isnull(jul,0) + isnull(aug,0) + isnull(sep,0) + isnull(oct,0) + 
    isnull(nov,0) + isnull(dec,0)) y
where category in ('lodging','airlines','auto')

UNION 

select 
    firstname, 
    'Other' as Category, 
    other as yearlyTotal
from #tbl t

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM