我不确定是否使用枢轴,因为我对此感到困惑。我在Fiddle
获得了一些样本数据我正在寻找的结果是(如果为空记录则无效或空字符串)
Name Product m1SALES m1STARGET m1GROWTH m2SALES m2STARGET m2GROWTH m3SALES m3STARGET M3GROWTH
-----------------------------------------------------------------------------------------
John A 5 100 5 10 100 10 NULL NULL NULL
John B NULL NULL NULL NULL NULL NULL 20 100 20
我在任何时候都只需要四分之一的记录。 1,2,3或4,5,6或7,8,9或10,11,12。 其中月份+年份取决于数据,某个月可能没有销售记录。
我试过选择数据取决于月份+年份从同一张桌子3次,但没有运气。
答案 0 :(得分:0)
这有点原始,但没有支点就可以完成工作:
SELECT DISTINCT
name ,
product ,
( SELECT sales
FROM yourtable b
WHERE a.product = b.product
AND MONTH = 1
AND YEAR = 2014
) m1sales ,
( SELECT starget
FROM yourtable b
WHERE a.product = b.product
AND MONTH = 1
AND YEAR = 2014
) m1starget ,
( SELECT growth
FROM yourtable b
WHERE a.product = b.product
AND MONTH = 1
AND YEAR = 2014
) m1growth ,
( SELECT sales
FROM yourtable b
WHERE a.product = b.product
AND MONTH = 2
AND YEAR = 2014
) m2sales ,
( SELECT starget
FROM yourtable b
WHERE a.product = b.product
AND MONTH = 2
AND YEAR = 2014
) m2starget ,
( SELECT growth
FROM yourtable b
WHERE a.product = b.product
AND MONTH = 2
AND YEAR = 2014
) m2growth ,
( SELECT sales
FROM yourtable b
WHERE a.product = b.product
AND MONTH = 3
AND YEAR = 2014
) m3sales ,
( SELECT starget
FROM yourtable b
WHERE a.product = b.product
AND MONTH = 3
AND YEAR = 2014
) m3starget ,
( SELECT growth
FROM yourtable b
WHERE a.product = b.product
AND MONTH = 3
AND YEAR = 2014
) m3growth
FROM yourtable a