Query returns no records - sql

Hello i am making schema for purchasing orders these orders can be ordered by certain user and then received by another user.
so i created the below schema.
Schema
The issue is when UserID column in PurchaseOrders and Deliveries tables has different values the query returns no records.
Query
SELECT
dbo.Users.FirstName,
dbo.Users.LastName,
dbo.PurchaseOrders.PurchaseOrderDate,
dbo.Deliveries.ExpectedDeliveryDate,
dbo.Deliveries.ActualDeliveryDate
FROM dbo.PurchaseOrders
INNER JOIN dbo.Users
ON dbo.PurchaseOrders.UserID = dbo.Users.UserID
INNER JOIN dbo.Deliveries
ON dbo.PurchaseOrders.PurchaseOrderID = dbo.Deliveries.PurchaseOrderID
AND dbo.Users.UserID = dbo.PurchaseOrders.UserID
AND dbo.Users.UserID = dbo.Deliveries.UserID

You need two different joins to Users. You also need to learn to use table aliases:
SELECT pu.FirstName as purchase_FirstName, pu.LastName as purchase_LastName,
du.FirstName as delivery_FirstName, du.LastName as delivery_LastName,
po.PurchaseOrderDate,
d.ExpectedDeliveryDate, d.ActualDeliveryDate
FROM dbo.PurchaseOrders po JOIN
dbo.Deliveries d
ON po.PurchaseOrderID = d.PurchaseOrderID JOIN
dbo.Users pu
ON p.UserID = pu.UserID JOIN
dbo.Users du
ON d.UserId = du.UserId;
Your query returns no records because of this condition:
AND dbo.Users.UserID = dbo.PurchaseOrders.UserID
AND dbo.Users.UserID = dbo.Deliveries.UserID
This obviously means that dbo.PurchaseOrders.UserID = dbo.Deliveries.UserID. So, if this is not true, then no records match the condition.

When you use Inner Join, if the data doesnt match, you will not get any records. You need to use LEFT JOIN and also the second join doesnt need condition with user id. Try the below query.
SELECT
dbo.Users.FirstName,
dbo.Users.LastName,
dbo.PurchaseOrders.PurchaseOrderDate,
dbo.Deliveries.ExpectedDeliveryDate,
dbo.Deliveries.ActualDeliveryDate
FROM dbo.PurchaseOrders
INNER JOIN dbo.Users
ON dbo.PurchaseOrders.UserID = dbo.Users.UserID
LEFT JOIN dbo.Deliveries
ON dbo.PurchaseOrders.PurchaseOrderID = dbo.Deliveries.PurchaseOrderID

Related

SQL - merge two queries

I have the two below queries, the first query returns a list of externalId.
select e.externalId
from data_entity db
inner join assign_entity a on db.fk_assign = a.assignmentId
inner join exercise_entity e on a.fk_exercice = e.externalId;
The second query returns an internalId of each externalId returned by the first query:
select me.internal_id
from entity me
inner join entity_roles r on me.externalid = r.entity_externalid
where r.roles = 'c9237a21-f6da-4ebe-81b0-ddecd51da86b';
How can I merge the two queries in a single query to return for each externalId its internalId like this?
This should do what is required I think. Since there is no fiddle/data present, I can not test it.
with external as
(select e.externalId as externalId from data_entity db
inner join assign_entity a on db.fk_assign = a.assignmentId
inner join exercise_entity e on a.fk_exercice = e.externalId)
select distinct e.externalId, me.internal_id from entity me
inner join entity_roles r on me.externalid = r.entity_externalid
inner join external e on r.roles = e.externalId;
You can use EXISTS:
select me.internal_id, me.externalId
from entity me
inner join entity_roles r on me.externalid = r.entity_externalid
WHERE r.roles = 'c9237a21-f6da-4ebe-81b0-ddecd51da86b'
and exists (
select * from data_entity db
inner join assign_entity a on db.fk_assign = a.assignmentId
inner join exercise_entity e on a.fk_exercice = e.externalId
where e.externalId = me.externalId
);
Probably this query is simpler than what it is now, but based only on your pictures and queries this would do.
PS: Next time, please provide a good sample of your data tables. Pictures do not help much to anyone.
Without some data or a fiddle it is difficult to help, but I think this should work:
SELECT me.internal_id,
me.externalid
FROM entity me
INNER JOIN entity_roles r
ON me.externalid = r.entity_externalid
WHERE r.roles = 'c9237a21-f6da-4ebe-81b0-ddecd51da86b'
AND EXISTS (SELECT *
FROM data_entity db
INNER JOIN exercise_entity e
ON a.fk_exercice = e.externalid
INNER JOIN assign_entity a
ON db.fk_assign = a.assignmentid
WHERE me.externalid = e.externalid);

Retrieving Records from Multiple Tables issue

Hello i am making schema for purchasing orders these orders can be ordered by certain user and then received by another user.
The issue is when UserID value in Deliveries table is null the query return no records.
UserID value is null because the order is not Delivered yet.
Query
SELECT pu.FirstName as purchase_FirstName, pu.LastName as purchase_LastName,
du.FirstName as delivery_FirstName, du.LastName as delivery_LastName,
po.PurchaseOrderDate,
d.ExpectedDeliveryDate, d.ActualDeliveryDate
FROM dbo.PurchaseOrders po JOIN
dbo.Deliveries d
ON po.PurchaseOrderID = d.PurchaseOrderID JOIN
dbo.Users pu
ON po.UserID = pu.UserID JOIN
dbo.Users du
ON d.UserId = du.UserId;
Schema
Most likely you need a LEFT JOIN here. A little formatting makes this a LOT easier to read. You might take a look at this article to understand the different types of joins. http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/
SELECT pu.FirstName as purchase_FirstName
, pu.LastName as purchase_LastName
, du.FirstName as delivery_FirstName
, du.LastName as delivery_LastName
, po.PurchaseOrderDate
, d.ExpectedDeliveryDate
, d.ActualDeliveryDate
FROM dbo.PurchaseOrders po
JOIN dbo.Users pu ON po.UserID = pu.UserID
left JOIN dbo.Deliveries d ON po.PurchaseOrderID = d.PurchaseOrderID
left JOIN dbo.Users du ON d.UserId = du.UserId;

How to select multiple many to many in relation with a single table

I'm currently working with database, but I've got stuck with a select query.
However, I'm not database expert.
The query should return the data from a table that has two relationships of many to many.
This is my tables Diagram that would shows the concept of my question
The Select Query should View three columns, which are VidTbl.Name, ActorTbl.Name and SubTitelTbl.name.
So, I've read and search in the Internet and I've given tries
First try
SELECT
VidTbl.NAME AS Video_Titel_Name,
ActorTbl.NAME AS Actor_Name
FROM ActorInVid
INNER JOIN VidTbl
ON VidTbl.Id = ActorInVid.FKVidId
INNER JOIN ActorTbl
ON ActorTbl.Id = ActorInVid.FKActorId
UNION all
SELECT
VidTbl.NAME AS Video_Titel_Name,
SubTitelTbl.NAME AS SubTitel_Langu
FROM SubTitelInVid
INNER JOIN VidTbl
ON VidTbl.Id = SubTitelInVid.FKVidId
INNER JOIN SubTitelTbl
ON SubTitelTbl.Id = SubTitelInVid.FKSTId
The Result I've got, it was wrong
Then I tried another way to solve this problem, but again I've got another error
second try
SELECT Temp1.*
From (SELECT VidTbl.Id AS Video_Id,
VidTbl.NAME AS Video_Titel_Name,
ActorTbl.NAME AS Actor_Name
FROM ActorInVid
INNER JOIN VidTbl
ON VidTbl.Id = ActorInVid.FKVidId
INNER JOIN ActorTbl
ON ActorTbl.Id = ActorInVid.FKActorId) AS Temp1
SELECT Temp2.*
FROM (SELECT VidTbl.Id AS Video_Id,
SubTitelTbl.NAME AS SubTitel_Langu
FROM SubTitelInVid
INNER JOIN VidTbl
ON VidTbl.Id = SubTitelInVid.FKVidId
INNER JOIN SubTitelTbl
ON SubTitelTbl.Id = SubTitelInVid.FKSTId) AS Temp2
SELECT *
FROM VidTbl
INNER JOIN Temp1
on Temp1.Video_Id = VidTbl.Id
INNER JOIN Temp2
on Temp2.Video_Id = VidTbl.Id
The error, I've got in the last select that was wrong
Thanks a lot for your help any ways
I wish that my question is clear and useful
Thanks again.
You are close. This should work...
SELECT
VidTbl.Name,
ActorTbl.Name,
SubTitelTbl.name
FROM VidTbl
INNER JOIN ActorInVid ON VidTbl.Id = ActorInVid.FKVidId
INNER JOIN ActorTbl ON ActorTbl.Id = ActorInVid.FKActorId
INNER JOIN SubTitelInVid ON VidTbl.Id = SubTitelInVid.FKVidId
INNER JOIN SubTitelTbl ON SubTitelTbl.Id = SubTitelInVid.FKSTId
SELECT DISTINCT vt.Name, at.Name, st.Name
FROM VidTbl vt
JOIN ActionInVid aiv ON aiv.VidId = vt.Id
JOIN SubtitleInVid siv ON siv.VidId = vt.Id
JOIN ActorTbl at ON at.Id = aiv.ActorId
JOIN SubTitleTbl st ON st.Id = siv.STId

WHERE clause not functioning as expected

I am trying to join some tables to receive some data and it is not working as expected. Here is my query:
SELECT USR.PK_User AS PKUser
,USR.NAME AS NAME
FROM [User] USR
LEFT JOIN aspnet_Users ASPU ON USR.NAME = ASPU.UserName
LEFT JOIN aspnet_UsersInRoles ASPUIR ON ASPU.UserId = ASPUIR.UserId
WHERE ASPUIR.RoleId = 'E8700479-902A-42F1-A500-4FA02CAC356C'
Nothing is being returned from this query. But if I do:
SELECT * FROM [aspnet_UsersInRoles] WHERE RoleId = 'E8700479-902A-42F1-A500-4FA02CAC356C'
I get 76 selected rows. If I just do:
SELECT USR.PK_User AS PKUser
,USR.NAME AS NAME
FROM [User] USR
LEFT JOIN aspnet_Users ASPU ON USR.NAME = ASPU.UserName
LEFT JOIN aspnet_UsersInRoles ASPUIR ON ASPU.UserId = ASPUIR.UserId
I get the expected rows returned. Additionally, if I add:
WHERE ASPUIR.RoleId IS NULL
I get rows back.
What am I doing wrong?
You need the condition in the ON not the WHERE.
Otherwise you just make it back into an inner join effectively.
SELECT USR.PK_User AS PKUser,
USR.NAME AS NAME
FROM [User] USR
LEFT JOIN aspnet_Users ASPU
ON USR.NAME = ASPU.UserName
LEFT JOIN aspnet_UsersInRoles ASPUIR
ON ASPU.UserId = ASPUIR.UserId
AND ASPUIR.RoleId = 'E8700479-902A-42F1-A500-4FA02CAC356C'
Try this:-
SELECT USR.PK_User AS PKUser,
USR.NAME AS NAME
FROM [User] USR
LEFT JOIN aspnet_Users ASPU
ON USR.NAME = ASPU.UserName
LEFT JOIN aspnet_UsersInRoles ASPUIR
ON ASPU.UserId = ASPUIR.UserId
AND ASPUIR.RoleId = 'E8700479-902A-42F1-A500-4FA02CAC356C'

Distinct from select statement with Left Joins

How do i select a Distinct value from a select statement with LEFT JOINS?
The value I'm after is one of either s_id from trainingTbl or setName from setsTbl.
The full string is provided below.
Dim strQuery As String =
"SELECT *
FROM userAssessmentTbl
LEFT JOIN trainingTbl ON userAssessmentTbl.tt_id = trainingTbl.tt_id
LEFT JOIN setsTbl ON trainingTbl.s_id = setsTbl.s_id
LEFT JOIN outcomesTbl ON userAssessmentTbl.o_id = outcomesTbl.o_id
WHERE UserId = #UserId
ORDER BY t_date DESC "
Try with GROUP BY for uniqueness
SELECT *
FROM userAssessmentTbl
LEFT JOIN trainingTbl ON userAssessmentTbl.tt_id = trainingTbl.tt_id LEFT JOIN setsTbl ON trainingTbl.s_id = setsTbl.s_id
LEFT JOIN outcomesTbl ON userAssessmentTbl.o_id = outcomesTbl.o_id
WHERE UserId = #UserId
GROUP BY trainingTbl.s_id
ORDER BY t_date DESC
try this
SELECT DISTINCT trainingTbl.s_id FROM
userAssessmentTbl LEFT JOIN trainingTbl ON userAssessmentTbl.tt_id = trainingTbl.tt_id
LEFT JOIN setsTbl ON trainingTbl.s_id = setsTbl.s_id
LEFT JOIN outcomesTbl ON userAssessmentTbl.o_id = outcomesTbl.o_id
WHERE UserId = #UserId ORDER BY t_date DESC
or this (not sure what exactly you need)
SELECT DISTINCT trainingTbl.s_id, setsTbl.setName FROM
userAssessmentTbl LEFT JOIN trainingTbl ON userAssessmentTbl.tt_id = trainingTbl.tt_id
LEFT JOIN setsTbl ON trainingTbl.s_id = setsTbl.s_id
LEFT JOIN outcomesTbl ON userAssessmentTbl.o_id = outcomesTbl.o_id
WHERE UserId = #UserId ORDER BY t_date DESC
In the first place your problem is that you are using select * (A SQL antipattern and something that should NEVER appear in production code) and thus each record is unique so adding distinct or group by will not work.
It is not the left joins causing the proble, an inner join would have the same problem. The problem is that you have a one-to-many relationship between the tables and thus have multiple records when you join them. If you only want one record per id in the first table, then you must specify which record or individual data field in the other tables you want - either use aggregate functions to get the record you want to match or do some sort of filtering in the join to make sure you have a one-to-one relationship. Without knowing your data and your business rules, I cannot suggest what is best to do.