Updating the SQL query from Count(*) to EXISTS in SQL Server - sql

I have the following SQL query,
CASE
WHEN (SELECT COUNT(*)
FROM MyTable AS Parameter
INNER JOIN Table ON Parameter.Attribute1 = Table.Attribute2
WHERE FD.DefID= Parameter.DefID AND Parameter.VTypeID = 1) = 0
THEN (
SELECT * from Table2)
ELSE
NULL
END AS Items
Basically, I would like to ensure that conditional execution is only if the query result count is 0.
How should I modify it to use EXISTS/NOT EXISTS keyword?

You can use exists to do that like so:
SELECT * from Table2
WHERE NOT EXISTS(
SELECT 1
FROM MyTable AS Parameter
INNER JOIN Table ON Parameter.Attribute1 = Table.Attribute2
WHERE FD.DefID= Parameter.DefID AND Parameter.VTypeID = 1)

Related

Multiple columns return in CASE Statement SQL

I have two tables and based on the sum of a field in TABLE1 I have to return different datasets from TABLE2:
I am trying to achieve this through a Case statement but getting an error saying subselect must have only one field.
Is there a better way to do this? simply when the sum of a column in table1 is 0 do not select anything from table2
TABLE1:
TABLE2:
MY SQL:
SELECT
CASE
WHEN SUM(transaction_unit_failed) > 0
THEN (
SELECT sale_event_nr, business_unit, transaction_nr, transaction_unit_failed_number
FROM TABLE2
)
WHEN SUM(transaction_unit_failed) = 0
THEN (
SELECT sale_event_nr, business_unit, transaction_nr, transaction_unit_failed_number
FROM TABLE2
WHERE 1 = 2
)
FROM TABLE1
select * from table2
where exists (
select 1
from table1
having sum(transaction_unit_failed) > 0
);
Similarly:
select * from table2
where (
select sum(transaction_unit_failed)
from table1
) > 0;
https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=3f68d250bc9a3235767b86626092799e
You could certainly write it as a join if there were a compelling reason. It would eliminate the convenience of nicely using * to return only the columns from the one table.
select *
from table2 inner join (
select sum(transaction_unit_failed) as stuf
from table1
) on stuf > 0;
SELECT sale_event_nr, business_unit, transaction_nr, transaction_unit_failed_number
FROM TABLE2
WHERE (SELECT SUM(transaction_unit_failed) > 0
FROM TABLE1)

Select statement in Case statement in Oracle

SELECT (CASE WHEN T.ID = ( SELECT cte.REFERENCE FROM trans cte WHERE T.ID
= CTE.PARENT_ID) THEN cte.REFERENCE ELSE null END) AS name
FROM trans T
Example: I am picking one transaction value as an example. In trans table whose ID=1 then in the same table I need to look for PARENT_ID=1.
when I look for parent_ID=1 then it's ID value will be different.
This is not ID=Parent_ID.
once I look for parent_ID=1 then print its corresponding reference value as name.
I tried above sql statement in oracle, but it didn't work. Could you please let me know, how to write this statement in case statement.
Instead of a subquery, why not try a self-join?
SELECT CASE
WHEN nvl(t1.id,-1) = nvl(t2.reference, -1) THEN t2.reference
ELSE 1
END AS number_col
FROM trans t LEFT JOIN trans t2 ON (t.id = t2.parent_id);
You can also try it as a subquery without a case statement
SELECT t.id,
NVL ((SELECT t2.reference
FROM trans t2
WHERE t.id = t2.parent_id AND t.id = t2.reference AND ROWNUM = 1),
1) AS number_val
FROM trans t

SQL Select where condition : value 1 <> value 2

Need your help to know if possible to select values from a table with the below condition :
Table content : matching between 2 objects
(Id_obj_A; name_obj_A; country_obj_A; Id_obj_B; name_obj_B; country_obj_B)
Select *
from table
Where (only if country_obj_A <> country_obj_B)
Many thanks for your help
Yes. There are a few ways, one is to use NOT EXISTS like this:
select
*
from tableA
where NOT EXISTS (
select NULL
from tableB
where tableB.country_obj_B = tableA.country_obj_A
)
or, using NOT IN
select
*
from tableA
where country_obj_A NOT IN (
select country_obj_B
from tableB
)
or, using a LEFT JOIN then exclude the joined rows:
select
*
from tableA
left join tableB on tableA.country_obj_A = tableB.country_obj_B
where tableB.country_obj_B IS NULL

How to only return non-empty results for multi-part query

I have a query with the following structure:
DECLARE #VARIABLE as varchar(30)
SET #VARIABLE = 'Thing to look up'
select * from ref.Table1 where columnX = #VARIABLE
select * from ref.Table2 where columnX2 = #VARIABLE
...
select * from ref.TableN where columnXn = #VARIABLE
I use it to check many reference tables that all have different columns, but use the same Identifier that I'm looking for with #VARIABLE.
There are 50+ tables I'm checking. I only want to return results when there is a match with #VARIABLE in that specific table.
I'm not a developer, but I'm sure there is a more 'programmatic' way of doing this. What is a more recommended way?
edit:
To be clear, my current query works in the SQL Server 2012 UI, where I do my day-to-day work. I run the query, and I get about 50 tables returned. Most have nothing matched, so I only see the header. I would like to only return the tables where there is a match.
Ok it seems like this is an ad-hoc query that you run in some front end like query analyzer or visual studio.
You can use an if statement and if exists, like this:
IF EXISTS(select * from ref.Table1 where columnX = #VARIABLE)
select * from ref.Table1 where columnX = #VARIABLE
;
To only run the query once to speed it up do this:
select * from ref.Table1 INTO #mytmp1 where columnX = #VARIABLE
IF EXISTS(SELECT * FROM #mytmp1)
SELECT * FROM #mytmp1
;
As #LaughingVergil points out you could have an issue if your id is not in the first table you can solve that like this:
select Table1.*, Table2.*, Table3.*, TableN.*
from (
SELECT #VARIABLE as Criteria
) base
left join ref.Table1 where columnX = base.Criteria
left join ref.Table2 where columnX2 = base.Criteria
left join ref.Table3 where columnX3 = base.Criteria
-- ...
left join ref.TableN where columnXN = base.Criteria
to "discern" what table data is coming from
select
Table1.Field1 as Table1.Field1, Table1.Field2 as Table1.Field2, -- etc
Table2.Field1 as Table2.Field1, Table2.Field2 as Table2.Field2, -- etc
Table3.Field1 as Table3.Field1, Table3.Field2 as Table3.Field2, -- etc
TableN.Field1 as TableN.Field1, TableN.Field2 as TableN.Field2 -- etc
from (
SELECT #VARIABLE as Criteria
) base
left join ref.Table1 where columnX = base.Criteria
left join ref.Table2 where columnX2 = base.Criteria
left join ref.Table3 where columnX3 = base.Criteria
-- ...
left join ref.TableN where columnXN = base.Criteria
original answer
Typically in SQL we use JOINs for this kind of situation (you will probably need to be more specific about columns unless you don't care about multiple columns with the same name)
DECLARE #VARIABLE as varchar(30)
SET #VARIABLE = 'Thing to look up'
select Table1.*, Table2.*, Table3.*, TableN.*
from ref.Table1
left join ref.Table2 where columnX2 = columnX
left join ref.Table3 where columnX3 = columnX
-- ...
left join ref.TableN where columnXN = columnX
where columnX = #VARIABLE
Often a view is used to make it easier (here you have to be specific about column names or the create view will fail).
create view myview as
select Table1.*, Table2.*, Table3.*, TableN.*
from ref.Table1
left join ref.Table2 where columnX2 = columnX
left join ref.Table3 where columnX3 = columnX
-- ...
left join ref.TableN where columnXN = columnX
then
select *
from myview
where columnX = #VARIABLE
Your basic query form should be - If you want the tables the reference is in:
SELECT 'Table1', * FROM Table1 WHERE columnx = #VARIABLE
UNION
SELECT 'Table2', * FROM Table2 WHERE columnx2 = #VARIABLE
UNION ...
SELECT 'TableN', * FROM TableN WHERE columnxN = #VARIABLE
If you want to know whether it exists in any of the tables:
SELECT * FROM Table1 WHERE columnX = #VARIABLE
UNION
SELECT * FROM Table2 WHERE columnX2 = #VARIABLE
UNION ...
SELECT * FROM TableN WHERE columnXN = #VARIABLE
NOTE: UNION will de-duplicate rows. If you want to include duplicate rows, then change the UNION to UNION ALL

How to do a Select in a Select

I have a table containing a unique ID field. Another field (REF) contains a reference to another dataset's ID field.
Now I have to select all datasets where REF points to a dataset that doesn't exist.
SELECT * FROM table WHERE ("no dataset with ID=REF exists")
How can I do this?
3 ways
SELECT * FROM YourTable y WHERE NOT EXISTS
(SELECT * FROM OtherTable o WHERE y.Ref = o.Ref)
SELECT * FROM YourTable WHERE Ref NOT IN
(SELECT Ref FROM OtherTable WHERE Ref IS NOT NULL)
SELECT y.* FROM YourTable y
LEFT OUTER JOIN OtherTable o ON y.Ref = o.Ref
WHERE o.Ref IS NULL
See also Five ways to return all rows from one table which are not in another table
Try this:
SELECT * FROM TABLE WHERE NOT EXISTS
(SELECT * FROM OtherTable WHERE TABLE.Ref = OtherTable.ID)
I think this should work
SELECT * FROM table WHERE id NOT IN (SELECT ref_id FROM ref_table)
or with JOIN
SELECT table.*
FROM table LEFT JOIN ref_table ON table.id = ref_table.ref_id
WHERE ref_table.ref_id IS NULL
SELECT
table1.*
FROM
table1
LEFT JOIN table2 ON table1.id = table2.ref
WHERE
table2.ref IS NULL
You can do a subquery like:
select * from table where somefield not in (select otherfield from sometable where ID=REF)
SELECT *
FROM table
WHERE ((SELECT COUNT(*) FROM table2 WHERE table2.id = table.ref) = 0)
Something like that :
SELECT * FROM table WHERE ID NOT IN(SELECT REF FROM Table2 )
Yes you can use
select * from x where not exist ( select * from y )