Answered select only LAST 6 periods

  • vendredi 10 août 2012 14:59
     
     

     Hi all, 

    my need is to select only LAST 6 periods. I tried TOP(6) with ORDER BY [date] DESC, but it seems "DESC" is ignored. I receive the same wrong result (the first 6 periods instead of the last 6 periods) with "DESC" and without it. Help me please, I'm new to sql server. 

    SELECT     TOP (6) CONVERT(varchar, StatementID) AS statementid, contractid, accountingDate2
    FROM         (SELECT     TOP (100) PERCENT StatementID, contractid, SUBSTRING(CONVERT(varchar, accountingDate, 112), 1, 6) AS accountingDate2, COUNT(1) AS cnt
                           FROM          vpvbkiR_Record AS t
                           WHERE      (CONVERT(varchar, StatementID) = '135b8c93-0801-44e3-9dc8-319608') AND (contractid = 1)
                           GROUP BY StatementID, contractid, SUBSTRING(CONVERT(varchar, accountingDate, 112), 1, 6)
                           ORDER BY accountingDate2 DESC) AS k

Toutes les réponses

  • vendredi 10 août 2012 15:25
     
     Traitée A du code

     this works for me: 

    SELECT     TOP (6) CONVERT(varchar, StatementID) AS statementid, contractid, accountingDate2
    FROM         (SELECT     StatementID, contractid, SUBSTRING(CONVERT(varchar, accountingDate, 112), 1, 6) AS accountingDate2, COUNT(1) AS cnt
    FROM          vpvbkiR_Record AS t
    WHERE      (CONVERT(varchar, StatementID) = '135b8c93-0801-44e3-9dc8-319608') AND (contractid = 1)
    GROUP BY StatementID, contractid, SUBSTRING(CONVERT(varchar, accountingDate, 112), 1, 6)) AS k
    ORDER BY accountingDate2 DESC

    • Marqué comme réponse ymarkiv vendredi 10 août 2012 15:25
    •