locked
calculate count of rows for each group separately in SQL RRS feed

  • Question

  • SELECT  [Dt],
          [CustomerName],
          [ItemRelation],
          [ProductName],
          [SaleCount],
          [DocumentNum],
          [DocumentDate],
          [IsPromo],
          [CustomerType],
          [CalendarYear]

      FROM [Action].[dbo].[promo_data]


      where
      
    [ItemRelation]=11202and[DocumentNum]=398and[CalendarYear]=2018
    or[ItemRelation]=11353and[DocumentNum]=216and[CalendarYear]=2017
    or[ItemRelation]=11511and[DocumentNum]=14and[CalendarYear]=2017

    for each group i want calculate count of rows

    i.e. desired output

    [ItemRelation] [DocumentNum] [CalendarYear] rows
    11202 398 2018 10
    11353 216 2017 20


    Friday, November 23, 2018 9:09 AM

Answers

  • SELECT
    [ItemRelation],
          [DocumentNum],
          [CalendarYear],
    count(*) as Cnt
    
      FROM [Action].[dbo].[promo_data]
      where
    ([ItemRelation]=11202and[DocumentNum]=398and[CalendarYear]=2018)
    or ([ItemRelation]=11353and[DocumentNum]=216and[CalendarYear]=2017)
    or ([ItemRelation]=11511and[DocumentNum]=14and[CalendarYear]=2017)
    group by ItemRelation, DocumentNum, CalendarYear
    Try this.

    HTH, Regards, Dean Savović, www.comminus.hr, www.savovic.com.hr

    • Marked as answer by merzavazeh Friday, November 23, 2018 9:31 AM
    Friday, November 23, 2018 9:22 AM