当前位置:主页>delphi7/进销存和数据库> 文章内容

这样的查询用一个sql能否完成???

发布时间:2010-01-21 | QQ免费站
1楼: 一个交易流水表mytable,字段有fa(机构代码),fb(交易金额),fc(交易日期),fc(交易标志) 现在想使用一个sql统计出 成功交易(交易标志为1)的笔数, 成功交易的金额, 失败交易(交易标志为1)的笔数, 失败交易的金额, 交易日期总数, 其中交易日期的总数可以去掉 字串9

2楼: 笨方法 select a.succount,b.succsum,c.failecount,d.failesum from (select count(*) as succount from mytable where fd=1 ) a, (select sum(fb) as succsum from mytable where fd=1 ) b, (select count(*) as failecount from mytable where fd=0 ) c, (select sum(fb) as failesum from mytable where fd=0 ) d 字串2

3楼: select (select count(*) from mytable where fd=1) as 交易数, (select sum(fb) from mytable where fd=1) as 交易总金额, (select count(*) from mytable where fd=0) as 失败交易数, (select sum(fb) from mytable where fd=0) as 失败交易金额, (select count(distinct fc) from mytable) as 日期数

字串2

4楼: SELECT fc, COUNT(*), SUM(fb) FROM mytable WHERE ... GROUP BY fc 字串3

5楼: select aa.fa,(select count(fc) from mytable where fc=‘1‘ and fa=aa.fa) 成功交易, (select sum(fb) from mytable where fc=‘1‘ and fa=aa.fa) 成功交易金额, (select count(fc) from mytable where fc=‘0‘ and fa=aa.fa) 失败交易, (select sum(fb) from mytable where fc=‘0‘ and fa=aa.fa) 失败交易金额,aa.fc1 交易日期总数 from (select fa,count(fc) fc1 from mytable group by fa) as aa 字串2

6楼: select count(*) from mytable group by (交易标志)
字串9

7楼: to 诸位: 我想达到的查询结果是每个机构一条记录 字串6

8楼: 用我的就可以达到你的要结果 字串5

9楼: 对,没错没错

字串3

10楼: select distinct fa, (select count(*) from mytable where fd=1 and fa=m.fa) as 交易数, (select sum(fb) from mytable where fd=1 and fa=m.fa) as 交易总金额, (select count(*) from mytable where fd=0 and fa=m.fa) as 失败交易数, (select sum(fb) from mytable where fd=0 and fa=m.fa) as 失败交易金额, (select count(distinct fc) from mytable where fa=m.fa) as 日期数 from mytable m 字串2

11楼: 我试试

字串7

12楼: 别花时间,只能够分开5次查询

字串2

13楼: 分开查询在效率和易用上都不如一条复合SQL语句 不是有过忠告吗,能用一条复杂的SQL实现的就不要用多条 字串7

14楼: select distinct mt.fa,cgjybs.bs,cgjybs.je,sbjybs.bs,sbjybs.je,zje.je from mytable mt left join (select count(*) as bs from mytable where fd=1 and fa = mt.fa ) cgjybs on cgjybs.fa = mt.fa left join (select sum(fb) as je from mytable where fd=1 and fa = mt.fa ) cgjyje on cgjyje.fa = mt.fa (select count(*) as bs from mytable where fd=0 and fa = mt.fa ) sbjybs on sbjybs.fa = mt.fa left join (select sum(fb) as je from mytable where fd=0 and fa = mt.fa ) sbjyje on sbjyje.fa = mt.fa left join (select sum(fb) as je from mytable where fa = mt.fa ) zje on zje.fa = mt.fa where fc = ‘交易日期‘ order by mt.fa 字串1

15楼: 多人接受答案了。 字串8