How can I get the RESULT using SQL?
You can use the Multiple JOINS as follows:
select R.PRJ_NAME, R.SELLER_IDX, CS.COMPANY_NAME, R.BUYER_IDX, CB.COMAPY_NAME
from RECORDS R
join PROJECT Ps on Ps.prj_name = r.seller_idx
join PROJECT Pb on Pb.prj_name = r.buyer_idx
join COMPANY Cs on Cs.company_idx = Ps.company_idx
join COMPANY Cb on Cb.company_idx = Pb.company_idx
I suspect that there are really two tables, projects and companies. If so:
select p.proj_name, p.seller_idx, cs.company_name as seller_name,
p.buyer_idx, cb.company_name as buyer_name
from projects p left join
companies cs
on p.seller_idx = cs.company_idx left join
companies cb
on p.buyer_idx = cb.company_idx;
Related
I am currently having problems trying the run a query for some tables.
Below is what I am trying to do, I just can't seem to get it to work.
I have also duplicated constituent and gifts to show you the relations between soft credit and gifts/constituents
Link to Table Relations Image
SELECT
Constituent.lookup_id,
Constituent.name,
SplitGifts.amount,
SplitGifts.giftaidamount
FROM
dbo.Gifts Gifts
INNER JOIN dbo.Constituent Constituent
ON Constituent.id = Gifts.constituent_id
INNER JOIN dbo.SplitGifts SplitGifts
ON SplitGifts.giftid = Gifts.id
LEFT JOIN dbo.SoftCredit SoftCredit
ON SoftCredit.giftid = Gifts.id
INNER JOIN dbo.Constituent Constituent_1
ON Constituent_1.id = SoftCredit.constituentid
INNER JOIN dbo.Gifts Gifts_1
ON Gifts_1.id = SoftCredit.giftid
INNER JOIN dbo.Package Package
ON Package.id = SplitGifts.packageid
WHERE
Package.lookup_id = N'CORPCHAL'
Basically, I want the
amount and gift_aid_amount from [SplitGifts]
Constituent Name & lookup_id from [constituent] to show up for all Gifts however if a soft credit exists for that gift I need it to get the same fields via the [SoftCredit] table -> Gifts -> SplitGifts -> Fields
You could try if the query below works.
SELECT
Constituent.lookup_id,
Constituent.name,
SplitGifts.amount,
SplitGifts.giftaidamount
FROM
dbo.Gifts Gifts
LEFT JOIN dbo.SoftCredit SoftCredit ON SoftCredit.giftid = Gifts.id
INNER JOIN dbo.Gifts Gifts_1 ON
Gifts_1.id = SoftCredit.giftid OR
(SoftCredit.giftid IS NULL AND Gifts_1.id = Gifts.id)
INNER JOIN dbo.Constituent Constituent ON
Constituent.id = SoftCredit.constituentid OR
(SoftCredit.constituentid IS NULL AND Constituent.id = Gifts_1.constituent_id)
INNER JOIN dbo.SplitGifts SplitGifts ON SplitGifts.giftid = Gifts_1.id
INNER JOIN dbo.Package Package ON Package.id = SplitGifts.packageid
WHERE
Package.lookup_id = N'CORPCHAL'
It joins back to table Gifts (using alias Gifts_1) on the gift reference in SoftCredit or to itself if there is no SoftCredit.
Table Constituent is joined in a similar fashion: it joins on the value of SoftCredit.constituentid and when NULL, it falls back to Gifts_1.constituent_id.
All next joins regarding the gift should refer to Gifts_1 then.
I have not tested it though. But it might give you a hint in a possible solution direction.
You are my last hope. I've spend all day but I haven't decided how to create this query.
This is my current database diagram
Every storage have 1 roster
Every roster consist from chief and worker. Information about them is on stuff table .
What I try to do? Get all storage(square adress), worker_name, worker_surname, chief_name, chief_surname.
What I have so far
select storage_address,
storage_square,
stuffs.stuff_name as chiefSurname,
stuffs.stuff_surname as chiefName from storages
inner join storageRoster on storageRoster.storageRoster_id=storages.storage_roster_id
inner join Chiefs on storageRoster.chief_id = Chiefs.chief_id
inner join stuffs on Chiefs.chief_stuff_id = stuffs.stuff_id
But in this query I can only get chiefs on every storage. Help please. I am desperate.
You need to join the stuff table twice, once for worker and once for chief:
select storage_address,
storage_square,
cs.stuff_name as chiefSurname,
cs.stuff_surname as chiefName,
ws.stuff_name as workerSurname,
ws.stuff_surname as workerName
from storages
inner join storageRoster on storageRoster.storageRoster_id=storages.storage_roster_id
inner join Chiefs on storageRoster.chief_id = Chiefs.chief_id
inner join stuffs cs on Chiefs.chief_stuff_id = cs.stuff_id
inner join Workers on storageRoster.worker_id = Workers.chief_id
inner join ws cs on Workers.worker_stuff_id = ws.stuff_id
I created this relationship with Access database but when I am creating a query to return the values I dont get the all the data.
How can I get all the data from tube and tmc tables by ProjectType or ProjectId ?
So I should get something like this query but the query generated by access is not working
SELECT Project.ProjectName,
ProjectSubTypesId.ProjectSubTypesId,
Tube.TubeName, Tube.Duration,
Tmc.TmcName
FROM Tmc
INNER JOIN
(Tube INNER JOIN
(Project INNER JOIN ProjectSubTypesId ON Project.ProjectId = ProjectSubTypesId.ProjectId)
ON Tube.TubeId = ProjectSubTypesId.TubeId) ON Tmc.TmcId = ProjectSubTypesId.TmcId;
Try this:
SELECT Project.ProjectName,
ProjectSubTypesId.ProjectSubTypesId
FROM (
(ProjectSubTypes
INNER JOIN Project USING (ProjectId)
)
INNER JOIN Tube USING(TubeId)
)
INNER JOIN Tmc USING(TmcId)
GROUP BY ProjectSubTypes.ProjectId, ProjectSubTypes.TubeId, ProjectSubTypes.TmcId
ORDER BY ProjectSubTypes.ProjectId;
Something like this work?
SELECT Project.ProjectName,
ProjectSubTypesId.ProjectSubTypesId,
Tube.TubeName, Tube.Duration,
Tmc.TmcName
FROM ((ProjectSubTypes
INNER JOIN Project ON Project.ProjectId = ProjectSubTypes.ProjectId)
INNER JOIN Tube ON Tube.TubeId = ProjectSubTypes.TubeId)
INNER JOIN Tmc ON Tmc.TmcId = ProjectSubTypes.TmcId
ORDER BY ProjectSubTypes.ProjectId;
This should give you a list of all sub-types along with their corresponding Tube, Tmc, and Project data.
If it is possible to have multiple ProjectSubTypes rows with the same TubeId and TmcId then consider adding this line before the ORDER BY line:
GROUP BY ProjectSubTypes.ProjectId, ProjectSubTypes.TubeId, ProjectSubTypes.TmcId
I need to place the PeopleID in several tables for my new database to link all of the peole information. I have tried several times to write a simple update statement please help. Every time I get close I get AMBIGUOUS COLUMN ERROR I don't know what else to do.
Update CONTRACT
Set PeopleID = B.PeopleID
from People A
Inner join
(
Select PeopleId, F.ContractID
From People A
Inner Join Person PRSN on PRSN.PersonID = A.PersonID
Inner Join DARPA_IMPORT_REAL..persnl oldP on oldP.pl_pid = PRSN.PersonID
Left outer join Contract F on F.ContractID = oldP.kn_254id
) B on A.PeopleID = B.PeopleID
Go
try this
Update CONTRACT Set CONTRACT.PeopleID = B.PeopleID
from People A
Inner join (
Select A.PeopleId, F.ContractID
From People AA
Inner Join Person PRSN on PRSN.PersonID = AA.PersonID
Inner Join DARPA_IMPORT_REAL..persnl oldP on oldP.pl_pid = PRSN.PersonID
Left outer join Contract F on F.ContractID = oldP.kn_254id ) B
on A.PeopleID = B.PeopleID
you were asigning the ´A´ alias twice so I recomend using different aliases always
I am using SQL Server 2008 R2 Standard and I still very new to SQL Server.
I am trying to find when a x (measurement) has trend data that flatlines for longer than 48 intervals of the date time.
Thanks in advance.
My base query structure is
SELECT sites.site_name,
measurements.measurement_name,
trend_data_temp.trend_data_avg,
trend_data_temp.trend_data_time
FROM sites
INNER JOIN group_sites
ON sites.site_id = group_sites.site_id
INNER JOIN groups
ON group_sites.group_id = groups.group_id
INNER JOIN measurements
ON sites.site_id = measurements.site_id
INNER JOIN trend_data
ON measurements.measurement_id = trend_data.measurement_id
INNER JOIN trend_data_temp
ON measurements.measurement_id = trend_data_temp.measurement_id
GROUP BY sites.site_name,
measurements.measurement_name,
trend_data_temp.trend_data_avg,
trend_data_temp.trend_data_time
ORDER BY sites.site_name,
measurements.measurement_name
slightly in the dark without any outlines of input data and output data but maybe you need something like this (not tested)
SELECT
sites.site_name,
measurements.measurement_name,
trend_data_temp.trend_data_avg,
[occurances] = count(trend_data_temp.trend_data_time)
FROM sites
INNER JOIN group_sites
ON sites.site_id = group_sites.site_id
INNER JOIN groups
ON group_sites.group_id = groups.group_id
INNER JOIN measurements
ON sites.site_id = measurements.site_id
INNER JOIN trend_data
ON measurements.measurement_id = trend_data.measurement_id
INNER JOIN trend_data_temp
ON measurements.measurement_id = trend_data_temp.measurement_id
GROUP BY
sites.site_name,
measurements.measurement_name,
trend_data_temp.trend_data_avg
HAVING count(trend_data_temp.trend_data_time) >=48