答案 0 :(得分:0)
select Division, Period, 'Total Cost' as Description, TotalCost as Value
from MyTable
union
select Division, Period, 'Discount' as Description, Discount as Value
from MyTable
union
select Division, Period, 'Net Sales' as Description, NetSales as Value
from MyTable
union
select Division, Period, 'Net Returns' as Description, NetReturns as Value
from MyTable
union
select Division, Period, 'Gross Sales' as Description, GrossSales as Value
from MyTable
union
select Division, Period, 'Gross Returns' as Description, GrossReturns as Value
from MyTable
您也可以将其包装在视图中,然后使用您需要的任何排序或过滤对视图进行选择
答案 1 :(得分:0)
在任何平台上UNION ALL都能正常工作。有些平台有UNPIVOT,但你没有说你正在使用什么。
SELECT Division, Period, 'Total Cost' AS DESCRIPTION, TotalCost as Value
FROM A_TABLENAME_YOU_DID_NOT_SAY
UNION ALL
SELECT Division, Period, 'Discount' AS DESCRIPTION, DISCOUNT AS Value
FROM A_TABLENAME_YOU_DID_NOT_SAY
UNION ALL
SELECT Division, Period, 'Net Sales' AS Descripion, NETSALES AS Value
FROM A_TABLENAME_YOU_DID_NOT_SAY
UNION ALL
SELECT Division, Period, 'Net Returns' AS DESCRIPTION, NETRETURNS AS Value
FROM A_TABLENAME_YOU_DID_NOT_SAY
UNION ALL
SELECT Division, Period, 'Gross Sales' AS DESCRIPTION, GROSSSALES AS Value
FROM A_TABLENAME_YOU_DID_NOT_SAY
UNION ALL
SELECT Division, Period, 'Gross Returns' AS DESCRIPTION, GROSSRETURNS AS Value
FROM A_TABLENAME_YOU_DID_NOT_SAY