Finding records in another table - sql-server

I wanted to show that a record in one table exists in another table and used the code below. Would this be possible?
CASE WHEN EXISTS (
SELECT *
FROM tblA a
INNER JOIN tblB b ON a.AccountID = b.AccountID
) THEN 'YES' ELSE 'NO' END

As Dave Gugg mentioned in his comment that is possible. The alternative is using a LEFT JOIN and for those rows that are not matched in table B display NO, for those that match display YES:
SELECT a.*, CASE WHEN b.AccountID IS NOT NULL THEN 'YES' ELSE 'NO' END as YesNoCol
FROM tblA a
LEFT JOIN tblB b ON a.AccountID = b.AccountID

Nope. Do something like this instead:
SELECT CASE WHEN (SELECT COUNT(*)
FROM tblA AS A
INNER JOIN tblB AS B
ON A.AccountID = B.AccountID) > 1
THEN 'YES' ELSE 'NO' END AS T;

Related

SQL multi-part identifier can't be bound in SELECT

Is it possible to add a case using a column from other tables than first in the from section?
I can't use anything like C.code or Y.anything in the SELECT part:
SELECT
fromTableA, fromTableA, fromTableA,
CASE
WHEN fromTableA = anyValue THEN 'is_ok'
WHEN B.fromTableB = anyValue THEN 'couldnt.be.bound'
WHEN fromTableB = anyValue THEN 'invalid.column.name'
END AS X
FROM
(SELECT fromTableA,
SUM(fromTableA),
CASE WHEN A.fromTableA = 'anything' THEN 'still ok'
WHEN C.fromTableC = 'allowed' THEN 'no problem'
FROM tableA A
JOIN tableB B ON A.id = B.id
JOIN tableC C ON C.id = B.id
Having SUM(fromTableA) > 0
) AS Y
Edit: What I need is to use columns from table B or C in the outer Select ( I just can't remove the inner select because i would lost cases and aggregation operations there are in the inner select).
The following should be all you need, a derived table here doesn't accomplish anything.
select A.Cols...,
case
when A.col1 = anyValue then 'is_ok'
when B.Col1 = anyValue then 'couldnt.be.bound'
when B.Col2 = anyValue then 'invalid.column.name'
end as X
from tableA A
join tableB B on A.id = B.id
join tableC C on C.id = B.id;

Why is my query inside of a case when failing?

I'm trying to run a SQL Server Case query and I'm running into an error. Let me try to explain why I'm doing what I'm doing, as well as what I'm looking for in the end result.
I have two Tables. 1 that has an opportunity ID (in this case A.ID) and the second table has accounts with one column I'm interested in (B.Q3ABM__C). What I'm asking SQL to do in this case when is that for every A.id that is null in in the B.Q3ABM__C colum give me a 1, and then again vice versa.
Why is this failing?
SELECT B.ID as Account_ID, B.FULL_ACCOUNT_ID__C, A.ID as Opportunity_ID,A.name, stagename,closedate,A.createddate,
OPPORTUNITY_PRODUCT__C,AMOUNT, B.Q3ABM__C, B.Drip_Campaign_Code__c,OPPORTUNITYTYPE_STRING__C,
CASE
WHEN (SELECT A.id FROM SF_OPPORTUNITY as a
LEFT JOIN SF_ACCOUNT as b on A.Accountid = b.ID where B.Q3ABM__C IS NULL) THEN 1
ELSE 0
END AS 'Does Not Exist in Q3DM',
CASE
WHEN (SELECT A.id FROM SF_OPPORTUNITY as a
LEFT JOIN SF_ACCOUNT as b on A.Accountid = b.ID where B.Q3ABM__C IS NOT NULL) THEN 1
ELSE 0
END AS 'Exists in Q3DM'
FROM SF_OPPORTUNITY as a
LEFT JOIN SF_ACCOUNT as b on a.ACCOUNTID = b.ID
WHERE A.createddate >= '7-1-2019'
AND CLOSEDATE <= '12-31-2019'
Thanks!
If you are selecting records in specific period, but you want to check for NULLs and NOT NULLs for all records, you can check for which ID there are NULL values. If NULL vales exists, then we can calculate the first Does Not Exist in Q3DM column, if not exists the second Exists in Q3DM:
WITH DataSource AS
(
SELECT DISTINCT A.id
FROM SF_OPPORTUNITY as a
LEFT JOIN SF_ACCOUNT as b
on A.Accountid = b.ID
where B.Q3ABM__C IS NULL
)
SELECT B.ID as Account_ID
,B.FULL_ACCOUNT_ID__C
,A.ID as Opportunity_ID
,A.name
,stagename
,closedate
,A.createddate
,OPPORTUNITY_PRODUCT__C
,AMOUNT
,B.Q3ABM__C
,B.Drip_Campaign_Code__c
,OPPORTUNITYTYPE_STRING__C
,CASE DS.Id IS NOT NULL THEN 1 ELSE 0 END 'Does Not Exist in Q3DM'
,CASE DS.Id IS NULL THEN 1 ELSE 0 END 'Exists in Q3DM'
FROM SF_OPPORTUNITY as a
LEFT JOIN SF_ACCOUNT as b
on a.ACCOUNTID = b.ID
LEFT JOIN DataSource DS
ON A.ACCOUNTID = DS.ID
WHERE A.createddate >= '7-1-2019'
AND CLOSEDATE <= '12-31-2019';
If you are interested only in the for the given period and checking the value for the current record only:
SELECT B.ID as Account_ID
,B.FULL_ACCOUNT_ID__C
,A.ID as Opportunity_ID
,A.name
,stagename
,closedate
,A.createddate
,OPPORTUNITY_PRODUCT__C
,AMOUNT
,B.Q3ABM__C
,B.Drip_Campaign_Code__c
,OPPORTUNITYTYPE_STRING__C
,CASE DS.Id IS NULL THEN 1 ELSE 0 END 'Does Not Exist in Q3DM'
,CASE DS.Id IS NOT NULL THEN 1 ELSE 0 END 'Exists in Q3DM'
FROM SF_OPPORTUNITY as a
LEFT JOIN SF_ACCOUNT as b
on a.ACCOUNTID = b.ID
LEFT JOIN DataSource DS
ON A.ACCOUNTID = DS.ID
WHERE A.createddate >= '7-1-2019'
AND CLOSEDATE <= '12-31-2019'
Try this (Use column in 'Select' and 'Where' clause as per your requirements):
SELECT A.*,B.* ,
CASE WHEN B.Q3ABM__C IS NULL THEN 1 ELSE 0 END AS 'DOES NOT EXISIT IN Q3DM',
CASE WHEN B.Q3ABM__C IS NOT NULL THEN 1 ELSE 0 END AS 'EXISITS IN Q3DM'
FROM PLS.SF_OPPORTUNITY A
FULL JOIN PLS.SF_ACCOUNT B ON A.ACCOUNTID=B.ID

SQL Server : left join - check for right table column value is null

I have a query as follows:
select --this select should always give me 1 record
tbl1.Id, tbl1.Name, tbl1.Address, tbl2.relNo,
CASE WHEN tbl3.Comments IS NOT NULL THEN 1 ELSE 0 END AS 'Required'
from
table1 tbl1
inner join
table2 tbl2 on tbl2.Id = tbl1.Id
left join -- This left join table gives me 5 records for one instance
(select
R.Id, C.Comments
from
tblC C
inner join
tblR R on R.Id = C.id) tbl3 on tbl3.Id = tbl2.Id
I want to write a CASE statement on the rows my left join is giving to check for null value as above and my final select query always return only 1 row. Is there a way to check if all five Comments Column values from my left join be checked for NULLs in the above query?
I would take a shortcut an use a COUNT() OVER PARTITION
CASE WHEN COUNT(*) OVER (PARTITION BY tbl3.Id) =0 THEN 0 ELSE 1 END AS 'Required'
You would have to DISTINCT your output above. Another option would be to GROUP BY and filter in the HAVING clause.
select --this select should always give me 1 record
tbl1.Id, tbl1.Name, tbl1.Address, tbl2.relNo
From table1 tbl1
inner join table2 tbl2 on tbl2.Id = tbl1.Id
left join (-- This left join table gives me 5 records for one instance
SELECT R.Id,
C.Comments
FROM tblC C
INNER JOIN tblR R on R.Id = C.id
) tbl3 on tbl3.Id = tbl2.Id
GROUP BY
Id, Name, Address, relNo
HAVING
COUNT(*) = 5
Is this what you're looking for?
(CASE WHEN (select count(tbl3.id) FROM tbl3 WHERE tbl3.Comments IS NULL) then 1 else 0 end) as 'RequiredVal'
select --this select should always give me 1 record
tbl1.Id, tbl1.Name, tbl1.Address, tbl2.relNo,
CASE WHEN tbl3.Comments IS NOT NULL THEN 1 ELSE 0 END AS 'Required'
, (CASE WHEN (select count(tbl3.id) FROM tbl3 WHERE tbl3.Comments IS NULL) then 1 else 0 end) as 'RequiredVal'
From table1 tbl1
inner join table2 tbl2 on tbl2.Id = tbl1.Id
left join (-- This left join table gives me 5 records for one instance
SELECT R.Id,
C.Comments
FROM tblC C
INNER JOIN tblR R on R.Id = C.id
) tbl3 on tbl3.Id = tbl2.Id

How to compare data any field of two table in SQL Server?

I have two tables with same columns.
Table A have ID, Name, Des, Status and
Table B have ID, Name, Des, Status.
I want to compare data any field of Table B with Table A, except column ID because same.
As same picture above, when FETCH data of Table B, detect data of ID ID001 and ID003 not same, idea of my mind same
IF (SELECT COUNT (SELECT * FROM TABLE A RIGHT JOIN TABLE B ON A.ID = B.ID) != 0)
BEGIN
PRINT 'BLAH BLAH, NOT SAME'
END
If you have idea or solution, share for me, Thank you so much.
You can use CHECKSUM or BINARY_CHECKSUM:
SELECT a.*, b.*
FROM TableA a INNER JOIN TableB b ON b.ID = a.ID
WHERE CHECKSUM(b.Name, b.Des, b.Status) <> CHECKSUM(a.Name, a.Des, a.Status)
See also this link. It should be faster then multiple OR conditions.
IF (SELECT COUNT(*)
FROM TableA a INNER JOIN TableB b ON b.ID = a.ID
WHERE BINARY_CHECKSUM(b.Name, b.Des, b.Status)
<> BINARY_CHECKSUM(a.Name, a.Des, a.Status)
>0
PRINT 'Not the same.'
Since it is not too much clear how do you want to show your differences, this is one approach:
SELECT A.ID,
(CASE WHEN A.Name <> B.Name THEN 'Diff Name' ELSE '') NameCompare,
(CASE WHEN A.Des <> B.Des THEN 'Diff Des' ELSE '') DESCompare,
(CASE WHEN A.Status <> B.Status THEN 'Diff Status' ELSE '') StatusCompare
FROM A
INNER JOIN B
ON A.ID = B.ID
This is a simple join
To get all different rows, you can say:
select a.*, b.*
from TableA a inner join TableB b on b.ID = a.ID
where b.Name <> a.Name or b.Des <> a.Des or b.Status <> a.Status

How to display if a field exists in other table?

I want to show/display if the product is found each transaction.
tblProducts
ID PRODCODE PRODDESC
1 PFX-321 MILK CHOCO
2 PDF-875 COFFEE JELLY
3 PWA-718 MILK SHAKE
tblTransactions
TCODE PRODCODE
BMX2213391 PFX-321
BMX2213391 PDF-875
PDFSD92851 PDF-875
I want the results to display like this
TCODE PRODCODE FOUND
BMX2213391 PFX-321 YES
BMX2213391 PDF-875 YES
BMX2213391 PWA-718 NO
PDFSD92851 PFX-321 NO
PDFSD92851 PDF-875 YES
PDFSD92851 PWA-718 NO
I tried, INNER JOIN, FULL OUTER JOIN, LEFT JOIN and RIGHT JOIN but I don't get the exact data I need.
Here are the queries I test.
SELECT * FROM tblProducts a INNER JOIN tblTransactions b ON a.PRODCODE = b.PRODCODE
SELECT * FROM tblProducts a FULL OUTER JOIN tblTransactions b ON a.PRODCODE = b.PRODCODE
SELECT * FROM tblProducts a LEFT JOIN tblTransactions b ON a.PRODCODE = b.PRODCODE
SELECT * FROM tblProducts a RIGHT JOIN tblTransactions b ON a.PRODCODE = b.PRODCODE
I'm pretty sure this works - SQLFiddle here: http://sqlfiddle.com/#!3/65eb1/23
WITH AllVals AS
(SELECT a.PRODCODE, b.TCODE
FROM tblProducts a
CROSS JOIN tblTransactions b)
SELECT DISTINCT c.PRODCODE,
c.TCODE,
CASE WHEN d.PRODCODE IS NULL THEN 'NO' ELSE 'YES' END AS FOUND
FROM AllVals c
LEFT OUTER JOIN tblTransactions d
ON c.PRODCODE = d.PRODCODE
AND c.TCODE = d.TCODE
http://sqlfiddle.com/#!3/65eb1/24
select DT.TCODE, DT.PRODCODE, case when (Tr2.TCODE IS null and Tr2.PRODCODE IS null) then 'No' else 'Yes' END as FOUND
from tblTransactions Tr2 right join
(
select distinct Tr.TCODE, p.PRODCODE
from tblProducts p cross join tblTransactions Tr
) DT
on DT.PRODCODE = Tr2.PRODCODE and DT.TCODE = Tr2.TCODE;

Resources