我目前使用PIVOT生成此查询,生成如下表:
Name |Total |01-10-2014 |02-10-2014
dd |500 | 300 |200
bb |400 | 200 |200
ff |150 | 100 |50
查询就像这样
select * from (
select
COALESCE(RNAME, 'NO NAME') as Name,
Total,
CONVERT(varchar(10), date1,105) as Date,
BILLAMT
from #temp
where date1 >= '2014-10-01' and date1 <= '2014-10-02'
) as D
PIVOT(SUM(BILLAMT) for Date in ([01-10-2014],[02-10-2014])) as P
我需要order by RNAME
。并且右端的总数 ..任何帮助将不胜感激......
答案 0 :(得分:0)
如果您知道列列表然后尝试其他,则需要将其更改为动态数据透视。
SELECT Name,
[01-10-2014],
[02-10-2014],
Total
FROM (SELECT COALESCE(RNAME, 'NO NAME') AS Name,
Total,
CONVERT(VARCHAR(10), date1, 105) AS Date,
BILLAMT
FROM #temp
WHERE date1 >= '2014-10-01'
AND date1 <= '2014-10-02') AS D
PIVOT(Sum(BILLAMT)
FOR Date IN ([01-10-2014],
[02-10-2014])) AS P
ORDER BY Name