Sub queries in case statement











up vote
0
down vote

favorite












I am trying put a case statement, this case statement is based on condition from 2 different sub queries. My problem is, I am not able get it work.



This SQL sub queries is giving a count which I am using in case statement.



Error I am getting is:




Incorrrect syntax near x and y.




(which are my subqueries)



Select 'Days=' as Days,
case
when x.yest >=0 then '1 Day'
when y.DayBefore <=3 then '1 Day'
else '2 Day'
end as Days
from
(
Select count(*) as yest
from
[Main], [DailyStatus]
where Approved_Date is null
Submitted_Date = cast(LAST_WEEKDAY_DATE as date) x,

(
Select count(*) as DayBefore
from [Main], [DailyStatus]
where Approved_Date is null
and Submitted_Date = dateadd(dd, -1, cast(LAST_WEEKDAY_DATE as date))
)y


Below are my tables:



Main
Approved_Date
Submitted_Date



DailyStatus
Last_Weekday_date



I highly appreciate any help on this.
Thanks, Shikha










share|improve this question









New contributor




Shenk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    One thing is that you are missing the keyword AND in your WHERE Clause in the first subquery... as well as a rouge comma in the same area
    – ArcherBird
    yesterday












  • and before x a closing parenthesis is missing (the one before that closes the cast).
    – Dávid Laczkó
    yesterday










  • Sort your brackets out: x, looks like it should have a ) bracket before it
    – Caius Jard
    yesterday

















up vote
0
down vote

favorite












I am trying put a case statement, this case statement is based on condition from 2 different sub queries. My problem is, I am not able get it work.



This SQL sub queries is giving a count which I am using in case statement.



Error I am getting is:




Incorrrect syntax near x and y.




(which are my subqueries)



Select 'Days=' as Days,
case
when x.yest >=0 then '1 Day'
when y.DayBefore <=3 then '1 Day'
else '2 Day'
end as Days
from
(
Select count(*) as yest
from
[Main], [DailyStatus]
where Approved_Date is null
Submitted_Date = cast(LAST_WEEKDAY_DATE as date) x,

(
Select count(*) as DayBefore
from [Main], [DailyStatus]
where Approved_Date is null
and Submitted_Date = dateadd(dd, -1, cast(LAST_WEEKDAY_DATE as date))
)y


Below are my tables:



Main
Approved_Date
Submitted_Date



DailyStatus
Last_Weekday_date



I highly appreciate any help on this.
Thanks, Shikha










share|improve this question









New contributor




Shenk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    One thing is that you are missing the keyword AND in your WHERE Clause in the first subquery... as well as a rouge comma in the same area
    – ArcherBird
    yesterday












  • and before x a closing parenthesis is missing (the one before that closes the cast).
    – Dávid Laczkó
    yesterday










  • Sort your brackets out: x, looks like it should have a ) bracket before it
    – Caius Jard
    yesterday















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying put a case statement, this case statement is based on condition from 2 different sub queries. My problem is, I am not able get it work.



This SQL sub queries is giving a count which I am using in case statement.



Error I am getting is:




Incorrrect syntax near x and y.




(which are my subqueries)



Select 'Days=' as Days,
case
when x.yest >=0 then '1 Day'
when y.DayBefore <=3 then '1 Day'
else '2 Day'
end as Days
from
(
Select count(*) as yest
from
[Main], [DailyStatus]
where Approved_Date is null
Submitted_Date = cast(LAST_WEEKDAY_DATE as date) x,

(
Select count(*) as DayBefore
from [Main], [DailyStatus]
where Approved_Date is null
and Submitted_Date = dateadd(dd, -1, cast(LAST_WEEKDAY_DATE as date))
)y


Below are my tables:



Main
Approved_Date
Submitted_Date



DailyStatus
Last_Weekday_date



I highly appreciate any help on this.
Thanks, Shikha










share|improve this question









New contributor




Shenk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I am trying put a case statement, this case statement is based on condition from 2 different sub queries. My problem is, I am not able get it work.



This SQL sub queries is giving a count which I am using in case statement.



Error I am getting is:




Incorrrect syntax near x and y.




(which are my subqueries)



Select 'Days=' as Days,
case
when x.yest >=0 then '1 Day'
when y.DayBefore <=3 then '1 Day'
else '2 Day'
end as Days
from
(
Select count(*) as yest
from
[Main], [DailyStatus]
where Approved_Date is null
Submitted_Date = cast(LAST_WEEKDAY_DATE as date) x,

(
Select count(*) as DayBefore
from [Main], [DailyStatus]
where Approved_Date is null
and Submitted_Date = dateadd(dd, -1, cast(LAST_WEEKDAY_DATE as date))
)y


Below are my tables:



Main
Approved_Date
Submitted_Date



DailyStatus
Last_Weekday_date



I highly appreciate any help on this.
Thanks, Shikha







sql subquery case






share|improve this question









New contributor




Shenk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Shenk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday









Tyler Roper

12k11640




12k11640






New contributor




Shenk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









Shenk

1




1




New contributor




Shenk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Shenk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Shenk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1




    One thing is that you are missing the keyword AND in your WHERE Clause in the first subquery... as well as a rouge comma in the same area
    – ArcherBird
    yesterday












  • and before x a closing parenthesis is missing (the one before that closes the cast).
    – Dávid Laczkó
    yesterday










  • Sort your brackets out: x, looks like it should have a ) bracket before it
    – Caius Jard
    yesterday
















  • 1




    One thing is that you are missing the keyword AND in your WHERE Clause in the first subquery... as well as a rouge comma in the same area
    – ArcherBird
    yesterday












  • and before x a closing parenthesis is missing (the one before that closes the cast).
    – Dávid Laczkó
    yesterday










  • Sort your brackets out: x, looks like it should have a ) bracket before it
    – Caius Jard
    yesterday










1




1




One thing is that you are missing the keyword AND in your WHERE Clause in the first subquery... as well as a rouge comma in the same area
– ArcherBird
yesterday






One thing is that you are missing the keyword AND in your WHERE Clause in the first subquery... as well as a rouge comma in the same area
– ArcherBird
yesterday














and before x a closing parenthesis is missing (the one before that closes the cast).
– Dávid Laczkó
yesterday




and before x a closing parenthesis is missing (the one before that closes the cast).
– Dávid Laczkó
yesterday












Sort your brackets out: x, looks like it should have a ) bracket before it
– Caius Jard
yesterday






Sort your brackets out: x, looks like it should have a ) bracket before it
– Caius Jard
yesterday














2 Answers
2






active

oldest

votes

















up vote
1
down vote













Indent your sql properly and it will make these things easier to spot



Select 
'Days=' as Days,
case
when x.yest >=0 then '1 Day'
when y.DayBefore <=3 then '1 Day'
else '2 Day'
end as Days
from
(
Select
count(*) as yest
from
[Main]
CROSS JOIN
[DailyStatus]
where
Approved_Date is nulL AND
Submitted_Date = cast(LAST_WEEKDAY_DATE as date)
) x
CROSS JOIN
(
Select
count(*) as DayBefore
from
[Main]
CROSS JOIN
[DailyStatus]
where
Approved_Date is null and
Submitted_Date = dateadd(dd, -1, cast(LAST_WEEKDAY_DATE as date))
)y


Also, doing your joins like that (from table1, table2) fell out of favour about 20 years ago. Please use modern join syntax






share|improve this answer





















  • @ArcherBird- your solution worked. Thanks a ton :)
    – Shenk
    yesterday










  • Bird- I indented the code but when I post it, it got un indented itself. Thanks a ton for Cross Join tip.
    – Shenk
    23 hours ago










  • Cross join isn't often used, but your query didn't seem to have any related data entities so it's the correct option for the context
    – Caius Jard
    23 hours ago




















up vote
0
down vote













Syntax errors edited, as well as some formatting to make it a bit easier to read. PS - not a fan of your implicit joins here. You should be explicit by using <join type> JOIN .... ON...



SELECT
'Days=' AS [Days]
, CASE
WHEN [x].[yest] >= 0 THEN '1 Day'
WHEN [y].[DayBefore] <= 3 THEN '1 Day'
ELSE '2 Day'
END AS [Days]
FROM
(
SELECT
COUNT(*) AS [yest]
FROM
[Main]
, [DailyStatus]
WHERE [Approved_Date] IS NULL
AND [Submitted_Date] = CAST([LAST_WEEKDAY_DATE] AS DATE)
) AS [x]
,
(
SELECT
COUNT(*) AS [DayBefore]
FROM
[Main]
, [DailyStatus]
WHERE [Approved_Date] IS NULL
AND [Submitted_Date] = DATEADD([dd], -1, CAST([LAST_WEEKDAY_DATE] AS DATE))
) AS [y]





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
    });


    }
    });






    Shenk is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343713%2fsub-queries-in-case-statement%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    Indent your sql properly and it will make these things easier to spot



    Select 
    'Days=' as Days,
    case
    when x.yest >=0 then '1 Day'
    when y.DayBefore <=3 then '1 Day'
    else '2 Day'
    end as Days
    from
    (
    Select
    count(*) as yest
    from
    [Main]
    CROSS JOIN
    [DailyStatus]
    where
    Approved_Date is nulL AND
    Submitted_Date = cast(LAST_WEEKDAY_DATE as date)
    ) x
    CROSS JOIN
    (
    Select
    count(*) as DayBefore
    from
    [Main]
    CROSS JOIN
    [DailyStatus]
    where
    Approved_Date is null and
    Submitted_Date = dateadd(dd, -1, cast(LAST_WEEKDAY_DATE as date))
    )y


    Also, doing your joins like that (from table1, table2) fell out of favour about 20 years ago. Please use modern join syntax






    share|improve this answer





















    • @ArcherBird- your solution worked. Thanks a ton :)
      – Shenk
      yesterday










    • Bird- I indented the code but when I post it, it got un indented itself. Thanks a ton for Cross Join tip.
      – Shenk
      23 hours ago










    • Cross join isn't often used, but your query didn't seem to have any related data entities so it's the correct option for the context
      – Caius Jard
      23 hours ago

















    up vote
    1
    down vote













    Indent your sql properly and it will make these things easier to spot



    Select 
    'Days=' as Days,
    case
    when x.yest >=0 then '1 Day'
    when y.DayBefore <=3 then '1 Day'
    else '2 Day'
    end as Days
    from
    (
    Select
    count(*) as yest
    from
    [Main]
    CROSS JOIN
    [DailyStatus]
    where
    Approved_Date is nulL AND
    Submitted_Date = cast(LAST_WEEKDAY_DATE as date)
    ) x
    CROSS JOIN
    (
    Select
    count(*) as DayBefore
    from
    [Main]
    CROSS JOIN
    [DailyStatus]
    where
    Approved_Date is null and
    Submitted_Date = dateadd(dd, -1, cast(LAST_WEEKDAY_DATE as date))
    )y


    Also, doing your joins like that (from table1, table2) fell out of favour about 20 years ago. Please use modern join syntax






    share|improve this answer





















    • @ArcherBird- your solution worked. Thanks a ton :)
      – Shenk
      yesterday










    • Bird- I indented the code but when I post it, it got un indented itself. Thanks a ton for Cross Join tip.
      – Shenk
      23 hours ago










    • Cross join isn't often used, but your query didn't seem to have any related data entities so it's the correct option for the context
      – Caius Jard
      23 hours ago















    up vote
    1
    down vote










    up vote
    1
    down vote









    Indent your sql properly and it will make these things easier to spot



    Select 
    'Days=' as Days,
    case
    when x.yest >=0 then '1 Day'
    when y.DayBefore <=3 then '1 Day'
    else '2 Day'
    end as Days
    from
    (
    Select
    count(*) as yest
    from
    [Main]
    CROSS JOIN
    [DailyStatus]
    where
    Approved_Date is nulL AND
    Submitted_Date = cast(LAST_WEEKDAY_DATE as date)
    ) x
    CROSS JOIN
    (
    Select
    count(*) as DayBefore
    from
    [Main]
    CROSS JOIN
    [DailyStatus]
    where
    Approved_Date is null and
    Submitted_Date = dateadd(dd, -1, cast(LAST_WEEKDAY_DATE as date))
    )y


    Also, doing your joins like that (from table1, table2) fell out of favour about 20 years ago. Please use modern join syntax






    share|improve this answer












    Indent your sql properly and it will make these things easier to spot



    Select 
    'Days=' as Days,
    case
    when x.yest >=0 then '1 Day'
    when y.DayBefore <=3 then '1 Day'
    else '2 Day'
    end as Days
    from
    (
    Select
    count(*) as yest
    from
    [Main]
    CROSS JOIN
    [DailyStatus]
    where
    Approved_Date is nulL AND
    Submitted_Date = cast(LAST_WEEKDAY_DATE as date)
    ) x
    CROSS JOIN
    (
    Select
    count(*) as DayBefore
    from
    [Main]
    CROSS JOIN
    [DailyStatus]
    where
    Approved_Date is null and
    Submitted_Date = dateadd(dd, -1, cast(LAST_WEEKDAY_DATE as date))
    )y


    Also, doing your joins like that (from table1, table2) fell out of favour about 20 years ago. Please use modern join syntax







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered yesterday









    Caius Jard

    7,54911135




    7,54911135












    • @ArcherBird- your solution worked. Thanks a ton :)
      – Shenk
      yesterday










    • Bird- I indented the code but when I post it, it got un indented itself. Thanks a ton for Cross Join tip.
      – Shenk
      23 hours ago










    • Cross join isn't often used, but your query didn't seem to have any related data entities so it's the correct option for the context
      – Caius Jard
      23 hours ago




















    • @ArcherBird- your solution worked. Thanks a ton :)
      – Shenk
      yesterday










    • Bird- I indented the code but when I post it, it got un indented itself. Thanks a ton for Cross Join tip.
      – Shenk
      23 hours ago










    • Cross join isn't often used, but your query didn't seem to have any related data entities so it's the correct option for the context
      – Caius Jard
      23 hours ago


















    @ArcherBird- your solution worked. Thanks a ton :)
    – Shenk
    yesterday




    @ArcherBird- your solution worked. Thanks a ton :)
    – Shenk
    yesterday












    Bird- I indented the code but when I post it, it got un indented itself. Thanks a ton for Cross Join tip.
    – Shenk
    23 hours ago




    Bird- I indented the code but when I post it, it got un indented itself. Thanks a ton for Cross Join tip.
    – Shenk
    23 hours ago












    Cross join isn't often used, but your query didn't seem to have any related data entities so it's the correct option for the context
    – Caius Jard
    23 hours ago






    Cross join isn't often used, but your query didn't seem to have any related data entities so it's the correct option for the context
    – Caius Jard
    23 hours ago














    up vote
    0
    down vote













    Syntax errors edited, as well as some formatting to make it a bit easier to read. PS - not a fan of your implicit joins here. You should be explicit by using <join type> JOIN .... ON...



    SELECT
    'Days=' AS [Days]
    , CASE
    WHEN [x].[yest] >= 0 THEN '1 Day'
    WHEN [y].[DayBefore] <= 3 THEN '1 Day'
    ELSE '2 Day'
    END AS [Days]
    FROM
    (
    SELECT
    COUNT(*) AS [yest]
    FROM
    [Main]
    , [DailyStatus]
    WHERE [Approved_Date] IS NULL
    AND [Submitted_Date] = CAST([LAST_WEEKDAY_DATE] AS DATE)
    ) AS [x]
    ,
    (
    SELECT
    COUNT(*) AS [DayBefore]
    FROM
    [Main]
    , [DailyStatus]
    WHERE [Approved_Date] IS NULL
    AND [Submitted_Date] = DATEADD([dd], -1, CAST([LAST_WEEKDAY_DATE] AS DATE))
    ) AS [y]





    share|improve this answer

























      up vote
      0
      down vote













      Syntax errors edited, as well as some formatting to make it a bit easier to read. PS - not a fan of your implicit joins here. You should be explicit by using <join type> JOIN .... ON...



      SELECT
      'Days=' AS [Days]
      , CASE
      WHEN [x].[yest] >= 0 THEN '1 Day'
      WHEN [y].[DayBefore] <= 3 THEN '1 Day'
      ELSE '2 Day'
      END AS [Days]
      FROM
      (
      SELECT
      COUNT(*) AS [yest]
      FROM
      [Main]
      , [DailyStatus]
      WHERE [Approved_Date] IS NULL
      AND [Submitted_Date] = CAST([LAST_WEEKDAY_DATE] AS DATE)
      ) AS [x]
      ,
      (
      SELECT
      COUNT(*) AS [DayBefore]
      FROM
      [Main]
      , [DailyStatus]
      WHERE [Approved_Date] IS NULL
      AND [Submitted_Date] = DATEADD([dd], -1, CAST([LAST_WEEKDAY_DATE] AS DATE))
      ) AS [y]





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Syntax errors edited, as well as some formatting to make it a bit easier to read. PS - not a fan of your implicit joins here. You should be explicit by using <join type> JOIN .... ON...



        SELECT
        'Days=' AS [Days]
        , CASE
        WHEN [x].[yest] >= 0 THEN '1 Day'
        WHEN [y].[DayBefore] <= 3 THEN '1 Day'
        ELSE '2 Day'
        END AS [Days]
        FROM
        (
        SELECT
        COUNT(*) AS [yest]
        FROM
        [Main]
        , [DailyStatus]
        WHERE [Approved_Date] IS NULL
        AND [Submitted_Date] = CAST([LAST_WEEKDAY_DATE] AS DATE)
        ) AS [x]
        ,
        (
        SELECT
        COUNT(*) AS [DayBefore]
        FROM
        [Main]
        , [DailyStatus]
        WHERE [Approved_Date] IS NULL
        AND [Submitted_Date] = DATEADD([dd], -1, CAST([LAST_WEEKDAY_DATE] AS DATE))
        ) AS [y]





        share|improve this answer












        Syntax errors edited, as well as some formatting to make it a bit easier to read. PS - not a fan of your implicit joins here. You should be explicit by using <join type> JOIN .... ON...



        SELECT
        'Days=' AS [Days]
        , CASE
        WHEN [x].[yest] >= 0 THEN '1 Day'
        WHEN [y].[DayBefore] <= 3 THEN '1 Day'
        ELSE '2 Day'
        END AS [Days]
        FROM
        (
        SELECT
        COUNT(*) AS [yest]
        FROM
        [Main]
        , [DailyStatus]
        WHERE [Approved_Date] IS NULL
        AND [Submitted_Date] = CAST([LAST_WEEKDAY_DATE] AS DATE)
        ) AS [x]
        ,
        (
        SELECT
        COUNT(*) AS [DayBefore]
        FROM
        [Main]
        , [DailyStatus]
        WHERE [Approved_Date] IS NULL
        AND [Submitted_Date] = DATEADD([dd], -1, CAST([LAST_WEEKDAY_DATE] AS DATE))
        ) AS [y]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered yesterday









        ArcherBird

        640216




        640216






















            Shenk is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            Shenk is a new contributor. Be nice, and check out our Code of Conduct.













            Shenk is a new contributor. Be nice, and check out our Code of Conduct.












            Shenk is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343713%2fsub-queries-in-case-statement%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

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            RAC Tourist Trophy