27,579
社区成员
发帖
与我相关
我的任务
分享
年度 月份 客户名称 期初欠款 本期发货 本期收款 期未欠款
2008 5 机电公司 0 4414918 0 4414918
2008 6 机电公司 4414918 0 0 4414918
2008 7 机电公司 4414918 4404412.5 0 8819330.5
2008 8 机电公司 8819330.5 0 0 8819330.5
2008 9 机电公司 8819330.5 638020 0 9457350.5
2008 10 机电公司 9457350.5 2607752.03 0 12065102.53
2008 11 机电公司 12065102.53 2114778.37 0 14179880.9
2008 12 机电公司 14179880.9 1752792.74 4065269.96 11867403.68
2009 1 机电公司 11867403.68 2291839.66 0 14159243.34
2009 2 机电公司 14159243.34 393470.45 6215610.94 8337102.85
2009 3 机电公司 8337102.85 470836.2 1533779.29 7274159.76
2009 4 机电公司 7274159.76 828239.79 898839.66 7203559.89
2009 5 机电公司 7203559.89 768673.24 1210784.92 6761448.21
2009 6 机电公司 6761448.21 1136892.59 615565.11 7282775.69
2009 7 机电公司 7282775.69 1194280.25 320015.39 8157040.55
2009 8 机电公司 8157040.55 668776.83 765912.99 8059904.39
2009 9 机电公司 8059904.39 773609.45 443273.52 8390240.32
2009 10 机电公司 8390240.32 1210225.65 1054655.72 8545810.25
2009 11 机电公司 8545810.25 740384.75 1196675.03 8089519.97
2009 12 机电公司 8089519.97 1107224.07 976816.05 8219927.99
2010 1 机电公司 8219927.99 100000 100000 8219927.99
如何用SQL查询得到:
年度 客户名称 期初欠款 累计发货 累计收款 期未欠款
2008 机电公司 0 15932673.64 4065269.96 11867403.68
2009 机电公司 11867403.68 11584452.93 15231928.62 8219927.99
2010 机电公司 8219927.99 100000 100000 8219927.99
create table # (年度 int,月份 int,客户名称 varchar(8),
期初欠款 decimal(12,2),本期发货 decimal(12,2),本期收款 decimal(12,2),期未欠款 decimal(12,2));
insert into #
select 2008,5,'机电公司',0,4414918,0,4414918 union all
select 2008,6,'机电公司',4414918,0,0,4414918 union all
select 2008,7,'机电公司',4414918,4404412.5,0,8819330.5 union all
select 2008,8,'机电公司',8819330.5,0,0,8819330.5 union all
select 2008,9,'机电公司',8819330.5,638020,0,9457350.5 union all
select 2008,10,'机电公司',9457350.5,2607752.03,0,12065102.53 union all
select 2008,11,'机电公司',12065102.53,2114778.37,0,14179880.9 union all
select 2008,12,'机电公司',14179880.9,1752792.74,4065269.96,11867403.68 union all
select 2009,1,'机电公司',11867403.68,2291839.66,0,14159243.34 union all
select 2009,2,'机电公司',14159243.34,393470.45,6215610.94,8337102.85 union all
select 2009,3,'机电公司',8337102.85,470836.2,1533779.29,7274159.76 union all
select 2009,4,'机电公司',7274159.76,828239.79,898839.66,7203559.89 union all
select 2009,5,'机电公司',7203559.89,768673.24,1210784.92,6761448.21 union all
select 2009,6,'机电公司',6761448.21,1136892.59,615565.11,7282775.69 union all
select 2009,7,'机电公司',7282775.69,1194280.25,320015.39,8157040.55 union all
select 2009,8,'机电公司',8157040.55,668776.83,765912.99,8059904.39 union all
select 2009,9,'机电公司',8059904.39,773609.45,443273.52,8390240.32 union all
select 2009,10,'机电公司',8390240.32,1210225.65,1054655.72,8545810.25 union all
select 2009,11,'机电公司',8545810.25,740384.75,1196675.03,8089519.97 union all
select 2009,12,'机电公司',8089519.97,1107224.07,976816.05,8219927.99 union all
select 2010,1,'机电公司',8219927.99,100000,100000,8219927.99
select [年度],[客户名称],
(select [期初欠款] from # where t.[年度]=[年度] and [月份]=min(t.[月份])) [期初欠款],
sum([本期发货]) [累计发货],
sum([本期收款]) [累计收款],
(select [期未欠款] from # where t.[年度]=[年度] and [月份]=max(t.[月份])) [期未欠款]
from # t group by [年度],[客户名称]
/*
2008 机电公司 0.00 15932673.64 4065269.96 11867403.68
2009 机电公司 11867403.68 11584452.93 15231928.62 8219927.99
2010 机电公司 8219927.99 100000.00 100000.00 8219927.99
*/
-- 改一下
select [年度],[客户名称],
(select top 1 [期初欠款] from # where t.[年度]=[年度] and [月份]=min(t.[月份])) [期初欠款],
sum([本期发货]) [累计发货],
sum([本期收款]) [累计收款],
(select top 1 [期未欠款] from # where t.[年度]=[年度] and [月份]=max(t.[月份])) [期未欠款]
from # t group by [年度],[客户名称]
-- 即使不加 top 1,子查询应该也只会返回 1 个值。SQL Server 2000 的查询引擎不够“智能”。
select a.fyear as [年度],b.fname as [客户名称],
(select fbeginbalancefor from t_rp_contactbal where fyear=a.fyear and fperiod=min(a.fperiod)) as [期初余额],
sum(a.fdebitfor) as [发货],
sum(a.fcreditfor) as [收款],
(select fendbalancefor from t_rp_contactbal where fyear=a.fyear and fperiod=max(a.fperiod)) as [期未余额]
from t_rp_contactbal a inner join t_organization b on a.frp=1 and a.fcustomer=b.fitemid
group by a.fyear,b.fname order by b.fname
消息 512,级别 16,状态 1,第 1 行
子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
(0 行受影响)
SELECT 年度,客户名称, SUM(期初欠款),SUM(本期发货),SUM(本期收款),SUM(期未欠款)
FROM TABLE
GROUP BY 年度,客户名称
--题目意思我明白,就是xman的这个语句看得不大懂,能否解释一下?
select [年度],[客户名称],
(select [期初欠款] from # where t.[年度]=[年度] and [月份]=min(t.[月份])) [期初欠款],
sum([本期发货]) [累计发货],
sum([本期收款]) [累计收款],
(select [期未欠款] from # where t.[年度]=[年度] and [月份]=max(t.[月份])) [期未欠款]
from # t group by [年度],[客户名称]
--查看SQL SERVER版本
select @@version
Microsoft SQL Server 2000 - 8.00.760 (Intel X86) Dec 17 2002 14:22:05 Copyright (c) 1988-2003 Microsoft Corporation Personal Edition on Windows NT 5.0 (Build 2195: Service Pack 4)