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,









share|improve this question




















  • 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










  • I think you should remove the python tag - this seems like a strictly SQL question.
    – pault
    Dec 14 '17 at 2:34

















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,









share|improve this question




















  • 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










  • I think you should remove the python tag - this seems like a strictly SQL question.
    – pault
    Dec 14 '17 at 2:34















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,









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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
















  • 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










  • I think you should remove the python tag - this seems like a strictly SQL question.
    – 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














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






share|improve this answer




























    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?






    share|improve this answer






























      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,





      share|improve this answer





















        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%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






        share|improve this answer

























          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






          share|improve this answer























            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






            share|improve this answer












            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







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 14 '17 at 2:32









            pault

            13.7k31744




            13.7k31744
























                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?






                share|improve this answer



























                  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?






                  share|improve this answer

























                    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?






                    share|improve this answer














                    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?







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Dec 14 '17 at 9:00

























                    answered Dec 14 '17 at 8:54









                    Used_By_Already

                    22.2k21838




                    22.2k21838






















                        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,





                        share|improve this answer

























                          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,





                          share|improve this answer























                            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,





                            share|improve this answer












                            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,






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 12 at 6:40









                            Ben Squire

                            9519




                            9519






























                                draft saved

                                draft discarded




















































                                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.




                                draft saved


                                draft discarded














                                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





















































                                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

                                List item for chat from Array inside array React Native

                                Thiostrepton

                                Caerphilly