跨表使用group by,case when的问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了跨表使用group by,case when的问题相关的知识,希望对你有一定的参考价值。

请问高手,我想根据日期从里表a和表b统计出czonghe列汇总数,得出第2图片的数据,有谁知道怎么写的,万分感谢
这就是两张表的内容

参考技术A select cdate,sum(czonghe) as czonghe
from
(select cdate,czonghe from a
union all
select cdate,czonghefrom b
) as t1
group by cdate
参考技术B 把两张表发出来追问

表a: 表b:
cdate czonghe cdate czonghe
2012-4-1 10 2012-4-2 20
2012-4-1 20 2012-4-2 50
2012-4-2 30 2012-4-3 74
2012-4-2 40 2012-4-3 11

追答

因为你的字段都一样
可以先把两张表合成一张表在group by 就好了
select c.cdate,sum(c.czonghe) from
((select a.cdate ,a.czonghe from table_015 a)
union all
(select b.cdate,b.czonghe from table_016 b)) c
group by c.cdate;

CDATE SUM(C.CZONGHE)
----------- --------------
2012-4-1 30
2012-4-2 140
2012-4-3 85

追问

日期 a表汇总 b表汇总
2012-4-1 60 0
2012-4-2 140 70
2012-4-3 0 85
我想要显示的结果是这样的

追答

你的4-1 a表总也没有60啊

追问

日期 a表汇总 b表汇总
2012-4-1 30 0
2012-4-2 70 70
2012-4-3 0 85
刚刚那个结果复制错了,是这个结果

追答

create table table_017 (
cdate date,aczonghe int,bczonghe int);
insert into table_017(
cdate ,aczonghe,bczonghe)
select
c.cdate,
(case when c.biaoshi ='a'
then c.AZONGHE else 0
end)
,
(case when c.biaoshi ='b'
then c.AZONGHE else 0
end)
from
((select cdate,sum(czonghe) azonghe,'a' biaoshi from table_015 group by cdate)
union all
(select cdate,sum(czonghe) bzonghe ,'b' from table_016 group by cdate) ) c
SQL> select cdate, sum(aczonghe) aczonghe,sum(bczonghe) bczonghe from table_017 group by cdate;

CDATE ACZONGHE BCZONGHE
----------- ---------- ----------
2012-4-1 30 0
2012-4-2 70 70
2012-4-3 0 85

本回答被提问者采纳

sql service (case when then else end ..... group by)

1.

原表
courseid coursename score 
------------------------------------- 
1 java 70 
2 oracle 90 
3 xml 40 
4 jsp 30 
5 servlet 80 
------------------------------------- 
为了便于阅读, 查询此表后的结果显式如下( 及格分数为60): 
courseid coursename score mark 
--------------------------------------------------- 
1 java 70 pass 
2 oracle 90 pass 
3 xml 40 fail 
4 jsp 30 fail 
5 servlet 80 pass 
--------------------------------------------------- 
写出此查询语句

 1 USE T4st
 2 
 3 GO
 4 IF(EXISTS(SELECT * FROM sysobjects WHERE name =\'courseTes\'))
 5 DROP TABLE courseTes
 6 GO
 7 
 8 CREATE TABLE courseTes
 9 (
10    courseid INT NOT NULL PRIMARY KEY IDENTITY,
11    coursename NVARCHAR(50) NOT NULL,
12    score INT NOT NULL
13 )
14 
15 INSERT dbo.courseTes
16         ( coursename, score )
17 VALUES  ( N\'java\', -- coursename - nvarchar(50)
18           N\'70\'  -- score - int
19           )
20 INSERT dbo.courseTes
21         ( coursename, score )
22 VALUES  ( N\'oracle\', -- coursename - nvarchar(50)
23           N\'90\'  -- score - int
24           )
25 INSERT dbo.courseTes
26         ( coursename, score )
27 VALUES  ( N\'xml\', -- coursename - nvarchar(50)
28           N\'40\'  -- score - int
29           )
30 INSERT dbo.courseTes
31         ( coursename, score )
32 VALUES  ( N\'jsp\', -- coursename - nvarchar(50)
33           N\'30\'  -- score - int
34           )
35 INSERT dbo.courseTes
36         ( coursename, score )
37 VALUES  ( N\'servlet\', -- coursename - nvarchar(50)
38           N\'80\'  -- score - int
39           )
40 SELECT * FROM courseTes
41 
42 SELECT t.courseid,t.coursename,t.score,
43 (
44    CASE
45    WHEN t.score > 60 THEN \'pass\'
46    ELSE \'fail\'
47    END  
48 ) 
49 AS mark
50 FROM courseTes AS t

运行结果如下:

2.

表内容:
2005-05-09 胜
2005-05-09 胜
2005-05-09 负
2005-05-09 负
2005-05-10 胜
2005-05-10 负
2005-05-10 负

如果要生成下列结果, 该如何写sql语句?

   时间          胜    负
2005-05-09  2     2
2005-05-10  1     2

SQL语句

 1 USE T4st
 2 
 3 GO
 4 IF(EXISTS(SELECT * FROM sysobjects WHERE name =\'Score\'))
 5 DROP TABLE Score
 6 GO
 7 
 8 CREATE TABLE Score
 9 (
10    TimeId NVARCHAR(50) NOT NULL,
11    SuccessOrFail NVARCHAR(50) NOT NULL,
12 )
13 
14 INSERT Score
15        (TimeId,SuccessOrFail)
16 VALUES (
17         N\'2005-05-09\',
18         N\'\'
19         )
20 INSERT Score
21        (TimeId,SuccessOrFail)
22 VALUES (
23         N\'2005-05-09\',
24         N\'\'
25         )
26 INSERT Score
27        (TimeId,SuccessOrFail)
28 VALUES (
29         N\'2005-05-09\',
30         N\'\'
31         )
32 INSERT Score
33        (TimeId,SuccessOrFail)
34 VALUES (
35         N\'2005-05-09\',
36         N\'\'
37         )
38 INSERT Score
39        (TimeId,SuccessOrFail)
40 VALUES (
41         N\'2005-05-10\',
42         N\'\'
43         )
44 INSERT Score
45        (TimeId,SuccessOrFail)
46 VALUES (
47         N\'2005-05-10\',
48         N\'\'
49         )
50 INSERT Score
51        (TimeId,SuccessOrFail)
52 VALUES (
53         N\'2005-05-10\',
54         N\'\'
55         )
56 
57 SELECT * FROM Score
58 
59 SELECT s.TimeId AS \'时间\',
60 SUM(
61   CASE
62   WHEN s.SuccessOrFail =\'\' THEN 1
63   ELSE 0
64   END
65 ) AS \'\',
66 SUM(
67   CASE
68   WHEN s.SuccessOrFail =\'\' THEN 1
69   ELSE 0
70   END
71 ) AS \'\'
72 FROM Score AS s GROUP BY s.TimeId

运行结果如下:

 

以上是关于跨表使用group by,case when的问题的主要内容,如果未能解决你的问题,请参考以下文章

Oracle SQL - 使用 CASE WHEN 语句时 GROUP BY 无效

关于case when结合group by用时的写法举例(转)

sql service (case when then else end ..... group by)

group by用法

sql group排序问题

使用 partition by 和 case when