| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 838 人关注过本帖
标题:这个一条查询语句该怎么写?
只看楼主 加入收藏
等待冰柠檬
Rank: 2
来 自:浙江温州
等 级:论坛游民
帖 子:208
专家分:70
注 册:2008-6-25
结帖率:66.67%
收藏
已结贴  问题点数:10 回复次数:6 
这个一条查询语句该怎么写?
Select VehicleBrands,sum(Amount)
          from CostReports
          where CostReports.CostType = 1 and CreateTime between StartDate and EndDate
          group by VehicleBrands

      Select VehicleBrands,sum(Amount/ApportionMonth)
          from CostReports
          where CostReports.CostType = 2  and ThisMonth < StartMonth + ApportionMonth
      and ApportionMonth > 0
          group by VehicleBrands
select VehicleBrands,sum(Amount)
          from CostReports
          where CostReports.CostType = 3
          group by VehicleBrands
如何将这三条语句查询的的结果进行累加
Select VehicleBrands,sum(第1种查询的结果+第2种查询的结果+第3种查询的结果) as ReportCost
from CostReports
group by VehicleBrands
注意:第X种查询的结果用查询语句表示


[ 本帖最后由 等待冰柠檬 于 2010-8-5 15:19 编辑 ]
搜索更多相关主题的帖子: 语句 查询 
2010-08-05 15:05
happynight
Rank: 8Rank: 8
等 级:贵宾
威 望:15
帖 子:807
专家分:760
注 册:2008-4-26
收藏
得分:2 
SELECT a.VehicleBrands,suma+sumb+sumc
FROM (
Select VehicleBrands,sum(Amount) suma
          from CostReports
          where CostReports.CostType = 1 and CreateTime between StartDate and EndDate
          group by VehicleBrands) a
INNER JOIN
(      Select VehicleBrands,sum(Amount/ApportionMonth) sumb
          from CostReports
          where CostReports.CostType = 2  and ThisMonth < StartMonth + ApportionMonth
      and ApportionMonth > 0
          group by VehicleBrands)b
ON a.VehicleBrands=b.VehicleBrands
INNER JOIN (
select VehicleBrands,sum(Amount) sumc
          from CostReports
          where CostReports.CostType = 3
          group by VehicleBrands
) c
ON a.VehicleBrands=c.VehicleBrands
注意:假定 三个查询返回的VehicleBrands都能一一对应到
2010-08-05 16:51
等待冰柠檬
Rank: 2
来 自:浙江温州
等 级:论坛游民
帖 子:208
专家分:70
注 册:2008-6-25
收藏
得分:0 
好像有问题吧!版主!
2010-08-06 10:11
happynight
Rank: 8Rank: 8
等 级:贵宾
威 望:15
帖 子:807
专家分:760
注 册:2008-4-26
收藏
得分:0 
以下是引用等待冰柠檬在2010-8-6 10:11:40的发言:

好像有问题吧!版主!
什么问题?请看我后面附加的条件说明,如果正是在我的说明造成的或者是另外的原因 详细说明下
2010-08-06 10:58
dearwolf4128
Rank: 5Rank: 5
来 自:陕西西安
等 级:职业侠客
威 望:6
帖 子:79
专家分:365
注 册:2010-7-2
收藏
得分:2 
select distinct VehicleBrands,((Select VehicleBrands,sum(Amount) suma from CostReports where CostReports.CostType = 1 and CreateTime between StartDate and EndDate group by VehicleBrands)+( Select VehicleBrands,sum(Amount/ApportionMonth) from CostReports where CostReports.CostType = 2  and ThisMonth < StartMonth + ApportionMonth
  and ApportionMonth > 0 group by VehicleBrands) +
(select VehicleBrands,sum(Amount)  from CostReports  where CostReports.CostType = 3
          group by VehicleBrands)) as ReportCost from CostReports  where  VehicleBrands in
(select VehicleBrands from CostReports)

我测过了,可以实现
2010-08-06 12:10
pumbaax10
Rank: 1
等 级:新手上路
帖 子:2
专家分:5
注 册:2010-8-6
收藏
得分:2 
其实如果在VehicleBrands不能一一对应的情况下,你可以先把三个表的VehicleBrands查找出来distinct的,然后在left outer join那三个sum的表,然后就可以加总了。
2010-08-06 13:52
aei135
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:12
帖 子:232
专家分:1176
注 册:2009-4-6
收藏
得分:2 
楼上说得没错,就这样
SELECT distinct CostReports.VehicleBrands,isnull(suma,0)+isnull(sumb,0)+isnull(sumc,0) amt
FROM CostReports  
left join (
Select VehicleBrands,sum(Amount) suma
          from CostReports
          where CostReports.CostType = 1 and CreateTime between StartDate and EndDate
          group by VehicleBrands) a
on CostReports.VehicleBrands=a.VehicleBrands
left JOIN
(      Select VehicleBrands,sum(Amount/ApportionMonth) sumb
          from CostReports
          where CostReports.CostType = 2  and ThisMonth < StartMonth + ApportionMonth
      and ApportionMonth > 0
          group by VehicleBrands)b
ON CostReports.VehicleBrands=b.VehicleBrands
left JOIN (
select VehicleBrands,sum(Amount) sumc
          from CostReports
          where CostReports.CostType = 3
          group by VehicleBrands
) c
ON CostReports.VehicleBrands=c.VehicleBrands
2010-08-06 14:43
快速回复:这个一条查询语句该怎么写?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014337 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved