Create table test (name char(10),km char(10),cj int)
go
insert test values('张三','语文',80)
insert test values('张三','数学',86)
insert test values('张三','英语',75)
insert test values('李四','语文',78)
insert test values('李四','数学',85)
insert test values('李四','英语',78)

declare @sql varchar(8000)
set @sql = 'select name'
select @sql = @sql + ',sum(case km when '''+km+''' then cj end) ['+km+']'
from (select distinct km from test) as a
select @sql = @sql+' from test group by name'
print(@sql)
exec(@sql)

drop table test


------------------------------

select name,
sum(case km when '数学' then cj end) [数学],
sum(case km when '英语' then cj end) [英语],
sum(case km when '语文' then cj end) [语文]

from test group by name

结果是执行的中间?
SQL语句在有WHERE语句存在的时候才从最右边开始执行?

-----------------------------------------------------------

group by...IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
group by...      
WHERE TABLE_NAME = 'author_sales')
group by...   
DROP TABLE author_sales
group by...
GO
group by...
IF EXISTS(SELECT name FROM sysobjects 
group by...      
WHERE name = 'get_author_sales' AND type = 'P')
group by...   
DROP PROCEDURE get_author_sales
group by...
GO
group by...
USE pubs
group by...
CREATE TABLE author_sales
group by...( data_source   
varchar(20),
group by...  au_id         
varchar(11),
group by...  au_lname      
varchar(40),
group by...  sales_dollars 
smallmoney
group by...)
group by...
GO
group by...
CREATE PROCEDURE get_author_sales 
group by...
AS 
group by...   
SELECT 'PROCEDURE', authors.au_id, authors.au_lname, 
group by...      
SUM(titles.price * sales.qty) 
group by...   
FROM authors INNER JOIN titleauthor 
group by...      
ON authors.au_id = titleauthor.au_id INNER JOIN titles
group by...      
ON titleauthor.title_id = titles.title_id INNER JOIN sales
group by...      
ON titles.title_id = sales.title_id
group by...   
WHERE authors.au_id like '8%'
group by...   
GROUP BY authors.au_id, authors.au_lname
group by...
GO
group by...
--INSERTgroup by...SELECT example
group by...
USE pubs
group by...
INSERT author_sales
group by...   
SELECT 'SELECT', authors.au_id, authors.au_lname, 
group by...      
SUM(titles.price * sales.qty) 
group by...   
FROM authors INNER JOIN titleauthor 
group by...      
ON authors.au_id = titleauthor.au_id INNER JOIN titles
group by...      
ON titleauthor.title_id = titles.title_id INNER JOIN sales
group by...      
ON titles.title_id = sales.title_id
group by...   
WHERE authors.au_id LIKE '8%'
group by...   
GROUP BY authors.au_id, authors.au_lname
group by...
group by...
--INSERTgroup by...EXECUTE procedure example
group by...
INSERT author_sales EXECUTE get_author_sales
group by...
group by...
--INSERTgroup by...EXECUTE('string') example
group by...
INSERT author_sales 
group by...
EXECUTE 
group by...(
'
group by...SELECT 
''EXEC STRING'', authors.au_id, authors.au_lname, 
group by...   SUM(titles.price * sales.qty) 
group by...   FROM authors INNER JOIN titleauthor 
group by...      ON authors.au_id = titleauthor.au_id INNER JOIN titles
group by...      ON titleauthor.title_id = titles.title_id INNER JOIN sales
group by...      ON titles.title_id = sales.title_id
group by...   WHERE authors.au_id like 
''8%''
group by...   GROUP BY authors.au_id, authors.au_lname
group by...
')
group by...
group by...
--Show results.
group by...
SELECT * FROM author_sales
group by...
group by...



相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-26
  • 2021-05-23
  • 2022-12-23
猜你喜欢
  • 2021-08-15
  • 2021-06-03
  • 2022-01-26
  • 2021-08-12
相关资源
相似解决方案