多行总和和计算字段
Posted
技术标签:
【中文标题】多行总和和计算字段【英文标题】:Multi Row Sum and calculated field 【发布时间】:2014-05-30 21:20:42 【问题描述】:我正在尝试找到一种将计算行插入 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,它总结了该用户的所有不同 Spend 并从总数中减去它。因此,如果 Jay 的总支出为 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
【问题讨论】:
从 NZ 功能看来,MS Access 是这样吗? 【参考方案1】:我创建了自己的快速表并写了一个简短的例子来说明如何做到这一点。但是,在处理这个问题时,我注意到,按照我写这篇文章的方式,您必须知道您想要哪些类别,因为那里有自己的订单项,然后将其他类别放入“其他”字段。
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
【讨论】:
我现在认识到您可能在 MS Access 中工作,如果是这种情况,那么这可能无法像我写的那样工作。 是的,这一切都在 MS Access 中完成。感谢您迄今为止的所有帮助......我会尝试上面的建议。以上是关于多行总和和计算字段的主要内容,如果未能解决你的问题,请参考以下文章