SQL select statement in where clause - sql

Hi there I am trying to execute a query but cannot seem to get it right.
SELECT *
FROM table
WHERE id IN (SELECT *
FROM table
WHERE description = 'A')
AND description = 'B'
Above is the query that I have got, the select * from table where description = A works as expected when ran alone I just need to make the where clause to work so I can see any id that has a description of A and B.

You will be getting multiple columns from the sub query when I assume you only want the id column:
SELECT *
FROM table
WHERE id IN (SELECT id
FROM table
WHERE description = 'A')
AND description = 'B'

No need for the select in the where clause
SELECT *
FROM table
WHERE id IN ('A', 'B')

Try this:
SELECT *
FROM table
WHERE description IN ('A', 'B')

it should be:
select * from table where id in (select id from table where description = 'A') and description = 'B'
but this query will give you zero result as you select records with description = 'A' and description = 'B', if you want to get records with either description of A or B, then you should write as
select * from table where description = 'A' or description = 'B'
or
select * from table where description in ('A','B')

SELECT distinct AnaTablo.Id , AnaTablo.FirmaAdi , AnaTablo.FirmaId , AnaTablo.KayitTarihi ,
users.Email Personel, (SELECT top 1 sabitler.Ayar from tblSabitAyarlar sabitler WHERE sabitler.Tur = 29 and sabitler.Deger in
(SELECT top 1 IslemId from tblEFaturaTakipIslem Islem WHERE AnaTablo.Id = Islem.EFaturaTakipId order by KayitTarihi desc))YapilanIslem,
AnaTablo.Eposta , AnaTablo.Aciklama
from tblEFaturaTakip AnaTablo left join AspNetUsers users on AnaTablo.PersonelId = users.Id

Related

Does Switch(Select) Works between TRANSFORM And SELECT?

I am trying to add a Switch between a Transform and a Select But this throw the following error.
Multi-level GROUP BY clause not allowed in subquery
Below is a sample of my SQL Query
TRANSFORM SUM(Rate*
SWITCH(Name = 'Name1',(SELECT sum(MktValue)/(select sum(MktValue) FROM Table2 WHERE Portefeuille IN ( 'Name1' , 'Name2') AND LaDate = #27/10/2021#) FROM Table2 WHERE Name IN ( 'Name1') AND LaDate = #27/10/2021#),Name = 'Name2',0.75,true,0)
SELECT Sector
FROM Table1
WHERE Name IN ( 'Name1' , 'Name2') AND LaDate = #27/10/2021#"
GROUP BY Sector
Pivot Name
What am I doing wrong ?
Please note that Switch() is used to calculate factor. ( MktValue / Sum(AllMktValue))
The below query works
TRANSFORM SUM(Rate * 0.33)
SELECT Sector
FROM Table1
WHERE Name IN ( 'Name1' , 'Name2') AND LaDate = #27/10/2021#"
GROUP BY Sector
Pivot Name
Since the above query is working fine I assume my issue come from the SWITCH
Try with:
TRANSFORM SUM(Rate * SWITCH(
Name = 'Name1',
(SELECT Sum(MktValue) FROM Table2 WHERE Name IN ('Name1') AND LaDate = #2021/10/27#) /
(SELECT Sum(MktValue) FROM Table2 WHERE Portefeuille IN ('Name1','Name2') AND LaDate = #2021/10/27#),
Name = 'Name2', 0.75,
True, 0)

How do i create a DB2 UNION query using variables from a list

So i have a union query like:
select count(id)
from table 1
where membernumber = 'x'
and castnumber = 'y'
union
select count(id)
from table 1
where membernumber = 'x'
and castnumber = 'y'
union
etc...
There will be over 200 unions coming from a list 2x 200 table with values for x and y in each row. So each union query has to get the value of x and y from the corresponding row (not in any particular order).
How can i achieve that ?
Thanks
Try this:
DECLARE GLOBAL TEMPORARY TABLE
SESSION.PARAMETERS
(
MEMBERNUMBER INT
, CASTNUMBER INT
) DEFINITION ONLY WITH REPLACE
ON COMMIT PRESERVE ROWS NOT LOGGED;
-- Insert all the the constants in your application with
INSERT INTO SESSION.PARAMETERS
(MEMBERNUMBER, CASTNUMBER)
VALUES (?, ?);
-- I don't know the meaning of the result you want to get
-- but it's equivalent
select distinct count(t.id)
from table1 t
join session.parameters p
on p.membernumber = t.membernumber
and p.castnumber = t.castnumber
group by t.membernumber, t.castnumber;

NULL id in SQL statement

Select *
from tbl
where id = '1fa3bcdc9a1cf60f02a2ae774e2cf166'
or matching_id = 'ea74c270-65fd-46d0-898b-faf1a7bf7e16'
My id column below comes back with a NULL for one of my events. They are related using the matching_id key. Is there a way I can write a case statement to populate that id field so its not NULL?
You can use coalesce() and in case of null id fetch a value (any value?) for the id matching the matching_id like this:
Select
coalesce(
id,
(select max(id) from tbl where matching_id = 'ea74c270-65fd-46d0-898b-faf1a7bf7e16')
) as id,
matching_id,
event_name
from tbl
where
id = '1fa3bcdc9a1cf60f02a2ae774e2cf166'
or
matching_id = 'ea74c270-65fd-46d0-898b-faf1a7bf7e16'
Edit.
Try this in case of unknown values:
Select
coalesce(
t.id,
(select max(id) from tbl where matching_id = t.matching_id)
) as id,
coalesce(
t.matching_id,
(select max(matching_id) from tbl where id = t.id)
) as matching_id,
t.event_name
from tbl t
Ended up writing something like this..
[snapshot 1
with find_event AS (
Select tbl1. id, tbl1.matching_id
from tbl1
where tbl1.id IS NOT NULL
)
Select
CASE WHEN find_event. id IS NOT NULL
THEN find_event. id
WHEN find_event.id IS NULL
THEN tbl2. id
ELSE tbl2. id
END as final_id,
tbl2.matching_id,
tbl2. id,
tbl2.event_name,
find _event. id as find_canonical_id
from tbl2
left JOIN find_event on find_event.matching_id = tbl2.matching_id
where
tbl2.transaction_canonical_id = '1fa3bcdc9a1cf60f02a2ae774e2cf166'
or
tbl2.matching_id = 'ea74c270-65fd-46d0-898b-faf1a7bf7e16'
If you want to replace the column values with the values from your parameters, you could use something like this:
with ids (id, mid) as (
values
('1fa3bcdc9a1cf60f02a2ae774e2cf166', 'ea74c270-65fd-46d0-898b-faf1a7bf7e16')
)
Select coalesce(tbl.id, ids.id),
tbl.matching_id,
tbl.event_name
from tbl
join ids on ids.id = tbl.id or ids.mid = tbl.matching_id;
You may be able to utilize the ISNULL() function for this. Maybe something like:
Select
ISNULL(id, matching_id) AS [myid],
matching_id,
event_name
from tbl
where id = '1fa3bcdc9a1cf60f02a2ae774e2cf166'
or matching_id = 'ea74c270-65fd-46d0-898b-faf1a7bf7e16'

select specific records using IN

I need to select records that has ID = 10,23,30 so I wrote this SQL
Select * from mytable where position(id in '10,23,30') > 0
But the problem I get additional records where ID = 1 and 2
Any ideas how to select only what I need ?
No need for position, just do IN:
Select * from mytable
where id in (10,23,30)
Use IN operator:
Select * from mytable where id in (10,23,30)

return a default record from a sql query

I have a sql query that I run against a sql server database eg.
SELECT * FROM MyTable WHERE Id = 2
This may return a number of records or may return none. If it returns none, I would like to alter my sql query to return a default record, is this possible and if so, how? If records are returned, the default record should not be returned. I cannot update the data so will need to alter the sql query for this.
Another way (you would get an empty initial rowset returned);
SELECT * FROM MyTable WHERE Id = 2
IF (##ROWCOUNT = 0)
SELECT ...
SELECT TOP 1 * FROM (
SELECT ID,1 as Flag FROM MyTable WHERE Id = 2
UNION ALL
SELECT 1,2
) qry
ORDER BY qry.Flag ASC
You can have a look to this post. It is similar to what you are asking
Return a value if no rows are found SQL
I hope that it can guide you to the correct path.
if not exists (SELECT top 1 * FROM mytable WHERE id = 2)
select * from mytable where id= 'whatever_the_default_id_is'
else
select * from mytable where id = 2
If you have to return whole rows of data (and not just a single column) and you have to create a single SQL query then do this:
Left join actual table to defaults single-row table
select
coalesce(a.col1, d.col1) as col1,
coalesce(a.col2, d.col2) as col2,
...
from (
-- your defaults record
select
default1 as col1,
default2 as col2,
...) as d
left join actual as a
on ((1 = 1) /* or any actual table "where" conditions */)
The query need to return the same number of fields, so you shouldn't do a SELECT * FROM but a SELECT value FROM if you want to return a default value.
With that in mind
SELECT value FROM MyTable WHERE Id = 2
UNION
SELECT CASE (SELECT count(*) FROM MyTable WHERE Id = 2)
WHEN 0 THEN 'defaultvalue'
END