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
sql select count where multiple-columns
add a comment |
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
sql select count where multiple-columns
add a comment |
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
sql select count where multiple-columns
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
sql select count where multiple-columns
edited Nov 10 at 1:06
K.Dᴀᴠɪs
5,980102140
5,980102140
asked Nov 9 at 21:31
sql_slayerrr
31
31
add a comment |
add a comment |
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
Thank you! This was an incredible help. I did modify slightly the approach:
– sql_slayerrr
Nov 11 at 21:12
add a comment |
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
Thank you! This was an incredible help. I did modify slightly the approach:
– sql_slayerrr
Nov 11 at 21:12
add a comment |
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
Thank you! This was an incredible help. I did modify slightly the approach:
– sql_slayerrr
Nov 11 at 21:12
add a comment |
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
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
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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