How to write One query for multiple groupings? - tsql

In the queries below, how can I make just one query that will give me the results, instead of making copies with diff groupings and unioning them?
If possible.
Thanks in advance!!
`create table #temp1 (col1 varchar(50), col2 varchar(50), col3 varchar(50), col4 varchar(50), col5 varchar(50), sumit int)
insert into #temp1 values('AEAMS','CE Europe', 'Belarus', 'Govt', 'Int Fed Gvt', 1)
insert into #temp1 values('AEAMS','CE Europe', 'Belarus', 'Govt', 'Public Lib', 1)
insert into #temp1 values('AEDS','Japan', 'Japan C', 'Acad', 'CollUnive', 1)
insert into #temp1 values('AEDS','Japan', 'Japan F', 'Acad', 'Med', 1)
insert into #temp1 values('A- Regular Databases','UK and Ireland', 'Ireland', 'School', 'HIGH SCHOOL', 1)
Select col1 CC, null GM, null Terr, null Mkt, null Seg, sum(sumit) SS
from #temp1
group by col1
Union
Select col1 CC, col2 GM, null Terr, null Mkt, null Seg, sum(sumit) SS
from #temp1
group by col1, col2
Union
Select col1 CC, col2 GM, col3 Terr, null Mkt, null Seg, sum(sumit) SS
from #temp1
group by col1, col2, col3
Union
Select col1 CC, col2 GM, col3 Terr, col4 Mkt, null Seg, sum(sumit) SS
from #temp1
group by col1, col2, col3, col4, col5

Try using WITH ROLLUP:
Select col1 CC, col2 GM, col3 Terr, col4 Mkt, null Seg, sum(sumit) SS
from #temp1
group by col1, col2, col3, col4, col5
with rollup
SQL Fiddle Example

Related

Union two tables with partially overlapping column and returning rows with non-null column values if exists

I have following table in postgres 11. I am joining following two tables using 'union'
Table1
col1 col2 col3 col4
4894775 NCT00000172 drug galantamine
4894772 NCT00000174 drug rivastigmine
4895618 NCT00000241 drug flupenthixol
Table2
col1 col2 col3 col4 col5 col6
4894775 NCT00000172 drug galantamine galantamine {N06DA}
4895618 NCT00000241 drug flupenthixol flupenthixol {N05AF}
I would like to have the two tables joined such that whenever col5 and col6 are not null then pick those rows else pick the null rows.
I have tried following query so far:
select distinct on (col1, col2, col3, col4, col5)
col1,
col2,
col3,
col4,
coalesce(b.col5, a.col5),
coalesce(b.col6, a.col6),
from (
SELECT
col1,
col2,
col3,
col4,
null as col5,
null as col6
from table1 a
union
select col1,
col2,
col3,
col4,
col5,
col6
from table2 b
order by col1
The desired output is:
col1 col2 col3 col4 col5 col6
4894775 NCT00000172 drug galantamine galantamine {N06DA}
4894772 NCT00000174 drug rivastigmine (null) (null)
4895618 NCT00000241 drug flupenthixol flupenthixol {N05AF}
It looks like you want a left join:
select col1, col2, col3, col4, t2.col5, t2.col6
from table1 t1
left join table2 t2 using(col1, col2, col3, col4)
If there may be "missing" rows in both tables, then you can just change the left join to a full join.

How can I Union two DB2 tables with the same structure except four additional columns in one of the tables?

How can I Union two DB2 tables with the same structure except four additional columns in one of the tables?
I have gone through other similar questions and tried the following options. None of them worked. I am working on DB2.
1)
Select Col1, Col2, Col3, Col4, Col5 from Table1
Union
Select Col1, Col2, Col3, Null as Col4, Null as Col5 from Table2
SQL Error [42703]: NULL IS NOT VALID IN THE CONTEXT WHERE IT IS USED. SQLCODE=-206, SQLSTATE=42703, DRIVER=3.62.56
2)
Select Col1, Col2, Col3, Col4, Col5 from Table1
Union
Select Col1, Col2, Col3, '' as Col4, '' as Col5 from Table2
SQL Error [42825]: THE CORRESPONDING COLUMNS, 17, OF THE OPERANDS OF A SET OPERATOR ARE NOT COMPATIBLE. SQLCODE=-415, SQLSTATE=42825, DRIVER=3.62.56
Please advice.
Try
cast(NULL as varchar(20)) as Col4
Instead of varchar(20) use whatever type the column has in the other table

Incorrect syntax near the keyword 'IF', IF after WITH

I am trying to make an IF after a WITH and it is giving me the error Incorrect syntax near the keyword 'IF'
below an example of the stored procedure I am writing
CREATE PROCEDURE [dbo].[Proc_MyProc]
#MODE INT = null
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
WITH ss as (
select col1, col2, col3, col4, col5
from TableTest
)
IF #MODE = 1
select col1 as A, col2, sum(col5) as col5sum
from ss
group by col1, col2
order by col5sum desc
ELSE IF #MODE = 2
select col1, col2, sum(col5) as col5sum
from ss
group by col1, col2, col3
order by col5sum desc
ELSE IF #MODE = 3
select col1, col2, sum(col5) as col5sum
from ss
group by col1, col2, col4
order by col5sum desc
END
GO
I tried to remove the WITH and the syntax error gone, but off course it is not a solution
Thanks :)
The WITH goes with the SELECT, so you need to repeat the WITH or use a temporary table.
In this case, though, the WITH really has no value -- let me assume that it is simplified.
The first method would be:
if #MODE = 1
with ss as (
select col1, col2, col3, col4, col5
from TableTest
)
select col1 as A, col2, sum(col5) as col3sum
from #ss
group by col1, col2
having SnQuantity > 0
order by availability desc;
. . .
The second method would be:
select col1, col2, col3, col4, col5
into #ss
from TableTest;
if #MODE = 1
select col1 as A, col2, sum(col5) as col3sum
from #ss
group by col1, col2
having SnQuantity > 0
order by availability desc;
. . .

Filtering data by four columns

I have some data in ORACLE in following format-
Now I want to filter this data as follows-
I want to exclude those rows where all col1, col2, col3, col4 are 'N'
I tried following so that I can get columns where all col1, col2, col3, col4 are not 'N'
Select * from Table1
Where (col1 != ‘N’ and col2 != ‘N’ and col3 != ‘N’ and col4 != ‘N’)
But it is not working.
What extra condition do I need to include here to get the desired result.
try something like
Select * from Table1
Where not (col1 = ‘N’ and col2 = ‘N’ and col3 = ‘N’ and col4 = ‘N’)
or
Select * from Table1
Where (col1 != ‘N’ or col2 != ‘N’ or col3 != ‘N’ or col4 != ‘N’)

How to write this T-SQL WHERE condition?

I've got two tables:
TableA
Col1
Col2
TableB
Col3
Col4
I want to join them together:
SELECT * from TableA join TableB ON (...)
Now, in place of ... I need to write an expression that evaluates to:
If Col3 is not null, then true iif Col1==Col3; otherwise
If Col3 is null, then true iif Col2==Col4
What would be the most elegant way to do this?
ON (Col1=Col3 OR (Col3 IS NULL AND Col2=Col4))
should do the trick (if Col3 is null, Col1=Col3 cannot evalutate to TRUE)
Try this:
SELECT *
FROM TableA
JOIN TableB
ON Col1 = Col3
OR (Col3 IS NULL AND Col2 = Col4)