首页 新闻 会员 周边 捐助

sql 语句如何写

0
[已解决问题] 解决于 2015-07-10 14:05

今天想了下,还是没有把写来,还请高手们帮我看下。

拼写的表如下:

select 1 as 序号 ,'2010-2-3' as 日期,'是' as 状
union all
select 2 as 序号 ,'2010-2-3' as 日期,'否' as 状
union all
select 3 as 序号 ,'2010-2-4' as 日期,'是' as 状
union all
select 4 as 序号 ,'2010-2-4' as 日期,'否' as 状
union all
select 5 as 序号 ,'2010-2-3' as 日期,'是' as 状
union all
select 6 as 序号 ,'2010-2-3' as 日期,'是' as 状

union all
select 7 as 序号 ,'2010-2-4' as 日期,'是' as 状

 

 

KenyonLi的主页 KenyonLi | 初学一级 | 园豆:165
提问于:2015-07-09 21:50
< >
分享
最佳答案
0

select [Time] as 日期,
         SUM(CASE WHEN [状态]='是' THEN 1 ELSE 0 END) as 是,
         SUM(CASE WHEN [状态]='否' THEN 1 ELSE 0 END) as 否
from [Table]
group by [Time]

这样查一下就出来了,用不着写那么多。

奖励园豆:5
at小怪兽 | 小虾三级 |园豆:760 | 2015-07-10 13:47
其他回答(3)
0

我只是知道大概的思路,在结果中得到的数据都是分组的情况,所以在查询语句中有ground by,你可以试试。

指尖流逝 | 园豆:202 (菜鸟二级) | 2015-07-09 22:13
0
WITH CTE AS (
    select 1 as 序号 ,'2010-2-3' as 日期,'' asunion all
    select 2 as 序号 ,'2010-2-3' as 日期,'' asunion all
    select 3 as 序号 ,'2010-2-4' as 日期,'' asunion all
    select 4 as 序号 ,'2010-2-4' as 日期,'' asunion all
    select 5 as 序号 ,'2010-2-3' as 日期,'' asunion all
    select 6 as 序号 ,'2010-2-3' as 日期,'' asunion all
    select 7 as 序号 ,'2010-2-4' as 日期,'' as 状
)
SELECT 日期, SUM(是) 是, SUM(否) 否 FROM (
    SELECT 日期, 
    CASE WHEN='' THEN 1 ELSE 0 END AS 是,
    CASE WHEN='' THEN 0 ELSE 1 END ASFROM CTE
)A GROUP BY 日期
liqipeng | 园豆:1160 (小虾三级) | 2015-07-09 22:51
0

WITH CTE AS (
select 1 as 序号 ,'2010-2-3' as 日期,'是' as 状
union all
select 2 as 序号 ,'2010-2-3' as 日期,'否' as 状
union all
select 3 as 序号 ,'2010-2-4' as 日期,'是' as 状
union all
select 4 as 序号 ,'2010-2-4' as 日期,'否' as 状
union all
select 5 as 序号 ,'2010-2-3' as 日期,'是' as 状
union all
select 6 as 序号 ,'2010-2-3' as 日期,'是' as 状
union all
select 7 as 序号 ,'2010-2-4' as 日期,'是' as 状
)
SELECT 日期,

COUNT(CASE WHEN 状='是' THEN 1 END) AS 是,

COUNT(CASE WHEN 状='否' THEN 1 END) AS 否
FROM CTE GROUP BY 日期

小白菜T | 园豆:564 (小虾三级) | 2015-07-10 09:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册