Query to categorize rows based on a “time” column without using a CASE expressionWhy are numbers tables “invaluable”?SQL to select random mix of rows fairlyOptimising query on view that merges similar tables with a clear discriminatorData Warehouse - Slowly Changing Dimensions with Many to Many RelationshipsUpdate all rows from a table with random foreign key from another tableOptimizing a simple query on a large tableSubquery ORDER BY doesn't work on MySQL 5.6, but works on 5.5Optimize finding newest x rows from categoryReportviwer matrix show all months/weeks of yearWhy would adding an index on a MySQL table slow it down significantly but ok on SQL Server and PostgreSQLFunction in JOIN on DISTINCT values executes for each row instead of distinct parameters
Is the book wrong about the Nyquist Sampling Criterion?
Why do people keep telling me that I am a bad photographer?
Can you use "едать" and "игрывать" in the present and future tenses?
Does "Captain Marvel" contain spoilers for "Avengers: Infinity War"?
Can there be a single technologically advanced nation, in a continent full of non-technologically advanced nations?
Gerrymandering Puzzle - Rig the Election
When an imagined world resembles or has similarities with a famous world
Start job from another SQL server instance
Is an HNN extension of a virtually torsion-free group virtually torsion-free?
To kill a cuckoo
Python 3 - simple temperature program
Why would a military not separate its forces into different branches?
Has a commercial or military jet bi-plane ever been manufactured?
Agena docking and RCS Brakes in First Man
What do "Sech" and "Vich" mean in this sentence?
Should I mention being denied entry to UK due to a confusion in my Visa and Ticket bookings?
How does summation index shifting work?
What was the first story to feature the plot "the monsters were human all along"?
Install LibreOffice-Writer Only not LibreOffice whole package
Are pressure-treated posts that have been submerged for a few days ruined?
Adding command shortcuts to /bin
Checking if two expressions are related
Correct way of drawing empty, half-filled and fully filled circles?
Why do these characters still seem to be the same age after the events of Endgame?
Query to categorize rows based on a “time” column without using a CASE expression
Why are numbers tables “invaluable”?SQL to select random mix of rows fairlyOptimising query on view that merges similar tables with a clear discriminatorData Warehouse - Slowly Changing Dimensions with Many to Many RelationshipsUpdate all rows from a table with random foreign key from another tableOptimizing a simple query on a large tableSubquery ORDER BY doesn't work on MySQL 5.6, but works on 5.5Optimize finding newest x rows from categoryReportviwer matrix show all months/weeks of yearWhy would adding an index on a MySQL table slow it down significantly but ok on SQL Server and PostgreSQLFunction in JOIN on DISTINCT values executes for each row instead of distinct parameters
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
There is a ProductTT
table as you can see below:
[dbo].[ProductTT] (ID int , Product Varchar(50) , Time Int)
...which contains the following rows:
1 XX 0030
2 UY 0354
3 YY 0517
4 ZZ 0712
5 WW 0415
6 GG 1112
7 MM 1030
8 HH 0913
Note: The format of the data in time
column is hh:mm
so 0030 is 00:30.
I want to write a query to categorize the rows based on their time
value. I need to have 4 categories like this:
category1 00 to 03
category2 03 to 06
category3 06 to 09
category4 09 to 12
I need to see how many rows pertain to each category.
My attempt so far
What I've written so far is like this:
With CTE
AS (select ID,
product,
[time],
Case
When left(time,2)>=00 and left(time,2)< 03 then 'group1'
when left(time,2)>=03 and left(time,2)< 06 then 'group2'
when left(time,2)>=06 and left(time,2)< 09 then 'group3'
when left(time,2)>=09 and left(time,2)<=12 then 'group4' End AS groupID
from [dbo].[ProductTT]
)
select groupid,count(*) as recordcount
from cte
group by groupid
My question
That query works fine but I just want to know whether there are better ways to write this query and avoid using a CASE expression.
sql-server t-sql query-performance optimization
add a comment |
There is a ProductTT
table as you can see below:
[dbo].[ProductTT] (ID int , Product Varchar(50) , Time Int)
...which contains the following rows:
1 XX 0030
2 UY 0354
3 YY 0517
4 ZZ 0712
5 WW 0415
6 GG 1112
7 MM 1030
8 HH 0913
Note: The format of the data in time
column is hh:mm
so 0030 is 00:30.
I want to write a query to categorize the rows based on their time
value. I need to have 4 categories like this:
category1 00 to 03
category2 03 to 06
category3 06 to 09
category4 09 to 12
I need to see how many rows pertain to each category.
My attempt so far
What I've written so far is like this:
With CTE
AS (select ID,
product,
[time],
Case
When left(time,2)>=00 and left(time,2)< 03 then 'group1'
when left(time,2)>=03 and left(time,2)< 06 then 'group2'
when left(time,2)>=06 and left(time,2)< 09 then 'group3'
when left(time,2)>=09 and left(time,2)<=12 then 'group4' End AS groupID
from [dbo].[ProductTT]
)
select groupid,count(*) as recordcount
from cte
group by groupid
My question
That query works fine but I just want to know whether there are better ways to write this query and avoid using a CASE expression.
sql-server t-sql query-performance optimization
2
So..everything between 1201 and 23:59 gets NULL for category? Why do you not store time as aTIME
data type?
– Michael Kutz
Apr 30 at 10:28
4
What is the purpose of avoiding a CASE expression? You should make this clear because some alternatives are just different syntax for the same thing.
– Aaron Bertrand♦
Apr 30 at 10:45
add a comment |
There is a ProductTT
table as you can see below:
[dbo].[ProductTT] (ID int , Product Varchar(50) , Time Int)
...which contains the following rows:
1 XX 0030
2 UY 0354
3 YY 0517
4 ZZ 0712
5 WW 0415
6 GG 1112
7 MM 1030
8 HH 0913
Note: The format of the data in time
column is hh:mm
so 0030 is 00:30.
I want to write a query to categorize the rows based on their time
value. I need to have 4 categories like this:
category1 00 to 03
category2 03 to 06
category3 06 to 09
category4 09 to 12
I need to see how many rows pertain to each category.
My attempt so far
What I've written so far is like this:
With CTE
AS (select ID,
product,
[time],
Case
When left(time,2)>=00 and left(time,2)< 03 then 'group1'
when left(time,2)>=03 and left(time,2)< 06 then 'group2'
when left(time,2)>=06 and left(time,2)< 09 then 'group3'
when left(time,2)>=09 and left(time,2)<=12 then 'group4' End AS groupID
from [dbo].[ProductTT]
)
select groupid,count(*) as recordcount
from cte
group by groupid
My question
That query works fine but I just want to know whether there are better ways to write this query and avoid using a CASE expression.
sql-server t-sql query-performance optimization
There is a ProductTT
table as you can see below:
[dbo].[ProductTT] (ID int , Product Varchar(50) , Time Int)
...which contains the following rows:
1 XX 0030
2 UY 0354
3 YY 0517
4 ZZ 0712
5 WW 0415
6 GG 1112
7 MM 1030
8 HH 0913
Note: The format of the data in time
column is hh:mm
so 0030 is 00:30.
I want to write a query to categorize the rows based on their time
value. I need to have 4 categories like this:
category1 00 to 03
category2 03 to 06
category3 06 to 09
category4 09 to 12
I need to see how many rows pertain to each category.
My attempt so far
What I've written so far is like this:
With CTE
AS (select ID,
product,
[time],
Case
When left(time,2)>=00 and left(time,2)< 03 then 'group1'
when left(time,2)>=03 and left(time,2)< 06 then 'group2'
when left(time,2)>=06 and left(time,2)< 09 then 'group3'
when left(time,2)>=09 and left(time,2)<=12 then 'group4' End AS groupID
from [dbo].[ProductTT]
)
select groupid,count(*) as recordcount
from cte
group by groupid
My question
That query works fine but I just want to know whether there are better ways to write this query and avoid using a CASE expression.
sql-server t-sql query-performance optimization
sql-server t-sql query-performance optimization
edited Apr 30 at 19:25
MDCCL
6,90331846
6,90331846
asked Apr 30 at 9:51
Pantea TourangPantea Tourang
967
967
2
So..everything between 1201 and 23:59 gets NULL for category? Why do you not store time as aTIME
data type?
– Michael Kutz
Apr 30 at 10:28
4
What is the purpose of avoiding a CASE expression? You should make this clear because some alternatives are just different syntax for the same thing.
– Aaron Bertrand♦
Apr 30 at 10:45
add a comment |
2
So..everything between 1201 and 23:59 gets NULL for category? Why do you not store time as aTIME
data type?
– Michael Kutz
Apr 30 at 10:28
4
What is the purpose of avoiding a CASE expression? You should make this clear because some alternatives are just different syntax for the same thing.
– Aaron Bertrand♦
Apr 30 at 10:45
2
2
So..everything between 1201 and 23:59 gets NULL for category? Why do you not store time as a
TIME
data type?– Michael Kutz
Apr 30 at 10:28
So..everything between 1201 and 23:59 gets NULL for category? Why do you not store time as a
TIME
data type?– Michael Kutz
Apr 30 at 10:28
4
4
What is the purpose of avoiding a CASE expression? You should make this clear because some alternatives are just different syntax for the same thing.
– Aaron Bertrand♦
Apr 30 at 10:45
What is the purpose of avoiding a CASE expression? You should make this clear because some alternatives are just different syntax for the same thing.
– Aaron Bertrand♦
Apr 30 at 10:45
add a comment |
3 Answers
3
active
oldest
votes
You stored Time
as an int
but then displayed it as a string (with leading zeros). Those don't get stored, so in order to perform calculations that need to handle the leading zeros, you need to convert to a string first (your current query doesn't do this, so either your query doesn't work, or that table structure is not accurate). Since this is a linear calculation (groups of 3), you can simplify away the CASE
expression by simply dividing the first two digits in the time by 3 (and thanks to SQL Server's integer division, the remainder gets discarded, and we add 1 to go from 0-3 to 1-4). Of course, there is an exception, because you want 12 PM to be in group 4, not group 5. With a CASE
expression this could just be left to the ELSE
clause, but if you eliminate CASE
, you will have to deal with that exception explicitly - that's all the COALESCE
/NULLIF
stuff at the end.
;WITH x AS
(
SELECT ID, Product, [Time] = RIGHT('000'+CONVERT(varchar(4),[Time]),4)
FROM dbo.ProductTT
), y AS
(
SELECT ID, Product, [Time], h = CONVERT(char(2),[Time])
FROM x
)
SELECT ID, Product, [Time],
[GroupID] = 'group' + CONVERT(char(1),h/3+1-COALESCE(NULLIF(h%11,1)-h%11,1))
FROM y;
Results:
ID Product Time GroupID
-- ------- ---- -------
1 XX 0030 group1
2 UY 0354 group2
3 YY 0517 group2
4 ZZ 0712 group3
5 WW 0415 group2
6 GG 1112 group4
7 MM 1030 group4
8 HH 0913 group4
I strongly recommend you use the actual time
data type, as that is what it was designed for. Then you can use DATEPART(HOUR(
in your calculations instead of messy string manipulation, the query above is less complex and, as a bonus, you get built-in validation, to avoid invalid times like 1369
and 9997
. Or if the leading zeros are important but you don't care about validation, use char(4)
instead of int
.
I also think you need to handle the case where an event happens in the afternoon.
And FWIW I am not sure why you don't want to use a CASE
expression here. It's a few more characters, sure, but it's a lot more clear what the query is actually doing. Code that is self-documenting is much more valuable than code that is slightly shorter. This is simpler IMHO, and would be even simpler if you used the right data types:
;WITH x AS
(
SELECT ID, Product, [Time] = RIGHT('000'+CONVERT(varchar(4),[Time]),4)
FROM dbo.ProductTT
)
SELECT ID, Product, [Time],
GroupID = 'group' + CASE CONVERT(char(2),[Time])/3
WHEN 0 THEN '1'
WHEN 1 THEN '2'
WHEN 2 THEN '3'
ELSE '4' END
FROM x;
add a comment |
I'd use a numbers table to categorize the values in dbo.ProductTT.
I've created a simple MCVE1 to show how this works. FYI, in future, it would be great if you'd provide code like this when asking a question. It helps everyone.
USE tempdb;
IF OBJECT_ID(N'dbo.ProductTT', N'U') IS NOT NULL
BEGIN
DROP TABLE dbo.ProductTT;
END
CREATE TABLE dbo.ProductTT
(
ID int NOT NULL PRIMARY KEY CLUSTERED
, Product varchar(50) NOT NULL
, CreateTime int NOT NULL
, FormattedCreateTime AS RIGHT('0000' + CONVERT(varchar(4), CreateTime), 4)
);
INSERT INTO dbo.ProductTT
VALUES (1, 'XX', 0030)
, (2, 'UY', 0354)
, (3, 'YY', 0517)
, (4, 'ZZ', 0712)
, (5, 'WW', 0415)
, (6, 'GG', 1112)
, (7, 'MM', 1030)
, (8, 'HH', 0913)
, (9, 'H1', 1230)
, (10, 'H2', 1359)
, (11, 'H3', 2359);
IF OBJECT_ID(N'dbo.TimeGroups', N'U') IS NOT NULL
BEGIN
DROP TABLE dbo.TimeGroups;
END
CREATE TABLE dbo.TimeGroups
(
TimeGroupStart int NOT NULL
, TimeGroupEnd int NOT NULL
, TimeGroupName varchar(9) NOT NULL
, PRIMARY KEY CLUSTERED (TimeGroupStart, TimeGroupEnd)
);
INSERT INTO dbo.TimeGroups (TimeGroupStart, TimeGroupEnd, TimeGroupName)
VALUES (0, 3, '00 to 03')
, (3, 6, '03 to 06')
, (6, 9, '06 to 09')
, (9, 12, '09 to 12')
, (12, 15, '12 to 15')
, (15, 18, '15 to 18')
, (18, 21, '18 to 21')
, (21, 24, '21 to 24');
The "numbers table" in the code above is called "TimeGroups".
To get the desired output, you simply join the two tables together, as in:
SELECT tg.TimeGroupName
, TimeGroupCount = COUNT(1)
FROM dbo.ProductTT tt
INNER JOIN dbo.TimeGroups tg ON (tt.CreateTime / 100) >= tg.TimeGroupStart
AND (tt.CreateTime / 100) < tg.TimeGroupEnd
GROUP BY tg.TimeGroupName
ORDER BY tg.TimeGroupName;
The output looks like:
╔═══════════════╦════════════════╗
║ TimeGroupName ║ TimeGroupCount ║
╠═══════════════╬════════════════╣
║ 00 to 03 ║ 1 ║
║ 03 to 06 ║ 3 ║
║ 06 to 09 ║ 1 ║
║ 09 to 12 ║ 3 ║
║ 12 to 15 ║ 2 ║
║ 21 to 24 ║ 1 ║
╚═══════════════╩════════════════╝
Note that the JOIN
clause in the above query specifies the range as greater-than-or-equal to the start of the category, and less-than the end of the category. If you used less-than-or-equal-to for the end of the range, you'd have ProductTT
rows showing up in multiple categories, which is clearly incorrect.
You can see how the join works with this simple query:
SELECT tt.*
, Category = tt.CreateTime / 100
FROM dbo.ProductTT tt
The output looks like:
╔════╦═════════╦════════════╦═════════════════════╦══════════╗
║ ID ║ Product ║ CreateTime ║ FormattedCreateTime ║ Category ║
╠════╬═════════╬════════════╬═════════════════════╬══════════╣
║ 1 ║ XX ║ 30 ║ 0030 ║ 0 ║
║ 2 ║ UY ║ 354 ║ 0354 ║ 3 ║
║ 3 ║ YY ║ 517 ║ 0517 ║ 5 ║
║ 4 ║ ZZ ║ 712 ║ 0712 ║ 7 ║
║ 5 ║ WW ║ 415 ║ 0415 ║ 4 ║
║ 6 ║ GG ║ 1112 ║ 1112 ║ 11 ║
║ 7 ║ MM ║ 1030 ║ 1030 ║ 10 ║
║ 8 ║ HH ║ 913 ║ 0913 ║ 9 ║
║ 9 ║ H1 ║ 1230 ║ 1230 ║ 12 ║
║ 10 ║ H2 ║ 1359 ║ 1359 ║ 13 ║
║ 11 ║ H3 ║ 2359 ║ 2359 ║ 23 ║
╚════╩═════════╩════════════╩═════════════════════╩══════════╝
1 - I own the website pointed to in that link
1
This is a little off because a time of1230
will get dropped out of the result. You can either change the way the ranges work (category 4 end = 13), or change the query to <= and make the end of each range 2, 5, 8, 12. It's a bit convoluted because it looks linear but there are actually 13 possible hour values here. I wonder what's going to happen when something is recorded after 1 PM...
– Aaron Bertrand♦
Apr 30 at 15:54
2
Thanks for pointing that out, @AaronBertrand - I've updated the answer to gracefully handle a 24 hour clock.
– Max Vernon
Apr 30 at 19:23
add a comment |
You could convert the hour part of the time string and divide it by three. The integer of this division plus 1 is equal to your group number.
(00/3) + 1 = 1
(01/3) + 1 = 1
(02/3) + 1 = 1
(03/3) + 1 = 2
(04/3) + 1 = 2
...
In that way you will no longer need the case.
New contributor
thanks for your answer but i did not get that. I was wondering if you could explain a little bit more please.
– Pantea Tourang
Apr 30 at 10:05
1
You use the case because you want to know if the time belongs to the group1, group2 and so on. One way to avoid the case is to figure out what group the time belongs using the formulas that I give to you. You can calculate the groupId field using that formula: "group"&((to_int(to_int(left(time,2)))/3)+1). I do not know the function to convert string to int in your database so a used to_int in the example.
– Jandisson
Apr 30 at 10:19
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "182"
;
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
);
);
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%2fdba.stackexchange.com%2fquestions%2f237015%2fquery-to-categorize-rows-based-on-a-time-column-without-using-a-case-expressio%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You stored Time
as an int
but then displayed it as a string (with leading zeros). Those don't get stored, so in order to perform calculations that need to handle the leading zeros, you need to convert to a string first (your current query doesn't do this, so either your query doesn't work, or that table structure is not accurate). Since this is a linear calculation (groups of 3), you can simplify away the CASE
expression by simply dividing the first two digits in the time by 3 (and thanks to SQL Server's integer division, the remainder gets discarded, and we add 1 to go from 0-3 to 1-4). Of course, there is an exception, because you want 12 PM to be in group 4, not group 5. With a CASE
expression this could just be left to the ELSE
clause, but if you eliminate CASE
, you will have to deal with that exception explicitly - that's all the COALESCE
/NULLIF
stuff at the end.
;WITH x AS
(
SELECT ID, Product, [Time] = RIGHT('000'+CONVERT(varchar(4),[Time]),4)
FROM dbo.ProductTT
), y AS
(
SELECT ID, Product, [Time], h = CONVERT(char(2),[Time])
FROM x
)
SELECT ID, Product, [Time],
[GroupID] = 'group' + CONVERT(char(1),h/3+1-COALESCE(NULLIF(h%11,1)-h%11,1))
FROM y;
Results:
ID Product Time GroupID
-- ------- ---- -------
1 XX 0030 group1
2 UY 0354 group2
3 YY 0517 group2
4 ZZ 0712 group3
5 WW 0415 group2
6 GG 1112 group4
7 MM 1030 group4
8 HH 0913 group4
I strongly recommend you use the actual time
data type, as that is what it was designed for. Then you can use DATEPART(HOUR(
in your calculations instead of messy string manipulation, the query above is less complex and, as a bonus, you get built-in validation, to avoid invalid times like 1369
and 9997
. Or if the leading zeros are important but you don't care about validation, use char(4)
instead of int
.
I also think you need to handle the case where an event happens in the afternoon.
And FWIW I am not sure why you don't want to use a CASE
expression here. It's a few more characters, sure, but it's a lot more clear what the query is actually doing. Code that is self-documenting is much more valuable than code that is slightly shorter. This is simpler IMHO, and would be even simpler if you used the right data types:
;WITH x AS
(
SELECT ID, Product, [Time] = RIGHT('000'+CONVERT(varchar(4),[Time]),4)
FROM dbo.ProductTT
)
SELECT ID, Product, [Time],
GroupID = 'group' + CASE CONVERT(char(2),[Time])/3
WHEN 0 THEN '1'
WHEN 1 THEN '2'
WHEN 2 THEN '3'
ELSE '4' END
FROM x;
add a comment |
You stored Time
as an int
but then displayed it as a string (with leading zeros). Those don't get stored, so in order to perform calculations that need to handle the leading zeros, you need to convert to a string first (your current query doesn't do this, so either your query doesn't work, or that table structure is not accurate). Since this is a linear calculation (groups of 3), you can simplify away the CASE
expression by simply dividing the first two digits in the time by 3 (and thanks to SQL Server's integer division, the remainder gets discarded, and we add 1 to go from 0-3 to 1-4). Of course, there is an exception, because you want 12 PM to be in group 4, not group 5. With a CASE
expression this could just be left to the ELSE
clause, but if you eliminate CASE
, you will have to deal with that exception explicitly - that's all the COALESCE
/NULLIF
stuff at the end.
;WITH x AS
(
SELECT ID, Product, [Time] = RIGHT('000'+CONVERT(varchar(4),[Time]),4)
FROM dbo.ProductTT
), y AS
(
SELECT ID, Product, [Time], h = CONVERT(char(2),[Time])
FROM x
)
SELECT ID, Product, [Time],
[GroupID] = 'group' + CONVERT(char(1),h/3+1-COALESCE(NULLIF(h%11,1)-h%11,1))
FROM y;
Results:
ID Product Time GroupID
-- ------- ---- -------
1 XX 0030 group1
2 UY 0354 group2
3 YY 0517 group2
4 ZZ 0712 group3
5 WW 0415 group2
6 GG 1112 group4
7 MM 1030 group4
8 HH 0913 group4
I strongly recommend you use the actual time
data type, as that is what it was designed for. Then you can use DATEPART(HOUR(
in your calculations instead of messy string manipulation, the query above is less complex and, as a bonus, you get built-in validation, to avoid invalid times like 1369
and 9997
. Or if the leading zeros are important but you don't care about validation, use char(4)
instead of int
.
I also think you need to handle the case where an event happens in the afternoon.
And FWIW I am not sure why you don't want to use a CASE
expression here. It's a few more characters, sure, but it's a lot more clear what the query is actually doing. Code that is self-documenting is much more valuable than code that is slightly shorter. This is simpler IMHO, and would be even simpler if you used the right data types:
;WITH x AS
(
SELECT ID, Product, [Time] = RIGHT('000'+CONVERT(varchar(4),[Time]),4)
FROM dbo.ProductTT
)
SELECT ID, Product, [Time],
GroupID = 'group' + CASE CONVERT(char(2),[Time])/3
WHEN 0 THEN '1'
WHEN 1 THEN '2'
WHEN 2 THEN '3'
ELSE '4' END
FROM x;
add a comment |
You stored Time
as an int
but then displayed it as a string (with leading zeros). Those don't get stored, so in order to perform calculations that need to handle the leading zeros, you need to convert to a string first (your current query doesn't do this, so either your query doesn't work, or that table structure is not accurate). Since this is a linear calculation (groups of 3), you can simplify away the CASE
expression by simply dividing the first two digits in the time by 3 (and thanks to SQL Server's integer division, the remainder gets discarded, and we add 1 to go from 0-3 to 1-4). Of course, there is an exception, because you want 12 PM to be in group 4, not group 5. With a CASE
expression this could just be left to the ELSE
clause, but if you eliminate CASE
, you will have to deal with that exception explicitly - that's all the COALESCE
/NULLIF
stuff at the end.
;WITH x AS
(
SELECT ID, Product, [Time] = RIGHT('000'+CONVERT(varchar(4),[Time]),4)
FROM dbo.ProductTT
), y AS
(
SELECT ID, Product, [Time], h = CONVERT(char(2),[Time])
FROM x
)
SELECT ID, Product, [Time],
[GroupID] = 'group' + CONVERT(char(1),h/3+1-COALESCE(NULLIF(h%11,1)-h%11,1))
FROM y;
Results:
ID Product Time GroupID
-- ------- ---- -------
1 XX 0030 group1
2 UY 0354 group2
3 YY 0517 group2
4 ZZ 0712 group3
5 WW 0415 group2
6 GG 1112 group4
7 MM 1030 group4
8 HH 0913 group4
I strongly recommend you use the actual time
data type, as that is what it was designed for. Then you can use DATEPART(HOUR(
in your calculations instead of messy string manipulation, the query above is less complex and, as a bonus, you get built-in validation, to avoid invalid times like 1369
and 9997
. Or if the leading zeros are important but you don't care about validation, use char(4)
instead of int
.
I also think you need to handle the case where an event happens in the afternoon.
And FWIW I am not sure why you don't want to use a CASE
expression here. It's a few more characters, sure, but it's a lot more clear what the query is actually doing. Code that is self-documenting is much more valuable than code that is slightly shorter. This is simpler IMHO, and would be even simpler if you used the right data types:
;WITH x AS
(
SELECT ID, Product, [Time] = RIGHT('000'+CONVERT(varchar(4),[Time]),4)
FROM dbo.ProductTT
)
SELECT ID, Product, [Time],
GroupID = 'group' + CASE CONVERT(char(2),[Time])/3
WHEN 0 THEN '1'
WHEN 1 THEN '2'
WHEN 2 THEN '3'
ELSE '4' END
FROM x;
You stored Time
as an int
but then displayed it as a string (with leading zeros). Those don't get stored, so in order to perform calculations that need to handle the leading zeros, you need to convert to a string first (your current query doesn't do this, so either your query doesn't work, or that table structure is not accurate). Since this is a linear calculation (groups of 3), you can simplify away the CASE
expression by simply dividing the first two digits in the time by 3 (and thanks to SQL Server's integer division, the remainder gets discarded, and we add 1 to go from 0-3 to 1-4). Of course, there is an exception, because you want 12 PM to be in group 4, not group 5. With a CASE
expression this could just be left to the ELSE
clause, but if you eliminate CASE
, you will have to deal with that exception explicitly - that's all the COALESCE
/NULLIF
stuff at the end.
;WITH x AS
(
SELECT ID, Product, [Time] = RIGHT('000'+CONVERT(varchar(4),[Time]),4)
FROM dbo.ProductTT
), y AS
(
SELECT ID, Product, [Time], h = CONVERT(char(2),[Time])
FROM x
)
SELECT ID, Product, [Time],
[GroupID] = 'group' + CONVERT(char(1),h/3+1-COALESCE(NULLIF(h%11,1)-h%11,1))
FROM y;
Results:
ID Product Time GroupID
-- ------- ---- -------
1 XX 0030 group1
2 UY 0354 group2
3 YY 0517 group2
4 ZZ 0712 group3
5 WW 0415 group2
6 GG 1112 group4
7 MM 1030 group4
8 HH 0913 group4
I strongly recommend you use the actual time
data type, as that is what it was designed for. Then you can use DATEPART(HOUR(
in your calculations instead of messy string manipulation, the query above is less complex and, as a bonus, you get built-in validation, to avoid invalid times like 1369
and 9997
. Or if the leading zeros are important but you don't care about validation, use char(4)
instead of int
.
I also think you need to handle the case where an event happens in the afternoon.
And FWIW I am not sure why you don't want to use a CASE
expression here. It's a few more characters, sure, but it's a lot more clear what the query is actually doing. Code that is self-documenting is much more valuable than code that is slightly shorter. This is simpler IMHO, and would be even simpler if you used the right data types:
;WITH x AS
(
SELECT ID, Product, [Time] = RIGHT('000'+CONVERT(varchar(4),[Time]),4)
FROM dbo.ProductTT
)
SELECT ID, Product, [Time],
GroupID = 'group' + CASE CONVERT(char(2),[Time])/3
WHEN 0 THEN '1'
WHEN 1 THEN '2'
WHEN 2 THEN '3'
ELSE '4' END
FROM x;
edited Apr 30 at 11:33
answered Apr 30 at 10:59
Aaron Bertrand♦Aaron Bertrand
155k18301500
155k18301500
add a comment |
add a comment |
I'd use a numbers table to categorize the values in dbo.ProductTT.
I've created a simple MCVE1 to show how this works. FYI, in future, it would be great if you'd provide code like this when asking a question. It helps everyone.
USE tempdb;
IF OBJECT_ID(N'dbo.ProductTT', N'U') IS NOT NULL
BEGIN
DROP TABLE dbo.ProductTT;
END
CREATE TABLE dbo.ProductTT
(
ID int NOT NULL PRIMARY KEY CLUSTERED
, Product varchar(50) NOT NULL
, CreateTime int NOT NULL
, FormattedCreateTime AS RIGHT('0000' + CONVERT(varchar(4), CreateTime), 4)
);
INSERT INTO dbo.ProductTT
VALUES (1, 'XX', 0030)
, (2, 'UY', 0354)
, (3, 'YY', 0517)
, (4, 'ZZ', 0712)
, (5, 'WW', 0415)
, (6, 'GG', 1112)
, (7, 'MM', 1030)
, (8, 'HH', 0913)
, (9, 'H1', 1230)
, (10, 'H2', 1359)
, (11, 'H3', 2359);
IF OBJECT_ID(N'dbo.TimeGroups', N'U') IS NOT NULL
BEGIN
DROP TABLE dbo.TimeGroups;
END
CREATE TABLE dbo.TimeGroups
(
TimeGroupStart int NOT NULL
, TimeGroupEnd int NOT NULL
, TimeGroupName varchar(9) NOT NULL
, PRIMARY KEY CLUSTERED (TimeGroupStart, TimeGroupEnd)
);
INSERT INTO dbo.TimeGroups (TimeGroupStart, TimeGroupEnd, TimeGroupName)
VALUES (0, 3, '00 to 03')
, (3, 6, '03 to 06')
, (6, 9, '06 to 09')
, (9, 12, '09 to 12')
, (12, 15, '12 to 15')
, (15, 18, '15 to 18')
, (18, 21, '18 to 21')
, (21, 24, '21 to 24');
The "numbers table" in the code above is called "TimeGroups".
To get the desired output, you simply join the two tables together, as in:
SELECT tg.TimeGroupName
, TimeGroupCount = COUNT(1)
FROM dbo.ProductTT tt
INNER JOIN dbo.TimeGroups tg ON (tt.CreateTime / 100) >= tg.TimeGroupStart
AND (tt.CreateTime / 100) < tg.TimeGroupEnd
GROUP BY tg.TimeGroupName
ORDER BY tg.TimeGroupName;
The output looks like:
╔═══════════════╦════════════════╗
║ TimeGroupName ║ TimeGroupCount ║
╠═══════════════╬════════════════╣
║ 00 to 03 ║ 1 ║
║ 03 to 06 ║ 3 ║
║ 06 to 09 ║ 1 ║
║ 09 to 12 ║ 3 ║
║ 12 to 15 ║ 2 ║
║ 21 to 24 ║ 1 ║
╚═══════════════╩════════════════╝
Note that the JOIN
clause in the above query specifies the range as greater-than-or-equal to the start of the category, and less-than the end of the category. If you used less-than-or-equal-to for the end of the range, you'd have ProductTT
rows showing up in multiple categories, which is clearly incorrect.
You can see how the join works with this simple query:
SELECT tt.*
, Category = tt.CreateTime / 100
FROM dbo.ProductTT tt
The output looks like:
╔════╦═════════╦════════════╦═════════════════════╦══════════╗
║ ID ║ Product ║ CreateTime ║ FormattedCreateTime ║ Category ║
╠════╬═════════╬════════════╬═════════════════════╬══════════╣
║ 1 ║ XX ║ 30 ║ 0030 ║ 0 ║
║ 2 ║ UY ║ 354 ║ 0354 ║ 3 ║
║ 3 ║ YY ║ 517 ║ 0517 ║ 5 ║
║ 4 ║ ZZ ║ 712 ║ 0712 ║ 7 ║
║ 5 ║ WW ║ 415 ║ 0415 ║ 4 ║
║ 6 ║ GG ║ 1112 ║ 1112 ║ 11 ║
║ 7 ║ MM ║ 1030 ║ 1030 ║ 10 ║
║ 8 ║ HH ║ 913 ║ 0913 ║ 9 ║
║ 9 ║ H1 ║ 1230 ║ 1230 ║ 12 ║
║ 10 ║ H2 ║ 1359 ║ 1359 ║ 13 ║
║ 11 ║ H3 ║ 2359 ║ 2359 ║ 23 ║
╚════╩═════════╩════════════╩═════════════════════╩══════════╝
1 - I own the website pointed to in that link
1
This is a little off because a time of1230
will get dropped out of the result. You can either change the way the ranges work (category 4 end = 13), or change the query to <= and make the end of each range 2, 5, 8, 12. It's a bit convoluted because it looks linear but there are actually 13 possible hour values here. I wonder what's going to happen when something is recorded after 1 PM...
– Aaron Bertrand♦
Apr 30 at 15:54
2
Thanks for pointing that out, @AaronBertrand - I've updated the answer to gracefully handle a 24 hour clock.
– Max Vernon
Apr 30 at 19:23
add a comment |
I'd use a numbers table to categorize the values in dbo.ProductTT.
I've created a simple MCVE1 to show how this works. FYI, in future, it would be great if you'd provide code like this when asking a question. It helps everyone.
USE tempdb;
IF OBJECT_ID(N'dbo.ProductTT', N'U') IS NOT NULL
BEGIN
DROP TABLE dbo.ProductTT;
END
CREATE TABLE dbo.ProductTT
(
ID int NOT NULL PRIMARY KEY CLUSTERED
, Product varchar(50) NOT NULL
, CreateTime int NOT NULL
, FormattedCreateTime AS RIGHT('0000' + CONVERT(varchar(4), CreateTime), 4)
);
INSERT INTO dbo.ProductTT
VALUES (1, 'XX', 0030)
, (2, 'UY', 0354)
, (3, 'YY', 0517)
, (4, 'ZZ', 0712)
, (5, 'WW', 0415)
, (6, 'GG', 1112)
, (7, 'MM', 1030)
, (8, 'HH', 0913)
, (9, 'H1', 1230)
, (10, 'H2', 1359)
, (11, 'H3', 2359);
IF OBJECT_ID(N'dbo.TimeGroups', N'U') IS NOT NULL
BEGIN
DROP TABLE dbo.TimeGroups;
END
CREATE TABLE dbo.TimeGroups
(
TimeGroupStart int NOT NULL
, TimeGroupEnd int NOT NULL
, TimeGroupName varchar(9) NOT NULL
, PRIMARY KEY CLUSTERED (TimeGroupStart, TimeGroupEnd)
);
INSERT INTO dbo.TimeGroups (TimeGroupStart, TimeGroupEnd, TimeGroupName)
VALUES (0, 3, '00 to 03')
, (3, 6, '03 to 06')
, (6, 9, '06 to 09')
, (9, 12, '09 to 12')
, (12, 15, '12 to 15')
, (15, 18, '15 to 18')
, (18, 21, '18 to 21')
, (21, 24, '21 to 24');
The "numbers table" in the code above is called "TimeGroups".
To get the desired output, you simply join the two tables together, as in:
SELECT tg.TimeGroupName
, TimeGroupCount = COUNT(1)
FROM dbo.ProductTT tt
INNER JOIN dbo.TimeGroups tg ON (tt.CreateTime / 100) >= tg.TimeGroupStart
AND (tt.CreateTime / 100) < tg.TimeGroupEnd
GROUP BY tg.TimeGroupName
ORDER BY tg.TimeGroupName;
The output looks like:
╔═══════════════╦════════════════╗
║ TimeGroupName ║ TimeGroupCount ║
╠═══════════════╬════════════════╣
║ 00 to 03 ║ 1 ║
║ 03 to 06 ║ 3 ║
║ 06 to 09 ║ 1 ║
║ 09 to 12 ║ 3 ║
║ 12 to 15 ║ 2 ║
║ 21 to 24 ║ 1 ║
╚═══════════════╩════════════════╝
Note that the JOIN
clause in the above query specifies the range as greater-than-or-equal to the start of the category, and less-than the end of the category. If you used less-than-or-equal-to for the end of the range, you'd have ProductTT
rows showing up in multiple categories, which is clearly incorrect.
You can see how the join works with this simple query:
SELECT tt.*
, Category = tt.CreateTime / 100
FROM dbo.ProductTT tt
The output looks like:
╔════╦═════════╦════════════╦═════════════════════╦══════════╗
║ ID ║ Product ║ CreateTime ║ FormattedCreateTime ║ Category ║
╠════╬═════════╬════════════╬═════════════════════╬══════════╣
║ 1 ║ XX ║ 30 ║ 0030 ║ 0 ║
║ 2 ║ UY ║ 354 ║ 0354 ║ 3 ║
║ 3 ║ YY ║ 517 ║ 0517 ║ 5 ║
║ 4 ║ ZZ ║ 712 ║ 0712 ║ 7 ║
║ 5 ║ WW ║ 415 ║ 0415 ║ 4 ║
║ 6 ║ GG ║ 1112 ║ 1112 ║ 11 ║
║ 7 ║ MM ║ 1030 ║ 1030 ║ 10 ║
║ 8 ║ HH ║ 913 ║ 0913 ║ 9 ║
║ 9 ║ H1 ║ 1230 ║ 1230 ║ 12 ║
║ 10 ║ H2 ║ 1359 ║ 1359 ║ 13 ║
║ 11 ║ H3 ║ 2359 ║ 2359 ║ 23 ║
╚════╩═════════╩════════════╩═════════════════════╩══════════╝
1 - I own the website pointed to in that link
1
This is a little off because a time of1230
will get dropped out of the result. You can either change the way the ranges work (category 4 end = 13), or change the query to <= and make the end of each range 2, 5, 8, 12. It's a bit convoluted because it looks linear but there are actually 13 possible hour values here. I wonder what's going to happen when something is recorded after 1 PM...
– Aaron Bertrand♦
Apr 30 at 15:54
2
Thanks for pointing that out, @AaronBertrand - I've updated the answer to gracefully handle a 24 hour clock.
– Max Vernon
Apr 30 at 19:23
add a comment |
I'd use a numbers table to categorize the values in dbo.ProductTT.
I've created a simple MCVE1 to show how this works. FYI, in future, it would be great if you'd provide code like this when asking a question. It helps everyone.
USE tempdb;
IF OBJECT_ID(N'dbo.ProductTT', N'U') IS NOT NULL
BEGIN
DROP TABLE dbo.ProductTT;
END
CREATE TABLE dbo.ProductTT
(
ID int NOT NULL PRIMARY KEY CLUSTERED
, Product varchar(50) NOT NULL
, CreateTime int NOT NULL
, FormattedCreateTime AS RIGHT('0000' + CONVERT(varchar(4), CreateTime), 4)
);
INSERT INTO dbo.ProductTT
VALUES (1, 'XX', 0030)
, (2, 'UY', 0354)
, (3, 'YY', 0517)
, (4, 'ZZ', 0712)
, (5, 'WW', 0415)
, (6, 'GG', 1112)
, (7, 'MM', 1030)
, (8, 'HH', 0913)
, (9, 'H1', 1230)
, (10, 'H2', 1359)
, (11, 'H3', 2359);
IF OBJECT_ID(N'dbo.TimeGroups', N'U') IS NOT NULL
BEGIN
DROP TABLE dbo.TimeGroups;
END
CREATE TABLE dbo.TimeGroups
(
TimeGroupStart int NOT NULL
, TimeGroupEnd int NOT NULL
, TimeGroupName varchar(9) NOT NULL
, PRIMARY KEY CLUSTERED (TimeGroupStart, TimeGroupEnd)
);
INSERT INTO dbo.TimeGroups (TimeGroupStart, TimeGroupEnd, TimeGroupName)
VALUES (0, 3, '00 to 03')
, (3, 6, '03 to 06')
, (6, 9, '06 to 09')
, (9, 12, '09 to 12')
, (12, 15, '12 to 15')
, (15, 18, '15 to 18')
, (18, 21, '18 to 21')
, (21, 24, '21 to 24');
The "numbers table" in the code above is called "TimeGroups".
To get the desired output, you simply join the two tables together, as in:
SELECT tg.TimeGroupName
, TimeGroupCount = COUNT(1)
FROM dbo.ProductTT tt
INNER JOIN dbo.TimeGroups tg ON (tt.CreateTime / 100) >= tg.TimeGroupStart
AND (tt.CreateTime / 100) < tg.TimeGroupEnd
GROUP BY tg.TimeGroupName
ORDER BY tg.TimeGroupName;
The output looks like:
╔═══════════════╦════════════════╗
║ TimeGroupName ║ TimeGroupCount ║
╠═══════════════╬════════════════╣
║ 00 to 03 ║ 1 ║
║ 03 to 06 ║ 3 ║
║ 06 to 09 ║ 1 ║
║ 09 to 12 ║ 3 ║
║ 12 to 15 ║ 2 ║
║ 21 to 24 ║ 1 ║
╚═══════════════╩════════════════╝
Note that the JOIN
clause in the above query specifies the range as greater-than-or-equal to the start of the category, and less-than the end of the category. If you used less-than-or-equal-to for the end of the range, you'd have ProductTT
rows showing up in multiple categories, which is clearly incorrect.
You can see how the join works with this simple query:
SELECT tt.*
, Category = tt.CreateTime / 100
FROM dbo.ProductTT tt
The output looks like:
╔════╦═════════╦════════════╦═════════════════════╦══════════╗
║ ID ║ Product ║ CreateTime ║ FormattedCreateTime ║ Category ║
╠════╬═════════╬════════════╬═════════════════════╬══════════╣
║ 1 ║ XX ║ 30 ║ 0030 ║ 0 ║
║ 2 ║ UY ║ 354 ║ 0354 ║ 3 ║
║ 3 ║ YY ║ 517 ║ 0517 ║ 5 ║
║ 4 ║ ZZ ║ 712 ║ 0712 ║ 7 ║
║ 5 ║ WW ║ 415 ║ 0415 ║ 4 ║
║ 6 ║ GG ║ 1112 ║ 1112 ║ 11 ║
║ 7 ║ MM ║ 1030 ║ 1030 ║ 10 ║
║ 8 ║ HH ║ 913 ║ 0913 ║ 9 ║
║ 9 ║ H1 ║ 1230 ║ 1230 ║ 12 ║
║ 10 ║ H2 ║ 1359 ║ 1359 ║ 13 ║
║ 11 ║ H3 ║ 2359 ║ 2359 ║ 23 ║
╚════╩═════════╩════════════╩═════════════════════╩══════════╝
1 - I own the website pointed to in that link
I'd use a numbers table to categorize the values in dbo.ProductTT.
I've created a simple MCVE1 to show how this works. FYI, in future, it would be great if you'd provide code like this when asking a question. It helps everyone.
USE tempdb;
IF OBJECT_ID(N'dbo.ProductTT', N'U') IS NOT NULL
BEGIN
DROP TABLE dbo.ProductTT;
END
CREATE TABLE dbo.ProductTT
(
ID int NOT NULL PRIMARY KEY CLUSTERED
, Product varchar(50) NOT NULL
, CreateTime int NOT NULL
, FormattedCreateTime AS RIGHT('0000' + CONVERT(varchar(4), CreateTime), 4)
);
INSERT INTO dbo.ProductTT
VALUES (1, 'XX', 0030)
, (2, 'UY', 0354)
, (3, 'YY', 0517)
, (4, 'ZZ', 0712)
, (5, 'WW', 0415)
, (6, 'GG', 1112)
, (7, 'MM', 1030)
, (8, 'HH', 0913)
, (9, 'H1', 1230)
, (10, 'H2', 1359)
, (11, 'H3', 2359);
IF OBJECT_ID(N'dbo.TimeGroups', N'U') IS NOT NULL
BEGIN
DROP TABLE dbo.TimeGroups;
END
CREATE TABLE dbo.TimeGroups
(
TimeGroupStart int NOT NULL
, TimeGroupEnd int NOT NULL
, TimeGroupName varchar(9) NOT NULL
, PRIMARY KEY CLUSTERED (TimeGroupStart, TimeGroupEnd)
);
INSERT INTO dbo.TimeGroups (TimeGroupStart, TimeGroupEnd, TimeGroupName)
VALUES (0, 3, '00 to 03')
, (3, 6, '03 to 06')
, (6, 9, '06 to 09')
, (9, 12, '09 to 12')
, (12, 15, '12 to 15')
, (15, 18, '15 to 18')
, (18, 21, '18 to 21')
, (21, 24, '21 to 24');
The "numbers table" in the code above is called "TimeGroups".
To get the desired output, you simply join the two tables together, as in:
SELECT tg.TimeGroupName
, TimeGroupCount = COUNT(1)
FROM dbo.ProductTT tt
INNER JOIN dbo.TimeGroups tg ON (tt.CreateTime / 100) >= tg.TimeGroupStart
AND (tt.CreateTime / 100) < tg.TimeGroupEnd
GROUP BY tg.TimeGroupName
ORDER BY tg.TimeGroupName;
The output looks like:
╔═══════════════╦════════════════╗
║ TimeGroupName ║ TimeGroupCount ║
╠═══════════════╬════════════════╣
║ 00 to 03 ║ 1 ║
║ 03 to 06 ║ 3 ║
║ 06 to 09 ║ 1 ║
║ 09 to 12 ║ 3 ║
║ 12 to 15 ║ 2 ║
║ 21 to 24 ║ 1 ║
╚═══════════════╩════════════════╝
Note that the JOIN
clause in the above query specifies the range as greater-than-or-equal to the start of the category, and less-than the end of the category. If you used less-than-or-equal-to for the end of the range, you'd have ProductTT
rows showing up in multiple categories, which is clearly incorrect.
You can see how the join works with this simple query:
SELECT tt.*
, Category = tt.CreateTime / 100
FROM dbo.ProductTT tt
The output looks like:
╔════╦═════════╦════════════╦═════════════════════╦══════════╗
║ ID ║ Product ║ CreateTime ║ FormattedCreateTime ║ Category ║
╠════╬═════════╬════════════╬═════════════════════╬══════════╣
║ 1 ║ XX ║ 30 ║ 0030 ║ 0 ║
║ 2 ║ UY ║ 354 ║ 0354 ║ 3 ║
║ 3 ║ YY ║ 517 ║ 0517 ║ 5 ║
║ 4 ║ ZZ ║ 712 ║ 0712 ║ 7 ║
║ 5 ║ WW ║ 415 ║ 0415 ║ 4 ║
║ 6 ║ GG ║ 1112 ║ 1112 ║ 11 ║
║ 7 ║ MM ║ 1030 ║ 1030 ║ 10 ║
║ 8 ║ HH ║ 913 ║ 0913 ║ 9 ║
║ 9 ║ H1 ║ 1230 ║ 1230 ║ 12 ║
║ 10 ║ H2 ║ 1359 ║ 1359 ║ 13 ║
║ 11 ║ H3 ║ 2359 ║ 2359 ║ 23 ║
╚════╩═════════╩════════════╩═════════════════════╩══════════╝
1 - I own the website pointed to in that link
edited Apr 30 at 19:23
answered Apr 30 at 13:57
Max VernonMax Vernon
53.1k13116234
53.1k13116234
1
This is a little off because a time of1230
will get dropped out of the result. You can either change the way the ranges work (category 4 end = 13), or change the query to <= and make the end of each range 2, 5, 8, 12. It's a bit convoluted because it looks linear but there are actually 13 possible hour values here. I wonder what's going to happen when something is recorded after 1 PM...
– Aaron Bertrand♦
Apr 30 at 15:54
2
Thanks for pointing that out, @AaronBertrand - I've updated the answer to gracefully handle a 24 hour clock.
– Max Vernon
Apr 30 at 19:23
add a comment |
1
This is a little off because a time of1230
will get dropped out of the result. You can either change the way the ranges work (category 4 end = 13), or change the query to <= and make the end of each range 2, 5, 8, 12. It's a bit convoluted because it looks linear but there are actually 13 possible hour values here. I wonder what's going to happen when something is recorded after 1 PM...
– Aaron Bertrand♦
Apr 30 at 15:54
2
Thanks for pointing that out, @AaronBertrand - I've updated the answer to gracefully handle a 24 hour clock.
– Max Vernon
Apr 30 at 19:23
1
1
This is a little off because a time of
1230
will get dropped out of the result. You can either change the way the ranges work (category 4 end = 13), or change the query to <= and make the end of each range 2, 5, 8, 12. It's a bit convoluted because it looks linear but there are actually 13 possible hour values here. I wonder what's going to happen when something is recorded after 1 PM...– Aaron Bertrand♦
Apr 30 at 15:54
This is a little off because a time of
1230
will get dropped out of the result. You can either change the way the ranges work (category 4 end = 13), or change the query to <= and make the end of each range 2, 5, 8, 12. It's a bit convoluted because it looks linear but there are actually 13 possible hour values here. I wonder what's going to happen when something is recorded after 1 PM...– Aaron Bertrand♦
Apr 30 at 15:54
2
2
Thanks for pointing that out, @AaronBertrand - I've updated the answer to gracefully handle a 24 hour clock.
– Max Vernon
Apr 30 at 19:23
Thanks for pointing that out, @AaronBertrand - I've updated the answer to gracefully handle a 24 hour clock.
– Max Vernon
Apr 30 at 19:23
add a comment |
You could convert the hour part of the time string and divide it by three. The integer of this division plus 1 is equal to your group number.
(00/3) + 1 = 1
(01/3) + 1 = 1
(02/3) + 1 = 1
(03/3) + 1 = 2
(04/3) + 1 = 2
...
In that way you will no longer need the case.
New contributor
thanks for your answer but i did not get that. I was wondering if you could explain a little bit more please.
– Pantea Tourang
Apr 30 at 10:05
1
You use the case because you want to know if the time belongs to the group1, group2 and so on. One way to avoid the case is to figure out what group the time belongs using the formulas that I give to you. You can calculate the groupId field using that formula: "group"&((to_int(to_int(left(time,2)))/3)+1). I do not know the function to convert string to int in your database so a used to_int in the example.
– Jandisson
Apr 30 at 10:19
add a comment |
You could convert the hour part of the time string and divide it by three. The integer of this division plus 1 is equal to your group number.
(00/3) + 1 = 1
(01/3) + 1 = 1
(02/3) + 1 = 1
(03/3) + 1 = 2
(04/3) + 1 = 2
...
In that way you will no longer need the case.
New contributor
thanks for your answer but i did not get that. I was wondering if you could explain a little bit more please.
– Pantea Tourang
Apr 30 at 10:05
1
You use the case because you want to know if the time belongs to the group1, group2 and so on. One way to avoid the case is to figure out what group the time belongs using the formulas that I give to you. You can calculate the groupId field using that formula: "group"&((to_int(to_int(left(time,2)))/3)+1). I do not know the function to convert string to int in your database so a used to_int in the example.
– Jandisson
Apr 30 at 10:19
add a comment |
You could convert the hour part of the time string and divide it by three. The integer of this division plus 1 is equal to your group number.
(00/3) + 1 = 1
(01/3) + 1 = 1
(02/3) + 1 = 1
(03/3) + 1 = 2
(04/3) + 1 = 2
...
In that way you will no longer need the case.
New contributor
You could convert the hour part of the time string and divide it by three. The integer of this division plus 1 is equal to your group number.
(00/3) + 1 = 1
(01/3) + 1 = 1
(02/3) + 1 = 1
(03/3) + 1 = 2
(04/3) + 1 = 2
...
In that way you will no longer need the case.
New contributor
New contributor
answered Apr 30 at 10:02
JandissonJandisson
1111
1111
New contributor
New contributor
thanks for your answer but i did not get that. I was wondering if you could explain a little bit more please.
– Pantea Tourang
Apr 30 at 10:05
1
You use the case because you want to know if the time belongs to the group1, group2 and so on. One way to avoid the case is to figure out what group the time belongs using the formulas that I give to you. You can calculate the groupId field using that formula: "group"&((to_int(to_int(left(time,2)))/3)+1). I do not know the function to convert string to int in your database so a used to_int in the example.
– Jandisson
Apr 30 at 10:19
add a comment |
thanks for your answer but i did not get that. I was wondering if you could explain a little bit more please.
– Pantea Tourang
Apr 30 at 10:05
1
You use the case because you want to know if the time belongs to the group1, group2 and so on. One way to avoid the case is to figure out what group the time belongs using the formulas that I give to you. You can calculate the groupId field using that formula: "group"&((to_int(to_int(left(time,2)))/3)+1). I do not know the function to convert string to int in your database so a used to_int in the example.
– Jandisson
Apr 30 at 10:19
thanks for your answer but i did not get that. I was wondering if you could explain a little bit more please.
– Pantea Tourang
Apr 30 at 10:05
thanks for your answer but i did not get that. I was wondering if you could explain a little bit more please.
– Pantea Tourang
Apr 30 at 10:05
1
1
You use the case because you want to know if the time belongs to the group1, group2 and so on. One way to avoid the case is to figure out what group the time belongs using the formulas that I give to you. You can calculate the groupId field using that formula: "group"&((to_int(to_int(left(time,2)))/3)+1). I do not know the function to convert string to int in your database so a used to_int in the example.
– Jandisson
Apr 30 at 10:19
You use the case because you want to know if the time belongs to the group1, group2 and so on. One way to avoid the case is to figure out what group the time belongs using the formulas that I give to you. You can calculate the groupId field using that formula: "group"&((to_int(to_int(left(time,2)))/3)+1). I do not know the function to convert string to int in your database so a used to_int in the example.
– Jandisson
Apr 30 at 10:19
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2fdba.stackexchange.com%2fquestions%2f237015%2fquery-to-categorize-rows-based-on-a-time-column-without-using-a-case-expressio%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
2
So..everything between 1201 and 23:59 gets NULL for category? Why do you not store time as a
TIME
data type?– Michael Kutz
Apr 30 at 10:28
4
What is the purpose of avoiding a CASE expression? You should make this clear because some alternatives are just different syntax for the same thing.
– Aaron Bertrand♦
Apr 30 at 10:45