如何为这句交叉查询语句加where条件

seven7676 2011-09-27 11:06:25
tableA

Id PersonID Level LogTime
1 01 1 2011-8-29
3 05 3 2011-7-24
7 07 2 2011-8-25




tableC

LevelId LevelName
1 级别一
2 级别二
3 级别三



我想要的结果是用交叉表查询出一个(X行显示tableC的LevelName,有多少显示多少,y列显示tableA的PersonID,有多少显示多少

还有就是对于X行的每行最后要来个对该行数量进行总计,对于y列的每列要在最后来个对该列的数字进行总计



if object_id('[tableA]') is not null drop table [tableA]
go
create table [tableA]([Id] int,[PersonID] varchar(20),[Level] int,[LogTime] datetime)
insert [tableA]
select 1,'01',1,'2011-8-29' union all
select 3,'05',3,'2011-7-24' union all
select 7,'07',2,'2011-8-25'
--> 测试数据:[tableC]
if object_id('[tableC]') is not null drop table [tableC]
go
create table [tableC]([LevelId] int,[LevelName] varchar(6))
insert [tableC]
select 1,'级别一' union all
select 2,'级别二' union all
select 3,'级别三'
--------------开始查询--------------------------
declare @sql varchar(8000)
set @sql = 'select isnull(a.personid,''总计'') '
select @sql = @sql + ' , sum(case b.LevelName when ''' + LevelName + ''' then 1 else 0 end) [' + LevelName + ']'
from (select distinct LevelName from tablec) as a
set @sql = @sql + ', count(a.PersonID) as 总计 from tablea a join tablec b on a.Level=b.Levelid group by a.personid with rollup'
exec(@sql)
----------------结果----------------------------
/* 级别二 级别三 级别一 总计
-------------------- ----------- ----------- ----------- -----------
01 0 0 1 1
05 0 1 0 1
07 1 0 0 1
总计 1 1 1 3


上面的SQL语句可以实现效果
但是现在我想再加个where语句,where PersonID=07 and LogTime=2011-8-25 and Level=2
请问这个where语句应该放在上面的SQL语句中的哪里?


...全文
150 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
--小F-- 2011-09-27
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql = 'select case when a.personid is null then ''总计'' else '''' end '
select @sql = @sql + ' , sum(case b.LevelName when ''' + LevelName + ''' then 1 else 0 end) [' + LevelName + ']'
from (select distinct LevelName from tablec) as a
set @sql = @sql + ', count(a.PersonID) as 总计 from tablea a join tablec b on a.Level=b.Levelid
where PersonID=''07'' and LogTime=''2011-8-25'' and Level=2
group by a.personid with rollup'
exec(@sql)
-晴天 2011-09-27
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 seven7676 的回复:]
引用 6 楼 seven7676 的回复:
引用 3 楼 fredrickhu 的回复:
SQL code
if object_id('[tableA]') is not null drop table [tableA]
go
create table [tableA]([Id] int,[PersonID] varchar(20),[Level] int,[LogTime] datet……
说错了,07是PersonID,不能去掉的

[/Quote]

去掉的在上面...
奔四在望 2011-09-27
  • 打赏
  • 举报
回复
帮顶了
-晴天 2011-09-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 seven7676 的回复:]
引用 3 楼 fredrickhu 的回复:
SQL code
if object_id('[tableA]') is not null drop table [tableA]
go
create table [tableA]([Id] int,[PersonID] varchar(20),[Level] int,[LogTime] datetime)
insert [tableA]
……
[/Quote]
create table [tableA]([Id] int,[PersonID] varchar(20),[Level] int,[LogTime] datetime)
insert [tableA]
select 1,'01',1,'2011-8-29' union all
select 3,'05',3,'2011-7-24' union all
select 7,'07',2,'2011-8-25'
--> 测试数据:[tableC]
if object_id('[tableC]') is not null drop table [tableC]
go
create table [tableC]([LevelId] int,[LevelName] varchar(6))
insert [tableC]
select 1,'级别一' union all
select 2,'级别二' union all
select 3,'级别三'
go

--------------开始查询--------------------------

declare @sql varchar(8000)
set @sql = 'select case when a.personid is null then ''总计'' else '''' end '
select @sql = @sql + ' , sum(case b.LevelName when ''' + LevelName + ''' then 1 else 0 end) [' + LevelName + ']'
from (select distinct LevelName from tablec) as a
set @sql = @sql + ', count(a.PersonID) as 总计 from tablea a join tablec b on a.Level=b.Levelid
where PersonID=''07'' and LogTime=''2011-8-25'' and Level=2
group by a.personid with rollup'
exec(@sql)
/*
级别二 级别三 级别一 总计
---- ----------- ----------- ----------- -----------
1 0 0 1
总计 1 0 0 1

(2 行受影响)

*/
seven7676 2011-09-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 seven7676 的回复:]
引用 3 楼 fredrickhu 的回复:
SQL code
if object_id('[tableA]') is not null drop table [tableA]
go
create table [tableA]([Id] int,[PersonID] varchar(20),[Level] int,[LogTime] datetime)
insert [tableA]
……
[/Quote]



说错了,07是PersonID,不能去掉的
-晴天 2011-09-27
  • 打赏
  • 举报
回复
create table [tableA]([Id] int,[PersonID] varchar(20),[Level] int,[LogTime] datetime)
insert [tableA]
select 1,'01',1,'2011-8-29' union all
select 3,'05',3,'2011-7-24' union all
select 7,'07',2,'2011-8-25'
--> 测试数据:[tableC]
if object_id('[tableC]') is not null drop table [tableC]
go
create table [tableC]([LevelId] int,[LevelName] varchar(6))
insert [tableC]
select 1,'级别一' union all
select 2,'级别二' union all
select 3,'级别三'
go

--------------开始查询--------------------------

declare @sql varchar(8000)
set @sql = 'select isnull(a.personid,''总计'') '
select @sql = @sql + ' , sum(case b.LevelName when ''' + LevelName + ''' then 1 else 0 end) [' + LevelName + ']'
from (select distinct LevelName from tablec) as a
set @sql = @sql + ', count(a.PersonID) as 总计 from tablea a join tablec b on a.Level=b.Levelid
where PersonID=''07'' and LogTime=''2011-8-25'' and Level=2
group by a.personid with rollup'
exec(@sql)
/*
级别二 级别三 级别一 总计
-------------------- ----------- ----------- ----------- -----------
07 1 0 0 1
总计 1 0 0 1

(2 行受影响)

*/
seven7676 2011-09-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 fredrickhu 的回复:]
SQL code
if object_id('[tableA]') is not null drop table [tableA]
go
create table [tableA]([Id] int,[PersonID] varchar(20),[Level] int,[LogTime] datetime)
insert [tableA]
select 1,'01',1,'2011-……
[/Quote]




----------------结果----------------------------
/* 级别二 级别三 级别一 总计
-------------------- ----------- ----------- ----------- -----------
07 1 0 0 1
总计 1 0 0 1


效果是有了,但是如何将左上角的07去掉?
jyh070207 2011-09-27
  • 打赏
  • 举报
回复
放在group by前面

--------------开始查询--------------------------
declare @sql varchar(8000)
set @sql = 'select isnull(a.personid,''总计'') '
select @sql = @sql + ' , sum(case b.LevelName when ''' + LevelName + ''' then 1 else 0 end) [' + LevelName + ']'
from (select distinct LevelName from tablec) as a
set @sql = @sql + ', count(a.PersonID) as 总计 from tablea a join tablec b on a.Level=b.Levelid
where a.PersonID=07 and a.LogTime=''2011-8-25'' and a.Level=2
group by a.personid with rollup'
exec(@sql)
AcHerat 元老 2011-09-27
  • 打赏
  • 举报
回复

declare @sql varchar(8000)
set @sql = 'select isnull(a.personid,''总计'') '
select @sql = @sql + ' , sum(case b.LevelName when ''' + LevelName + ''' then 1 else 0 end) [' + LevelName + ']'
from (select distinct LevelName from tablec) as a
set @sql = @sql + ', count(a.PersonID) as 总计 from tablea a join tablec b on a.Level=b.Levelid '
+'where a.PersonID=''07'' and a.LogTime=''2011-8-25'' and a.[Level]=2 group by a.personid with rollup'
exec(@sql)
--小F-- 2011-09-27
  • 打赏
  • 举报
回复
if object_id('[tableA]') is not null drop table [tableA]
go
create table [tableA]([Id] int,[PersonID] varchar(20),[Level] int,[LogTime] datetime)
insert [tableA]
select 1,'01',1,'2011-8-29' union all
select 3,'05',3,'2011-7-24' union all
select 7,'07',2,'2011-8-25'
--> 测试数据:[tableC]
if object_id('[tableC]') is not null drop table [tableC]
go
create table [tableC]([LevelId] int,[LevelName] varchar(6))
insert [tableC]
select 1,'级别一' union all
select 2,'级别二' union all
select 3,'级别三'
--------------开始查询--------------------------
declare @sql varchar(8000)
set @sql = 'select isnull(a.personid,''总计'') '
select @sql = @sql + ' , sum(case b.LevelName when ''' + LevelName + ''' then 1 else 0 end) [' + LevelName + ']'
from (select distinct LevelName from tablec) as a
set @sql = @sql + ', count(a.PersonID) as 总计 from tablea a join tablec b on a.Level=b.Levelid where PersonID=07 and LogTime=''2011-8-25'' and Level=2
group by a.personid with rollup'
exec(@sql)
----------------结果----------------------------
/* 级别二 级别三 级别一 总计
-------------------- ----------- ----------- ----------- -----------
07 1 0 0 1
总计 1 0 0 1

(2 行受影响)
*/
AcHerat 元老 2011-09-27
  • 打赏
  • 举报
回复

declare @sql varchar(8000)
set @sql = 'select isnull(a.personid,''总计'') '
select @sql = @sql + ' , sum(case b.LevelName when ''' + LevelName + ''' then 1 else 0 end) [' + LevelName + ']'
from (select distinct LevelName from tablec) as a
set @sql = @sql + ', count(a.PersonID) as 总计 from tablea a join tablec b on a.Level=b.Levelid where PersonID=07 and LogTime=2011-8-25 and Level=2
group by a.personid with rollup'
exec(@sql)
chuanzhang5687 2011-09-27
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql = 'select isnull(a.personid,''总计'') '
select @sql = @sql + ' , sum(case b.LevelName when ''' + LevelName + ''' then 1 else 0 end) [' + LevelName + ']'
from (select distinct LevelName from tablec) as a
set @sql = @sql + ', count(a.PersonID) as 总计 from tablea a join tablec b on a.Level=b.Levelid
where PersonID=07 and LogTime=2011-8-25 and Level=2 group by a.personid with rollup'
exec(@sql)

34,873

社区成员

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

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