SQL Server 2005: Using select in case statement - sql-server-2005

I have a temporary table whose data is populated from another table using select.
For eg.,
Table1's fields:
A, B, C
Table2's fields
A, D, E --A is foreign key from TableA
#sometemp's fields
X, Y, Z
insert into #sometemp (X, Y, Z)
select D, E, case
when (Table1's C is 0) then 0
when (Table1's C is 1) then somefunction(#arg1, #arg2)
end
from Table2
I cant figure it out how Table1's C value can be checked in the when clauses. Any ideas?

Related

using if condition in snowflake

I have two tables as shown below with columns:
Table A
a, b, c, id
Table B
d, e, f, g, h, id
Now I need perform a query basically I will get a id from user, so I need to check if that id is present in table A or table B. So the record will be present in any of one table
SELECT * FROM tableA WHERE id = 123
OR
SELECT * FROM tableB WHERE id = 123
So the response will be either columns of tableA or columns of tableB. But I can't perform a union since the columns should be equal among two tables.
So it's basically a if condition, how can I get the desired output in Snowflake.
And using if is the best optimized approach or any other way is there
You can use union all -- assuming the types are compatible. Just pad the smaller table:
select a, b, c, null as g, null as h, id
from a
where id = 123
union all
select d, e, f, g, h, id
from b
where id = 123;
If you want the columns separated, then a full join accomplishes that:
select *
from a full join
b
using (id)
where a.id = 123 or b.id = 123;

Syntax error for WHERE clause when using proc sql

I am new to SAS and I need to recreate a query I had running using R.
The syntax rules may be different in SAS but I dont see where I am going wrong here
Table "Old" columns: A, B, C, D, E
Table "New" columns: A, B, C, D, E
PROC SQL;
create table delta as
SELECT *
FROM New
WHERE
(A, B, C)
IN(
SELECT (A, B, C)
FROM New
EXCEPT
SELECT A, B, C
FROM Old);
QUIT;
My code should find delta rows based on A, B, C variables.
Error Message on comma
WHERE(A, B, C): ERROR 79-322: Expecting a (.
I'm not in sas but could be that this db don't allow the use of tuple in WHERE IN clause.
in this case you could try refactoring your quesry as an inner join
SELECT *
FROM New N
INNER JOIN (
SELECT A, B, C
FROM New
EXCEPT
SELECT A, B, C
FROM Old
) T ON T.A = N.A
AND T.B = N.B
AND T.C = N.C

Integrate two sql queries

I have these two queries:
SELECT DISTINCT a, b, c, d, FROM x WHERE b IN (1, 2)
SELECT DISTINCT c, d, FROM y
I would now like to merge these queries such that the statement initiated in the first query only includes rows where the c, d combination is in the output resulting from the second query. Any thoughts on how to do this? My table is large, so efficiency is important.
Use exists?
SELECT DISTINCT a, b, c, d
FROM x
WHERE b IN (1, 2) AND
EXISTS (SELECT 1 FROM y WHERE x.c = y.c and x.d = y.d);
When using exists, the select distinct is only necessary if x has duplicate values. Otherwise it is not necessary.
And, for performance, you want an index on y(c, d). Also, an index on x(b, a, c, d) would also be helpful in most databases.
Note: The distinct is not necessary in the subquery. In some databases, you can use in with composite values as well.
SELECT DISTINCT x.a,x.b,x.c,x.d
FROM x
INNER JOIN y ON x.c = y.c
AND x.d = y.d
WHERE b in (1,2)
Regarding efficiency, your indexing will determine how well that performs.

Postgres - insert and composite foreign keys

Say I have a table with the following columns:
a: integer
b: integer
c: integer
d: integer
code: text
(a, b) is a foreign key to another table 1
(c, d) is a foreign key to another table 2
Inserting is easy:
INSERT INTO table(a, b, c, d, code) VALUES(x1, y1, x2, y2, 'my code')
Now, I would like to insert while fetching the values of my composite foreign keys a,b and c,d in a subquery. Something like this:
INSERT INTO table(a, b, c, d, code) VALUES
((SELECT a, b FROM other-table-1 WHERE ...),
(SELECT c, d FROM other-table-2 WHERE ...), 'my code')
The query above doesn't work ofcourse, but it illustrates what I am trying to achieve.
Another try, but also not working (the sub-query has to return one column):
INSERT INTO table(a, b, code)
SELECT (SELECT a, b FROM other-table-1 WHERE ...),
(SELECT c, d FROM other-table-2 WHERE ...), 'my code')
Is this somehow possible?
You have to use the below syntax to insert records, if 'my code' is always is the static
INSERT INTO table(a, b, code)
SELECT a, b, 'my code' FROM other-table WHERE ...
If you have multiple table, then you can use syntax like this using CTE
INSERT INTO table(a, b, c, d, code)
WITH t1 AS (
SELECT a, b FROM other-table-1 WHERE ...
), t2 AS (
SELECT c, d FROM other-table-2 WHERE ...
)
select t1.a, t1.b, t2.c, t2.d, 'my code' from t1,t2
Your query should be structuerd something like this:
INSERT INTO table(a, b, c, d, code)
SELECT x.a, x.b, y.c, y.d, 'my code' FROM other-table-1 AS x
CROSS JOIN other-table-2 AS y
WHERE ...

Getting Unique result set from two table in SQL Server

I m new to SQL Server, please help me with the solution to following problem.
I have two tables which have the following columns:
Table-A only has one column with data like (A, B, C, D)
Table-B is a temporary table which can contain (B, B, C, C, E, E, F, G)
I need to insert contents of table B into table A.
Conditions are
if table A already contains any alphabet in B then that alphabet should be ignored.
Only a unique alphabet needs to be inserted into table A. i.e. Only one E can be inserted from two set of E and
if F and G also needs to be inserted.
How can I possibly solve this problem?
Thanking you in advance....
Try this....
INSERT INTO Table (Column)
SELECT DISTINCT tt.Column
FROM TempTab tt
WHERE tt.Column NOT IN (SELECT DISTINCT Column FROM Table)
Here is the SQL FIDDLE with this query.
INSERT INTO A(col)
(SELECT distinct col from B
minus
(SELECT distinct col FROM A INTERSECT SELECT distinct col FROM B))