表A
ID COL1 COL2
1 0.5 SDS
2 2 SD
3 1.5 FG
4 1 GG
5 4 GGH
-------------------------
表B
ID A B C FLAG
1 12 200 300 1
1 13 300 400 0
3 14 400 500 0
4 22 500 600 0表A与表B必须关联,且得按ID分组,写条SQL语句,查询出如下结果
(不能使用其他存储或者函数)
----------------------------------------
ID | A.COL1 |按flag=1统计A次数|按flag=1统计A次数| B求和 |
1 0.5 1 0 500
2 2 0 0 0
3 1.5 0 1 400
4 1 0 1 500
5 4 0 0 0
ID COL1 COL2
1 0.5 SDS
2 2 SD
3 1.5 FG
4 1 GG
5 4 GGH
-------------------------
表B
ID A B C FLAG
1 12 200 300 1
1 13 300 400 0
3 14 400 500 0
4 22 500 600 0表A与表B必须关联,且得按ID分组,写条SQL语句,查询出如下结果
(不能使用其他存储或者函数)
----------------------------------------
ID | A.COL1 |按flag=1统计A次数|按flag=1统计A次数| B求和 |
1 0.5 1 0 500
2 2 0 0 0
3 1.5 0 1 400
4 1 0 1 500
5 4 0 0 0
按flag=1统计A次数| B求和 -->是flag=0吧
好难明白
-------------------------
A.COL1 :即要全部将A表中COL1中的内容全部显示出来
按flag=1统计A次数:即select count(*) from B where flag = 1
按flag=1统计A次数:即select count(*) from B where flag = 0
B求和 :即select sum(B) from B 求和,按ID分组求和按flag=1统计A次数 , 按flag=1统计A次数 , B求和,是各列的统计条件,我是为了方便,直接将其当作列名
(select id,c_a=count(*) from b where flag='1' group by id) bb,
(select id,c_a=count(*) from b where flag='0' group by id) cc,
(select id,b_sum=sum(B) from b group by id) dd
where
a.id*=bb.id and a.id*=cc.id and a.id*=dd.id
left outer join
(select
id
,sum(case when flag = 1 then 1 else 0 end) as flag_1
,sum(case when flag = 1 then 1 else 0 end) as flag_0
,sum(B) B_Total
from b group by id) temp_b
on temp_a.id = temp_b.id
insert a
select 1,0.5,'SDS'
union all select 2,2,'SD'
union all select 3, 1.5, 'FG'
union all select 4, 1 , 'GG'
union all select 5, 4 , 'GGH'-------------------------
create table B(ID int, A int, B int, C int, FLAG int)
insert b
select 1 , 12 , 200, 300, 1
union all select 1, 13, 300, 400, 0
union all select 3 , 14, 400, 500, 0
union all select 4, 22, 500, 600, 0
select a.id,a.col1,[按flag=1统计A次数]=isnull(sum(t.flag),0),[按flag=0统计A次数]=isnull(sum(case when t.flag=0 then 1 end ),0),B求和=isnull(t.s,0)
from a left join (
select id,s=b+c,flag from b t where exists (select * from b where id=t.id and t.b+c<isnull(b+c,0)) or id in (select id from b group by id having count(id)=1)
) t on a.id =t.id
group by a.id,a.col1,t.s
drop table a,b
ID | A.COL1 |按flag=1统计A次数|按flag=1统计A次数| B求和 |
1 0.5 1 0 500第二个按flag=1统计A的次数为什么是零?是因为ID=1有一条flag=1 的记录吗?
A.ID ,
A.COL1,
SUM(case B.FLAG when 1 then 1 else 0 end),
SUM(case B.FLAG when 0 then 1 else 0 end),
SUM(B.B)
from
A
left join
B
on
A.ID=B.ID
group by
A.ID,A.COL1
select A.ID,A.COL1,sum(case when B.flag=1 then 1 else 0 end) a,
sum(case when B.flag=0 then 1 else 0 end) b,sum(B.B)
from A left join B on A.ID=B.ID
group by A.ID,A.COL1
select A.[ID],A.[COL1],
sum(case B.flag when 1 then 1 else 0 end) Flag_1,
sum(case B.flag when 0 then 1 else 0 end) Flag_0,
isnull(sum(B.B),0) Sum_B
from A
left join B on A.[ID]=B.[ID]
group by A.[ID],A.[COL1] order by a.[id]
ID | A.COL1 |按flag=1统计A次数|按flag=1统计A次数| B求和 |
1 0.5 1 0 500第二个按flag=1统计A的次数为什么是零?是因为ID=1有一条flag=1 的记录吗?
---------------------------
是的,即表A中ID在表B中只出现一次
-----
开始发帖快了,敲错和少写了字
ID | A.COL1 |按flag=1统计A表ID在B表中出现的次数|按flag=0统计A....的次数| B求和 |
1 0.5 1 0 500
to y_dong119(woman is a dead circulation) :
是说错了嘛,嗨,当时敲快了字,原以为大家看结果能明白的!
---------
题目再说一片,那位高手有空就再写次
---------
表A
ID COL1 COL2
1 0.5 SDS
2 2 SD
3 1.5 FG
4 1 GG
5 4 GGH
-------------------------
表B
ID A B C FLAG
1 12 200 300 1
1 13 300 400 0
3 14 400 500 0
4 22 500 600 0---------
ID | 结果一|结果二 |结果三 | 结果四 |
1 0.5 1 0 500
2 2 0 0 0
3 1.5 0 1 400
4 1 0 1 500
5 4 0 0 0
---------------------
ID列:显示A表中所有ID,因为A表中ID 包含 B表ID
结果一:显示A表中所有数据,反正,前面两列与A表保持一致
结果二:统计A表中ID在B表中出现的次数,条件是B表的FLAG = 1
结果三:统计A表中ID在B表中出现的次数,条件是B表的FLAG = 0
结果四:按ID分组统计B的和
---------------------
十分感谢 leo_lesley(leo) ( ) :
----
你结果错了,估计是我将题目写错,你的结果是:
id col1 按flag=1统计A次数 按flag=0统计A次数 B求和
----------- ----------- ------------ ------------ -----------
1 .50 1 0 500
2 2.00 0 0 0
3 1.50 0 1 900
4 1.00 0 1 1100
5 4.00 0 0 0
----
900和1100不对
----
总之,我自己都快被弄晕了,有空大家再写一下,我最近忙死了!
其他帖,暂时还没看,过两天来揭贴,麻烦再改改,以前题目错了