Filtering data by four columns - select

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’)

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.

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;
. . .

How to write One query for multiple groupings?

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

Multiply combination of columns based on a column value

How to multiply values of the columns based on the value in one column.
Example
Col1 Col2 Col3 Col4
10 10 10 casea (multiply col1 * col2)
20 20 20 caseb (multiply col1 * col3)
30 30 30 casec (multiply col2 * col3)
A query like:
select col1, col2, col3, col4, total
would return
10,10,10,casea,100
20,20,20,caseb,400
30,30,30,casec,900
Of course performance is an important issue as always.
Thanks for participating.
select
Col1,
Col2,
Col3,
Col4,
CASE Col4
WHEN 'casea' then col1*col2
WHEN 'caseb' then col1*col3
WHEN 'casec' then col2*col3
END AS Total
FROM YourTable

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)