Join Two Select Statements With Different Columns And Condition - sql

I'm not good at asking question, so i'll give an example of what i want to have.
if i = 1 and xi = 0 then
select a,b,c,d,e,f,g where z = 1
elseif i=0 and xi = 1 then
select a,c,f,h,l,n where w = var
elseif i=1 and xi=1 then
select a,b,c,d,e,f,g, where z = 1
union all
select a,c,f,h,l,n where w = var
end if
How can I join the 2 select statement if their columns are not equal and they both have a unique condition?

Based on the conditions you can create derived tables to fetch desired columns and then to get a union of the two tables add null values in column list of derived tables which have less number of columns:
Pseudo code:
select * from
(select a,b,c,d,e,f,g
where z = 1
and 1 = case when i = 1 and xi = 0 then 1
when i = 1 and xi = 1 then 1
else 0
end) as T1
union all
(select a,c,f,h,l,n ,null -- add null value to equate number of columns
where w = var
and 1 = case when i=0 and xi = 1 then 1
when i=1 and xi = 1 then 1
else 0
end) as T2
Hope this helps!!!

If it is not a requirement not to use dynamic sql I will opt for that one.
Another idea will be to use user defined function returnin tables.
So you encapsulate there the logic...

Related

Simple way to check % of total if I add one criteria

I have the following script:
select count (*)
from somewhere
where 1=1
and criteria 1 = 'xxx'
and criteria 2 = 'xx'
and criteria 3 = 'x'
Is there a nice way to calculate the % of the count with the first 2 criterias vs the count with all 3 criterias ?
So far I'm using something that is not very elegant:
Select x.nb / y.nb
from
(select count (*) as nb
from somewhere
where 1=1
and criteria 1 = 'xxx'
and criteria 2 = 'xx'
and criteria 3 = 'x' ) x
JOIN
(select count (*) as nb
from somewhere
where 1=1
and criteria 1 = 'xxx'
and criteria 2 = 'xx' ) y on 1 = 1
Thanks!
You can use avg():
select avg(case when criteria3 = 'x' then 1.0 else 0 end) as ratio
from somewhere
where criteria 1 = 'xxx' and criteria 2 = 'xx' ;
Or more concisely as:
select avg( (criteria3 = 'x')::int ) as ratio
from somewhere
where criteria 1 = 'xxx' and criteria 2 = 'xx' ;

Union different tables with differents columns and data

My problem is that I have 4 differents SELECT with
SELECT COUNT (*) AS regular
WHERE experience = 1 AND bl = 1
SELECT COUNT (*) AS rptmm
WHERE experience = 1 AND bl = 0
SELECT COUNT (*) AS new
WHERE experience = 0 AND bl = 0
SELECT COUNT (*) AS rptss
WHERE experience = 0 AND bl = 1
I want that the results appear together whith the respective names like:
regular rptmm new rptss
10 5 2 6
Firstly, I'd suggest not to use Count()*. There are many answers on this site explaining why so I am not going to repeat it.
Instead, I'd suggest you to use a query like this:
SELECT (SELECT COUNT (tab.someColumnName)
FROM TableName tab
WHERE tab.experience = 1 AND tab.bl = 1) AS 'Regular',
(SELECT COUNT (tab.someColumnName)
FROM TableName tab
WHERE tab.experience = 1 AND tab.bl = 0) AS 'rptmm',
(SELECT COUNT (tab.someColumnName)
FROM TableName tab
WHERE tab.experience = 0 AND tab.bl = 0) AS 'New',
(SELECT COUNT (tab.someColumnName)
FROM TableName tab
WHERE tab.experience = 0 AND tab.bl = 1) AS 'rptss'
Hope this helps!!!
Just put UNION ALL between your four statements you will get four rows with each count on its own row. However, you will lose the column name. You could also use join to get one row with four columnes. Just put the keyword join between each sql statement.
SELECT COUNT (*) AS regular
WHERE experience = 1 AND bl = 1
JOIN
SELECT COUNT (*) AS rptmm
WHERE experience = 1 AND bl = 0
JOIN
SELECT COUNT (*) AS new
WHERE experience = 0 AND bl = 0
JOIN
SELECT COUNT (*) AS rptss
WHERE experience = 0 AND bl = 1
You could create a temp table to hold all of this data for you: Replace Name1, Name2, Name3,Name4 with whatever you want to call them. These will be the column headers.
CREATE TABLE #Temp(
NAME1 INT
,NAME2 INT
,NAME3 INT
,NAME4 INT
)
INSERT INTO #Temp
(NAME1)
SELECT COUNT(*) AS regular
WHERE experience = 1 AND bl = 1
INSERT INTO #Temp
(NAME2)
SELECT COUNT(*) AS regular
WHERE experience = 1 AND bl = 0
INSERT INTO #Temp
(NAME3)
SELECT COUNT(*) AS regular
WHERE experience = 0 AND bl = 0
INSERT INTO #Temp
(NAME4)
SELECT COUNT(*) AS regular
WHERE experience = 0 AND bl = 1*
SELECT * FROM #Temp

SQL Implementing 'IN' operator with 'OR'

I am working on a legacy system which has a custom java implementation for generating SQL queries. That doesn't support 'IN' operation.
To implement 'IN' I have written something like
SELECT * from Q
WHERE IS_HIDDEN = 0 AND ID = 1
OR ID = 2 OR ID = 3 AND IS_DELETED = 0;
I know that the one like below would have been fine.
SELECT * from Q
WHERE IS_HIDDEN = 0 AND (ID = 1
OR ID = 2 OR ID = 3) AND IS_DELETED = 0 ;
Both these return the same result but I'm not too confident about SQL operator priorities. I had read that AND takes precedence
Is it safe to assume that both the SQL statemets are equivalent.
The actual query that I wanted to write is
SELECT * from Q
WHERE IS_HIDDEN = 0 AND ID IN(1, 2, 3) AND IS_DELETED = 0;
The DB in question is oracle 10g.
Update: The reason that this was working is because the oracle CBO rearranges the subclauses in the where clause.
No your queries are not the same
SELECT * from Q
WHERE IS_HIDDEN = 0 AND ID = 1
OR ID = 2 OR ID = 3 AND IS_DELETED = 0;
is like
SELECT * FROM Q WHERE IS_HIDDEN = 0 AND ID = 1
UNION
SELECT * FROM Q WHERE ID = 2
UNION
SELECT * FROM Q WHERE ID = 3 AND IS_DELETED = 0
when you use the parentheses for your ORs then you have the same like the IN-Clause
You can try it: SQLFiddle
You first query is equal to the IN. You should use that:
Your second query is like this:
SELECT * from Q
WHERE (IS_HIDDEN = 0 AND ID = 1) OR ID = 2 OR (ID = 3 AND IS_DELETED = 0);
If IS_HIDDEN is 1 or DELETED Is 1, but ID is 2, your query will still give you records. Try it..

Return different rows for each column in a row

I have data which is presented in multiple rows and columns with 0 or 1 values. What I'm trying to do is create a unique row for each 1, but there are sometimes multiple 1's in a row. For ex:
**A B C D**
1 0 1 1
0 0 0 1
1 1 0 0
I would like to have return six rows, all in one column like so
**RETURN**
A
C
D
D
A
B
Thanks in advance!
You can do this with a union all statement:
select val
from ((select 'A' as val from t where A = 1) union all
(select 'B' from t where B = 1) union all
(select 'C' from t where C = 1) union all
(select 'D' from t where D = 1)
) t
As a note: I hope you have other columns that you can include in the output. SQL tables are, by definition, not ordered. So, you really have no idea in your example of the original source for any given value.

Normalizing Data on a Table

I need help to transform data on a table, reducing a series of columns to one single column. An example follows below:
Frequency_1 integer,
Frequency_2 integer,
Frequency_3 integer,
Frequency_4 integer,
These columns currently hold 1 or 0. Only one column will hold 1.
The new column should be defined as
Frequency integer
And this new column should hold a value between 1 and 4, depending on which of the old columns had its value = 1.
Could you suggest an SQL command to accomplish this?
You could come up with something more complicated if you want, but why not just do this?
SELECT Frequency_1 +
(Frequency_2 * 2) +
(Frequency_3 * 3) +
(Frequency_4 * 4) AS Frequency
To actually make the change, you can create the column first, update the value in the new column, then delete the old columns.
SELECT
CASE WHEN Frequency_1 = 1 THEN 1
WHEN Frequency_2 = 1 THEN 2
WHEN Frequency_3 = 1 THEN 3
WHEN Frequency_4 = 1 THEN 4
ELSE 0 END AS Frequency
FROM TABLE
update table_name
set frequency =
case when frequency_1 = 1 then 1 else
case when frequency_2 = 1 then 2 else
case when frequency_3 = 1 then 3 else
case when frequency_4 = 1 then 4
end
end
end
end
Like this:
select Frequency =
Frequency_1 * 1 +
Frequency_2 * 2 +
Frequency_3 * 3 +
Frequency_4 * 4
from ATable
or this:
select Frequency = case
when Frequency_1 = 1 then 1
when Frequency_2 = 1 then 2
when Frequency_3 = 1 then 3
when Frequency_4 = 1 then 4
else 0
end
from ATable