I got 4 tables as follows:
tbProjekt
--------------
Id
every Machine has ProjektId which belongs to:
tblMaszyna
--------------
Id
ProjektId
tblElement
--------------
Id
Name
in this table i am associating elements with machines:
tblMaszElem
--------------
Id
IdElem
IdMach
I would like to take those elements - Name from tblElement which belongs to machines which belongs to specified ProjectId. So lets say for ProjectId 10 How can i achieve that?
select e.Name
from tbElement e
inner join tbMaszElem me on me.IdElem = e.Id
inner join tbMaszyna m on m.Id = me.IdMach
inner join tbProject p on p.Id = m.ProjektId
where
p.Id = 10
This should do. This selects the Name column of all entries in the tbElement table which are associated to a machine that's associated to a project where the project ID is 10.
Please check this sample and its comment
select
te.name
from
tblMaszElem tmem
inner join tblElement te on te.id = tmem.IdElem
inner join tblMaszyna tmzy on tmzy.id = tmem.IdMach
--inner join tbProjekt tp on tp.id = tmzy.ProjektId --i think this should be avoidable
where
tp.id = 10
Related
I am new to SQL. I have written the code below but am getting stuck with the COUNT function. I want to only display the ClientIDs that have 2 or more ServiceIDs on the Service table. I tried doing a nested select within the join on the Service table originally and was getting error messages. Now with the code below, I am getting an error
Msg 164, Level 15, State 1, Line 13
Each GROUP BY expression must contain at least one column that is not an outer reference.
I am trying to achieve the following. THANK YOU!
client Id
Service ID
Count
1
2
3
1
3
3
1
4
3
2
5
4
2
6
4
2
7
4
2
8
4
SELECT DISTINCT
O.OrgName,
Referral.ClientID,
Client.FirstName,
Client.LastName,
P.ProviderName AS 'School',
E.ProgramID,
LI.ListLabel AS 'Reason',
Race.ListLabel AS 'Race/Ethnicity',
Gender.ListLabel AS 'Sex/Gender',
[ServiceId],
(SELECT COUNT([ServiceID])
GROUP BY Referral.ClientID
HAVING COUNT([ServiceID]) >= 2) AS 'Number of Supports'
FROM
ProviderReferral Referral
JOIN
Provider P ON ReferFromProviderID = P.EntityID
JOIN
ProviderReferralExt ON Referral.ProviderReferralID = ProviderReferralExt.ProviderReferralID
INNER JOIN
MultiSelectValue MSV ON MSV.ContextID = Referral.ProviderReferralID
AND MSV.ContextTypeID = 87
AND MSV.ListID = 1000001179
INNER JOIN
Client ON Referral.ClientID = Client.EntityID
INNER JOIN
EnrollmentMember ON client.EntityID = EnrollmentMember.ClientID
INNER JOIN
Enrollment E ON EnrollmentMember.EnrollmentID = E.EnrollmentID
AND E.X_CMNonCM = 1
INNER JOIN
ListItem LI ON LI.ListValue = MSV.ListValue
AND LI.ListID = 1000001179
INNER JOIN
ListItem Race ON Race.ListValue = client.Race
AND Race.ListID = 1000000068
INNER JOIN
ListItem Gender ON Gender.ListValue = Client.Gender
AND Gender.ListID = 1
INNER JOIN
Service ON E.EnrollmentID = Service.EnrollmentID -- the supports table
JOIN
Organization O ON o.EntityID = p.OrganizationID
WHERE
P.OrganizationID = 33847
AND E.ProgramID = 1325
AND referral.DeletedDate = '9999-12-31'
AND o.DeletedDate = '9999-12-31'
AND enrollmentmember.DeletedDate = '9999-12-31'
ORDER BY
referral.ClientID, client.FirstName
You can use window function count as follows:
Select * from
(Select ...
...
Count([ServiceID]) over (partition by referral.clientid) as cnt
From ...
...
) t where cnt > 2;
Please note order by should be last clause in your query. Use it accordingly.
Based on the request in the comment, Adding the code into your original query as follows:
select * from
(select Referral.ClientID,
Client.FirstName,
Client.LastName,
P.ProviderName as School,
E.ProgramID,
LI.ListLabel as Reason,
Race.ListLabel as "Race/Ethnicity",
Gender.ListLabel as "Sex/Gender",
[ServiceId],
Count([ServiceID]) over (partition by referral.clientid) as cnt -- this -- added open parenthesis before partition
FROM ProviderReferral Referral
JOIN Provider P on ReferFromProviderID=P.EntityID
JOIN ProviderReferralExt on Referral.ProviderReferralID=ProviderReferralExt.ProviderReferralID
INNER JOIN MultiSelectValue MSV on MSV.ContextID = Referral.ProviderReferralID AND
MSV.ContextTypeID=87 AND MSV.ListID=1000001179
INNER JOIN Client on Referral.ClientID=Client.EntityID
INNER JOIN EnrollmentMember on client.EntityID=EnrollmentMember.ClientID
INNER JOIN Enrollment E on EnrollmentMember.EnrollmentID=E.EnrollmentID and E.X_CMNonCM=1
INNER JOIN ListItem LI on LI.ListValue = MSV.ListValue and LI.ListID = 1000001179
INNER JOIN ListItem Race on Race.ListValue=client.Race and Race.ListID=1000000068
INNER JOIN ListItem Gender on Gender.ListValue=Client.Gender and Gender.ListID=1
INNER JOIN Service on E.EnrollmentID=Service.EnrollmentID -- the supports table
JOIN Organization O on o.EntityID =p.OrganizationID
Where P.OrganizationID=33847
and E.ProgramID=1325
and referral.DeletedDate = '9999-12-31' and o.DeletedDate='9999-12-31'
and enrollmentmember.DeletedDate='9999-12-31') t
where cnt > 2 -- this
order by ClientID, FirstName
Can anyone help I'm new to SQL and trying to figure out the below question see image for the table structure;
Question = Select account name, contact last name, case number, quote number, quote date and quote value for the f third-largest quote ever created for each of the accounts in the EC1 area
So far I got;
Select
a.accountname, cc.lastname, c.casenumber,
q.quotenumber, q.quotedate, q.quotevalue
from
TBL_Quote q
Left join
TBL_case c On q.caseid = c.caseid
Left join
tbl_contact cc On c.contactID = cc. contactID
Left join
tbl_account a On a.accountid = cc.accountid
Where
left(a.postcode, 3) like 'EC1'
and for the third:
SELECT TOP 1 value
FROM
(SELECT DISTINCT TOP 3 value
FROM tbl_quote
ORDER BY value DESC) a
ORDER BY value
I can't seem to combine the top 3 and the query is it best to overpartion by ?
I would suggest joins and a row-limiting clause:
select ac.accountName, co.lastName, ca.caseNumber, qu.quoteNumber
from tbl_account ac
inner join tbl_contact co on co.accountId = ac.accountId
inner join tbl_case ca on ca.contactId = co.contactId
inner join tbl_quote qu on qu.caseId = ca.quoteId
where ac.postcode like 'EC1%'
order by len(qu.value) desc
offset 2 rows fetch next 1 row only
I keep getting an error in this query
I have two tables, dbo.litters and dbo.dogs.
The information of all dogs is in the dbo.dogs table and are referenced only by ID in the dbo.litters table.
Version 2
Select
Puppy.Reg, Puppy.Name, puppy.LitterId,
p1.Reg, p1.Name, p2.Reg, p2.Name
From
dbo.Dogs Puppy
Join
dbo.litters as p on Puppy.LitterId = p.Id
Join
dbo.Dogs as p1 on Puppy.Id = p.FatherId
Join
dbo.Dogs as p2 on Puppy.Id = p.MotherId
Where
puppy.Reg = 'xxxxx/xxxx'
Results of the query version 2
I want to show the mother and father of the puppy and this information is only in the dbo.litters table.
I'm new to this and I can't see where I go wrong.
Select
d.id, d.Reg, d.Name, d.LitterId, l.FatherId, l.MotherId
From
dbo.Dogs D
inner join
dbo.Litters L on d.LitterId = l.Id
where
d.Reg = 'XXXXXX/XXXX'`
This query returns the information I need but not the name of the mother and father.
The results looks like this
ID Reg nr Name LitterID Father ID MotherID
---------------------------------------------------------------
20590 xxxxx/xxxx Fido 8525 11942 12007
I have made a small version in fiddle so everyone see the structure :)
Database in fiddle
Database in fiddle with solution
You used alias for table name but in time of join that does not used. That's why it thrown error, so use alias name in join:
Select
d.id, d.Regnr, d.Name, d.LitterId, l.FatherId, l.MotherId,D1.Name as fathername,
D2.Name as motherName
From dbo.Litters L
left join dbo.Dogs D
on d.LitterId = l.Id
left join dbo.Dogs D1 on L.FatherId=D1.ID
left join dbo.Dogs D2 on L.MotherId=D2.ID
where
d.Regnr = 'NO34567/2012'
id Regnr Name LitterId FatherId MotherId fathername motherName
3 NO34567/2012 Fido 9000 2 1 king Queen
You want information about a litter, so that's where you start. Next, you want to limit to the litter of one pup, so you join the dog, and filter on its reg. Finally you want to add information about its parents, so you join those specific dogs. You end up with something like:
Select puppy.Reg
, puppy.Name
, puppy.LitterId
, dad.Reg
, dad.Name
, mom.Reg
, mom.Name
From dbo.litters as l
Join dbo.Dogs puppy
on Puppy.LitterId = l.Id
Join dbo.Dogs as dad
on dad.id = l.FatherId
Join dbo.Dogs as mom
on mom.Id = l.MotherId
Where puppy.Reg = 'xxxxx/xxxx'
I had to guess those join conditions, because you didn't provide the information.
SELECT EMPLOYEE.Fname,EMPLOYEE.Lname,
D.Dnumber,
SUM(WORKS_ON.HOURS) AS SUMHOUR
FROM PROJECT
INNER JOIN DEPARTMENT D ON D.Dnumber = PROJECT.Dnum
INNER JOIN EMPLOYEE ON PROJECT.Dnum= EMPLOYEE.Dno
INNER JOIN WORKS_ON ON WORKS_ON.Pno = PROJECT.Pnumber
GROUP BY EMPLOYEE.Fname,EMPLOYEE.Lname, D.Dnumber
I'm writing a code that lists people with the highest SUMHOUR.
Now, I've found who has the biggest sum, but I can't set condition like max(sum()) for displaying them.
This is my output. In this image, people with Dnumber '5' have highest SUMHOUR '150' and I want to display them. What should I do?
One simple approach uses TOP:
SELECT TOP 1 WITH TIES
e.Fname,
e.Lname,
d.Dnumber,
SUM(w.HOURS) AS SUMHOUR
FROM PROJECT p
INNER JOIN DEPARTMENT d
ON d.Dnumber = p.Dnum
INNER JOIN EMPLOYEE e
ON p.Dnum = e.Dno
INNER JOIN WORKS_ON w
ON w.Pno = p.Pnumber
GROUP BY
e.Fname,
e.Lname,
d.Dnumber
ORDER BY
SUMHOUR DESC;
You have puted Dnumber in group by so it returns highest SUMHOUR in each Dnumber.
So sloution is just remove Dnumber from group by then it return highest SUMHOUR only.
I have a mssql database name Organization
I have 3 tables:
Table 1 named Organization
Table 2 named Product
Table 3 named IPAddress
Table 1 returns about 1000 results which is fine.
SELECT distinct OrganizationName, AssetNumber
FROM OrganizationList
Where OrganizationName = 'orgname'
All I would like to see is the asset numbers with each org. Im not sure how to do this. I know a need a join and have tried a bunch of examples from the net however am not able to get it working.
Also I would like to be able to get the IPaddress for each asset
The problem i an running into is i need to match the OrganizationID col from Organization with the OrganizationID col from Product and i need to match the ProductId from Product and the ProductId from IPAddress
Any help would be greatly appreciated. Please let me know if you will need any more info.
Thank You
Should also list out the IP address for you as well.
SELECT distinct c1.OrganizationName, c2.AssetNumber, c3.IPAddress
FROM Organization c1
INNER JOIN Product c2
ON c1.OrganizationID = c2.OrganizationID
INNER JOIN IPAddress c3
ON c2.ProductId = c3.ProductId
Where c1.OrganizationName = 'orgname'
I am doing some guessing here since you didn't give us all of the information needed to fully help you. How about the following query:
SELECT DISTINCT o.OrganizationName, p.AssetNumber, ip.IPaddress
FROM Organization AS o
INNER JOIN Product AS p
ON o.OrganizationID = p.OrganizationID
INNER JOIN IPAddress AS ip
ON p.ProductId = ip.ProductId
Where o.OrganizationName = 'orgname'
I didn't see any need to join the OrganizationList table. If you do indeed need it then add the following join:
INNER JOIN OrganizationList AS ol
ON o.OrganizationID = ol.OrganizationID
for user3022598
Take result of this query in a temp table:
SELECT distinct o.OrganizationName, p.AssetNumber, i.IPAddress
FROM Organization o
INNER JOIN Product p
ON o.OrganizationID = p.OrganizationID
INNER JOIN IPAddress i
ON p.ProductId = i.ProductId
Where o.OrganizationName = 'orgname'
then you can apply disctint on ipaddress field.
Try this one:
select o.OrgName, o.AssetNo
from OrgList o
join Prdct p
on o.orgid = p.orgid
Union All
select p.productid/name, i.ipadress
from product p
join ipadres i
on p.prdid=i.prdid
Plz check it & let me know if works or not.