2017-01-24 6 views
0

Also ich eine Abfrage für ein Diagramm in .net erstellen. Mein Problem ist, ich habe keine Ahnung, wie man den Monat zum nächsten Monat addiert. Also hier ist meine Abfrage unten und was ich davon bekomme.Microsoft SQL Zur Abfrage hinzufügen Monthly

Ich möchte das zum Beispiel April wäre = bis April + Jan, Fev, März ... und so weiter, also Juni wäre = bis Juni + Jan, Feb, März, April, Mai ....

SELECT COUNT(*) As 'Deployed', 
    DATENAME(YEAR, LastModified) As 'Year', 
    DATEPART(Month, LastModified), 
    DATENAME(Month, LastModified) As 'Month' 
FROM [InventoryDatabase].[dbo].[Hardware_RefreshList] 
WHERE Asset_Type = 'Laptop' AND Status = 'Completed' AND Department != 'SNBc' 
     AND DATENAME(YEAR, LastModified) = '2017' 
GROUP BY DATEPART(Month, LastModified), DATENAME(YEAR, LastModified), 
     Month(LastModified), DATENAME(Month, LastModified), 
     DATEADD(MONTH, DATEDIFF(MONTH, 0, LastModified), 0) 
ORDER BY DATEPART(Month, LastModified) 
Result from the query 

2 2017 3 March 6 2017 4 April 8 2017 6 June 6 2017 7 July 9 2017 9 September 29 2017 10 October 10 2017 11 November 54 2017 12 December

+0

Die Frage ist ein "bisschen" verwirrend. 'Add to the next monate' bedeutet eine Summe für Jab + Feb, eine weitere für Feb + March oder was auch immer. Im nächsten Abschnitt wird jedoch beschrieben, dass vom Beginn des Jahres bis zum Zielmonat hinzugefügt wird. Was übrigens "Year to Month" heißt, und hat viele Antworten in SO –

+0

Bitte posten Sie Ihre ursprünglichen Daten, was das gewünschte Ergebnis ist, und eine * klare * Beschreibung dessen, was Sie tun möchten. Stellen Sie sicher, dass die Abfrage auch korrekt formatiert ist. So wie es ist, ist es schwer zu erraten, was jedes der Ergebnisfelder anzeigen soll. –

+0

Wollen Sie den ganzen Monat bekommen, auch wenn es keinen Wert für einen bestimmten Monat gibt? – McNets

Antwort

0

Sie können dies tun, mit count() over()

select 
    [Year]  = datepart(year , LastModified) 
    , [Month]  = datepart(Month, LastModified) 
    , [MonthName] = datename(Month, LastModified) 
    , DeployedThisMonth = count(*) 
    , DeployedSoFar = count(*) over (
     order by datepart(year , LastModified) 
       , datepart(Month, LastModified) 
    ) 
    from [InventoryDatabase].[dbo].[Hardware_RefreshList] 
    where Asset_Type = 'Laptop' 
    and status = 'Completed' 
    and Department != 'snbc' 
    and datepart(year, LastModified) = 2017 
    group by 
     datepart(Month, LastModified) 
    , datepart(year, LastModified) 
    , Month(LastModified) 
    , datename(Month, LastModified) 
    --, dateadd(month, datediff(month, 0, LastModified), 0) 
    order by datepart(Month, LastModified) 
+0

SqkZim, die Count() und OveR() funktionieren wird. Danke für die Hilfe –

Verwandte Themen