Join two select statements - sql

Could somebody please, give me some advice on how to join the following two select statements:
SELECT TOP 200 *
FROM dbo.Creation_LimitsAndExclusions WITH (nolock)
WHERE LeagueCodeID = 37
and
select *
from dbo.Creation_Markets with (nolock)
where ClassID = 9 and IsParentMatch = 1
Thanks in advance.

I've no idea what the key is, but if the key was CreationID in both tables a join would look like this:
SELECT TOP 200 *
FROM dbo.Creation_LimitsAndExclusions WITH (nolock)
LEFT JOIN dbo.Creation_Markets ON Creation_LimitsAndExclusions.CreationID = Creation_Markets.CreationID
WHERE Creation_LimitsAndExclusions.LeagueCodeID = 37
AND Creation_Markets.ClassID = 9 and Creation_Markets.IsParentMatch = 1

Also don't know what the key is but I would add a cm.creationId IS NOT NULL condition in the where clause to ensure that we aren't taking rows from Creation_LimitsAndExclusions that are not matched
SELECT TOP 200 *
FROM dbo.Creation_LimitsAndExclusions cle WITH (nolock)
LEFT JOIN dbo.Creation_Markets cm
ON cle.CreationID = cm.CreationID
WHERE cle.LeagueCodeID = 37
AND cm.ClassID = 9
AND cm.IsParentMatch = 1
AND cm.creationID IS NOT NULL

Related

SELECT NOT IN with multiple columns in subquery

Regarding the statement below, sltrxid can exist as both ardoccrid and ardocdbid. I'm wanting to know how to include both in the NOT IN subquery.
SELECT *
FROM glsltransaction A
INNER JOIN cocustomer B ON A.acctid = B.customerid
WHERE sltrxstate = 4
AND araccttype = 1
AND sltrxid NOT IN(
SELECT ardoccrid,ardocdbid
FROM arapplyitem)
I would recommend not exists:
SELECT *
FROM glsltransaction t
INNER JOIN cocustomer c ON c.customerid = t.acctid
WHERE
??.sltrxstate = 4
AND ??.araccttype = 1
AND NOT EXISTS (
SELECT 1
FROM arapplyitem a
WHERE ??.sltrxid IN (a.ardoccrid, a.ardocdbid)
)
Note that I changed the table aliases to things that are more meaningful. I would strongly recommend prefixing the column names with the table they belong to, so the query is unambiguous - in absence of any indication, I represented this as ?? in the query.
IN sometimes optimize poorly. There are situations where two subqueries are more efficient:
SELECT *
FROM glsltransaction t
INNER JOIN cocustomer c ON c.customerid = t.acctid
WHERE
??.sltrxstate = 4
AND ??.araccttype = 1
AND NOT EXISTS (
SELECT 1
FROM arapplyitem a
WHERE ??.sltrxid = a.ardoccrid
)
AND NOT EXISTS (
SELECT 1
FROM arapplyitem a
WHERE ??.sltrxid = a.ardocdbid
)

Fetching values from second table in the Query

select *
from EFLOVRelationship R
where R.parentEFLOVValueId = '5320'
and R.childEFLOVId in (select Eflovid
from EFFieldLOVStaticValue
where efLovId = 49)
In the above query I want to fetch 2 columns from EFFieldLOVStaticValue table and display.
Thanks in advance
Use Inner Join
SELECT R.*,
V.field1,
V.field2
FROM eflovrelationship R
INNER JOIN effieldlovstaticvalue V
ON R.childeflovid = V.eflovid
WHERE R.parenteflovvalueid = '5320'
AND V.eflovid = 49
If your table has 1:N relationship then use Distinct to avoid duplicates in result
If you want just one record from effieldlovstaticvalue table for each R.childeflovid column then use Cross Apply but you need to use required column to order the result and choose the top 1 record
SELECT R.*,
CS.field1,
CS.field2
FROM eflovrelationship R
CROSS apply (SELECT TOP 1 field1,
field2
FROM effieldlovstaticvalue V
WHERE R.childeflovid = V.eflovid
AND eflovid = 49
ORDER BY someordercolumn) CS
WHERE R.parenteflovvalueid = '5320'
Use join:
select R.*, sv.?, sv.?
from EFLOVRelationship R join
EFFieldLOVStaticValue sv
ON sv.Eflovid = R.childEFLOVId and sv.efLovId = 49
where R.parentEFLOVValueId = '5320'

Can this be made into a sql join

I want to find where mydata has a Q value but not at least one corresponding d value. How would I solve using a left join or right join?
If it can not be solved using joins, please give some insight into why not, because I am not seeing it.
Below is the solution that I found which works against data provided.
SELECT distinct tablea.mykey
FROM mytest as tablea
where tablea.mydata = 'Q'
and tablea.mykey not in (select distinct tableb.mykey
FROM mytest as tableb
where tableb.mydata = 'd')
mykey mydata
7 d
5 Q
5 d
5 d
6 Q
6 d
6 a
9 Q
9 a
9 a
You can use an outer join and then select only the non-matches
SELECT distinct tablea.mykey
FROM mytest as a
left join mytest as b on a.mykey = b.mykey
and b.mydata = 'd'
where a.mydata = 'Q'
and b.mykey is null

How to exclude records with certain values in sql select

How do I only select the stores that don't have client 5?
StoreId ClientId
------- ---------
1 4
1 5
2 5
2 6
2 7
3 8
I'm trying something like this:
SELECT SC.StoreId FROM StoreClients
INNER JOIN StoreClients SC
ON StoreClients.StoreId = SC.StoreId
WHERE SC.ClientId = 5
GROUP BY StoreClients.StoreId
That seems to get me all the stores that have that client but I can't do the opposite because if I do <> 5 ill still get Store 1 and 2 which I don't want.
I'm basically trying to use this result in another query's EXISTS IN clause
One way:
SELECT DISTINCT sc.StoreId
FROM StoreClients sc
WHERE NOT EXISTS(
SELECT * FROM StoreClients sc2
WHERE sc2.StoreId = sc.StoreId AND sc2.ClientId = 5)
SELECT SC.StoreId
FROM StoreClients SC
WHERE SC.StoreId NOT IN (SELECT StoreId FROM StoreClients WHERE ClientId = 5)
In this way neither JOIN nor GROUP BY is necessary.
SELECT DISTINCT a.StoreID
FROM tableName a
LEFT JOIN tableName b
ON a.StoreID = b.StoreID AND b.ClientID = 5
WHERE b.StoreID IS NULL
SQLFiddle Demo
OUTPUT
╔═════════╗
║ STOREID ║
╠═════════╣
║ 3 ║
╚═════════╝
SELECT StoreId
FROM StoreClients
WHERE StoreId NOT IN (
SELECT StoreId
FROM StoreClients
Where ClientId=5
)
SQL Fiddle
You can use EXCEPT syntax, for example:
SELECT var FROM table1
EXCEPT
SELECT var FROM table2
<> will surely give you all values not equal to 5.
If you have more than one record in table it will give you all except 5.
If on the other hand you have only one, you will get surely one.
Give the table schema so that one can help you properly

SQL JOIN and WHERE statement

I have a problem getting this sql statemen to return what I want:
I want it to return a list of properties both the employee or Job_Profile. If one of them do not have the property it should return NULL in that row/column
Now the sql looks like:
SELECT Parameter.Patameter_Description ParamName,
Job_Profile.Title, Job_Property.Mark JobMark,
Emp_Property.Mark EmpMark,
Emp_Id--, (Employee.First_Name + ' ' + Employee.Last_Name) EmpName
FROM Job_Property
INNER JOIN Job_Profile ON Job_Profile.Title = Job_Property.Job_Title
INNER JOIN Parameter ON Job_Property.Parameter_Id = Parameter.Id
RIGHT JOIN Emp_Property ON Emp_Property.Parameter_Id = Job_Property.Parameter_Id
INNER JOIN Employee ON Emp_Property.Emp_Id = Employee.Enterprise_Staff_Id
WHERE Employee.Enterprise_Staff_Id = 22
AND Job_Profile.Title =
(SELECT
Employee.Job_Profile_Name
FROM Employee WHERE Employee.Enterprise_Staff_Id = 22)
The result is:
Analyse test 1 3 22
And I would like it to be something like this:
Analyse test 1 3 22
Data test 3 NULL NULL or 22
economic test 4 NULL NULL or 22
Service test 2 NULL NULL or 22
I know there is a problem when I:
- join Emp_Property
- Make the WHERE statement
Try LEFT OUTER JOIN when joining Emp_Property
I found a solution, I had to make temp tables and join them:
CREATE TABLE #CompareJob
(Parameter_Id INT
,Parameter_Name VARCHAR(MAX)
,Jobprofile VARCHAR(30)
,Job_Mark INT
)
INSERT INTO #CompareJob(Parameter_Id,Parameter_Name, Jobprofile ,Job_Mark)
SELECT Parameter.Id, Parameter.Patameter_Description, Job_Profile.Title, Job_Property.Mark
FROM Job_Property
INNER JOIN Job_Profile ON Job_Profile.Title = Job_Property.Job_Title
INNER JOIN Parameter ON Job_Property.Parameter_Id = Parameter.Id
WHERE Job_Profile.Title = (SELECT Employee.Job_Profile_Name FROM Employee WHERE Employee.Enterprise_Staff_Id = 22)
CREATE TABLE #CompareEmp
(Parameter_Id INT
,Parameter_Name VARCHAR(MAX)
,Emp_Id INT
,Emp_Name VARCHAR(100)
,Emp_Mark INT
)
INSERT INTO #CompareEmp(Parameter_Id,Parameter_Name, Emp_Id , Emp_Name ,Emp_Mark)
SELECT Parameter.Id, Parameter.Patameter_Description, Employee.Enterprise_Staff_Id, (Employee.First_Name + ' ' + Employee.Last_Name) empname, Emp_Property.Mark
FROM Emp_Property
INNER JOIN Employee ON Employee.Enterprise_Staff_Id = Emp_Property.Emp_Id
INNER JOIN Parameter ON Parameter.Id = Emp_Property.Parameter_Id
WHERE Employee.Enterprise_Staff_Id = 22
SELECT * FROM #CompareJob
FULL OUTER JOIN #CompareEmp ON #CompareJob.Parameter_Id = #CompareEmp.Parameter_Id
Agree with Danny, use the 'LEFT OUTER JOIN' method instead of 'INNER JOIN' as this will only return rows where an entry is found in both tables.