发布网友 发布时间:2022-04-22 08:20
共9个回答
热心网友 时间:2022-04-11 16:59
create TABLE Table1 (Name varchar(50),Subject varchar(50))
INSERT INTO TABLE1 VALUES('A','a1')
INSERT INTO TABLE1 VALUES('A','a2')
INSERT INTO TABLE1 VALUES('A','a3')
INSERT INTO TABLE1 VALUES('A','a4')
INSERT INTO TABLE1 VALUES('A','a5')
INSERT INTO TABLE1 VALUES('A','a6')
INSERT INTO TABLE1 VALUES('A','a7')
INSERT INTO TABLE1 VALUES('B','b1')
INSERT INTO TABLE1 VALUES('B','b2')
INSERT INTO TABLE1 VALUES('B','b3')
INSERT INTO TABLE1 VALUES('B','b4')
INSERT INTO TABLE1 VALUES('B','b5')
INSERT INTO TABLE1 VALUES('B','b11')
INSERT INTO TABLE1 VALUES('B','b23')
INSERT INTO TABLE1 VALUES('B','b34')
INSERT INTO TABLE1 VALUES('B','b8')
INSERT INTO TABLE1 VALUES('B','b9')
INSERT INTO TABLE1 VALUES('B','b15')
就生成了表单TABLE1,如果把公司名称A,B横向输出,Subject的话,比较简单的是把Subject显示在1列,里面元素用","隔开,如果分列显示,就要找到Subject最长的列,否则影响效率;
因为不知道你的参数最多是多少,所以就在1列中显示.
可以写一个函数F_str
create function F_str(@name varchar)
returns nvarchar(100)
as
begin
declare @S nvarchar(100)
select @S=isnull(@S+',','')+Subject from table1 where Name=@name
return @S
end
生成函数后在调用即可:
select distinct Name,Subject=dbo.F_str(Name) from Table1
自己用的话注意每次实验删除函数,不然创建2次重名的函数是不可以的
因为我的Name是动态读取的,不管多少公司,都是没问题的,唯一的不同就是
产品显示出来是在一列当中,而不是分列显示,不知道满足你的需求吗.
Name Subject
A a1,a2,a3.....
B b1,b2,b3.....
C c1,c2,c3.....
... .....
这种格式的,Subject是不分列的,在一列.
要是需要分列的话,实现比较麻烦,需要的话,和我说.
declare @c int,@sql varchar(5000)
set @c=(select max(a) from (select count(*)a from table1 group by name)b)
declare @i int
set @i=1
select x=@c,y='列'+cast(@c as varchar) into #temp
while @i<@c
begin
insert into #temp values(@i,'列'+cast(@i as varchar))
set @i=@i+1
end
select y,name,subject into #temp2 from #temp t left join (select ROW_NUMBER() over(PARTITION BY name order by name) as id,name,subject from table1 ) tb on tb.id=t.x
set @sql = 'select name'
select @sql = @sql + ' , max(case y when ''' + y + ''' then subject else null end )[' + y + ']'
from (select distinct y from #temp2 ) as a
set @sql = @sql + ' from #temp2 group by name'
exec(@sql)
drop table #temp
drop table #temp2
SQL2005可以用,2000估计不能用,2000函数不全
热心网友 时间:2022-04-11 18:17
示例
A. 使用带有简单 CASE 函数的 SELECT 语句
在 SELECT 语句中,简单 CASE 函数仅检查是否相等,而不进行其它比较。下面的示例使用 CASE 函数更改图书分类显示,以使其更易于理解。
USE pubs
GO
SELECT Category =
CASE type
WHEN 'popular_comp' THEN 'Popular Computing'
WHEN 'mod_cook' THEN 'Modern Cooking'
WHEN 'business' THEN 'Business'
WHEN 'psychology' THEN 'Psychology'
WHEN 'trad_cook' THEN 'Traditional Cooking'
ELSE 'Not yet categorized'
END,
CAST(title AS varchar(25)) AS 'Shortened Title',
price AS Price
FROM titles
WHERE price IS NOT NULL
ORDER BY type, price
COMPUTE AVG(price) BY type
GO
下面是结果集:
Category Shortened Title Price
------------------- ------------------------- --------------------------
Business You Can Combat Computer S 2.99
Business Cooking with Computers: S 11.95
Business The Busy Executive's Data 19.99
Business Straight Talk About Compu 19.99
avg
==========================
13.73
Category Shortened Title Price
------------------- ------------------------- --------------------------
Modern Cooking The Gourmet Microwave 2.99
Modern Cooking Silicon Valley Gastronomi 19.99
avg
==========================
11.49
Category Shortened Title Price
------------------- ------------------------- --------------------------
Popular Computing Secrets of Silicon Valley 20.00
Popular Computing But Is It User Friendly? 22.95
avg
==========================
21.48
Category Shortened Title Price
------------------- ------------------------- --------------------------
Psychology Life Without Fear 7.00
Psychology Emotional Security: A New 7.99
Psychology Is Anger the Enemy? 10.95
Psychology Prolonged Data Deprivatio 19.99
Psychology Computer Phobic AND Non-P 21.59
avg
==========================
13.50
Category Shortened Title Price
------------------- ------------------------- --------------------------
Traditional Cooking Fifty Years in Buckingham 11.95
Traditional Cooking Sushi, Anyone? 14.99
Traditional Cooking Onions, Leeks, and Garlic 20.95
avg
==========================
15.96
(21 row(s) affected)
B. 使用带有简单 CASE 函数和 CASE 搜索函数的 SELECT 语句
在 SELECT 语句中,CASE 搜索函数允许根据比较值在结果集内对值进行替换。下面的示例根据图书的价格范围将价格(money 列)显示为文本注释。
USE pubs
GO
SELECT 'Price Category' =
CASE
WHEN price IS NULL THEN 'Not yet priced'
WHEN price < 10 THEN 'Very Reasonable Title'
WHEN price >= 10 and price < 20 THEN 'Coffee Table Title'
ELSE 'Expensive book!'
END,
CAST(title AS varchar(20)) AS 'Shortened Title'
FROM titles
ORDER BY price
GO
下面是结果集:
Price Category Shortened Title
--------------------- --------------------
Not yet priced Net Etiquette
Not yet priced The Psychology of Co
Very Reasonable Title The Gourmet Microwav
Very Reasonable Title You Can Combat Compu
Very Reasonable Title Life Without Fear
Very Reasonable Title Emotional Security:
Coffee Table Title Is Anger the Enemy?
Coffee Table Title Cooking with Compute
Coffee Table Title Fifty Years in Bucki
Coffee Table Title Sushi, Anyone?
Coffee Table Title Prolonged Data Depri
Coffee Table Title Silicon Valley Gastr
Coffee Table Title Straight Talk About
Coffee Table Title The Busy Executive's
Expensive book! Secrets of Silicon V
Expensive book! Onions, Leeks, and G
Expensive book! Computer Phobic And
Expensive book! But Is It User Frien
(18 row(s) affected)
C. 使用带有 SUBSTRING 和 SELECT 的 CASE 函数
下面的示例使用 CASE 和 THEN 生成一个有关作者、图书标识号和每个作者所著图书类型的列表。
USE pubs
SELECT SUBSTRING((RTRIM(a.au_fname) + ' '+
RTRIM(a.au_lname) + ' '), 1, 25) AS Name, a.au_id, ta.title_id,
Type =
CASE
WHEN SUBSTRING(ta.title_id, 1, 2) = 'BU' THEN 'Business'
WHEN SUBSTRING(ta.title_id, 1, 2) = 'MC' THEN 'Modern Cooking'
WHEN SUBSTRING(ta.title_id, 1, 2) = 'PC' THEN 'Popular Computing'
WHEN SUBSTRING(ta.title_id, 1, 2) = 'PS' THEN 'Psychology'
WHEN SUBSTRING(ta.title_id, 1, 2) = 'TC' THEN 'Traditional Cooking'
END
FROM titleauthor ta JOIN authors a ON ta.au_id = a.au_id
下面是结果集:
Name au_id title_id Type
------------------------- ----------- -------- -------------------
Johnson White 172-32-1176 PS3333 Psychology
Marjorie Green 213-46-15 BU1032 Business
Marjorie Green 213-46-15 BU2075 Business
Cheryl Carson 238-95-7766 PC1035 Popular Computing
Michael O'Leary 267-41-2394 BU1111 Business
Michael O'Leary 267-41-2394 TC7777 Traditional Cooking
Dean Straight 274-80-9391 BU7832 Business
Abraham Bennet 409-56-7008 BU1032 Business
Ann Dull 427-17-2319 PC8888 Popular Computing
Burt Gringlesby 472-27-2349 TC7777 Traditional Cooking
Charlene Locksley 486-29-1786 PC9999 Popular Computing
Charlene Locksley 486-29-1786 PS7777 Psychology
Reginald Blotchet-Halls 8-92-1872 TC4203 Traditional Cooking
Akiko Yokomoto 672-71-3249 TC7777 Traditional Cooking
Innes del Castillo 712-45-1867 MC2222 Modern Cooking
Michel DeFrance 722-51-5454 MC3021 Modern Cooking
Stearns MacFeather 724-80-9391 BU1111 Business
Stearns MacFeather 724-80-9391 PS1372 Psychology
Livia Karsen 756-30-7391 PS1372 Psychology
Sylvia Panteley 807-91-6654 TC3218 Traditional Cooking
Sheryl Hunter 846-92-7186 PC8888 Popular Computing
Anne Ringer 9-46-2035 MC3021 Modern Cooking
Anne Ringer 9-46-2035 PS2091 Psychology
Albert Ringer 998-72-3567 PS2091 Psychology
Albert Ringer 998-72-3567 PS2106 Psychology
(25 row(s) affected)
热心网友 时间:2022-04-11 19:52
普通行列转换
假设有张学生成绩表(t)如下
Name Subject Result
张三 语文 73
张三 数学 83
张三 物理 93
李四 语文 74
李四 数学 84
李四 物理 94
想变成
姓名 语文 数学 物理
张三 73 83 93
李四 74 84 94
create table #t
(
Name varchar(10) ,
Subject varchar(10) ,
Result int
)
insert into #t(Name , Subject , Result) values('张三','语文','73')
insert into #t(Name , Subject , Result) values('张三','数学','83')
insert into #t(Name , Subject , Result) values('张三','物理','93')
insert into #t(Name , Subject , Result) values('李四','语文','74')
insert into #t(Name , Subject , Result) values('李四','数学','83')
insert into #t(Name , Subject , Result) values('李四','物理','93')
declare @sql varchar(8000)
set @sql = 'select Name as ' + '姓名'
select @sql = @sql + ' , sum(case Subject when ''' + Subject + ''' then Result end) [' + Subject + ']'
from (select distinct Subject from #t) as a
set @sql = @sql + ' from #t group by name'
exec(@sql)
drop table #t
--结果
姓名 数学 物理 语文
---------- ----------- ----------- -----------
李四 83 93 74
张三 83 93 73
热心网友 时间:2022-04-11 21:43
典型的 字符合并问题。 具体可以参考邹建的方法:
http://blog.csdn.net/zjcxc/archive/2006/06/09/784287.aspx
这里有个我以前写的:
select *
into #ATMTran
from (
SELECT Location= 'L1', ATM = 'M1' ,TransDate = '01-31-2008'
union all
SELECT 'L1','M1','01-31-2008'
union all
SELECT 'L1','M1','02-01-2008'
union all
SELECT 'L1','M1','02-01-2008'
union all
SELECT 'L1','M2','01-30-2008'
union all
SELECT 'L1','M2','02-04-2008'
union all
SELECT 'L2','M1','02-01-2008'
union all
SELECT 'L2','M2','01-30-2008'
union all
SELECT 'L2','M2','02-02-2008'
union all
SELECT 'L2','M2','02-03-2008'
union all
SELECT 'L2','M2','02-05-2008'
) a
GO
SELECT * FROM
(SELECT DISTINCT Location,ATM FROM #ATMTran) A
OUTER APPLY
(SELECT TransDate = STUFF(
REPLACE(
REPLACE(
(SELECT TOP 3 TransDate FROM #ATMTran N WHERE Location=A.Location and ATM=A.ATM ORDER BY TransDate DESC FOR XML AUTO)
, '<N TransDate="', ',')
, '"/>', '')
, 1, 1, '')
) B
--or
SELECT
A.Location,
A.ATM,
TransDate = STUFF ((SELECT TOP 3
', ', TransDate
FROM #ATMTran ta
WHERE A.Location = ta.Location
AND A.ATM = ta.ATM
ORDER BY TransDate DESC
FOR XML PATH(''), TYPE).value('.', 'varchar(200)'), 1, 2, '')
FROM (
SELECT DISTINCT
Location,
ATM
FROM #ATMTran
) A
热心网友 时间:2022-04-11 23:51
楼主这个稍有些难度了,一句话是实现不了的。
可以用游标。
逐条循环,定义两个变量,
declare @C1 varchar(4000),@C2 varchar(4000)
set @C1=''
set @C2=''
if 企业名称=公司1
set @C1=@C1+车辆型号+' '
else
set @C2=@C2+车辆型号+' '
所有循环结束之后
select '公司1' name,@C1 型号
union
select '公司2' name,@C2 型号
热心网友 时间:2022-04-12 02:16
先读取 公司 然后再读取 车辆就是了
热心网友 时间:2022-04-12 04:57
select * 表 where id between 1 and 3
select * 表 where id between 28 and 31
热心网友 时间:2022-04-12 07:55
用case when then else语句
热心网友 时间:2022-04-12 11:10
百度查一下 group by 的用法