Postgres/Redshift DATEDIFF convert to FLOAT
up vote
0
down vote
favorite
I am using Python SQLAlchemy to connect to a Redshift instance. I am trying to calculate a DATEDIFF in minutes between 2 datetimes, which works. But when I try to CAST the result to FLOAT and use it in a CASE WHEN against ' < 1' , I get only 'THEN' results?
SELECT distinct u2.id,
CASE
WHEN
CAST(DATEDIFF(minute, u1.usa_start_datetime::timestamp,u1.usa_end_datetime::timestamp) * 1.00 as FLOAT) < 1.00
THEN 1.00 END as fsess_hr,
sql amazon-redshift
add a comment |
up vote
0
down vote
favorite
I am using Python SQLAlchemy to connect to a Redshift instance. I am trying to calculate a DATEDIFF in minutes between 2 datetimes, which works. But when I try to CAST the result to FLOAT and use it in a CASE WHEN against ' < 1' , I get only 'THEN' results?
SELECT distinct u2.id,
CASE
WHEN
CAST(DATEDIFF(minute, u1.usa_start_datetime::timestamp,u1.usa_end_datetime::timestamp) * 1.00 as FLOAT) < 1.00
THEN 1.00 END as fsess_hr,
sql amazon-redshift
1
What result are you expecting? There's noELSEclause, so you'll get1.0orNULLonly.
– pault
Dec 14 '17 at 2:09
I think you should remove thepythontag - this seems like a strictlySQLquestion.
– pault
Dec 14 '17 at 2:34
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am using Python SQLAlchemy to connect to a Redshift instance. I am trying to calculate a DATEDIFF in minutes between 2 datetimes, which works. But when I try to CAST the result to FLOAT and use it in a CASE WHEN against ' < 1' , I get only 'THEN' results?
SELECT distinct u2.id,
CASE
WHEN
CAST(DATEDIFF(minute, u1.usa_start_datetime::timestamp,u1.usa_end_datetime::timestamp) * 1.00 as FLOAT) < 1.00
THEN 1.00 END as fsess_hr,
sql amazon-redshift
I am using Python SQLAlchemy to connect to a Redshift instance. I am trying to calculate a DATEDIFF in minutes between 2 datetimes, which works. But when I try to CAST the result to FLOAT and use it in a CASE WHEN against ' < 1' , I get only 'THEN' results?
SELECT distinct u2.id,
CASE
WHEN
CAST(DATEDIFF(minute, u1.usa_start_datetime::timestamp,u1.usa_end_datetime::timestamp) * 1.00 as FLOAT) < 1.00
THEN 1.00 END as fsess_hr,
sql amazon-redshift
sql amazon-redshift
edited Dec 14 '17 at 10:28
Andy K
2,54953053
2,54953053
asked Dec 14 '17 at 0:50
PR102012
2071319
2071319
1
What result are you expecting? There's noELSEclause, so you'll get1.0orNULLonly.
– pault
Dec 14 '17 at 2:09
I think you should remove thepythontag - this seems like a strictlySQLquestion.
– pault
Dec 14 '17 at 2:34
add a comment |
1
What result are you expecting? There's noELSEclause, so you'll get1.0orNULLonly.
– pault
Dec 14 '17 at 2:09
I think you should remove thepythontag - this seems like a strictlySQLquestion.
– pault
Dec 14 '17 at 2:34
1
1
What result are you expecting? There's no
ELSE clause, so you'll get 1.0 or NULL only.– pault
Dec 14 '17 at 2:09
What result are you expecting? There's no
ELSE clause, so you'll get 1.0 or NULL only.– pault
Dec 14 '17 at 2:09
I think you should remove the
python tag - this seems like a strictly SQL question.– pault
Dec 14 '17 at 2:34
I think you should remove the
python tag - this seems like a strictly SQL question.– pault
Dec 14 '17 at 2:34
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
You are missing an ELSE clause in your CASE statement. If you want to get 1.0 if the difference is less than 1 and the difference as a floating point number otherwise, modify your query as follows:
SELECT distinct
u2.id,
CASE
WHEN CAST(DATEDIFF(minute, u1.usa_start_datetime::timestamp,u1.usa_end_datetime::timestamp) * 1.00 as FLOAT) < 1.00 THEN 1.00
ELSE CAST(DATEDIFF(minute, u1.usa_start_datetime::timestamp,u1.usa_end_datetime::timestamp) * 1.00 as FLOAT)
END as fsess_hr
FROM some_table
Also, I don't think you need the * 1.00
add a comment |
up vote
1
down vote
The return type of DATEDIFF() is BIGINT and you have chosen to use minutes as the unit. So deal with the integers within the case expression then cast the value to float
SELECT /* DISTINCT */ -- is this really needed?
u2.id
, CAST(
CASE WHEN DATEDIFF(MINUTE, u1.usa_start_datetime ::timestamp, u1.usa_end_datetime ::timestamp) < 1 THEN 1
ELSE DATEDIFF(MINUTE, u1.usa_start_datetime ::timestamp, u1.usa_end_datetime ::timestamp)
END
AS Float) AS fsess_hr
FROM ...
BUT you are still returning a number of minutes, so I don't follow why it is called fsess_hr, do you divide it by 60.0 at some point?
add a comment |
up vote
0
down vote
I find that ::float works with datediff just fine. Looks cleaner than cast() as float
SELECT distinct u2.id,
CASE
WHEN DATEDIFF(minute,u1.usa_start_datetime::timestamp,u1.usa_end_datetime::timestamp)::float < 1.00
THEN 1.00 END as fsess_hr,
add a comment |
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
});
}
});
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%2f47804346%2fpostgres-redshift-datediff-convert-to-float%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
up vote
1
down vote
You are missing an ELSE clause in your CASE statement. If you want to get 1.0 if the difference is less than 1 and the difference as a floating point number otherwise, modify your query as follows:
SELECT distinct
u2.id,
CASE
WHEN CAST(DATEDIFF(minute, u1.usa_start_datetime::timestamp,u1.usa_end_datetime::timestamp) * 1.00 as FLOAT) < 1.00 THEN 1.00
ELSE CAST(DATEDIFF(minute, u1.usa_start_datetime::timestamp,u1.usa_end_datetime::timestamp) * 1.00 as FLOAT)
END as fsess_hr
FROM some_table
Also, I don't think you need the * 1.00
add a comment |
up vote
1
down vote
You are missing an ELSE clause in your CASE statement. If you want to get 1.0 if the difference is less than 1 and the difference as a floating point number otherwise, modify your query as follows:
SELECT distinct
u2.id,
CASE
WHEN CAST(DATEDIFF(minute, u1.usa_start_datetime::timestamp,u1.usa_end_datetime::timestamp) * 1.00 as FLOAT) < 1.00 THEN 1.00
ELSE CAST(DATEDIFF(minute, u1.usa_start_datetime::timestamp,u1.usa_end_datetime::timestamp) * 1.00 as FLOAT)
END as fsess_hr
FROM some_table
Also, I don't think you need the * 1.00
add a comment |
up vote
1
down vote
up vote
1
down vote
You are missing an ELSE clause in your CASE statement. If you want to get 1.0 if the difference is less than 1 and the difference as a floating point number otherwise, modify your query as follows:
SELECT distinct
u2.id,
CASE
WHEN CAST(DATEDIFF(minute, u1.usa_start_datetime::timestamp,u1.usa_end_datetime::timestamp) * 1.00 as FLOAT) < 1.00 THEN 1.00
ELSE CAST(DATEDIFF(minute, u1.usa_start_datetime::timestamp,u1.usa_end_datetime::timestamp) * 1.00 as FLOAT)
END as fsess_hr
FROM some_table
Also, I don't think you need the * 1.00
You are missing an ELSE clause in your CASE statement. If you want to get 1.0 if the difference is less than 1 and the difference as a floating point number otherwise, modify your query as follows:
SELECT distinct
u2.id,
CASE
WHEN CAST(DATEDIFF(minute, u1.usa_start_datetime::timestamp,u1.usa_end_datetime::timestamp) * 1.00 as FLOAT) < 1.00 THEN 1.00
ELSE CAST(DATEDIFF(minute, u1.usa_start_datetime::timestamp,u1.usa_end_datetime::timestamp) * 1.00 as FLOAT)
END as fsess_hr
FROM some_table
Also, I don't think you need the * 1.00
answered Dec 14 '17 at 2:32
pault
13.7k31744
13.7k31744
add a comment |
add a comment |
up vote
1
down vote
The return type of DATEDIFF() is BIGINT and you have chosen to use minutes as the unit. So deal with the integers within the case expression then cast the value to float
SELECT /* DISTINCT */ -- is this really needed?
u2.id
, CAST(
CASE WHEN DATEDIFF(MINUTE, u1.usa_start_datetime ::timestamp, u1.usa_end_datetime ::timestamp) < 1 THEN 1
ELSE DATEDIFF(MINUTE, u1.usa_start_datetime ::timestamp, u1.usa_end_datetime ::timestamp)
END
AS Float) AS fsess_hr
FROM ...
BUT you are still returning a number of minutes, so I don't follow why it is called fsess_hr, do you divide it by 60.0 at some point?
add a comment |
up vote
1
down vote
The return type of DATEDIFF() is BIGINT and you have chosen to use minutes as the unit. So deal with the integers within the case expression then cast the value to float
SELECT /* DISTINCT */ -- is this really needed?
u2.id
, CAST(
CASE WHEN DATEDIFF(MINUTE, u1.usa_start_datetime ::timestamp, u1.usa_end_datetime ::timestamp) < 1 THEN 1
ELSE DATEDIFF(MINUTE, u1.usa_start_datetime ::timestamp, u1.usa_end_datetime ::timestamp)
END
AS Float) AS fsess_hr
FROM ...
BUT you are still returning a number of minutes, so I don't follow why it is called fsess_hr, do you divide it by 60.0 at some point?
add a comment |
up vote
1
down vote
up vote
1
down vote
The return type of DATEDIFF() is BIGINT and you have chosen to use minutes as the unit. So deal with the integers within the case expression then cast the value to float
SELECT /* DISTINCT */ -- is this really needed?
u2.id
, CAST(
CASE WHEN DATEDIFF(MINUTE, u1.usa_start_datetime ::timestamp, u1.usa_end_datetime ::timestamp) < 1 THEN 1
ELSE DATEDIFF(MINUTE, u1.usa_start_datetime ::timestamp, u1.usa_end_datetime ::timestamp)
END
AS Float) AS fsess_hr
FROM ...
BUT you are still returning a number of minutes, so I don't follow why it is called fsess_hr, do you divide it by 60.0 at some point?
The return type of DATEDIFF() is BIGINT and you have chosen to use minutes as the unit. So deal with the integers within the case expression then cast the value to float
SELECT /* DISTINCT */ -- is this really needed?
u2.id
, CAST(
CASE WHEN DATEDIFF(MINUTE, u1.usa_start_datetime ::timestamp, u1.usa_end_datetime ::timestamp) < 1 THEN 1
ELSE DATEDIFF(MINUTE, u1.usa_start_datetime ::timestamp, u1.usa_end_datetime ::timestamp)
END
AS Float) AS fsess_hr
FROM ...
BUT you are still returning a number of minutes, so I don't follow why it is called fsess_hr, do you divide it by 60.0 at some point?
edited Dec 14 '17 at 9:00
answered Dec 14 '17 at 8:54
Used_By_Already
22.2k21838
22.2k21838
add a comment |
add a comment |
up vote
0
down vote
I find that ::float works with datediff just fine. Looks cleaner than cast() as float
SELECT distinct u2.id,
CASE
WHEN DATEDIFF(minute,u1.usa_start_datetime::timestamp,u1.usa_end_datetime::timestamp)::float < 1.00
THEN 1.00 END as fsess_hr,
add a comment |
up vote
0
down vote
I find that ::float works with datediff just fine. Looks cleaner than cast() as float
SELECT distinct u2.id,
CASE
WHEN DATEDIFF(minute,u1.usa_start_datetime::timestamp,u1.usa_end_datetime::timestamp)::float < 1.00
THEN 1.00 END as fsess_hr,
add a comment |
up vote
0
down vote
up vote
0
down vote
I find that ::float works with datediff just fine. Looks cleaner than cast() as float
SELECT distinct u2.id,
CASE
WHEN DATEDIFF(minute,u1.usa_start_datetime::timestamp,u1.usa_end_datetime::timestamp)::float < 1.00
THEN 1.00 END as fsess_hr,
I find that ::float works with datediff just fine. Looks cleaner than cast() as float
SELECT distinct u2.id,
CASE
WHEN DATEDIFF(minute,u1.usa_start_datetime::timestamp,u1.usa_end_datetime::timestamp)::float < 1.00
THEN 1.00 END as fsess_hr,
answered Nov 12 at 6:40
Ben Squire
9519
9519
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2fstackoverflow.com%2fquestions%2f47804346%2fpostgres-redshift-datediff-convert-to-float%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
1
What result are you expecting? There's no
ELSEclause, so you'll get1.0orNULLonly.– pault
Dec 14 '17 at 2:09
I think you should remove the
pythontag - this seems like a strictlySQLquestion.– pault
Dec 14 '17 at 2:34