insert select alias value into column - sql

Not sure if this is the best title, but i want to select string values into an int column of a new table (the reason is to use keys with int data types rather than strings, so there are more columns not shown in this example)
table1.key1 table2.key2
a 1
b 2
c 3
a 1
one way i can do this is as follows but the syntax is very very long in some scenarios
insert into table2 (key2)
select 1
from table1
where key1 = 'a'
insert into table2 (key2)
select 2
from table1
where key1 ='b'
etc...
can someone show me how i could use a syntax that is shorter? also i have to keep identity insert set to off so an update statement will not work from what i understand.

SQL Fiddle Demo
Use a CASE expresion
insert into table2 (key2)
select CASE WHEN key1 = 'a' THEN 1
WHEN key1 = 'b' THEN 2
WHEN key1 = 'c' THEN 3
.....
ELSE -1
END as key2
from table1

Related

Count value across multiple columns

I am looking to count the number of times set of values occurred in a table. These values could occur in up to 10 different columns. I need to increment the count regardless of which column it is in. I know how I could count if they were all in the same column but not spanning multiple columns.
Values can be added in any order. I have about a thousand
Cpt1 Cpt2 Cpt3 Cpt4 Cpt5
63047 63048 63048 NULL NULL
I would want to for this row I'd expect this as the result
63047 1
63048 2
You could use a union all call to treat them as one column:
SELECT col, COUNT(*)
FROM (SELECT col1 FROM mytable
UNION ALL
SELECT col2 FROM mytable
UNION ALL
SELECT col3 FROM mytable
-- etc...
) t
GROUP BY col
It's not entirely clear what your table exactly looks like, but I'm guessing that what you're looking for is:
SELECT row_count = COUNT(*),
row_count_with_given_value = SUM ( CASE WHEN field1 = 'myValue' THEN 1
WHEN field2 = 'myValue' THEN 1
WHEN field3 = 'myValue' THEN 1
WHEN field4 = 'myValue' THEN 1 ELSE 0 END)
FROM myTable
Assuming the fieldx columns are not NULL-able, you could write it like this too:
SELECT row_count = COUNT(*),
row_count_with_given_value = SUM ( CASE WHEN 'myValue' IN (field1, field2, field3, field4) THEN 1 ELSE 0 END)
FROM myTable
Something like this might work (after adapting to your value domain and data types):
create table t1
(i1 int,
i2 int,
i3 int);
insert into t1 values (1,0,0);
insert into t1 values (1,1,1);
insert into t1 values (1,0,0);
declare #i int = 0;
select #i = #i + i1 + i2 + i3 from t1;
print #i;
drop table t1;
Output is: 5
Many databases support lateral joins, of one type of another. These can be used to simplify this operation. Using the SQL Server/Oracle 12C syntax:
select v.cpt, count(*)
from t cross apply
(values (cpt1), (cpt2), . . .
) v(cpt)
where cpt is not null
group by v.cpt;

Rename category in the column in SQL Server

Here is the query
select col1
from table
col1 contains these category values:
A
B
C
NULL
How can I rename null category to D?
If you want to make the change permanent
UPDATE table
SET col1 = 'D'
WHERE col1 IS NULL
From then on you can simply query with ...
SELECT col1
FROM table
... to get the desired result.
If there is more than one row having a NULL in col1, you need to filter by a unique key, preferably by the primary key (which every table should have by the way). Let's say you have a table like
id (PK) col1
--- ----
1 'A'
2 'B'
3 'C'
4 NULL
5 NULL
then you can fix it with
UPDATE table SET col1 = 'D' WHERE id = 4;
UPDATE table SET col1 = 'E' WHERE id = 5;
unless you can calculate the new value from another column, e.g.:
UPDATE table
SET col1 = UPPER(LEFT(name, 1))
Try this : ISNULL( ) function is used to replace NULL value with another value
select isnull(col1,'D') as col1
from table
SQL Server uses ISNULL().
SELECT ISNULL(value_to_check, use_this_instead_if_valuetocheck_is_null)
For your code:
select ISNULL(col1, 'D') AS col_name
from table
However, this will happen across the board for this column. You can't use this to make a sequence, like D then E then F. Any NULL value you come across in this column will change to D.

Update multiple columns based on different rows in another column sql server

I have a table
staff Type col1 col2
1 a 0 200
1 b 50 2000
1 c 2 200
2 a 0 100
2 b 25 1000
2 c 2 200
3 a 0 150
3 b 35 1500
3 c 2 200
I am just wondering if I could update both col1 and col2 based on the values in the Type column for each staff. My logic to update would be for example for staff 1
update col1 where Type =a is 200/(2000/50) =5
update col1 where Type =b is col1 - (200/(2000/50)) =50-5 = 45
update col2 where Type =b is 2000-200 = 1800
update col2 where Type =a to 0
Please see the resultant which I need to achieve is below and updated values(*)
staff Type col1 col2
1 a 5* 0*
1 b 45* 1800*
1 c 2 200
2 a 2.5* 0*
2 b 22.5* 900*
2 c 2 200
3 a 3.5* 0*
3 b 31.5* 1350*
3 c 2 200
Is it possible to get this done without using cursors/loops/functions/ctes?
Try below code
DECLARE #myTable TABLE (
staff INTEGER NOT NULL
,Type VARCHAR(1) NOT NULL
,col1 INTEGER NOT NULL
,col2 INTEGER NOT NULL
)
INSERT INTO #myTable VALUES(1,'a',0,200),
(1,'b',50,2000),
(1,'c',2,200),
(2,'a',0,100),
(2,'b',25,1000),
(2,'c',2,200),
(3,'a',0,150),
(3,'b',35,1500),
(3,'c',2,200)
UPDATE t1
SET Col1 = CASE WHEN Type ='a'
THEN col2/(Select Col2/Col1 FROM #myTable T2 WHERE T2.Type = 'b' AND T2.staff = T1.Staff)
WHEN Type ='b'
THEN col1 - ((Select Col2 FROM #myTable T2 WHERE T2.Type = 'a' AND T2.staff = T1.Staff)/(Col2/Col1))
ELSE COl1
END ,
Col2 = CASE WHEN Type = 'b'
THEN Col2 - (Select Col2 FROM #myTable T2 WHERE T2.Type = 'a' AND T2.staff = T1.Staff)
WHEN Type = 'a'
THEN 0
Else Col2
END
FROM #MyTable t1
SELECT * FROM #myTable
You can do it in one massive query, but you will likely need a lot of fail-safes to make it foolproof. For instance, you would've to account for a value of 0 in b:col1 or b:col2 (although assuming that you only want the cross multiplication, you can do a:col2*b:col1/b:col2 and it's much simpler for the computer (divisions are a mess) on top of having 1 less exception. The drawback is when you start working with massive numbers, but I'm gonna assume that you won't go over 1000000^2 so that's kinda irrelevant. Similarly, my code is gonna assume that there is always exactly one a row and a b row.
declare #test as table(
staff int,
Type char(1),
col1 decimal(6,2),
col2 decimal(6,2)
)
insert into #test values(1,'a',0,200);
insert into #test values(1,'b',50,2000);
insert into #test values(1,'c',2,200);
insert into #test values(2,'a',0,100);
insert into #test values(2,'b',25,1000);
insert into #test values(2,'c',2,200);
insert into #test values(3,'a',0,150);
insert into #test values(3,'b',35,1500);
insert into #test values(3,'c',2,200);
UPDATE
FirstTable
SET
FirstTable.col1 = CASE FirstTable.[type] WHEN 'a' THEN JoinedTable.col1 * FirstTable.col2 / JoinedTable.col2 ELSE FirstTable.col1 - FirstTable.col1 * JoinedTable.col2 / FirstTable.col2 END,
FirstTable.col2 = CASE FirstTable.[type] WHEN 'a' THEN 0 ELSE FirstTable.col2 - JoinedTable.col2 END
FROM
#test FirstTable
INNER JOIN
#test JoinedTable
ON
FirstTable.staff = JoinedTable.staff AND JoinedTable.[Type] = CASE FirstTable.[Type] WHEN 'a' THEN 'b' ELSE 'a' END
WHERE
FirstTable.[Type] in('a','b')
The behavior is as follow : if there is a 0 in a col2 for the Type b, it crashes. If there is more than one row for one staff with Type a or b, I believe it takes one of the 2 in a non deterministic fashion, which will lead to some non deterministic and potentially faulty results. If there is no Type a or b, it skips the staff entirely.
Otherwise (aka when it works), it simply updates rows with Type a and b based on their Type. Updating both in 1 query allow you to not use temporary tables and the likes since the new data doesn't override the old until the whole query is doen.
Also, try not to use keywords like Type for your columns or you may end up with some mistakes down the line.

Select where column in not null array

I am trying to do :
SELECT *
FROM table
WHERE column IN (SELECT col FROM table2 WHERE col2 = value )
but I want to check if the second request doesn't return a null array.
How is that possible?
Thanks in advance
Simply add a NOT NULL check in the subquery to omit the null values returned.
SELECT * FROM table WHERE column IN
(SELECT col FROM table2 WHERE col2 = value AND col IS NOT NULL);

sql query to return single value based on column it matches

I've a requirement where in if the query string matches column1 , return me 1. If it matches column 2 return 2 else if it matches column 3 return 3.
Table strunctre:
col1 col2 col3
11 12 13
22 23 24
If my query string is 23, then i'm expecting a return value of 2 as it matches col2.
Something like below:
select 1
from table1
where col1=querystring
and orderid=xxx
or select 2 from table1
where col2=querystring
and orderid=xxx
or select 3 from table1
where col3=querystring and orderid=xxx
Basically i'm expecting one query which return single value based on the column it matches.
Is it something doable in SQL as i'm not very good in DB skills.
Any help is highly appreciated.
There are a couple of approaches. If there is a guarantee that no more than one column will match at a time, a UNION will work:
SELECT 1 AS SomeCol
FROM table1
WHERE col1 = querystring
AND orderid = xxx
UNION
SELECT 2
FROM table1
WHERE col2 = querystring
AND orderid = xxx
UNION
SELECT 3
FROM table1
WHERE col3 = querystring
AND orderid = xxx;
If more than one match can happen, another approach is this (note the order of precedence is now col1, col2, col3 etc):
SELECT CASE
WHEN col1 = querystring THEN 1
WHEN col2 = querystring THEN 2
WHEN col3 = querystring THEN 3
END AS SomeCol
FROM table1
WHERE orderid = xxx;
Please try using case
declare #var int
set #var=23
select
case #var when col1 then 1
when col2 then 2
when col3 then 3 end
from YourTable
where
col1=#var OR
col2=#var OR
col3=#var
Try using IF-ELSE condition in your query and check with passed parameter.