Multiple counts and merge columns












0














I current have a query that grabs the number of parts made per hour between two dates:



DECLARE @StartDate datetime 
DECLARE @EndDate datetime

SET @StartDate = '10/10/2018'
SET @EndDate = '11/11/2018'

SELECT
CONVERT(VARCHAR(10), CAST(presstimes AS DATE), 111) AS ForDate,
DATEPART(HOUR, presstimes) AS OnHour,
COUNT(*) AS Totals
FROM
partmasterlist
WHERE
((presstimes >= @StartDate AND presstimes < dateAdd(d, 1, @EndDate))
AND (((presstimes IS NOT NULL))))
GROUP BY
CONVERT(VARCHAR(10), CAST(presstimes AS DATE), 111),
DATEPART(HOUR, presstimes)
ORDER BY
CONVERT(VARCHAR(10), CAST(presstimes AS DATE), 111) ASC;


Output:



Date        Hour QTY
---------------------
2018/11/06 11 16
2018/11/06 12 20
2018/11/06 13 29
2018/11/06 14 26


Now I need to add another qty column to count where "trimmingtimes" is set.



I can't figure out how to full join the date and hour columns (e.g. presstimes might have 20qty for Hour 2, but trimmingtimes is NULL for Hour 2);



Input:



ID    presstimes                         trimmingtimes
-----------------------------------------------------------------
1 2018-10-10 01:15:23.000 2018-10-10 01:15:23.000
2 2018-10-10 01:15:23.000 NULL
3 2018-10-10 02:15:23.000 NULL
4 NULL 2018-10-10 03:15:23.000


Output:



Date      hour    Press QTY   T QTY
------------------------------------
10/10/18 1 2 1
10/10/18 2 1 0
10/10/18 3 0 1









share|improve this question




















  • 1




    No idea what you are trying to do here. You may know what trimingtimes means but nobody else does. What you need is to post details of your problem. Here is a great place to start.
    – Sean Lange
    Nov 20 at 16:41
















0














I current have a query that grabs the number of parts made per hour between two dates:



DECLARE @StartDate datetime 
DECLARE @EndDate datetime

SET @StartDate = '10/10/2018'
SET @EndDate = '11/11/2018'

SELECT
CONVERT(VARCHAR(10), CAST(presstimes AS DATE), 111) AS ForDate,
DATEPART(HOUR, presstimes) AS OnHour,
COUNT(*) AS Totals
FROM
partmasterlist
WHERE
((presstimes >= @StartDate AND presstimes < dateAdd(d, 1, @EndDate))
AND (((presstimes IS NOT NULL))))
GROUP BY
CONVERT(VARCHAR(10), CAST(presstimes AS DATE), 111),
DATEPART(HOUR, presstimes)
ORDER BY
CONVERT(VARCHAR(10), CAST(presstimes AS DATE), 111) ASC;


Output:



Date        Hour QTY
---------------------
2018/11/06 11 16
2018/11/06 12 20
2018/11/06 13 29
2018/11/06 14 26


Now I need to add another qty column to count where "trimmingtimes" is set.



I can't figure out how to full join the date and hour columns (e.g. presstimes might have 20qty for Hour 2, but trimmingtimes is NULL for Hour 2);



Input:



ID    presstimes                         trimmingtimes
-----------------------------------------------------------------
1 2018-10-10 01:15:23.000 2018-10-10 01:15:23.000
2 2018-10-10 01:15:23.000 NULL
3 2018-10-10 02:15:23.000 NULL
4 NULL 2018-10-10 03:15:23.000


Output:



Date      hour    Press QTY   T QTY
------------------------------------
10/10/18 1 2 1
10/10/18 2 1 0
10/10/18 3 0 1









share|improve this question




















  • 1




    No idea what you are trying to do here. You may know what trimingtimes means but nobody else does. What you need is to post details of your problem. Here is a great place to start.
    – Sean Lange
    Nov 20 at 16:41














0












0








0







I current have a query that grabs the number of parts made per hour between two dates:



DECLARE @StartDate datetime 
DECLARE @EndDate datetime

SET @StartDate = '10/10/2018'
SET @EndDate = '11/11/2018'

SELECT
CONVERT(VARCHAR(10), CAST(presstimes AS DATE), 111) AS ForDate,
DATEPART(HOUR, presstimes) AS OnHour,
COUNT(*) AS Totals
FROM
partmasterlist
WHERE
((presstimes >= @StartDate AND presstimes < dateAdd(d, 1, @EndDate))
AND (((presstimes IS NOT NULL))))
GROUP BY
CONVERT(VARCHAR(10), CAST(presstimes AS DATE), 111),
DATEPART(HOUR, presstimes)
ORDER BY
CONVERT(VARCHAR(10), CAST(presstimes AS DATE), 111) ASC;


Output:



Date        Hour QTY
---------------------
2018/11/06 11 16
2018/11/06 12 20
2018/11/06 13 29
2018/11/06 14 26


Now I need to add another qty column to count where "trimmingtimes" is set.



I can't figure out how to full join the date and hour columns (e.g. presstimes might have 20qty for Hour 2, but trimmingtimes is NULL for Hour 2);



Input:



ID    presstimes                         trimmingtimes
-----------------------------------------------------------------
1 2018-10-10 01:15:23.000 2018-10-10 01:15:23.000
2 2018-10-10 01:15:23.000 NULL
3 2018-10-10 02:15:23.000 NULL
4 NULL 2018-10-10 03:15:23.000


Output:



Date      hour    Press QTY   T QTY
------------------------------------
10/10/18 1 2 1
10/10/18 2 1 0
10/10/18 3 0 1









share|improve this question















I current have a query that grabs the number of parts made per hour between two dates:



DECLARE @StartDate datetime 
DECLARE @EndDate datetime

SET @StartDate = '10/10/2018'
SET @EndDate = '11/11/2018'

SELECT
CONVERT(VARCHAR(10), CAST(presstimes AS DATE), 111) AS ForDate,
DATEPART(HOUR, presstimes) AS OnHour,
COUNT(*) AS Totals
FROM
partmasterlist
WHERE
((presstimes >= @StartDate AND presstimes < dateAdd(d, 1, @EndDate))
AND (((presstimes IS NOT NULL))))
GROUP BY
CONVERT(VARCHAR(10), CAST(presstimes AS DATE), 111),
DATEPART(HOUR, presstimes)
ORDER BY
CONVERT(VARCHAR(10), CAST(presstimes AS DATE), 111) ASC;


Output:



Date        Hour QTY
---------------------
2018/11/06 11 16
2018/11/06 12 20
2018/11/06 13 29
2018/11/06 14 26


Now I need to add another qty column to count where "trimmingtimes" is set.



I can't figure out how to full join the date and hour columns (e.g. presstimes might have 20qty for Hour 2, but trimmingtimes is NULL for Hour 2);



Input:



ID    presstimes                         trimmingtimes
-----------------------------------------------------------------
1 2018-10-10 01:15:23.000 2018-10-10 01:15:23.000
2 2018-10-10 01:15:23.000 NULL
3 2018-10-10 02:15:23.000 NULL
4 NULL 2018-10-10 03:15:23.000


Output:



Date      hour    Press QTY   T QTY
------------------------------------
10/10/18 1 2 1
10/10/18 2 1 0
10/10/18 3 0 1






sql sql-server tsql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 17:21









marc_s

570k12811021250




570k12811021250










asked Nov 20 at 16:35









Matthew Hait

405




405








  • 1




    No idea what you are trying to do here. You may know what trimingtimes means but nobody else does. What you need is to post details of your problem. Here is a great place to start.
    – Sean Lange
    Nov 20 at 16:41














  • 1




    No idea what you are trying to do here. You may know what trimingtimes means but nobody else does. What you need is to post details of your problem. Here is a great place to start.
    – Sean Lange
    Nov 20 at 16:41








1




1




No idea what you are trying to do here. You may know what trimingtimes means but nobody else does. What you need is to post details of your problem. Here is a great place to start.
– Sean Lange
Nov 20 at 16:41




No idea what you are trying to do here. You may know what trimingtimes means but nobody else does. What you need is to post details of your problem. Here is a great place to start.
– Sean Lange
Nov 20 at 16:41












1 Answer
1






active

oldest

votes


















1














I suspect you want something like this:



select convert(date, v.dt) as date,
datepart(hour, v.dt) as hour,
sum(ispress) as num_press,
sum(istrim) as num_trim
from partmasterlist pml cross apply
(values (pml.presstime, 1, 0), (pml.trimmingtime, 0, 1)
) v(dt, ispress, istrim)
group by convert(date, v.dt), datepart(hour, v.dt)
order by convert(date, v.dt), datepart(hour, v.dt);


You can add a where clause for a particular range.






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',
    autoActivateHeartbeat: false,
    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%2f53397517%2fmultiple-counts-and-merge-columns%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I suspect you want something like this:



    select convert(date, v.dt) as date,
    datepart(hour, v.dt) as hour,
    sum(ispress) as num_press,
    sum(istrim) as num_trim
    from partmasterlist pml cross apply
    (values (pml.presstime, 1, 0), (pml.trimmingtime, 0, 1)
    ) v(dt, ispress, istrim)
    group by convert(date, v.dt), datepart(hour, v.dt)
    order by convert(date, v.dt), datepart(hour, v.dt);


    You can add a where clause for a particular range.






    share|improve this answer


























      1














      I suspect you want something like this:



      select convert(date, v.dt) as date,
      datepart(hour, v.dt) as hour,
      sum(ispress) as num_press,
      sum(istrim) as num_trim
      from partmasterlist pml cross apply
      (values (pml.presstime, 1, 0), (pml.trimmingtime, 0, 1)
      ) v(dt, ispress, istrim)
      group by convert(date, v.dt), datepart(hour, v.dt)
      order by convert(date, v.dt), datepart(hour, v.dt);


      You can add a where clause for a particular range.






      share|improve this answer
























        1












        1








        1






        I suspect you want something like this:



        select convert(date, v.dt) as date,
        datepart(hour, v.dt) as hour,
        sum(ispress) as num_press,
        sum(istrim) as num_trim
        from partmasterlist pml cross apply
        (values (pml.presstime, 1, 0), (pml.trimmingtime, 0, 1)
        ) v(dt, ispress, istrim)
        group by convert(date, v.dt), datepart(hour, v.dt)
        order by convert(date, v.dt), datepart(hour, v.dt);


        You can add a where clause for a particular range.






        share|improve this answer












        I suspect you want something like this:



        select convert(date, v.dt) as date,
        datepart(hour, v.dt) as hour,
        sum(ispress) as num_press,
        sum(istrim) as num_trim
        from partmasterlist pml cross apply
        (values (pml.presstime, 1, 0), (pml.trimmingtime, 0, 1)
        ) v(dt, ispress, istrim)
        group by convert(date, v.dt), datepart(hour, v.dt)
        order by convert(date, v.dt), datepart(hour, v.dt);


        You can add a where clause for a particular range.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 at 18:28









        Gordon Linoff

        757k35291399




        757k35291399






























            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%2f53397517%2fmultiple-counts-and-merge-columns%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

            Costa Masnaga

            Fotorealismo

            Sidney Franklin