I've a table with 3 columns: Id, Price, Total.
I'm writing this sql statement:
SELECT Id, Price, Total,
CASE WHEN [Total] IS NULL THEN '0'
WHEN [Total] IS NOT NULL '1'
ELSE ''
END AS NewColumnName
FROM Table
If I run this sql, I have no error. But if I add a Where, like this:
WHERE NewColumnName= '1' the server return an error: the name of column NewColumnName is not valid.
Please help me!
Thanks a lot!!
RM
Can you try this way?
select *
from (
SELECT Id, Price, Total,
CASE WHEN [Total] IS NULL THEN '0'
WHEN [Total] IS NOT NULL '1'
ELSE ''
END AS NewColumnName
FROM Table
) x
where
NewColumnName= '1'
Try this,
SELECT Id, Price, Total,
CASE WHEN [Total] IS NULL THEN '0'
WHEN [Total] IS NOT NULL '1'
END AS NewColumnName
FROM Table
WHERE CASE WHEN [Total] IS NULL THEN '0'
WHEN [Total] IS NOT NULL '1'
END = '1'
Generally use a subquery,
SELECT Id, Price, Total, NewColumnName
FROM (
SELECT
Id,
Price,
Total,
CASE
WHEN [Total] IS NULL THEN '0'
ELSE '1'
END [NewColumnName]
FROM
Table) [WithNew]
WHERE
NewColumnName = '1';
or in your case you could do,
SELECT
Id,
Price,
Total,
1 [NewColumnName]
FROM
Table
WHERE
Total IS NOT NULL;
USE [AdventureWorks2012]
WITH cte as (
SELECT [AddressID], [AddressLine1]
, CASE WHEN [City] = 'Bothell' THEN 1 ELSE 0 END AS [NewColumn]
FROM Person.Address )
SELECT * FROM CTE
WHERE [NewColumn] = 1
So yours would be re-written like this:
WITH cte as (
SELECT Id, Price, Total,
CASE WHEN [Total] IS NULL THEN '0'
WHEN [Total] IS NOT NULL '1'
ELSE ''
END AS NewColumnName
FROM Table)
SELECT * FROM CTE where [NewColumnName] = 1
You can't use NewColumnName in the WHERE Clause. You will have to use the CASE statement again in WHERE Clause or do what #Lajos suggested.
You can just write your query like this:
SELECT Id, Price, Total,
FROM Table
WHERE Total IS NOT NULL
That's doing the same thing, except without the case statement.
Related
In my SQL query, I want to check if a column has the string 'test' and safe the value in a new column
When I use the new column in GROUP BY, I get error saying it can't find the new column:
SELECT
CAST([EvtTime] as date) AS myDay,
CASE WHEN [result] LIKE '%test%' THEN 1 ELSE 0 END AS testResult,
COUNT_BIG(*) AS myCount,
[name]
FROM [mytable]
GROUP BY myDay, testResult
I am using SQL Server.
CROSS APPLY is a clean way to solve this issue.
SELECT
CAST([EvtTime] as date) AS myDay,
X.testResult,
COUNT_BIG(*) AS myCount,
[name]
FROM myTable
CROSS APPLY (VALUES (CASE WHEN [result] LIKE '%test%' THEN 1 ELSE 0 END)) AS X(testResult)
GROUP BY myDay, testResult
You can use inline view:
SELECT *,
COUNT_BIG(*) AS myCount
FROM (
SELECT
CAST([EvtTime] as date) AS myDay,
CASE WHEN [result] LIKE '%test%' THEN 1 ELSE 0 END AS testResult,
[name]
FROM [MyTable]
) MyInlineView
GROUP BY myDay, testResult
I'm doing a query in SQL like this:
with cte as
(
select
t.ktokk, t.txt30,
case
when t.spras = 'P' then '1'
when t.spras = 'E' then '2'
else '3'
end as ord_ktokk
from
t077y t
order by
ord_ktokk
)
select *
from cte
and the result is that:
[Image]
Now I want to select only the distinct values in ktokk column. How can i do that?
thanks for the help
Following is my table:
I want to create a view with the following conditions in a select statement:
If count(employee_id) > 1 then only the record having status = 'Current' is picked in the view.
I tried:
select employee_id
, case when COUNT(employee_id) > 1 and statusval = 'Current' then 'Y' else 'N' end as val
from table1
group by employee_id
I hope someone can help me with this statement. Thanks.
Try this
DECLARE #T TABLE (ID INT, STATUS VARCHAR(50))
INSERT INTO #T VALUES (1,'Current'),(2,'Historical'),(2,'Historical'),(2,'Current')
SELECT * FROM #T M
INNER JOIN (SELECT ID FROM #T
GROUP BY ID
HAVING COUNT(ID) > 1) S ON S.ID=M.ID
WHERE M.STATUS = 'Current'
Just to show you how your attempt could have looked. You were nearly there, you just needed to use the windowed version of count e.g.
with cte as (
select Employee_Id, [Status]
, case when count(*) over (partition by id) > 1 and [Status] = 'Current' then 1 else 0 end Val
from Table1
)
select Employee_Id, [Status]
from cte
where Val = 1;
And this approach appears on the face of it to perform better than a join.
All of your sample data have exactly one row with 'Current'. So, the simplest solution appears to be:
select t.*
from t
where t.status = 'Current';
i have a question, How can i pivot an aggregation result to look like ..
I'm trying to pivot a simple aggregation using this query first:
select sync_action action, count(sync_action) total
FROM my_table
group by sync_action
and to pivot the table i'm using:
select * from (
select sync_action , count(sync_action) total
FROM my_table
group by sync_action )
pivot
(
count(sync_action)
for sync_action in ('delete','create')
)
;
and i don't know where is the error, because the result is:
the idea is have the same as the first image.
Can somebody help me?
Best regards
I would just use conditional aggregation:
select
sum(case when sync_action = 'delete' then total else 0 end) sum_delete,
sum(case when sync_action = 'create' then total else 0 end) sum_create
from mytable
where sync_action in ('delete', 'create')
You don't need to do group by, just do like
SELECT *
FROM mytable
pivot
( COUNT(sync_action)
FOR sync_action IN('delete','create')
);
In your query you need "SUM of total" instead of "Count of sync_action" in pivot section. Others are ok. If you use count, in your case you will always get 1.
select * from (
select sync_action , count(sync_action) total
FROM my_table
group by sync_action ) as p
pivot
(
sum(p.total)
for p.sync_action in ("delete","create")
)pt
I think you just want:
select sum(case when sync_action = 'delete' then 1 else 0 end) as delete,
sum(case when sync_action = 'create' then 1 else 0 end) as create
from my_table;
I don't see how pivot helps at all with what you want to do.
I am trying to create SQL Select that returns counts of a certain field based on a field.
So, here is what I am trying to do.
Select count(distinct id) as TotalCount, -- this will be the total of id
count(distinct id where type='A') as TotalA, -- this will be total when type='A'
count(distinct id where type='B') as TotalB -- This will be total when type = 'B'
from MyTable
Basically, TotalCount = TotalA + TotalB.
How can I achieve this in SQL Select Statement?
Thanks.
Select count(distinct id) as TotalCount, -- this will be the total of id
count(distinct case type when 'A' then id else NULL end) as TotalA,
count(distinct case type when 'B' then id else NULL end) as TotalB
from MyTable;
Of course TotalCount may or may not be TotalA + TotalB, depending on the actual data.
You can do it like that:
SELECT
count(distinct id) as TotalCount,
sum(CASE WHEN type = 'A' THEN 1 ELSE 0) as TotalA,
sum(CASE WHEN type = 'B' THEN 1 ELSE 0) as TotalB,
FROM
MyTable
Count per type:
SELECT
type,
count(DISTINCT id)
FROM
MyTable
GROUP BY
type
Why not simply UNION the separate queries.
Select 'all' as which, count(distinct id) as Total from mytable
union
select 'a' as which, count(distinct id) where type='A' as Total from mytable
union
select 'b' as which, count(distinct id) where type='B' as Total from mytable