请教一个SQL查询~

crescent_star 2012-07-19 11:44:47
假设我有一个表 tableA,结构如下:

id int 自增
info varchar(50)
type1 int type1 只有 2 种值,分别为 1,2
type2 int type2 只有 2 种值,分别为 3,4
tcount int

我想这样进行查询,输入一个字符串 query,假设这个 query 是“古天乐”,

select count(*) from A where info='古天乐' 的记录条数总共为200条,

其中:

select count(*) from A where info='古天乐' and type1 = 1 的记录条数为100条,
select count(*) from A where info='古天乐' and type1 = 2 的记录条数为100条,
select count(*) from A where info='古天乐' and type1 = 1 and type2 = 3 的记录数为50条,
select count(*) from A where info='古天乐' and type1 = 1 and type2 = 4 的记录数为50条,
select count(*) from A where info='古天乐' and type1 = 2 and type2 = 3 的记录数为50条,
select count(*) from A where info='古天乐' and type1 = 2 and type2 = 4 的记录数为50条,

想得到一个这样的查询结果,这个结果总共只有两条记录,括号内为解释:

字段名: 古天乐(字段名为query的值,但对应的记录是type1的值) 总记录数 记录数1 记录数2
字段意思:
第一条记录: 1 100(即type1=1) 50(即type1=1,type2=3) 50(即type1=1,type2=4)
第二条记录: 2 100(即type1=2) 50(即type1=2,type2=3) 50(即type1=2,typ2=4)

请问能用一条SQL语句解决么?
...全文
170 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
以学习为目的 2012-07-23
  • 打赏
  • 举报
回复

--author:josy
--editor:galenkeny
select 1 as type,
count(1) as cnt1,
sum(case when type2=3 then 1 else 0 end) as cnt2,
sum(case when type2=4 then 1 else 0 end) as cnt3
from tb
where info='古天乐' and type1=1

union all

select 2 as type,
count(1) as cnt1,
sum(case when type2=3 then 1 else 0 end) as cnt2,
sum(case when type2=4 then 1 else 0 end) as cnt3
from tb
where info='古天乐' and type1=2
jakecheng 2012-07-23
  • 打赏
  • 举报
回复
补充一下啊,上面的临时变量没有赋值哦,要赋值 之后才能用哦
jakecheng 2012-07-23
  • 打赏
  • 举报
回复
declare @name varchar(50);
select count(1),(select count(1) from tablename where type2=3),(select count(1) from tablename where type2=4) from tableName where info=@name
group by type1
tim_spac_126 2012-07-20
  • 打赏
  • 举报
回复
declare @q varchar(80) set @q='古天乐'

select type1
, count(1)
, count(case type2 when 3 then 1 end)
, count(case type2 when 4 then 1 end)
from A
where info=@q
group by type1
tim_spac_126 2012-07-20
  • 打赏
  • 举报
回复
declare @q varchar(80) set @q='古天乐'

select type1
, count(1)
, count(case type2 when 1 then 1 end)
, count(case type2 when 2 then 1 end)
from A
where info=@q
group by type1
百年树人 2012-07-20
  • 打赏
  • 举报
回复
try
select 1 as type,count(1) as cnt1,sum(case when type2=3 then 1 else 0 end) as cnt2,sum(case when type2=4 then 1 else 0 end)  as cnt3 from tb where info='古天乐' and type1=1
union all
select 2,count(1) as cnt1,sum(case when type2=3 then 1 else 0 end) as cnt2,sum(case when type2=4 then 1 else 0 end) as cnt3 from tb where info='古天乐' and type1=2
gogodiy 2012-07-20
  • 打赏
  • 举报
回复

SELECT COUNT(1) AS [总记录数],
SUM(CASE WHEN type2 = 3 THEN 1 ELSE 0 END) AS [记录数1],
SUM(CASE WHEN type2 = 4 THEN 1 ELSE 0 END) AS [记录数2]
FROM A
WHERE info='古天乐'
AND type1 = 1
UNION
SELECT COUNT(1) AS [总记录数],
SUM(CASE WHEN type2 = 3 THEN 1 ELSE 0 END) AS [记录数1],
SUM(CASE WHEN type2 = 4 THEN 1 ELSE 0 END) AS [记录数2]
FROM A
WHERE info='古天乐'
AND type1 = 2
FutureLiu 2012-07-20
  • 打赏
  • 举报
回复
感觉有给古天乐做广告的嫌疑。。。。
crescent_star 2012-07-19
  • 打赏
  • 举报
回复
帖子不能编辑嗯。。。坑爹。。。我怕我没有写清楚啊,四个字段嗯。

字段名: 古天乐(字段名为query的值,但对应的记录是type1的值),总记录数,记录数1,记录数2
第一条记录: 1,100(即type1=1),50(即type1=1,type2=3),50(即type1=1,type2=4)
第二条记录: 2,100(即type1=2),50(即type1=2,type2=3),50(即type1=2,type2=4)

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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