还是查询不会写。。

joejoe1991 2008-08-01 02:14:39
表结构:
CREATE TABLE [wenZhangTongJi] (
[hId] [int] IDENTITY (1, 1) NOT NULL ,
[hFrom] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[hTDate] [datetime] NOT NULL CONSTRAINT [DF_wenZhangTongJi_hTDate] DEFAULT (getdate()),
[hAdmin] [int] NOT NULL ,
[hBszt] [int] NOT NULL CONSTRAINT [DF_wenZhangTongJi_hBszt] DEFAULT (0),
CONSTRAINT [PK_wenZhangTongJi] PRIMARY KEY CLUSTERED
(
[hId]
) ON [PRIMARY]
) ON [PRIMARY]
GO

hId 编号
hFrom 数据来自哪个城市
hTDate 添加这条数据的时间,默认当前时间
hAdmin 哪个管理员添加的
hBszt 数据的状态,0表示未审核,1表示已接收,2表示未接收

现有数据:
hId hFrom hTDate hAdmin hBszt
26 邯郸 2008-8-1 10:49:25 2 1
27 邯郸 2008-8-1 10:49:29 2 1
28 邯郸 2008-8-1 10:49:33 2 2
29 邯郸 2008-8-1 10:49:37 2 1
30 邯郸 2008-7-1 10:49:48 2 1
31 邯郸 2008-7-1 10:49:51 2 2
32 邯郸 2007-7-16 10:50:04 2 1
33 邯郸 2007-7-16 10:50:09 2 0
34 邯郸 2008-8-16 10:59:59 2 2
35 邯郸 2008-8-1 11:01:12 2 0
36 沧州 2008-8-1 11:48:24 5 1
37 沧州 2008-8-1 11:49:25 5 2


现在要查询出来2008年,各个城市共提交了多少条数据,有多少条数据没有被审核,有多少条已被接收,有多少条没有被接收。

要求显示成这样的格式:


城市 总数据量 未审核的数量 已接收的数量 未接收的数量
邯郸 xx xx xx xx
沧州 xx xx xx xx


谢谢回答。。
...全文
137 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
linguojin11 2008-08-01
  • 打赏
  • 举报
回复
可以用sum(case when [列名] then 1 else 0 end)
ying_wzm 2008-08-01
  • 打赏
  • 举报
回复
select d.hfrom,d.alls,a.wsh,b.yjs,c.wjs from (select hfrom,alls=count(*) from wenZhangTongJi group by hfrom)d
left join
(select hfrom,wsh=count(*) from wenZhangTongJi where hbszt=0 group by hfrom ) a on a.hfrom=d.hfrom
left join
(select hfrom,yjs=count(*) from wenZhangTongJi where hbszt=1 group by hfrom) b on b.hfrom=d.hfrom
left join
(select hfrom,wjs=count(*) from wenZhangTongJi where hbszt=2 group by hfrom) c on c.hfrom=d.hfrom
芸香传奇 2008-08-01
  • 打赏
  • 举报
回复
select
Hfrom as 城市,
count(1) as 总数量,
sum(case when hBszt=0 then 1 else 0 end) as 未审核的数量,
sum(case when hBszt=1 then 1 else 0 end) as 已接收的数量,
sum(case when hBszt=2 then 1 else 0 end) as 未接收的数量
from wenZhangTongJi where hTDate >=2008 group by hFrom

joejoe1991 2008-08-01
  • 打赏
  • 举报
回复
结贴。。
LIHY70 2008-08-01
  • 打赏
  • 举报
回复

select hFrom as 城市, 总数据量=count(hFrom),
未审核的数量=sum(case when hBszt=0 then 1 else 0 end),
已接收的数量=sum(case when hBszt=1 then 1 else 0 end),
未接收的数量=sum(case when hBszt=2 then 1 else 0 end)
from wenZhangTongJi
where left(hTDate,4)='2008'
group by hFrom

LIHY70 2008-08-01
  • 打赏
  • 举报
回复


select hFrom as 城市, 总数据量=count(*),
未审核的数量=sum(case when hBszt=0 then 1 else 0 end),
已接收的数量=sum(case when hBszt=1 then 1 else 0 end),
未接收的数量=sum(case when hBszt=2 then 1 else 0 end)
from wenZhangTongJi
where left(hTDate,4)='2008'
group by hFrom


laorer 2008-08-01
  • 打赏
  • 举报
回复
交叉表?
hery2002 2008-08-01
  • 打赏
  • 举报
回复
.......
水族杰纶 2008-08-01
  • 打赏
  • 举报
回复
declare @t table
(hId int , hFrom varchar(10), hTDate datetime,hAdmin int, hBszt int )
insert @t select 26, '邯郸', '2008-8-1 10:49:25', 2, 1 union all select
27, '邯郸',' 2008-8-1 10:49:29', 2, 1 union all select
28, '邯郸',' 2008-8-1 10:49:33', 2, 2 union all select
29, '邯郸','2008-8-1 10:49:37' ,2 ,1 union all select
30, '邯郸', '2008-7-1 10:49:48', 2 ,1 union all select
31, '邯郸', '2008-7-1 10:49:51', 2 ,2 union all select
32, '邯郸', '2007-7-16 10:50:04', 2, 1 union all select
33, '邯郸', '2007-7-16 10:50:09', 2, 0 union all select
34, '邯郸', '2008-8-16 10:59:59', 2, 2 union all select
35, '邯郸', '2008-8-1 11:01:12', 2, 0 union all select
36, '沧州', '2008-8-1 11:48:24', 5, 1 union all select
37, '沧州', '2008-8-1 11:49:25', 5, 2
select
[hFrom] as 城市,
count(*) as 總数量,
sum(case when hBszt=0 then 1 else 0 end) as 未审核的数量,
sum(case when hBszt=1 then 1 else 0 end) as 已接收的数量,
sum(case when hBszt=2 then 1 else 0 end) as 未接收的数量
from
@t
group by [hFrom]
hery2002 2008-08-01
  • 打赏
  • 举报
回复
-->生成测试数据

declare @tb table([hId] int,[hFrom] nvarchar(2),[hTDate] Datetime,[hAdmin] int,[hBszt] nvarchar(1))
Insert @tb
select 26,N'邯郸','2008-8-1 10:49:25',2,N'1' union all
select 27,N'邯郸','2008-8-1 10:49:29',2,N'1' union all
select 28,N'邯郸','2008-8-1 10:49:33',2,N'2' union all
select 29,N'邯郸','2008-8-1 10:49:37',2,N'1' union all
select 30,N'邯郸','2008-7-1 10:49:48',2,N'1' union all
select 31,N'邯郸','2008-7-1 10:49:51',2,N'2' union all
select 32,N'邯郸','2007-7-16 10:50:04',2,N'1' union all
select 33,N'邯郸','2007-7-16 10:50:09',2,N'0' union all
select 34,N'邯郸','2008-8-16 10:59:59',2,N'2' union all
select 35,N'邯郸','2008-8-1 11:01:12',2,N'0' union all
select 36,N'沧州','2008-8-1 11:48:24',5,N'1' union all
select 37,N'沧州','2008-8-1 11:49:25',5,N'2'
Select [hFrom], count(1) as 总数据量,
sum(case when [hBszt] =0 then 1 else 0 end ) 未审核的数量,
sum(case when [hBszt] =1 then 1 else 0 end ) 已接收的数量,
sum(case when [hBszt] =2 then 1 else 0 end ) 未接收的数量
from @tb group by [hFrom]
/*
hFrom 总数据量 未审核的数量 已接收的数量 未接收的数量
----- ----------- ----------- ----------- -----------
沧州 2 0 1 1
邯郸 10 2 5 3
*/
中国风 2008-08-01
  • 打赏
  • 举报
回复
CREATE TABLE [wenZhangTongJi] (
[hId] [int] NOT NULL ,
[hFrom] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[hTDate] [datetime] NOT NULL CONSTRAINT [DF_wenZhangTongJi_hTDate] DEFAULT (getdate()),
[hAdmin] [int] NOT NULL ,
[hBszt] [int] NOT NULL CONSTRAINT [DF_wenZhangTongJi_hBszt] DEFAULT (0),
CONSTRAINT [PK_wenZhangTongJi] PRIMARY KEY CLUSTERED
(
[hId]
) ON [PRIMARY]
) ON [PRIMARY]
Insert [wenZhangTongJi]
select 26,N'邯郸','2008-8-1 10:49:25',2,1 union all
select 27,N'邯郸','2008-8-1 10:49:29',2,1 union all
select 28,N'邯郸','2008-8-1 10:49:33',2,2 union all
select 29,N'邯郸','2008-8-1 10:49:37',2,1 union all
select 30,N'邯郸','2008-7-1 10:49:48',2,1 union all
select 31,N'邯郸','2008-7-1 10:49:51',2,2 union all
select 32,N'邯郸','2007-7-16 10:50:04',2,1 union all
select 33,N'邯郸','2007-7-16 10:50:09',2,0 union all
select 34,N'邯郸','2008-8-16 10:59:59',2,2 union all
select 35,N'邯郸','2008-8-1 11:01:12',2,0 union all
select 36,N'沧州','2008-8-1 11:48:24',5,1 union all
select 37,N'沧州','2008-8-1 11:49:25',5,2

go
--drop table [wenZhangTongJi]
select
[hFrom] as 城市,
count(*) as 總数量,
sum(case when hBszt=0 then 1 else 0 end) as 未审核的数量,
sum(case when hBszt=1 then 1 else 0 end) as 已接收的数量,
sum(case when hBszt=2 then 1 else 0 end) as 未接收的数量
from
[wenZhangTongJi]
where
year([hTDate])=2008--加上年份
group by [hFrom]




(12 個資料列受到影響)
城市 總数量 未审核的数量 已接收的数量 未接收的数量
-------------------------------------------------- ----------- ----------- ----------- -----------
沧州 2 0 1 1
邯郸 8 1 4 3

(2 個資料列受到影響)

flairsky 2008-08-01
  • 打赏
  • 举报
回复
很详细了,不跟贴了
jacklygoodluck 2008-08-01
  • 打赏
  • 举报
回复
怎么这么厉害啊
  • 打赏
  • 举报
回复
select hFrom AS 城市
, count(1) AS 总数据量
,sum(case hbszt when '0' then 1 else 0 end) as 未审核的数量
,sum(case hbszt when '0' then 1 else 0 end) as 已接收的数量
,sum(case hbszt when '0' then 2 else 0 end) as 未接收的数量
from wenZhangTongJi
where hTdate between '2008-1-1' and '2008-12-31'
group by hfrom
中国风 2008-08-01
  • 打赏
  • 举报
回复
CREATE TABLE [wenZhangTongJi] (
[hId] [int] NOT NULL ,
[hFrom] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[hTDate] [datetime] NOT NULL CONSTRAINT [DF_wenZhangTongJi_hTDate] DEFAULT (getdate()),
[hAdmin] [int] NOT NULL ,
[hBszt] [int] NOT NULL CONSTRAINT [DF_wenZhangTongJi_hBszt] DEFAULT (0),
CONSTRAINT [PK_wenZhangTongJi] PRIMARY KEY CLUSTERED
(
[hId]
) ON [PRIMARY]
) ON [PRIMARY]
Insert [wenZhangTongJi]
select 26,N'邯郸','2008-8-1 10:49:25',2,1 union all
select 27,N'邯郸','2008-8-1 10:49:29',2,1 union all
select 28,N'邯郸','2008-8-1 10:49:33',2,2 union all
select 29,N'邯郸','2008-8-1 10:49:37',2,1 union all
select 30,N'邯郸','2008-7-1 10:49:48',2,1 union all
select 31,N'邯郸','2008-7-1 10:49:51',2,2 union all
select 32,N'邯郸','2007-7-16 10:50:04',2,1 union all
select 33,N'邯郸','2007-7-16 10:50:09',2,0 union all
select 34,N'邯郸','2008-8-16 10:59:59',2,2 union all
select 35,N'邯郸','2008-8-1 11:01:12',2,0 union all
select 36,N'沧州','2008-8-1 11:48:24',5,1 union all
select 37,N'沧州','2008-8-1 11:49:25',5,2

go
--drop table [wenZhangTongJi]
select
[hFrom] as 城市,
count(*) as 總数量,
sum(case when hBszt=0 then 1 else 0 end) as 未审核的数量,
sum(case when hBszt=1 then 1 else 0 end) as 已接收的数量,
sum(case when hBszt=2 then 1 else 0 end) as 未接收的数量
from
[wenZhangTongJi]
group by [hFrom]



城市 總数量 未审核的数量 已接收的数量 未接收的数量
-------------------------------------------------- ----------- ----------- ----------- -----------
沧州 2 0 1 1
邯郸 10 2 5 3

(2 個資料列受到影響)

jacklygoodluck 2008-08-01
  • 打赏
  • 举报
回复
写存储过程好了

34,576

社区成员

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

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