Create multiple counts that vary by condition









up vote
0
down vote

favorite












I have a simple query that works well, but I have a need to run it varying the conditional 'where' statement for each of 12 months * several years. I would like to produce 12 columns, each representing a count through that month; it would also be acceptable if this data were to be somehow represented in rows.



Current query:



SELECT 
field1
,field2
,field3
,count(distinct(table1.field4))

FROM table1
inner join table2
on table1.field4=table2.field4
where year_month <=201808

group by
field1
,field2
,field3

order by
field1
,field2
,field3









share|improve this question



























    up vote
    0
    down vote

    favorite












    I have a simple query that works well, but I have a need to run it varying the conditional 'where' statement for each of 12 months * several years. I would like to produce 12 columns, each representing a count through that month; it would also be acceptable if this data were to be somehow represented in rows.



    Current query:



    SELECT 
    field1
    ,field2
    ,field3
    ,count(distinct(table1.field4))

    FROM table1
    inner join table2
    on table1.field4=table2.field4
    where year_month <=201808

    group by
    field1
    ,field2
    ,field3

    order by
    field1
    ,field2
    ,field3









    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a simple query that works well, but I have a need to run it varying the conditional 'where' statement for each of 12 months * several years. I would like to produce 12 columns, each representing a count through that month; it would also be acceptable if this data were to be somehow represented in rows.



      Current query:



      SELECT 
      field1
      ,field2
      ,field3
      ,count(distinct(table1.field4))

      FROM table1
      inner join table2
      on table1.field4=table2.field4
      where year_month <=201808

      group by
      field1
      ,field2
      ,field3

      order by
      field1
      ,field2
      ,field3









      share|improve this question















      I have a simple query that works well, but I have a need to run it varying the conditional 'where' statement for each of 12 months * several years. I would like to produce 12 columns, each representing a count through that month; it would also be acceptable if this data were to be somehow represented in rows.



      Current query:



      SELECT 
      field1
      ,field2
      ,field3
      ,count(distinct(table1.field4))

      FROM table1
      inner join table2
      on table1.field4=table2.field4
      where year_month <=201808

      group by
      field1
      ,field2
      ,field3

      order by
      field1
      ,field2
      ,field3






      sql select count where multiple-columns






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 1:06









      K.Dᴀᴠɪs

      5,980102140




      5,980102140










      asked Nov 9 at 21:31









      sql_slayerrr

      31




      31






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          there you go :



          DECLARE @year INT,
          @i INT = 1,
          @date VARCHAR(6),
          @month INT = 1,
          @months INT = 12,
          @sql NVARCHAR(MAX)

          DECLARE @years TABLE (
          Id INT IDENTITY,
          myYear INT
          )

          INSERT INTO @years (myYear)
          VALUES (2016),
          (2017),
          (2018)

          CREATE TABLE #table (
          field1 [your data type],
          field2 [your data type],
          field3 [your data type],
          myDate VARCHAR(6),
          myCount INT
          )

          WHILE @i <= (SELECT MAX(Id) FROM @years)
          BEGIN
          SELECT @year = myYear
          FROM @years
          WHERE Id = @i

          WHILE @month <= @months
          BEGIN
          IF @month < 10
          SET @date = @year + '0' + CAST(@month as VARCHAR(2))
          ELSE
          SET @date = @year + CAST(@month as VARCHAR(2))

          SET @sql = 'INSERT INTO #table SELECT field1 ,field2 ,field3, year_month as myDate, count(distinct(table1.field4)) as myCount
          FROM table1 JOIN table2 ON table1.field4 = table2.field4
          WHERE year_month = ''' + CAST(@date AS NVARCHAR(MAX)) + ''' GROUP BY field1, field2, field3, year_month ORDER BY year_month, field1, field2, field3'

          EXECUTE sp_executesql @sql

          SET @month = @month + 1
          END

          SET @month = 1
          SET @i = @i + 1
          END

          SELECT * FROM #table

          DROP TABLE #table





          share|improve this answer




















          • Thank you! This was an incredible help. I did modify slightly the approach:
            – sql_slayerrr
            Nov 11 at 21:12










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233516%2fcreate-multiple-counts-that-vary-by-condition%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          there you go :



          DECLARE @year INT,
          @i INT = 1,
          @date VARCHAR(6),
          @month INT = 1,
          @months INT = 12,
          @sql NVARCHAR(MAX)

          DECLARE @years TABLE (
          Id INT IDENTITY,
          myYear INT
          )

          INSERT INTO @years (myYear)
          VALUES (2016),
          (2017),
          (2018)

          CREATE TABLE #table (
          field1 [your data type],
          field2 [your data type],
          field3 [your data type],
          myDate VARCHAR(6),
          myCount INT
          )

          WHILE @i <= (SELECT MAX(Id) FROM @years)
          BEGIN
          SELECT @year = myYear
          FROM @years
          WHERE Id = @i

          WHILE @month <= @months
          BEGIN
          IF @month < 10
          SET @date = @year + '0' + CAST(@month as VARCHAR(2))
          ELSE
          SET @date = @year + CAST(@month as VARCHAR(2))

          SET @sql = 'INSERT INTO #table SELECT field1 ,field2 ,field3, year_month as myDate, count(distinct(table1.field4)) as myCount
          FROM table1 JOIN table2 ON table1.field4 = table2.field4
          WHERE year_month = ''' + CAST(@date AS NVARCHAR(MAX)) + ''' GROUP BY field1, field2, field3, year_month ORDER BY year_month, field1, field2, field3'

          EXECUTE sp_executesql @sql

          SET @month = @month + 1
          END

          SET @month = 1
          SET @i = @i + 1
          END

          SELECT * FROM #table

          DROP TABLE #table





          share|improve this answer




















          • Thank you! This was an incredible help. I did modify slightly the approach:
            – sql_slayerrr
            Nov 11 at 21:12














          up vote
          0
          down vote



          accepted










          there you go :



          DECLARE @year INT,
          @i INT = 1,
          @date VARCHAR(6),
          @month INT = 1,
          @months INT = 12,
          @sql NVARCHAR(MAX)

          DECLARE @years TABLE (
          Id INT IDENTITY,
          myYear INT
          )

          INSERT INTO @years (myYear)
          VALUES (2016),
          (2017),
          (2018)

          CREATE TABLE #table (
          field1 [your data type],
          field2 [your data type],
          field3 [your data type],
          myDate VARCHAR(6),
          myCount INT
          )

          WHILE @i <= (SELECT MAX(Id) FROM @years)
          BEGIN
          SELECT @year = myYear
          FROM @years
          WHERE Id = @i

          WHILE @month <= @months
          BEGIN
          IF @month < 10
          SET @date = @year + '0' + CAST(@month as VARCHAR(2))
          ELSE
          SET @date = @year + CAST(@month as VARCHAR(2))

          SET @sql = 'INSERT INTO #table SELECT field1 ,field2 ,field3, year_month as myDate, count(distinct(table1.field4)) as myCount
          FROM table1 JOIN table2 ON table1.field4 = table2.field4
          WHERE year_month = ''' + CAST(@date AS NVARCHAR(MAX)) + ''' GROUP BY field1, field2, field3, year_month ORDER BY year_month, field1, field2, field3'

          EXECUTE sp_executesql @sql

          SET @month = @month + 1
          END

          SET @month = 1
          SET @i = @i + 1
          END

          SELECT * FROM #table

          DROP TABLE #table





          share|improve this answer




















          • Thank you! This was an incredible help. I did modify slightly the approach:
            – sql_slayerrr
            Nov 11 at 21:12












          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          there you go :



          DECLARE @year INT,
          @i INT = 1,
          @date VARCHAR(6),
          @month INT = 1,
          @months INT = 12,
          @sql NVARCHAR(MAX)

          DECLARE @years TABLE (
          Id INT IDENTITY,
          myYear INT
          )

          INSERT INTO @years (myYear)
          VALUES (2016),
          (2017),
          (2018)

          CREATE TABLE #table (
          field1 [your data type],
          field2 [your data type],
          field3 [your data type],
          myDate VARCHAR(6),
          myCount INT
          )

          WHILE @i <= (SELECT MAX(Id) FROM @years)
          BEGIN
          SELECT @year = myYear
          FROM @years
          WHERE Id = @i

          WHILE @month <= @months
          BEGIN
          IF @month < 10
          SET @date = @year + '0' + CAST(@month as VARCHAR(2))
          ELSE
          SET @date = @year + CAST(@month as VARCHAR(2))

          SET @sql = 'INSERT INTO #table SELECT field1 ,field2 ,field3, year_month as myDate, count(distinct(table1.field4)) as myCount
          FROM table1 JOIN table2 ON table1.field4 = table2.field4
          WHERE year_month = ''' + CAST(@date AS NVARCHAR(MAX)) + ''' GROUP BY field1, field2, field3, year_month ORDER BY year_month, field1, field2, field3'

          EXECUTE sp_executesql @sql

          SET @month = @month + 1
          END

          SET @month = 1
          SET @i = @i + 1
          END

          SELECT * FROM #table

          DROP TABLE #table





          share|improve this answer












          there you go :



          DECLARE @year INT,
          @i INT = 1,
          @date VARCHAR(6),
          @month INT = 1,
          @months INT = 12,
          @sql NVARCHAR(MAX)

          DECLARE @years TABLE (
          Id INT IDENTITY,
          myYear INT
          )

          INSERT INTO @years (myYear)
          VALUES (2016),
          (2017),
          (2018)

          CREATE TABLE #table (
          field1 [your data type],
          field2 [your data type],
          field3 [your data type],
          myDate VARCHAR(6),
          myCount INT
          )

          WHILE @i <= (SELECT MAX(Id) FROM @years)
          BEGIN
          SELECT @year = myYear
          FROM @years
          WHERE Id = @i

          WHILE @month <= @months
          BEGIN
          IF @month < 10
          SET @date = @year + '0' + CAST(@month as VARCHAR(2))
          ELSE
          SET @date = @year + CAST(@month as VARCHAR(2))

          SET @sql = 'INSERT INTO #table SELECT field1 ,field2 ,field3, year_month as myDate, count(distinct(table1.field4)) as myCount
          FROM table1 JOIN table2 ON table1.field4 = table2.field4
          WHERE year_month = ''' + CAST(@date AS NVARCHAR(MAX)) + ''' GROUP BY field1, field2, field3, year_month ORDER BY year_month, field1, field2, field3'

          EXECUTE sp_executesql @sql

          SET @month = @month + 1
          END

          SET @month = 1
          SET @i = @i + 1
          END

          SELECT * FROM #table

          DROP TABLE #table






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 3:09









          ujawg

          966




          966











          • Thank you! This was an incredible help. I did modify slightly the approach:
            – sql_slayerrr
            Nov 11 at 21:12
















          • Thank you! This was an incredible help. I did modify slightly the approach:
            – sql_slayerrr
            Nov 11 at 21:12















          Thank you! This was an incredible help. I did modify slightly the approach:
          – sql_slayerrr
          Nov 11 at 21:12




          Thank you! This was an incredible help. I did modify slightly the approach:
          – sql_slayerrr
          Nov 11 at 21:12

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233516%2fcreate-multiple-counts-that-vary-by-condition%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo