Pour ce problème, vous pouvez utiliser plusieurs approche pour obtenir le résultat. Certains d'entre eux doivent unifier le résultat en fonction du type QTR. Donc, c'est peut-être la façon simple d'obtenir le résultat. Tout d'abord, essayez d'obtenir juste QR actual_month basé sur le YearMonth donnée. La requête ressemble à:
SELECT
QTRType = cast(year(YearMonth as varchar(10)) + '-Actual',
Q1 = case when month(YearMonth) = 3 then Actual_Month else 0 end,
Q2 = case when month(YearMonth) = 6 then Actual_Month else 0 end,
Q3 = case when month(YearMonth) = 9 then Actual_Month else 0 end,
Q4 = case when month(YearMonth) = 12 then Actual_Month else 0 end
FROM Table1
WHERE
year(YearMonth) <= 2010
La requête retournerait au-dessus de tout type réel TRIM. Donc, la prochaine étape, avec la même approche, essaie d'obtenir le type de cible QTR et la projection QTR. Après l'avoir obtenu, juste l'union de toutes les requêtes en utilisant union tout le mot-clé.
La requête finale:
SELECT
QTRType = cast(year(YearMonth as varchar(10)) + '-Actual',
Q1 = case when month(YearMonth) = 3 then Actual_Month else 0 end,
Q2 = case when month(YearMonth) = 6 then Actual_Month else 0 end,
Q3 = case when month(YearMonth) = 9 then Actual_Month else 0 end,
Q4 = case when month(YearMonth) = 12 then Actual_Month else 0 end
FROM Table1
WHERE
year(YearMonth) <= 2010
UNION ALL
SELECT
QTRType = cast(year(YearMonth as varchar(10)) + '-Target',
Q1 = case when month(YearMonth) = 3 then Target_Month else 0 end,
Q2 = case when month(YearMonth) = 6 then Target_Month else 0 end,
Q3 = case when month(YearMonth) = 9 then Target_Month else 0 end,
Q4 = case when month(YearMonth) = 12 then Target_Month else 0 end
FROM Table1
WHERE
year(YearMonth) <= 2010
UNION ALL
SELECT
QTRType = cast(year(YearMonth as varchar(10)) + '-Projection',
Q1 = case when month(YearMonth) = 3 then Projection_Month else 0 end,
Q2 = case when month(YearMonth) = 6 then Projection_Month else 0 end,
Q3 = case when month(YearMonth) = 9 then Projection_Month else 0 end,
Q4 = case when month(YearMonth) = 12 then Projection_Month else 0 end
FROM Table1
WHERE
year(YearMonth) <= 2010
Hope, il peut vous éclairer :)
Cordialement, fritz
Note: Je ne l'ai pas tester la requête, mais je suis assez Assurez-vous que cela fonctionnera :)
Quels SGBDR utilisez-vous? –
Salut son Microsoft SQL 2008 –
Copyvio de http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/a4e47578-e30b-41d8-ba23-6a92c2504e26 - voir [Un utilisateur prend des questions de MSDN forums, en leur demandant sur SO, et en publiant les réponses SO sur MSDN] (http://meta.stackexchange.com/questions/65954/a-user-is-taking-questions-from-msdn-forums-asking-them -on-so-and-posting-the-s). – Dori