SELECT Country, EmployeeName, Name As [FullName], OutPut1, OutPut2, OutPut3,
(OutPut1 + OutPut2 + OutPut3) AS Total,
CASE WHEN Name IS NULL THEN '.........'
WHEN Name IS NOT NULL THEN '??????'
END)
AS ParentID
FROM CTE_Report;
The CASE WHEN in the above statement is giving trouble with the error below. Any one know what i'm missing?
Common table expression defined but not used..
Common table expression defined but not used..
this is mean that you create CTE but do not use it,
same error as in snapshot below
SELECT Country, EmployeeName, Name As [FullName], OutPut1, OutPut2, OutPut3,
(OutPut1 + OutPut2 + OutPut3) AS Total,
CASE WHEN Name IS NULL THEN '.........'
WHEN Name IS NOT NULL THEN '??????'
END
AS ParentID
FROM CTE_Report;
Its a parenthesis problem.
Try this
SELECT Country,
EmployeeName,
Name AS [FullName],
OutPut1,
OutPut2,
OutPut3,
( OutPut1 + OutPut2 + OutPut3 ) AS Total,
CASE
WHEN Name IS NULL THEN '.........'
ELSE '??????'
END AS ParentID
FROM CTE_Report;
Related
let's say I have this query
SELECT
s.name,
s.last_name,
s.created_date,
s.new_flag,
s.enrolled_date
CASE WHEN s.created_date IS NOT NULL AND s.new_flag IS = 0 THEN s.enrolled_date ELSE s.exit_date
END filter_query
FROM student.s WHERE TO_CHAR(filter_query,'YYYY-MM-DD') = TO_CHAR(NOW(), 'YYYY-MM-DD');
this query above says the filter_query column does not exist. how can we make this filter_query work in TO_CHAR function ?
You cannot use an alias for an expression like filter_query in the WHERE condition, since the WHERE condition is evaluated before the SELECT list.
You could use a subquery:
SELECT name,
last_name,
created_date,
new_flag,
enrolled_date,
filter_query
FROM (SELECT s.name,
s.last_name,
s.created_date,
s.new_flag,
s.enrolled_date,
CASE WHEN s.created_date IS NOT NULL
AND s.new_flag IS = 0
THEN s.enrolled_date
ELSE s.exit_date
END filter_query
FROM student.s) AS subq
WHERE TO_CHAR(filter_query, 'YYYY-MM-DD') = TO_CHAR(NOW(), 'YYYY-MM-DD');
I have a query like below getting the error - 'SELECT DISTINCT, ORDER BY expressions must appear in select list'
select distinct name
from fruits
order by case
when name = 'mango' then 1
else 2
end
This results 4 records, say
apple, mango, pear and grape
How can I make sure I get Mango as the first record always and the rest follow. I tried using the case statement, but not able to get the desired results. Any ideas will be appreciated.
I believe this should accomplish what you describe as needing.
select distinct
name,
case name when 'Mango' then 1 else 2 end as fruitOrder
from fruits
order by
fruitOrder
If you need to always have 'mango' in first position, no matter the other rows, this could be a way:
with fruits(name) as (
select 'apple' from dual union all
select 'mango' from dual union all
select 'pear' from dual union all
select 'grape' from dual
)
select name
from fruits
order by case
when name = 'mango' then 1
else 2
end
If you need to add a DISTINCT, this should work:
select distinct name,
case
when name = 'mango' then 1
else 2
end orderCol
from fruits
order by orderCol
This will give you 'Mango' followed by the others in order;
WITH get_rows AS
(SELECT DISTINCT item_type
FROM the_item)
SELECT item_type
FROM
(SELECT 1 as seq, item_type
FROM get_rows
WHERE item_type = 'Mango'
UNION ALL
SELECT 2 as seq, item_type
FROM get_rows
WHERE item_type <> 'Mango')
ORDER BY seq, item_type
I'm trying a query like this;
WITH CTE AS
(
SELECT
ID, NAME, DEPT,
CASE WHEN Fixed = 1 THEN 'Yes' ELSE '' END,
CASE WHEN NotFixed = 1 THEN 'Yes' ELSE '' END,
)
When I try to execute this query, I'm getting error like
No Column was specified for column 4 of CTE
What's wrong with the query?
Read the error message: You forgot to give your calculated columns a name. This goes for both column 4 and column 5. So change your query to something like this:
WITH CTE AS
(
SELECT
ID, NAME, DEPT,
CASE WHEN Fixed = 1 THEN 'Yes' ELSE '' END AS [Expr1],
CASE WHEN NotFixed = 1 THEN 'Yes' ELSE '' END AS [Expr2],
. . .
)
Feel free to change [Expr1] and [Expr2] to something more meaningful...
Is there a way do something like this (a conditional select from a nested subquery):
SELECT
Id,
Name WHERE Name LIKE '%Peter%' AS Peters,
Name WHERE Name LIKE '%Mike%' AS Mikes
FROM
(SELECT Id, Name FROM Customer) myset
How about:
SELECT
Id,
case when Name LIKE '%Peter%' then Name else null end AS Peters,
case when Name LIKE '%Mike%' then Name else null end AS Mikes
FROM
(SELECT Id, Name FROM Customer) myset
Additionally, you could use the table directly.
select Id, Peters, Mikes
from(
select
case when Name like '%Peter%' then Name end as Peters,
case when Name like '%Mike%' then Name end as Mikes
from Customer)t
Im trying to use case to vary the value im checking in a where clause but I'm getting the error:
incorrect syntax near the keyword 'CASE'
SQL Server 2005
select *
from table
where ((CASE when adsl_order_id like '95037%'
then select '000000'+substring(adsl_order_id,6,6)
ELSE select adsl_order_id
END)
not in (select mwebID from tmp_csv_dawis_bruger0105)
Here is one way to include a case statement in a Where clause:
SELECT * FROM sometable
WHERE 1 = CASE WHEN somecondition THEN 1
WHEN someothercondition THEN 2
ELSE ... END
You could try
SELECT *
FROM table
WHERE (SELECT CASE WHEN adsl_order_id LIKE '95037%'
THEN '000000' + SUBSTRING(adsl_order_id, 6, 6)
ELSE adsl_order_id
END)
NOT IN (select mwebID from tmp_csv_dawis_bruger0105)
A correlated subquery is one possibility:
select *
from mytable
where not exists (
select *
from
tmp_csv_dawis_bruger0105
where
mwebID =
CASE when mytable.adsl_order_id like '95037%' then '000000' + substring(mytable.adsl_order_id,6,6)
ELSE mytable.adsl_order_id END
)
I came to this question looking for an answer thinking WHERE CASE ... would be the solution. I could not adapt the answers to my problem, but this technique to use a parameter that could be null, partial, or specific, works:
CREATE PROC myproc #vJobID VARCHAR (11)
AS
SELECT * FROM Jobs
WHERE Jobs.JobID like coalesce(#vJobID+'%','%')
HTH
You have one too many opening parentheses before the CASE expression.
Put it in the SELECT clause...
select *, (CASE when adsl_order_id like '95037%'
then '000000'+substring(adsl_order_id,6,6)
ELSE adsl_order_id
END) AS Id
from table
where not in (select mwebID from tmp_csv_dawis_bruger0105)
Also, you don't need to "SELECT" the result of the CASE.