IF ELSE in SQL Server - sql

I have the following column in Microsoft SQL Server called Type and I want to create another column called Type 1 which would be 1 if account starts with letter A, 2 if account starts with D....
I am new with this so could someone please advise?
Type Type 1
AD 1
AV 1
AC 1
DE 2
DR 2
DG 2
KL 3
KL 3

Use CASE instead of IF:
SELECT
Type,
CASE
WHEN Type LIKE 'A%' then 1
WHEN Type Like 'D%' THEN 2
WHEN ...
END AS Type1
...

SELECT Type,
CASE WHEN SUBSTRING(Type, 1, 1)='A' THEN 1
WHEN SUBSTRING(Type, 1, 1)='D' THEN 2
WHEN SUBSTRING(Type, 1, 1)='K' THEN 3
ELSE 0 END as 'Type 1'
FROM TableName

As others have pointed out, a CASE statement will meet this need easily.
If you are going to be doing this translation often, consider adding a computed column to the table like so:
if object_id('ComputedColumnTest') is not null
drop table ComputedColumnTest;
go
create table ComputedColumnTest
(
TYPE varchar(2)
);
alter table ComputedColumnTest --<<<<<<<
add TYPE1 AS --<<<<<<<
( --<<<<<<<
case when [TYPE] like 'A%' then 1--<<<<<<<
when [TYPE] like 'D%' then 2--<<<<<<<
else 0 --<<<<<<<
end --<<<<<<<
) --<<<<<<<
insert into ComputedColumnTest([type]) values ('AD')
insert into ComputedColumnTest([type]) values ('AV')
insert into ComputedColumnTest([type]) values ('AC')
insert into ComputedColumnTest([type]) values ('DE')
insert into ComputedColumnTest([type]) values ('DR')
insert into ComputedColumnTest([type]) values ('DG')
insert into ComputedColumnTest([type]) values ('KL')
insert into ComputedColumnTest([type]) values ('KL')
select *
from ComputedColumnTest
where type1 = 2

If you want to put WHERE on computed column, you have to wrap it in subquery.
SELECT *
FROM (
SELECT Type
, (CASE LEFT(Type, 1)
WHEN 'A' THEN 1
WHEN 'D' THEN 2
END) AS Type1
) a
WHERE Type1 = 'some condition'

I know this question has been answered already but I thought I would add my 2 cents.
If the amount of possible values is going to get very large, causing the case statement to become very large you may want to consider creating a loopkup table for yourself.
You would the be able to just do a join between the 2 tables to get the relevant column value.

If you wanted to number using the first letter of the 'type' column in ascending order, use the DENSE_RANK() in SQL Server.
;WITH cte_1
AS
( SELECT Type
,DENSE_RANK() OVER (ORDER BY LEFT(Type,1) ) [Type 1]
FROM YourTable)
SELECT *
FROM cte_1
WHERE [Type 1]=1 --here you can add your filter criteria

Related

How best to Count(*) with a CASE STATEMENT?

The following SQL (on SQL Server) returns an error of:
Incorrect syntax near '*'
Is there something inherently wrong with using the following SELECT statement?:
SELECT
COUNT(CASE WHEN <conditions> THEN * ELSE NULL END) as conditionalcountall
FROM TABLE
I tried this variation which also failed:
SELECT
CASE WHEN <conditions> THEN COUNT(*) ELSE NULL END as conditionalcountall
FROM TABLE
I tend to like sum()
SELECT
SUM(CASE WHEN <conditions> THEN 1 ELSE 0 END) as conditionalcountall
FROM TABLE
Try This, it is Tested
SELECT
CASE WHEN 1 = 1 THEN COUNT(*) ELSE NULL END as conditionalcountall
FROM TABLE
1 = 1is example conditions
Demo:-
Create table #temp (id int , col1 varchar (10))
go
insert into #temp values (1 , 'aaaa')
insert into #temp values (2 , 'bbbb')
insert into #temp values (3 , 'cccc')
SELECT
CASE WHEN 1 = 1 THEN COUNT(*) ELSE NULL END as conditionalcountall
FROM #temp
Result:
In Case Condation like that id = 1 you should select Count(*) in CASE cluse in your query
like this:
SELECT
CASE WHEN id = 1 THEN (select COUNT(*) from #temp) ELSE NULL END as conditionalcountall
FROM #temp
Result:-
Note: if You used Count(*) directly, you counted the id column, so you should use group by as next:
SELECT
CASE WHEN id = 1 THEN COUNT(*) ELSE NULL END as conditionalcountall
FROM #temp
group by id
Result:
SELECT
CASE WHEN X THEN Y
ELSE Z
END *NEW COLUMN NAME*
, COUNT(*)
FROM
TABLE
GROUP BY
*NEW COLUMN NAME*
This should return two columns, one with the buckets/case statement and one with the count of the columns for each one of your buckets
This method was the most straightforward for myself
If you REALLY, REALLY want to use COUNT, then you can do this:
SELECT
COUNT(*)
FROM table
WHERE <conditions>

How to transpose a table with a variable but limited number of matching entries

I am working with the following table DDL:
CREATE TABLE foo (
globalkey INTEGER NOT NULL,
subkey INTEGER NOT NULL,
type VARCHAR(3) NOT NULL,
bar VARCHAR(4) NOT NULL
);
The table contains records of different type (e.g. "t1", "t2" and "t3"). The primary key of the table is (globalkey, subkey). For each globalkey there can be 1-n records some of them may share the same type. However there are only 0-3 number of records with type "t1".
I'd like to create a query that gives me all globalkeys that have at least one record with type "t1" and returns the 3 bar values as 3 different columns in the result.
So for the given data:
GLOBALKEY, SUBKEY, TYPE, BAR
1 1 t1 hit1
1 28 t1 hit1234
1 315 t2 miss
1 967 t1 hit4711
2 1 t5 miss
2 13 t5 miss
2 18 t1 hit9876
3 1 t2 miss
I'd like to get:
GLOBALKEY, BAR1, BAR2, BAR3
1, "hit1", "hit1234", "hit4711"
3, "hit9876", NULL , NULL
I created a SQLFiddle under: http://sqlfiddle.com/#!9/04855
I'd greatly appreciate any pointer on how to get this done!
Bye,
Markus
UPDATE #1: I did not make it clear enough in my initial version that the column BAR contains any arbitrary values, so I can't use the exact version of #Mita statement as this assumes that the values in the case statements are fixed ('hit1', 'hit2', ...). However in the meantime I found out how this problem can be overcome by using the row_number() window function. Check out my edit of #Mita's answer. Updated the SQLFiddle accourdingly...
The JSFiddle inserts are not with the data you gave in the example. The correct data should be:
INSERT INTO foo VALUES (1,1,'t1','hit1');
INSERT INTO foo VALUES (1,28,'t1','hit2');
INSERT INTO foo VALUES (1,315,'t2','miss');
INSERT INTO foo VALUES (1,967,'t1','hit3');
INSERT INTO foo VALUES (2,1,'t5','miss');
INSERT INTO foo VALUES (2,13,'t5','miss');
INSERT INTO foo VALUES (2,18,'t1','hit1');
INSERT INTO foo VALUES (3,1,'t2','miss');
And the query (if I understood you right) should be:
SELECT
globalkey,
MAX(CASE WHEN bar = 'hit1' THEN 'hit1' ELSE NULL END) AS bar1,
MAX(CASE WHEN bar = 'hit2' THEN 'hit2' ELSE NULL END) AS bar2,
MAX(CASE WHEN bar = 'hit3' THEN 'hit3' ELSE NULL END) AS bar3
FROM
foo
WHERE
type = 't1'
GROUP BY
globalkey
UPDATE #1: Mita's query assumes that 'hit1', 'hit2',... are fixed values. In my example they are not enumerated but could be any text. By using window functions you can however achieve what I was looking for. First select all matching rows (type='t1') and add a new column containg the row-number within the current globalkey.
Using this intermediary result set one can then transpose the table similiar to #MITA's answer.
So in DB2 you can do:
WITH tmp1 AS (SELECT globalkey, bar, row_number() OVER (partition by globalkey) AS rnr
WHERE
type='t1'
)
SELECT globalkey,
MAX(CASE WHEN rnr = 1 THEN bar ELSE NULL END) AS bar1,
MAX(CASE WHEN rnr = 2 THEN bar ELSE NULL END) AS bar2,
MAX(CASE WHEN rnr = 3 THEN bar ELSE NULL END) AS bar3
FROM
foo
WHERE
type = 't1'
GROUP BY
globalkey

SQL AVG with Case

I've a DB which stores a value from C to AAA, while C is the worst and AAA the best.
Now I need the average of this value and I don't know how to first convert the values into an int, calculate the average, round the average to an int and convert it back.
Definitions:
C = 1
B = 2
A = 3
AA = 4
AAA = 5
Is that even possible with an SQL statement? I tried to combine AVG and CASE, but I don't bring it to work...
Thanks for your help!
Regards,
select avg(case score
when 'C' then 1
when 'B' then 2
when 'A' then 3
when 'AA' then 4
when 'AAA' then 5
end) as avg_score
from the_table;
(this assumes that the column is called score)
To convert this back into the "character value", wrap the output in another case:
select case cast(avg_score as int)
when 1 then 'C'
when 2 then 'B'
when 3 then 'A'
when 4 then 'AA'
when 5 then 'AAA'
end as avg_score_value
from (
select avg(case score
when 'C' then 1
when 'B' then 2
when 'A' then 3
when 'AA' then 4
when 'AAA' then 5
end) as avg_score
from the_table;
) t
The above cast(avg_score as int) assumes ANSI SQL. Your DBMS might have different ways to cast a value to an integer.
I've created this example for u.
u can cast ur ranking into temp table, then calculate and when ur done, drop it.
create table sof (id int identity,a nvarchar (10))
insert into sof values ('a')
insert into sof values ('b')
insert into sof values ('c')
select case a when 'AAA ' then 5
when 'AA' then 4
when 'A' then 3
when 'B' then 2
else 1
end as av
into #temp
from sof
----for rounded
select ROUND(AVG(CAST(av AS FLOAT)), 4)
from #temp
--not rounded
select AVG (av)
from #temp

How do I determine if a group of data exists in a table, given the data that should appear in the group's rows?

I am writing data to a table and allocating a "group-id" for each batch of data that is written. To illustrate, consider the following table.
GroupId Value
------- -----
1 a
1 b
1 c
2 a
2 b
3 a
3 b
3 c
3 d
In this example, there are three groups of data, each with similar but varying values.
How do I query this table to find a group that contains a given set of values? For instance, if I query for (a,b,c) the result should be group 1. Similarly, a query for (b,a) should result in group 2, and a query for (a, b, c, e) should result in the empty set.
I can write a stored procedure that performs the following steps:
select distinct GroupId from Groups -- and store locally
for each distinct GroupId: perform a set-difference (except) between the input and table values (for the group), and vice versa
return the GroupId if both set-difference operations produced empty sets
This seems a bit excessive, and I hoping to leverage some other commands in SQL to simplify. Is there a simpler way to perform a set-comparison in this context, or to select the group ID that contains the exact input values for the query?
This is a set-within-sets query. I like to solve it using group by and having:
select groupid
from GroupValues gv
group by groupid
having sum(case when value = 'a' then 1 else 0 end) > 0 and
sum(case when value = 'b' then 1 else 0 end) > 0 and
sum(case when value = 'c' then 1 else 0 end) > 0 and
sum(case when value not in ('a', 'b', 'c') then 1 else - end) = 0;
The first three conditions in the having clause check that each elements exists. The last condition checks that there are no other values. This method is quite flexible, for various exclusions and inclusion conditions on the values you are looking for.
EDIT:
If you want to pass in a list, you can use:
with thelist as (
select 'a' as value union all
select 'b' union all
select 'c'
)
select groupid
from GroupValues gv left outer join
thelist
on gv.value = thelist.value
group by groupid
having count(distinct gv.value) = (select count(*) from thelist) and
count(distinct (case when gv.value = thelist.value then gv.value end)) = count(distinct gv.value);
Here the having clause counts the number of matching values and makes sure that this is the same size as the list.
EDIT:
query compile failed because missing the table alias. updated with right table alias.
This is kind of ugly, but it works. On larger datasets I'm not sure what performance would look like, but the nested instances of #GroupValues key off GroupID in the main table so I think as long as you have a good index on GroupID it probably wouldn't be too horrible.
If Object_ID('tempdb..#GroupValues') Is Not Null Drop Table #GroupValues
Create Table #GroupValues (GroupID Int, Val Varchar(10));
Insert #GroupValues (GroupID, Val)
Values (1,'a'),(1,'b'),(1,'c'),(2,'a'),(2,'b'),(3,'a'),(3,'b'),(3,'c'),(3,'d');
If Object_ID('tempdb..#FindValues') Is Not Null Drop Table #FindValues
Create Table #FindValues (Val Varchar(10));
Insert #FindValues (Val)
Values ('a'),('b'),('c');
Select Distinct gv.GroupID
From (Select Distinct GroupID
From #GroupValues) gv
Where Not Exists (Select 1
From #FindValues fv2
Where Not Exists (Select 1
From #GroupValues gv2
Where gv.GroupID = gv2.GroupID
And fv2.Val = gv2.Val))
And Not Exists (Select 1
From #GroupValues gv3
Where gv3.GroupID = gv.GroupID
And Not Exists (Select 1
From #FindValues fv3
Where gv3.Val = fv3.Val))

SQL - Combining incomplete

I'm using Oracle 10g. I have a table with a number of fields of varying types. The fields contain observations that have been made by made about a particular thing on a particular date by a particular site.
So:
ItemID, Date, Observation1, Observation2, Observation3...
There are about 40 Observations in each record. The table structure cannot be changed at this point in time.
Unfortunately not all the Observations have been populated (either accidentally or because the site is incapable of making that recording). I need to combine all the records about a particular item into a single record in a query, making it as complete as possible.
A simple way to do this would be something like
SELECT
ItemID,
MAX(Date),
MAX(Observation1),
MAX(Observation2)
etc.
FROM
Table
GROUP BY
ItemID
But ideally I would like it to pick the most recent observation available, not the max/min value. I could do this by writing sub queries in the form
SELECT
ItemID,
ObservationX,
ROW_NUMBER() OVER (PARTITION BY ItemID ORDER BY Date DESC) ROWNUMBER
FROM
Table
WHERE
ObservationX IS NOT NULL
And joining all the ROWNUMBER 1s together for an ItemID but because of the number of fields this would require 40 subqueries.
My question is whether there's a more concise way of doing this that I'm missing.
Create the table and the sample date
SQL> create table observation(
2 item_id number,
3 dt date,
4 val1 number,
5 val2 number );
Table created.
SQL> insert into observation values( 1, date '2011-12-01', 1, null );
1 row created.
SQL> insert into observation values( 1, date '2011-12-02', null, 2 );
1 row created.
SQL> insert into observation values( 1, date '2011-12-03', 3, null );
1 row created.
SQL> insert into observation values( 2, date '2011-12-01', 4, null );
1 row created.
SQL> insert into observation values( 2, date '2011-12-02', 5, 6 );
1 row created.
And then use the KEEP clause on the MAX aggregate function with an ORDER BY that puts the rows with NULL observations at the end. whatever date you use in the ORDER BY needs to be earlier than the earliest real observation in the table.
SQL> ed
Wrote file afiedt.buf
1 select item_id,
2 max(val1) keep( dense_rank last
3 order by (case when val1 is not null
4 then dt
5 else date '1900-01-01'
6 end) ) val1,
7 max(val2) keep( dense_rank last
8 order by (case when val2 is not null
9 then dt
10 else date '1900-01-01'
11 end) ) val2
12 from observation
13* group by item_id
SQL> /
ITEM_ID VAL1 VAL2
---------- ---------- ----------
1 3 2
2 5 6
I suspect that there is a more elegant solution to ignore the NULL values than adding the CASE statement to the ORDER BY but the CASE gets the job done.
i dont know about commands in oracle but in sql you could use some how that
first use pivot table is contains consecutives numbers 0,1,2...
i'm not sure but in oracle the function "isnull" is "NVL"
select items.ItemId,
case p.i = 0 then observation1 else '' end as observation1,
case p.i = 0 then observation1 else '' end as observation2,
case p.i = 0 then observation1 else '' end as observation3,
...
case p.i = 39 then observation4 else '' as observation40
from (
select items.ItemId
from table as items
where items.item = _paramerter_for_retrive_only_one_item /* select one item o more item where you filter items here*/
group by items.ItemId) itemgroup
left join
(
select
items.ItemId,
p.i,
isnull( max ( case p.i = 0 then observation1 else '' end ), '' ) as observation1,
isnull( max ( case p.i = 1 then observation2 else '' end ), '' ) as observation2,
isnull( max ( case p.i = 2 then observation3 else '' end), '' ) as observation3,
...
isnull( max ( case p.i = 39 then observation4), '' ) as observation40,
from
(select i from pivot where id < 40 /*you number of columns of observations, that attach one index*/
)
as p
cross join table as items
lef join table as itemcombinations
on item.itemid = itemcombinations.itemid
where items.item = _paramerter_for_retrive_only_one_item /* select one item o more item where you filter items here*/
and (p.i = 0 and not itemcombinations.observation1 is null) /* column 1 */
and (p.i = 1 and not itemcombinations.observation2 is null) /* column 2 */
and (p.i = 2 and not itemcombinations.observation3 is null) /* column 3 */
....
and (p.i = 39 and not itemcombinations.observation3 is null) /* column 39 */
group by p.i, items.ItemId
) as itemsimplified
on itemsimplified.ItemId = itemgroup.itemId
group by itemgroup.itemId
About pivot table
create an pivot table, Take a look at that
pivot table schema
name: pivot columns: {i : datatype int}
How populate
create foo table
schema foo
name: foo column: value datatype varchar
insert into foo
values('0'),
values('1'),
values('2'),
values('3'),
values('4'),
values('5'),
values('6'),
values('7'),
values('8'),
values('9');
/* insert 100 values */
insert into pivot
select concat(a.value, a.value) /* mysql */
a.value + a.value /* sql server */
a.value | a.value /* Oracle im not sure about that sintax */
from foo a, foo b
/* insert 1000 values */
insert into pivot
select concat(a.value, b.value, c.value) /* mysql */
a.value + b.value + c.value /* sql server */
a.value | b.value | c.value /* Oracle im not sure about that sintax */
from foo a, foo b, foo c
the idea about pivot table can consult in "Transact-SQL Cookbook By Jonathan Gennick, Ales Spetic"
I have to admit that the above solution (by Justin Cave) is simpler and easier to understand but this is another good option
at the end like you said you solved