2010-09-23 4 views
1

J'ai besoin d'aide pour créer une requête pour obtenir des informations sur l'année sélectionnée et l'année précédente de l'année sélectionnée. Doit être par trimestre (QTR) pour les deux années. QTR1 sera toujours le mois 03, QTR2 mois 06, QTR3 mois 09, & QTR4 mois 12. Aucun calcul n'est requis ou moyenné.Demande de dates multiples

champs/colonnes sont yearMonth stockées mm/jj/aaaa avec dd étant toujours 01, ACTUAL_MONTH (float) pour tous SCDO sur la base yearMonth actuellement sélectionné et un an avant, TARGET_MONTH (float) pour tous SCDO basé sur yearMonth sélectionnés et PROJECTION_MONTH (float) pour tous les QTR basés sur YEARMONTH sélectionnés.

Tableau

YEARMONTH (PK, Datetime) 
ACTUAL_MONTH(Float) 
TARGET_MONTH(Float) 
PROJECTION_MONTH(Float) 

devrait ressembler à cela le cas échéant yearMonth en 2010 a été choisi

    Q1 Q2 Q3 Q4 
09-Actual  xxx xxx xxx xxx 
10-Actual  xxx xxx xxx xxx 
10-Target  xxx xxx xxx xxx 
10-Projection xxx xxx xxx xxx 
+2

Quels SGBDR utilisez-vous? –

+0

Salut son Microsoft SQL 2008 –

+1

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

Répondre

0

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 :)