现有
表A
ID AccountName CarCode
-----------------------------------
1 重型汽车 1101,1102,1103,1104
2 中型汽车 1105,1106,1107,1108
3 轻型汽车 1109,1110,1111,1112表B
ID CarCode CarName SalePrice SaleNum Company
-----------------------------------------------
1 1101 东风101 85000 5 公司1
2 1102 跃进102 96000 3 公司2
3 1103 东风103 120000 2 公司1
4 1104 跃进104 135000 1 公司3
5 1105 长安101 84000 4 公司1
6 1106 长安102 97000 3 公司2
7 1107 北京101 78000 7 公司3
8 1108 北京102 86000 5 公司2
9 1109 奇瑞101 28000 4 公司1
10 1110 奇瑞102 34000 6 公司3
11 1111 吉利101 25000 3 公司1
12 1112 吉利102 33000 4 公司2现要得到如下统计结果:
序号 重型汽车 中型汽车 轻型汽车
公司1 665000 336000 187000
公司2 288000 721000 132000
公司3 135000 546000 204000请问查询语句该怎么写??
表A
ID AccountName CarCode
-----------------------------------
1 重型汽车 1101,1102,1103,1104
2 中型汽车 1105,1106,1107,1108
3 轻型汽车 1109,1110,1111,1112表B
ID CarCode CarName SalePrice SaleNum Company
-----------------------------------------------
1 1101 东风101 85000 5 公司1
2 1102 跃进102 96000 3 公司2
3 1103 东风103 120000 2 公司1
4 1104 跃进104 135000 1 公司3
5 1105 长安101 84000 4 公司1
6 1106 长安102 97000 3 公司2
7 1107 北京101 78000 7 公司3
8 1108 北京102 86000 5 公司2
9 1109 奇瑞101 28000 4 公司1
10 1110 奇瑞102 34000 6 公司3
11 1111 吉利101 25000 3 公司1
12 1112 吉利102 33000 4 公司2现要得到如下统计结果:
序号 重型汽车 中型汽车 轻型汽车
公司1 665000 336000 187000
公司2 288000 721000 132000
公司3 135000 546000 204000请问查询语句该怎么写??
解决方案 »
- 求高手帮忙。。。。
- 如何实现如下所述目的的数据库备份目的?
- sql server 主鍵為多個字段的查詢條件
- SQL语句,没有没办法用零填充空白的列,100分送上
- SQL数据文件被替换,如何恢复
- SQL语句求助贴
- 有一条SQL语句不知怎么写?是关于字符串合并的语句,急!帮忙!谢谢!
- 为什么有时候在查询分析器中右键打开表时,这个表是只读的?
- 如何用vb获取某字段的默认值? 急急急!!
- sql,table中 1.有哪個數據類型可以處理數值型1/3,1/7,1/9等不能除盡的數值;2.有哪個數據類型可以處理“所有”數據類型
- sql2005 大哥大姐帮帮忙
- 大家给看看这个sql怎么错了,提示"第 2 行: '=' 附近有语法错误。"
(id int identity,AccountName varchar(10),carcode varchar(30))
insert into a select '重型汽车' , '1101,1102,1103,1104'
insert into a select '中型汽车' , '1105,1106,1107,1108'
insert into a select '轻型汽车' , '1109,1110,1111,1112' create table B
(id int ,carcode varchar(10),carname varchar(10),saleprice int,salenum int,company varchar(10))
insert into b select 1 ,'1101', '东风101', 85000 , 5 , '公司1'
insert into b select 2 ,'1102', '跃进102', 96000 , 3 , '公司2'
insert into b select 3 ,'1103', '东风103', 120000 , 2 , '公司1'
insert into b select 4 ,'1104', '跃进104', 135000 , 1 , '公司3'
insert into b select 5 ,'1105', '长安101', 84000 , 4 , '公司1'
insert into b select 6 ,'1106', '长安102', 97000 , 3 , '公司2'
insert into b select 7 ,'1107', '北京101', 78000 , 7 , '公司3'
insert into b select 8 ,'1108', '北京102', 86000 , 5 , '公司2'
insert into b select 9 ,'1109', '奇瑞101', 28000 , 4 , '公司1'
insert into b select 10 ,'1110', '奇瑞102', 34000 , 6 , '公司3'
insert into b select 11 ,'1111', '吉利101', 25000 , 3 , '公司1'
insert into b select 12 ,'1112', '吉利102', 33000 , 4 , '公司2' create view c
as
select a.accountname,saleprice,salenum,company from a ,b
where Charindex(b.carcode,a.carcode)>0select * from cdeclare @sql varchar(1000)
set @sql='select company '
select @sql=@sql+',sum(case when accountname='''+accountname+''' then salenum*saleprice else 0 end) as '+accountname
from (select distinct accountname from c ) a
set @sql=@sql+' from c group by company'
exec(@sql)company 轻型汽车 中型汽车 重型汽车
---------- ----------- ----------- -----------
公司1 187000 336000 665000
公司2 132000 721000 288000
公司3 204000 546000 135000(3 行受影响)
declare @a char(100),@b char(100),@c char(100),@d char(100),@e char(100),@f char(100),@g char(100),@h char(100),@i char(100)
select @a= sum(saleprice * salenum) from B where company='公司1' and carcode in ('1101','1102','1103','1104')
select @b= sum(saleprice * salenum) from B where company='公司1' and carcode in ('1105','1106','1107','1108')
select @c= sum(saleprice * salenum) from B where company='公司1' and carcode in ('1109','1110','11011','1112')
select @d= sum(saleprice * salenum) from B where company='公司2' and carcode in ('1101','1102','1103','1104')
select @e= sum(saleprice * salenum) from B where company='公司2' and carcode in ('1105','1106','1107','1108')
select @f= sum(saleprice * salenum) from B where company='公司2' and carcode in ('1109','1110','11011','1112')
select @g= sum(saleprice * salenum) from B where company='公司3' and carcode in ('1101','1102','1103','1104')
select @h= sum(saleprice * salenum) from B where company='公司3' and carcode in ('1105','1106','1107','1108')
select @i= sum(saleprice * salenum) from B where company='公司3' and carcode in ('1109','1110','11011','1112')
select '公司1'as company,@a as 重型汽车,@b as 中型汽车,@c as 中型汽车 union all
select '公司2'as company,@d as 重型汽车,@e as 中型汽车,@f as 中型汽车 union all
select '公司3'as company,@g as 重型汽车,@h as 中型汽车,@i as 中型汽车
insert into A values(1, '重型汽车', '1101,1102,1103,1104')
insert into A values(2, '中型汽车', '1105,1106,1107,1108')
insert into A values(3, '轻型汽车', '1109,1110,1111,1112')
create table B(ID int, CarCode varchar(4),CarName varchar(10),SalePrice int,SaleNum int, Company varchar(10))
insert into B values(1 , '1101', '东风101', 85000 , 5 , '公司1')
insert into B values(2 , '1102', '跃进102', 96000 , 3 , '公司2')
insert into B values(3 , '1103', '东风103', 120000 , 2 , '公司1')
insert into B values(4 , '1104', '跃进104', 135000 , 1 , '公司3')
insert into B values(5 , '1105', '长安101', 84000 , 4 , '公司1')
insert into B values(6 , '1106', '长安102', 97000 , 3 , '公司2')
insert into B values(7 , '1107', '北京101', 78000 , 7 , '公司3')
insert into B values(8 , '1108', '北京102', 86000 , 5 , '公司2')
insert into B values(9 , '1109', '奇瑞101', 28000 , 4 , '公司1')
insert into B values(10, '1110', '奇瑞102', 34000 , 6 , '公司3')
insert into B values(11, '1111', '吉利101', 25000 , 3 , '公司1')
insert into B values(12, '1112', '吉利102', 33000 , 4 , '公司2')
goselect company ,
sum(case accountname when '重型汽车' then saleprice else 0 end) '重型汽车',
sum(case accountname when '中型汽车' then saleprice else 0 end) '中型汽车',
sum(case accountname when '轻型汽车' then saleprice else 0 end) '轻型汽车'
from
(
select B.Company , b.SalePrice , A.AccountName from B,A where charindex(','+B.carcode+',' , ',' + A.carcode + ',') > 0
) t
group by companydrop table A,B/*
company 重型汽车 中型汽车 轻型汽车
---------- ----------- ----------- -----------
公司1 205000 84000 53000
公司2 96000 183000 33000
公司3 135000 78000 34000(所影响的行数为 3 行)
*/
insert into A values(1, '重型汽车', '1101,1102,1103,1104 ')
insert into A values(2, '中型汽车', '1105,1106,1107,1108 ')
insert into A values(3, '轻型汽车', '1109,1110,1111,1112 ')
create table B(ID int, CarCode varchar(4),CarName varchar(10),SalePrice int,SaleNum int, Company varchar(10))
insert into B values(1 , '1101 ', '东风101 ', 85000 , 5 , '公司1 ')
insert into B values(2 , '1102 ', '跃进102 ', 96000 , 3 , '公司2 ')
insert into B values(3 , '1103 ', '东风103 ', 120000 , 2 , '公司1 ')
insert into B values(4 , '1104 ', '跃进104 ', 135000 , 1 , '公司3 ')
insert into B values(5 , '1105 ', '长安101 ', 84000 , 4 , '公司1 ')
insert into B values(6 , '1106 ', '长安102 ', 97000 , 3 , '公司2 ')
insert into B values(7 , '1107 ', '北京101 ', 78000 , 7 , '公司3 ')
insert into B values(8 , '1108 ', '北京102 ', 86000 , 5 , '公司2 ')
insert into B values(9 , '1109 ', '奇瑞101 ', 28000 , 4 , '公司1 ')
insert into B values(10, '1110 ', '奇瑞102 ', 34000 , 6 , '公司3 ')
insert into B values(11, '1111 ', '吉利101 ', 25000 , 3 , '公司1 ')
insert into B values(12, '1112 ', '吉利102 ', 33000 , 4 , '公司2 ')
go select company ,
sum(case accountname when '重型汽车 ' then SalePrice * SaleNum else 0 end) '重型汽车 ',
sum(case accountname when '中型汽车 ' then SalePrice * SaleNum else 0 end) '中型汽车 ',
sum(case accountname when '轻型汽车 ' then SalePrice * SaleNum else 0 end) '轻型汽车 '
from
(
select B.Company , B.SalePrice , B.SaleNum, A.AccountName from B,A where charindex( ','+B.carcode+ ',' , ',' + A.carcode + ',') > 0
) t
group by company drop table A,B
--动态SQL
create table A(ID int,AccountName varchar(10),CarCode varchar(50))
insert into A values(1, '重型汽车', '1101,1102,1103,1104')
insert into A values(2, '中型汽车', '1105,1106,1107,1108')
insert into A values(3, '轻型汽车', '1109,1110,1111,1112')
create table B(ID int, CarCode varchar(4),CarName varchar(10),SalePrice int,SaleNum int, Company varchar(10))
insert into B values(1 , '1101', '东风101 ', 85000 , 5 , '公司1')
insert into B values(2 , '1102', '跃进102 ', 96000 , 3 , '公司2')
insert into B values(3 , '1103', '东风103 ', 120000 , 2 , '公司1')
insert into B values(4 , '1104', '跃进104 ', 135000 , 1 , '公司3')
insert into B values(5 , '1105', '长安101 ', 84000 , 4 , '公司1')
insert into B values(6 , '1106', '长安102 ', 97000 , 3 , '公司2')
insert into B values(7 , '1107', '北京101 ', 78000 , 7 , '公司3')
insert into B values(8 , '1108', '北京102 ', 86000 , 5 , '公司2')
insert into B values(9 , '1109', '奇瑞101 ', 28000 , 4 , '公司1')
insert into B values(10, '1110', '奇瑞102 ', 34000 , 6 , '公司3')
insert into B values(11, '1111', '吉利101 ', 25000 , 3 , '公司1')
insert into B values(12, '1112', '吉利102 ', 33000 , 4 , '公司2')
go
declare @sql varchar(8000)
set @sql = 'select Company '
select @sql = @sql + ' , sum(case AccountName when ''' + AccountName + ''' then SalePrice * SaleNum else 0 end) [' + AccountName + ']'
from (select distinct AccountName from (select B.Company , B.SalePrice , B.SaleNum, A.AccountName from B,A where charindex(','+B.carcode+',' , ',' + A.carcode + ',') > 0) t) as a
set @sql = @sql + ' from (select B.Company , B.SalePrice , B.SaleNum, A.AccountName from B,A where charindex('',''+B.carcode+'','' , '','' + A.carcode + '','') > 0) t group by Company '
exec(@sql) drop table A,B /*
Company 轻型汽车 中型汽车 重型汽车
---------- ----------- ----------- -----------
公司1 187000 336000 665000
公司2 132000 721000 288000
公司3 204000 546000 135000
*/