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
sql subquery case
New contributor
add a comment |
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
sql subquery case
New contributor
1
One thing is that you are missing the keywordAND
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
add a comment |
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
sql subquery case
New contributor
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
sql subquery case
New contributor
New contributor
edited yesterday
Tyler Roper
12k11640
12k11640
New contributor
asked yesterday
Shenk
1
1
New contributor
New contributor
1
One thing is that you are missing the keywordAND
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
add a comment |
1
One thing is that you are missing the keywordAND
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
add a comment |
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
@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
add a comment |
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]
add a comment |
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
@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
add a comment |
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
@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
add a comment |
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
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
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
add a comment |
@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
add a comment |
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]
add a comment |
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]
add a comment |
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]
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]
answered yesterday
ArcherBird
640216
640216
add a comment |
add a comment |
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.
Shenk is a new contributor. Be nice, and check out our Code of Conduct.
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%2f53343713%2fsub-queries-in-case-statement%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
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