SQL求助

yuzhifu1 2010-02-20 06:41:21

年度 月份 客户名称 期初欠款 本期发货 本期收款 期未欠款
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
...全文
125 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
alexwangh 2010-02-20
  • 打赏
  • 举报
回复
楼上的老兄,你决定这样可以跑通么?
xman_78tom 2010-02-20
  • 打赏
  • 举报
回复

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
*/
waxwork6 2010-02-20
  • 打赏
  • 举报
回复
好奇地问一句,Tom,这么多文本数据,你将它转成SQL语句,用的是不是正则表达式啊?还是复制后慢慢改过来的?
引用 2 楼 xman_78tom 的回复:
SQL codecreatetable # (年度int,月份int,客户名称varchar(8),
期初欠款decimal(12,2),本期发货decimal(12,2),本期收款decimal(12,2),期未欠款decimal(12,2));insertinto #select2008,5,'机电公司',0,4414918,0,4414918unionallselect2008,6,'机电公?-
Zoezs 2010-02-20
  • 打赏
  • 举报
回复
你下面那些值是怎么算的啊?
xman_78tom 2010-02-20
  • 打赏
  • 举报
回复

-- 改一下
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 的查询引擎不够“智能”。
yuzhifu1 2010-02-20
  • 打赏
  • 举报
回复
这是一个帐表,上一行的期未数=下一行的期初数。二楼的计算正确,但引用到我的数据时却报错???
引用 1 楼 zoezs 的回复:
你下面那些值是怎么算的啊?
yuzhifu1 2010-02-20
  • 打赏
  • 举报
回复
还是不行! 数据库为SQL2000,用SQL2005查询
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 行受影响)
feegle_develop 2010-02-20
  • 打赏
  • 举报
回复

SELECT 年度,客户名称, SUM(期初欠款),SUM(本期发货),SUM(本期收款),SUM(期未欠款)
FROM TABLE
GROUP BY 年度,客户名称
老黎 2010-02-20
  • 打赏
  • 举报
回复

--题目意思我明白,就是xman的这个语句看得不大懂,能否解释一下?
select [年度],[客户名称],
(select [期初欠款] from # where t.[年度]=[年度] and [月份]=min(t.[月份])) [期初欠款],
sum([本期发货]) [累计发货],
sum([本期收款]) [累计收款],
(select [期未欠款] from # where t.[年度]=[年度] and [月份]=max(t.[月份])) [期未欠款]
from # t group by [年度],[客户名称]
老黎 2010-02-20
  • 打赏
  • 举报
回复
楼主,2楼的语句我在2000可以调通

--查看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)
引用楼主 yuzhifu1 的回复:
SQL code
年度 月份 客户名称 期初欠款 本期发货 本期收款 期未欠款20085 机电公司044149180441491820086 机电公司441491800441491820087 机电公司44149184404412.508819330.520088 机电公司8819330.5008819330.520089 机电公司?-
羽毛之家 2010-02-20
  • 打赏
  • 举报
回复
如果是按年份来进行求合的话,无非就是sum()与group by而已,这样的话上面已经列出查询语句了。
alexwangh 2010-02-20
  • 打赏
  • 举报
回复
不好意思,引错了
引用 5 楼 alexwangh 的回复:
不好意思,我想问您,为什么我运行的结果是:
子查询返回的值多于一个。当子查询跟随在 =、!=、 <、 <=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
alexwangh 2010-02-20
  • 打赏
  • 举报
回复
2000上面我没能跑通,提示就是这个
引用 6 楼 xman_78tom 的回复:
SQL Server 2005 和 2008 版上可以正确运行。SQL Server 2000 没有测试。
xman_78tom 2010-02-20
  • 打赏
  • 举报
回复
SQL Server 2005 和 2008 版上可以正确运行。SQL Server 2000 没有测试。

alexwangh 2010-02-20
  • 打赏
  • 举报
回复
不好意思,我想问您,为什么我运行的结果是:
子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
xman_78tom 2010-02-20
  • 打赏
  • 举报
回复
引用 3 楼 alexwangh 的回复:
楼上的老兄,你决定这样可以跑通么?

结果是我的 SQL Server 运行出来的,不是凑出来的。

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧