replace NOT IN with LEFT JOIN - sql

I've got a table structure like this
[condition]
condition_id, (pk)
question_id (fk)
[option]
condition_id, (fk)
ext_id (fk)
[external]
ext_id, (pk)
inst_id (fk)
[instance]
inst_id, (pk)
keeper_id (fk)
[keeper]
keeper_id, (pk)
org_id
[question]
question_id (pk)
org_id
[localization]
question_id (fk, pk),
org_id (pk),
language (pk)
label
I need to get all questions from question table with localizations (from localization table), that do not exist in condition table for certain ext_id.
My query is
SELECT
q.question_id as q_id,
l.label as q_value
FROM question q
INNER JOIN localization l
ON l.question_id = q.question_id
INNER JOIN external ex
ON ex.ext_id = 'EXTERNAL_ID'
INNER JOIN instance i
ON i.inst_id = ex.inst_id
INNER JOIN keeper k
ON k.keeper_id = i.keeper_id
WHERE q.org_id IN ('*', k.org_id)
AND l.org_id = '*'
AND l.language = 'EN'
AND q.question_id NOT IN (
SELECT
question_id
FROM condition c
INNER JOIN option o
ON o.condition_id = c.condition_id
WHERE o.ext_id = 'EXTERNAL_ID'
)
But how to replace that subquery with LEFT JOIN?

Here is one way
SELECT p.question_id AS q_id,
l.label AS q_value
FROM question q
INNER JOIN localization l
ON l.question_id = q.question_id
INNER JOIN EXTERNAL ex
ON ex.ext_id = 'EXTERNAL_ID'
INNER JOIN instance i
ON i.inst_id = ex.inst_id
INNER JOIN keeper k
ON k.keeper_id = i.keeper_id
LEFT JOIN (select distinct c.question_id
from condition c
JOIN option o
ON o.condition_id = c.condition_id
AND o.ext_id = 'EXTERNAL_ID' ) c
ON c.question_id = q.question_id
WHERE q.org_id IN ( '*', k.org_id )
AND l.org_id = '*'
AND l.language = 'EN'
AND c.question_id IS NULL
I am not a expert in Postgres but I prefer NOT EXISTS to do this
SELECT p.question_id AS q_id,
l.label AS q_value
FROM question q
INNER JOIN localization l
ON l.question_id = q.question_id
INNER JOIN EXTERNAL ex
ON ex.ext_id = 'EXTERNAL_ID'
INNER JOIN instance i
ON i.inst_id = ex.inst_id
INNER JOIN keeper k
ON k.keeper_id = i.keeper_id
WHERE q.org_id IN ( '*', k.org_id )
AND l.org_id = '*'
AND l.language = 'EN'
AND NOT EXISTS (SELECT 1
FROM condition c
INNER JOIN option o
ON o.condition_id = c.condition_id
WHERE o.ext_id = 'EXTERNAL_ID'
AND q.question_id = c.question_id)

Related

SQL Filtering rows with no duplicate value

Hi so I'm new to SQL and I'm trying to find a way in which I can obtain only the rows that have values that are not duplicate to each other in a specific column of table.
For example the Table below is called T1 and contains:
ID|Branch ID
1 444
2 333
3 444
4 111
5 555
6 333
The result I want will be
ID|Branch ID
4 111
5 555
So only showing non duplicate rows
Edit: I want to apply this to a large relational code. Here is a snippet of where I want it to be added
FROM dbo.LogicalLine
INNER JOIN dbo.Page ON dbo.LogicalLine.page_id = dbo.Page.id
INNER JOIN dbo.Branch ON dbo.LogicalLine.branch_id = dbo.Branch.id
The table LogicalLine will have a column called branch_id containing duplicate id values. I wish to filter those out showing only the non-duplicate branch_id like above example then INNER JOIN the Branch table into the LogicalLine which I have done.
Added -Full Code here:
SELECT
(SELECT name
FROM ParentDevice
WHERE (Dev1.type NOT LIKE '%cable%') AND (id = Dev1.parent_device_id))T1_DeviceID,
(SELECT name
FROM Symbol
WHERE (id = CP1.symbol_id) AND (type NOT LIKE '%cable%'))T1_DeviceName,
(SELECT name
FROM Location
WHERE (id = Page.location_id))T1_Location,
(SELECT name
FROM Installation
WHERE (id = Page.installation_id))T1_Installation,
(SELECT name
FROM ParentDevice
WHERE (Dev2.type NOT LIKE '%cable%') AND (id = Dev2.parent_device_id))T2_DeviceID,
(SELECT name
FROM Symbol
WHERE ( id = CP2.symbol_id) AND (type NOT LIKE '%cable%'))T2_DeviceName,
(SELECT name
FROM Location
WHERE (id = PD2.location_id))T2_Location,
(SELECT name
FROM Installation
WHERE (id = Page.installation_id))T2_Installation,
(SELECT devicefamily
FROM Device
WHERE (type LIKE '%cable%') AND (id = SymCable.device_id))CablePartNumber,
(SELECT name
FROM ParentDevice
WHERE (id = DevCable.parent_device_id) AND (DevCable.type LIKE '%cable%'))CableTag
FROM dbo.LogicalLine
INNER JOIN dbo.Page ON dbo.LogicalLine.page_id = dbo.Page.id
INNER JOIN dbo.Branch ON dbo.LogicalLine.branch_id = dbo.Branch.id
LEFT OUTER JOIN dbo.Symbol AS SymCable ON dbo.LogicalLine.cable_id = SymCable.id
LEFT OUTER JOIN dbo.Device AS DevCable ON SymCable.device_id = DevCable.id
LEFT OUTER JOIN dbo.ParentDevice AS ParentCable ON DevCable.parent_device_id = ParentCable.id
INNER JOIN dbo.SymbolCP AS CP1 ON dbo.Branch.cp1_id = CP1.id
INNER JOIN dbo.SymbolCP AS CP2 ON dbo.Branch.cp2_id = CP2.id
INNER JOIN dbo.Symbol AS S1 ON CP1.symbol_id = S1.id
INNER JOIN dbo.Symbol AS S2 ON CP2.symbol_id = S2.id
INNER JOIN dbo.Device AS Dev1 ON S1.device_id = Dev1.id
INNER JOIN dbo.Device AS Dev2 ON S2.device_id = Dev2.id
INNER JOIN dbo.ParentDevice AS PD1 ON Dev1.parent_device_id = PD1.id
INNER JOIN dbo.ParentDevice AS PD2 ON Dev2.parent_device_id = PD2.id
INNER JOIN dbo.Location AS L1 ON PD1.location_id = L1.id
INNER JOIN dbo.Location AS L2 ON PD2.location_id = L2.id
INNER JOIN dbo.Installation AS I1 ON L1.installation_id = I1.id
INNER JOIN dbo.Installation AS I2 ON L2.installation_id = I2.id
WHERE
(PD1.project_id = #Projectid) AND (dbo.LogicalLine.drawingmode LIKE '%Single Line%');
Select Id, BranchId from table t
Where not exists
(Select * from table
where id != t.Id
and BranchId = t.BranchId)
or
Select Id, BranchId
From table
Group By BranchId
Having count(*) == 1
EDIT: to modify as requested, simply add to your complete SQL query a Where clause:
Select l.Id BranchId, [plus whatever else you have in your select clause]
FROM LogicalLine l
join Page p ON p.id = l.page_Id
join Branch b ON b.Id = l.branch_id
Group By l.branch_id, [Plus whatever else you have in Select clause]
Having count(*) == 1
or
Select l.Id BranchId, [plus whatever else you have in your select clause]
FROM LogicalLine l
join Page p on p.id = l.page_Id
join Branch b on b.Id = l.branch_id
Where not exists
(Select * from LogicalLine
where id != l.Id
and branch_id = l.branch_id)

Too many results in query

I'm fetching some data from our database in MSSQL. Out of this data I want to determine who created the client entry and who took the first payment from this client.
There can be many payment entries for a client on a single booking/enquiry and at the moment, my query shows results for each payment. How can I limit the output to only show the first payment entry?
My query:
SELECT
c.FirstName,
c.LastName,
c.PostalCode,
o.OriginOfEnquiry,
s.SuperOriginName,
c.DateOfCreation,
DATEDIFF(day, c.DateOfCreation, p.DateOfCreation) AS DaysToPayment,
pc.PackageName,
CONCAT(u.FirstName, ' ', u.LastName) AS CreateUser,
(SELECT CONCAT(u.FirstName, ' ', u.LastName)
WHERE u.UserID = p.UserID ) AS PaymentUser
FROM tblBookings b
INNER JOIN tblPayments p
ON b.BookingID = p.BookingID
INNER JOIN tblEnquiries e
ON e.EnquiryID = b.EnquiryID
INNER JOIN tblCustomers c
ON c.CustomerID = e.CustomerID
INNER JOIN tblOrigins o
ON o.OriginID = e.OriginID
INNER JOIN tblSuperOrigins s
ON s.SuperOriginID = o.SuperOriginID
INNER JOIN tblBookingPackages bp
ON bp.bookingID = p.BookingID
INNER JOIN tblPackages pc
ON pc.PackageID = bp.packageID
INNER JOIN tblUsers u
ON u.UserID = c.UserID
WHERE c.DateOfCreation >= '2016-06-01' AND c.DateOfCreation < '2016-06-30'
AND p.PaymentStatusID IN (1,2)
AND e.CustomerID = c.CustomerID
AND p.DeleteMark != 1
AND c.DeleteMark != 1
AND b.DeleteMark != 1
;
I tried adding a "TOP 1" to the nested select statement for PaymentUser, but it made no difference.
you can use cross apply with top 1:
FROM tblBookings b
cross apply
(select top 1 * from tblPayments p where b.BookingID = p.BookingID) as p
Instead of table tblPayments specify sub-query like this:
(SELECT TOP 1 BookingID, UserID, DateOfCreation
FROM tblPayments
WHERE DeleteMark != 1
AND PaymentStatusID IN (1,2)
ORDER BY DateOfCreation) as p
I'm assuming that tblPayments has a primary key column ID. If it is true, you can use this statment:
FROM tblBookings b
INNER JOIN tblPayments p ON p.ID = (
SELECT TOP 1 ID
FROM tblPayments
WHERE BookingID = b.BookingID
AND DeleteMark != 1
AND PaymentStatusID IN (1,2)
ORDER BY DateOfCreation)

Using select inner join in sql

So I have 5 tables:
dane_osobowe:
(PK) id
imie
druzyna:
(PK) id
nazwa
gracz:
(PK) id
(FK) dane_osobowe
(FK) pozycja
kontrakt:
(PK) id
(FK) gracz
(FK) druzyna
pozycja:
(PK) id
nazwa
How can I chose all "gracz" from druzyna "1" which has "pozycja" 2?
I tried something like this:
SELECT *
FROM gracz AS gr
INNER JOIN kontrakt AS kg
ON gr.ID = kg.Gracz
INNER JOIN Druzyna AS d
ON kg.Druzyna = d.ID
WHERE d.ID = 1
INNER JOIN pozycja as poz
ON poz.id = gracz.pozycja
WHERE gracz.pozycja = 2
But it doesn't work :/ Somebody have idea what I'm doing wrong?
Try this:
SELECT *
FROM gracz AS gr
INNER JOIN kontrakt AS kg
ON gr.ID = kg.Gracz
INNER JOIN Druzyna AS d
ON kg.Druzyna = d.ID
--Remove the where condition from here
INNER JOIN pozycja as poz
ON poz.id = gracz.pozycja
WHERE gracz.pozycja = 2
and d.ID = 1 --Add it over here using "and"
ie, move all the where conditions together at the last.
EDIT:
To get the selected columns you can specify it like this:
SELECT d.id, gr.dane_osobowe, poz.nazwa
FROM gracz AS gr
INNER JOIN kontrakt AS kg
ON gr.ID = kg.Gracz
INNER JOIN Druzyna AS d
ON kg.Druzyna = d.ID
--Remove the where condition from here
INNER JOIN pozycja as poz
ON poz.id = gracz.pozycja
WHERE gracz.pozycja = 2
and d.ID = 1 --Add it over here using "and"

How to add a subquery to return count of related rows

I have a working SELECT that returns info on each company (i.e. subscriber). Certain descriptive data is returned from inner joins on "code" tables. My problem is how to add a new column to the result which is a count of the number of users belonging to each subscriber. SubscriberID in the user table is a foreign key:
ALTER TABLE dbo.UserInfo WITH CHECK ADD CONSTRAINT FK_UserInfo_SubscriberID
FOREIGN KEY(SubscriberID) REFERENCES dbo.Subscriber (SubscriberID)
ON UPDATE CASCADE
ON DELETE CASCADE
I added the LEFT OUTER JOIN below and the GROUP BY but I cannot figure out why the parser complains that U.SubscriberID is an invalid column name).
SELECT SubscriberID
,SubscriberName
,DaysUntilExpired
,SubscriptionStatus SubscriptionStatusCode
, C.Description SubscriptionStatus
,U.NumberUsers
FROM dbo.Subscriber S
JOIN dbo.CodeValue C ON S.SubscriptionStatus = C.Value
JOIN dbo.CodeNamespace N ON N.ID = C.CodeNamespaceID AND N.Name = 'SubscriptionStatus'
JOIN dbo.CodeValue V ON S.NotificationStatus = V.Value
JOIN dbo.CodeNamespace X ON X.ID = V.CodeNamespaceID AND X.Name = 'NotificationStatus'
LEFT OUTER JOIN (SELECT Count(*) AS NumberUsers FROM dbo.UserInfo) AS U
ON S.SubscriberID = U.SubscriberID
GROUP BY
S.SubscriberID
,SubscriberName
,DaysUntilExpired
,S.SubscriptionStatus
,SubscriptionStatus
You are probably looking for the following. You need to inline the subquery
SELECT SubscriberID
,SubscriberName
,DaysUntilExpired
,SubscriptionStatus SubscriptionStatusCode
, C.Description SubscriptionStatus
,(SELECT Count(*) AS NumberUsers FROM dbo.UserInfo where SubscriberID = S.SubscriberID) AS NumberUsers
FROM dbo.Subscriber S
JOIN dbo.CodeValue C ON S.SubscriptionStatus = C.Value
JOIN dbo.CodeNamespace N ON N.ID = C.CodeNamespaceID AND N.Name = 'SubscriptionStatus'
JOIN dbo.CodeValue V ON S.NotificationStatus = V.Value
JOIN dbo.CodeNamespace X ON X.ID = V.CodeNamespaceID AND X.Name = 'NotificationStatus'
GROUP BY
S.SubscriberID
,SubscriberName
,DaysUntilExpired
,S.SubscriptionStatus
,SubscriptionStatus
Sql complains because your LEFT OUTER JOIN subquery does not include SubscriberId in column list (and in a group by clause since you have a COUNT aggregate), so it can't make the join on this column.
You should do:
LEFT OUTER JOIN (SELECT SubscriberID , Count(*) AS NumberUsers FROM dbo.UserInfo GROUP BY SubscriberID ) AS U
ON S.SubscriberID = U.SubscriberID

SQL Server 2012 Query - Foreign Key from Same Table

I have a table named RoleDetails with this structure:
ROLE_ID - PK ,
NAME ,
PARENT_ROLE_ID FK ,
CREATED_BY_ID ,
MODIFIED_BY_ID
The other table is user_details
USER_ID ,
USERNAME ,
FULLNAME
How do I query this table so that in the result set
I get the RoleName, RoleId, ParentRoleId, ParentRoleName, CreatedByName and ModifiedByName.
So far I have tried:
SELECT
[ROLE_ID] AS ID, r.NAME AS [RoleName],
r.PARENT_ROLE_ID AS [ParentRoleID] ,
(SELECT rd.NAME FROM dbo.ROLES rd
WHERE rd.ROLE_ID = r.PARENT_ROLE_ID ) AS [ParentRoleName],
CONCAT(ud.FIRST_NAME, ' ', ud.LAST_name) AS [CreatedByName] ,
CONCAT(u.FIRST_NAME, ' ', u.LAST_name) AS [LastModifiedByName]
FROM
dbo.ROLES r
LEFT OUTER JOIN
user_details u ON r.MODIFIED_BY = u.USER_ID
LEFT OUTER JOIN
dbo.USER_DETAILS ud ON r.CREATED_BY = ud.USER_ID
WHERE
r.ROLE_ID = #iRoleID;
You will need to join the user_detail table twice. Once to get the create user and once to get the modify user.
SELECT rd.Name,
rd.Role_ID,
rd.Parent_role_id,
pr.Name,
ud.userName as CreatedByName,
udm.userName as ModifyByName
FROM RoleDetails as rd
INNER JOIN RoleDetails as pr
ON rd.Parent_Role_Id = pr.Role_id
INNER JOIN User_Detail as ud
on rd.Created_by_id = ud.user_id
INNER JOIN User_Detail as udm
ON rd.Modified_by_user = udm.user_id