日期:2014-05-16 浏览次数:20552 次
SELECT
name,
MAX(CASE WHEN woke='项目1' THEN df ELSE 0 END )AS '项目1',
MAX(CASE WHEN woke='项目2' THEN df ELSE 0 END )AS '项目2',
MAX(CASE WHEN woke='项目3' THEN df ELSE 0 END )AS '项目3',
SUM(qz*df) AS '总计',
ROW_NUMBER()OVER(ORDER BY SUM(qz*df)) AS 名次
FROM
dbo.tb
GROUP BY
name
----------------------------------------------------------------
-- Author :DBA_HuangZJ(发粪涂墙)
-- Date :2014-03-14 15:07:28
-- Version:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64)
-- Apr 2 2010 15:48:46
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([ID] int,[name] nvarchar(4),[woke] nvarchar(6),[qz] numeric(3,2),[df] int)
insert [huang]
select 1,N'张三',N'项目1',0.30,30 union all
select 2,N'张三',N'项目2',0.30,40 union all
select 3,N'张三',N'项目3',0.40,60 union all
select 4,N'李四',N'项目1',0.30,20 union all
select 5,N'李四',N'项目2',0.50,90 union all
select 6,N'王五',N'项目1',0.60,80
--------------生成数据--------------------------
IF OBJECT_ID('tempdb..#t','u')IS NOT NULL
DROP TABLE #t
select * ,(SELECT SUM(DF*QZ) FROM HUANG A WHERE A.NAME=HUANG.NAME )[合计] INTO #t
from [huang]
declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename([