日期:2014-05-17  浏览次数:20545 次

sql列转行 在线等!急急急
本帖最后由 u011690009 于 2013-08-14 11:05:08 编辑

如何将图1做成图2这样 (sum的时候是按Quname分组求和的)在线等!QQ:1197547385
SQL

------解决方案--------------------
行列转换,请参考:
http://blog.csdn.net/hdhai9451/article/details/5026933  

------解决方案--------------------
convert(decimal(9,2),SUM(sevenleijixiaofeiacct)/SUM(sevenhuoyueacct)) = 1.0*SUM(sevenleijixiaofeiacct)/SUM(sevenhuoyueacct)
------解决方案--------------------
引用:
日期咋弄成动态的

--拼SQL
DECLARE @sql NVARCHAR(MAX), @datelist NVARCHAR(MAX)
SELECT @datelist = STUFF((SELECT DISTINCT ','+QUOTENAME([date]) FROM #temp FOR XML PATH('')),1,1,'') --日期动态
SET @sql = N'
SELECT * FROM
(
    SELECT Quname, [Date],
        [SUM(newchongzhiacct)] = CAST(SUM(newchongzhiacct) AS DECIMAL(10, 2)),
        [SUM(todaychongzhi)] = CAST(SUM(todaychongzhi) AS DECIMAL(10, 2)),
        [SUM(newaddxiaofeiacct)] = CAST(SUM(newaddxiaofeiacct) AS DECIMAL(10, 2)),
        [SUM(sevenleijixiaofeiacct)] = CAST(SUM(sevenleijixiaofeiacct) AS DECIMAL(10, 2)),
        [SUM(sevenhuoyueacct)] = CAST(SUM(sevenhuoyueacct) AS DECIMAL(10, 2)),
        [SUM(sevenleijixiaofeiacct)/SUM(sevenhuoyueacct)] = CAST((CASE WHEN SUM(sevenhuoyueacct) = 0 THEN 0 ELSE SUM(sevenleijixiaofeiacct)/SUM(sevenhuoyueacct) end) AS DECIMAL(10, 2)),
        [SUM(todayxiaofei)] = CAST(SUM(todayxiaofei) AS DECIMAL(10, 2)),
        [SUM(todayxiaofeiacctnum)] = CAST(SUM(todayxiaofeiacctnum) AS DECIMAL(10, 2)),