100分求一个去掉重复SQL

qq497525725 2009-07-29 04:14:04
aa 表中 有 ID ,list_id, is_type(0或1) 等字段 都是int

select count(case when 1 then 1) from aa
查出 is_type=1 的条数
现在我想要 list_id 不重复 的is_type=1 的条数
distinct list_id 怎么加
...全文
271 42 打赏 收藏 转发到动态 举报
写回复
用AI写文章
42 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq497525725 2009-07-29
  • 打赏
  • 举报
回复
谢谢各位了 ~!
lsd123 2009-07-29
  • 打赏
  • 举报
回复
.
hailang7210 2009-07-29
  • 打赏
  • 举报
回复
select count(is_type),list_id from aa group by list_id where is_type = 1
等不到来世 2009-07-29
  • 打赏
  • 举报
回复
,count(distinct case when  Cha_IsPrivilege = 1  then stu_id end) as youtims 
htl258_Tony 2009-07-29
  • 打赏
  • 举报
回复
select * from  tb_charge sc
inner join tb_Curriculum_Schedule cc on sc.curr_id=cc.curr_id


这个出来的数据贴出来看看.不多吧,如果太多就贴部份.
qq497525725 2009-07-29
  • 打赏
  • 举报
回复

select substring(curr_beginclass,1,4)+' '+Curr_Term_Name as xueqi,--时间
count(distinct stu_id) as allPeople,--人数
count(stu_id) as stutimes,--人次
count(case when Cha_IsPrivilege = 1 then 1 end) as youPeople--优惠人次
,count(distinct stu_id case when Cha_IsPrivilege = 1 then 1 end) as youtims --优惠人数? 不会了因为 这里case 和 distinct 操作的不是同一列
from tb_charge sc
inner join tb_Curriculum_Schedule cc on sc.curr_id=cc.curr_id
group by substring(curr_beginclass,1,4),Curr_Term_Name

htl258_Tony 2009-07-29
  • 打赏
  • 举报
回复
源数据?
ks_reny 2009-07-29
  • 打赏
  • 举报
回复
1樓樹哥的就可以了,樓主想複雜了.
qq497525725 2009-07-29
  • 打赏
  • 举报
回复
2008 下学期 4 14 0 0
2008 一学期 1 7 1 1
2009 寒假短训 3 3 2 2
2009 上学期 3 8 0 0
2009 下学期 5 9 2 2
2009 一学期 1 1 1 1
qq497525725 2009-07-29
  • 打赏
  • 举报
回复

select substring(curr_beginclass,1,4)+' '+Curr_Term_Name as xueqi,--时间
count(distinct stu_id) as allPeople,--人数
count(stu_id) as stutimes,--人次
count(case when Cha_IsPrivilege = 1 then 1 end) as youPeople--优惠人次
,count(case when Cha_IsPrivilege = 1 then 1 end) as youtims --优惠人数? 不会了
from tb_charge sc
inner join tb_Curriculum_Schedule cc on sc.curr_id=cc.curr_id
group by substring(curr_beginclass,1,4),Curr_Term_Name

alisafan123 2009-07-29
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 josy 的回复:]
引用 12 楼 sql77 的回复:
引用 9 楼 josy 的回复:
引用 5 楼 guguda2008 的回复:
树哥我恨你


为了让你“爱”我,我撤了...

BTW:今天回帖不知道为什么老是要我登录,害我水分一个sf都没抢到.不知道大家有没有这个问题?

树哥我没有


莫非是RPWT?
[/Quote]

RPWT
htl258_Tony 2009-07-29
  • 打赏
  • 举报
回复
[Quote=引用 28 楼 qq497525725 的回复:]
引用 23 楼 htl258 的回复:
引用 21 楼 qq497525725 的回复:
我的意思是count 里 case  和 distinct  都加上可以吗 怎么加

SQL codeIFOBJECT_ID('tb')ISNOTNULLDROPTABLE tbGOCREATETABLE tb(idint ,list_idint,is_typeint )goinsert tbSELECT1,2,1UNIONALLSELECT2,1,0UNIONALLSELECT3,2,1UNIONALLSELECT4,4,1UNIONALLSELECT5,1,1goselectCOUNT(distinctcase list_idwhen2then1else list_idend)from tb/*
-----------
2

(1 行受影响)*/可以加,如这样.


但是 distinct 和 case 我想操作的不是同一列啊
[/Quote]你举例说明吧,提供下测试数据和想要的结果,以免误猜.
guguda2008 2009-07-29
  • 打赏
  • 举报
回复
[Quote=引用 28 楼 qq497525725 的回复:]
引用 23 楼 htl258 的回复:
引用 21 楼 qq497525725 的回复:
我的意思是count 里 case  和 distinct  都加上可以吗 怎么加

SQL codeIFOBJECT_ID('tb')ISNOTNULLDROPTABLE tbGOCREATETABLE tb(idint ,list_idint,is_typeint )goinsert tbSELECT1,2,1UNIONALLSELECT2,1,0UNIONALLSELECT3,2,1UNIONALLSELECT4,4,1UNIONALLSELECT5,1,1goselectCOUNT(distinctcase list_idwhen2then1else list_idend)from tb/*
-----------
2

(1 行受影响)*/可以加,如这样.


但是 distinct 和 case 我想操作的不是同一列啊
[/Quote]
那你把问题重新描述一遍吧
guguda2008 2009-07-29
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 josy 的回复:]
引用 17 楼 guguda2008 的回复:
引用 9 楼 josy 的回复:
引用 5 楼 guguda2008 的回复:
树哥我恨你


为了让你“爱”我,我撤了...

BTW:今天回帖不知道为什么老是要我登录,害我水分一个sf都没抢到.不知道大家有没有这个问题?

COOK问题?


不是,清了也没用
[/Quote]
没COOKIE都抢SF的这么快。。。。感到无与伦比的挫折感
qq497525725 2009-07-29
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 htl258 的回复:]
引用 21 楼 qq497525725 的回复:
我的意思是count 里 case  和 distinct  都加上可以吗 怎么加

SQL codeIFOBJECT_ID('tb')ISNOTNULLDROPTABLE tbGOCREATETABLE tb(idint ,list_idint,is_typeint )goinsert tbSELECT1,2,1UNIONALLSELECT2,1,0UNIONALLSELECT3,2,1UNIONALLSELECT4,4,1UNIONALLSELECT5,1,1goselectCOUNT(distinctcase list_idwhen2then1else list_idend)from tb/*
-----------
2

(1 行受影响)*/可以加,如这样.
[/Quote]

但是 distinct 和 case 我想操作的不是同一列啊
htl258_Tony 2009-07-29
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 feixianxxx 的回复:]
引用 23 楼 htl258 的回复:
引用 21 楼 qq497525725 的回复:
我的意思是count 里 case  和 distinct  都加上可以吗 怎么加

SQL codeIFOBJECT_ID('tb')ISNOTNULLDROPTABLE tbGOCREATETABLE tb(idint ,list_idint,is_typeint )goinsert tbSELECT1,2,1UNIONALLSELECT2,1,0UNIONALLSELECT3,2,1UNIONALLSELECT4,4,1UNIONALLSELECT5,1,1goselectCOUNT(distinctcase list_idwhen2then1else list_idend)from tb/*
-----------
2

(1 行受影响)*/可以加,如这样.

结果都不对。。
[/Quote]结果就是2,两个2已经都视为1,加上一个4,一个1一个4,合计2个.
guguda2008 2009-07-29
  • 打赏
  • 举报
回复
DECLARE @T TABLE(A INT)
INSERT INTO @T
SELECT 1 UNION ALL

SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4

SELECT COUNT(DISTINCT CASE A WHEN 3 THEN 1 ELSE A END )
FROM @T
feixianxxx 2009-07-29
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 htl258 的回复:]
引用 21 楼 qq497525725 的回复:
我的意思是count 里 case  和 distinct  都加上可以吗 怎么加

SQL codeIFOBJECT_ID('tb')ISNOTNULLDROPTABLE tbGOCREATETABLE tb(idint ,list_idint,is_typeint )goinsert tbSELECT1,2,1UNIONALLSELECT2,1,0UNIONALLSELECT3,2,1UNIONALLSELECT4,4,1UNIONALLSELECT5,1,1goselectCOUNT(distinctcase list_idwhen2then1else list_idend)from tb/*
-----------
2

(1 行受影响)*/可以加,如这样.
[/Quote]
结果都不对。。
feixianxxx 2009-07-29
  • 打赏
  • 举报
回复
count 里面不能用聚合函数的
htl258_Tony 2009-07-29
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 qq497525725 的回复:]
我的意思是count 里 case  和 distinct  都加上可以吗 怎么加
[/Quote]
IF OBJECT_ID('tb') IS NOT NULL
DROP TABLE tb
GO
CREATE TABLE tb(id int ,list_id int,is_type int )
go
insert tb SELECT 1,2,1
UNION ALL SELECT 2,1,0
UNION ALL SELECT 3,2,1
UNION ALL SELECT 4,4,1
UNION ALL SELECT 5,1,1
go
select COUNT(distinct case list_id when 2 then 1 else list_id end) from tb
/*
-----------
2

(1 行受影响)
*/
可以加,如这样.
加载更多回复(22)

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧