Using query result in another query - sql

I have 3 tables: cc_claim, cc_exposure, cc_new
I am trying to select the claimID from cc_claim by claimNumber and then use that ID to retrieve an exposureID from the cc_exposure table. Then finally I want to select the column from cc_new table that has that exposureID. Here is my code so far:
SELECT cc_claim.ID as test
FROM cc_claim
where ClaimNumber ='19D1000011'
JOIN (cc_exposure where AssignedUserID = test)
I am not sure if I am on the right track.. new to sql.

I think this might be what you had in mind:
SELECT c.ID AS test
FROM cc_claim c
INNER JOIN cc_exposure ex
ON c.ID = ex.AssignedUserID
WHERE c.ClaimNumber = '19D1000011';
Note that JOIN always comes after FROM, and before the WHERE clause.

you need to join the tables on the matching Ids to get what you need.
SELECT cc_claim.ID as test,new.exposureId
FROM cc_claim c
Inner JOIN cc_exposure ex
ON c.ID = ex.AssignedUserID
Inner JOIN cc_new new
ON ex.exposureId=new.exposureID
where c.ClaimNumber ='19D1000011'

Related

Join two SQL Queries to produce one table without headings

I have made two SQL queries both bring back the information i want which is great, i was wondering if i could combine the result into one instead of 2 separate results , i don't know if this can be done but either show without headings or new custom headings.
the SQL code i am using is below
SELECT T_PRODUCT_NUTRITIONALINFORMATION.C_ENERGY_KJ ,T_PRODUCT_NUTRITIONALINFORMATION.C_PROTEIN, T_PRODUCT_SELLING_PRICEDEFINITION.C_NETPRICE
FROM ((T_PRODUCT
INNER JOIN T_PRODUCT_NUTRITIONALINFORMATION ON T_PRODUCT.C_NUTRITIONALINFORMATION = T_PRODUCT_NUTRITIONALINFORMATION.C_ID)
INNER JOIN T_PRODUCT_SELLING_PRICEDEFINITION ON T_PRODUCT.C_SELLING = T_PRODUCT_SELLING_PRICEDEFINITION.C__OWNER_)
WHERE C_CODE LIKE 'STK000832';
SELECT T_PRODUCT_NUTRITIONALINFORMATION.C_ENERGY_KJ ,T_PRODUCT_NUTRITIONALINFORMATION.C_CARBOHYDRATESOFWHICHARESUGAR, T_PRODUCT_SELLING_PRICEDEFINITION.C_NETPRICE
FROM ((T_PRODUCT
INNER JOIN T_PRODUCT_NUTRITIONALINFORMATION ON T_PRODUCT.C_NUTRITIONALINFORMATION = T_PRODUCT_NUTRITIONALINFORMATION.C_ID)
INNER JOIN T_PRODUCT_SELLING_PRICEDEFINITION ON T_PRODUCT.C_SELLING = T_PRODUCT_SELLING_PRICEDEFINITION.C__OWNER_)
WHERE C_CODE LIKE 'STK000832';
the result is below
below is the result i am trying to achieve
Sure, you can use UNION ALL
SELECT T_PRODUCT_NUTRITIONALINFORMATION.C_ENERGY_KJ AS NewHeading1,
T_PRODUCT_NUTRITIONALINFORMATION.C_PROTEIN AS NewHeading2,
T_PRODUCT_SELLING_PRICEDEFINITION.C_NETPRICE AS NewHeading3
FROM ((T_PRODUCT
INNER JOIN T_PRODUCT_NUTRITIONALINFORMATION ON T_PRODUCT.C_NUTRITIONALINFORMATION = T_PRODUCT_NUTRITIONALINFORMATION.C_ID)
INNER JOIN T_PRODUCT_SELLING_PRICEDEFINITION ON T_PRODUCT.C_SELLING = T_PRODUCT_SELLING_PRICEDEFINITION.C__OWNER_)
WHERE C_CODE LIKE 'STK000832'
UNION ALL
SELECT T_PRODUCT_NUTRITIONALINFORMATION.C_ENERGY_KJ AS NewHeading1,
T_PRODUCT_NUTRITIONALINFORMATION.C_CARBOHYDRATESOFWHICHARESUGAR AS NewHeading2,
T_PRODUCT_SELLING_PRICEDEFINITION.C_NETPRICE AS NewHeading3
FROM ((T_PRODUCT
INNER JOIN T_PRODUCT_NUTRITIONALINFORMATION ON T_PRODUCT.C_NUTRITIONALINFORMATION = T_PRODUCT_NUTRITIONALINFORMATION.C_ID)
INNER JOIN T_PRODUCT_SELLING_PRICEDEFINITION ON T_PRODUCT.C_SELLING = T_PRODUCT_SELLING_PRICEDEFINITION.C__OWNER_)
WHERE C_CODE LIKE 'STK000832';

string_agg is to slow with big data and i need a faster solution

I am working with contacting a string from a result and it is taking to long to execute
this is the solution I have:
declare #damagedParties table (caseid varchar(max),FirstName varchar(max),LastName varchar(max),damagedName varchar(max))
insert #damagedParties
select t.caseid,ped.FirstName as FirstName,ped.LastName,concat(ped.FirstName,' ',ped.LastName) as damagedName
from [Case] t
inner join [KCC].[dbo].[Party] p1 on p1.CaseId = t.caseid
LEFT JOIN Person ped ON ped.PersonOrBusinessId = ped.PersonOrBusinessId and p1.PartyTypeRefData = 'kpcparty$PARTY_INJUREDPARTY_F'
select string_agg(d.damagedName,', ')
from #damagedParties d
group by d.caseid
The LEFT JOIN condition in the first query joins the Person table to itself on PersonOrBusinessId. Presumably, the JOIN should be from Person table to the Party table? Something like this
insert #damagedParties
select t.caseid, ped.FirstName, ped.LastName,
concat(ped.FirstName,' ',ped.LastName) as damagedName
from [Case] t
join [KCC].[dbo].[Party] p1 on p1.CaseId = t.caseid
left join Person ped ON p1.PersonOrBusinessId = ped.PersonOrBusinessId
where p1.PartyTypeRefData = 'kpcparty$PARTY_INJUREDPARTY_F';

Access: Integrating Subquery into excisting Query

The "Last" function in the query below (line 4 & 5)that I'm using is not exactly what I'm after. The last function finds the last record in that table.
What i need find is the most recent record in the table according to a date field.
SELECT
tblinmate.statusid,
tblinmate.activedate,
Last(tblclassificationhistory.classificationid) AS LastOfclassificationID,
Last(tblsquadhistory.squadid) AS LastOfsquadID,
tblperson.firstname,
tblperson.middlename,
tblperson.lastname,
tblinmate.prisonnumber,
tblinmate.droppeddate,
tblinmate.personid,
tblinmate.inmateid
FROM tblsquad
INNER JOIN (tblperson
INNER JOIN ((tblinmate
INNER JOIN (tblclassification
INNER JOIN tblclassificationhistory
ON tblclassification.classificationid =
tblclassificationhistory.classificationid)
ON tblinmate.inmateid =
tblclassificationhistory.inmateid)
INNER JOIN tblsquadhistory
ON tblinmate.inmateid =
tblsquadhistory.inmateid)
ON tblperson.personid = tblinmate.personid)
ON tblsquad.squadid = tblsquadhistory.squadid
GROUP BY tblinmate.statusid,
tblinmate.activedate,
tblperson.firstname,
tblperson.middlename,
tblperson.lastname,
tblinmate.prisonnumber,
tblinmate.droppeddate,
tblinmate.personid,
tblinmate.inmateid;
This query below does just that, finds the most recent record in a table according to a date field.
my problem is i dont know how to integrate this Query into the above to replace the "Last" function
SELECT a.inmateID,
a.classificationID,
b.max_date
FROM (
SELECT tblClassificationHistory.inmateID,
tblClassificationHistory.classificationID,
tblClassificationHistory.reclassificationDate
FROM tblinmate
INNER JOIN tblClassificationHistory
ON tblinmate.inmateID = tblClassificationHistory.inmateID
) a
INNER JOIN (
SELECT tblClassificationHistory.inmateID,
MAX(tblClassificationHistory.reclassificationDate) as max_date
FROM tblinmate
INNER JOIN tblClassificationHistory
ON tblinmate.inmateID = tblClassificationHistory.inmateID
GROUP BY tblClassificationHistory.inmateID
) b
ON a.inmateID = b.inmateID
AND a.reclassificationDate = b.max_date
ORDER BY a.inmateID;
I got a tip from another forum to combine queries like this
SELECT qryMainTemp.*, qrySquad.*, qryClassification.*
FROM (qryMainTemp INNER JOIN qrySquad ON qryMainTemp.inmateID = qrySquad.inmateID) INNER JOIN qryClassification ON qryMainTemp.inmateID = qryClassification.inmateID;
and it worked :) i separated the first query into the two queries it was made of and then combined the three like shown above.
Sadly this made another problem arise the query is now not up-datable..working on a solution for this

select columns from different tables with different data type columns

I want to know how to write a query, which selects specific columns(not common) from 2 different tables and combine them together.
I tried this, but didn't work:
SELECT ii.sequence
FROM Costs ii
WHERE ii.order_ID IN (SELECT book.order_ID
FROM BookInfo ci
WHERE ii.order_ID = ci.order_ID)
UNION
SELECT ft.released_title
FROM FinishedBook ft
WHERE ft.version IN (SELECT ii.iiversion
FROM Costs ii
WHERE ii.iiorder_ID IN (SELECT ci.order_ID
FROM BookInfo ci
WHERE ii.iiorder_ID = ci.order_ID))
ORDER BY sequence;
Isn't this a case of joining these tables and calling Distinct to avoid duplicates?
Try this:
select Distinct a.Sequence, b.RELEASED_TITLE
from IncludedIn a inner join FinishedTrack b
on a.OriginatesFrom = b.IIOriginatesFrom
Inner join CdInfo c on a.IIALBUM_ID = c.ALBUM_ID
Order By a.Sequence
For MSSQL Server, Use Join to get the result.
SELECT I.Sequence, F.Released_Title FROM FinishedTrack AS F
INNER JOIN IncludedIn AS I ON I.ORIGINATESFROM = F.IIORIGINATESFROM
INNER JOIN CdInfo AS A ON A.ALBUM_ID = I.IIALBUM_ID
ORDER BY I.Sequence DESC
You need to use a JOIN instead of a UNION:
SELECT ii.sequence, ft.released_title
FROM IncludedIn ii
INNER JOIN CdInfo ci ON ii.iialbumid = ci.album_id
INNER JOIN FinishedTrack ft on ft.originatesfrom = ii.iioriginatesfrom
ORDER BY ii.sequence;
This query might work for you
SELECT IncludedIn.SEQUENCE, FinishedTrack.RELEASED_TITLE
FROM FinishedTrack
INNER JOIN IncludedIn
ON FinishedTrack.ORIGINATESFROM=IncludedIn.IIORIGINATESFROM and
FinishedTrack.VERSION=IncludedIn.IIVERSION order by FinishedTrack.SEQUENCE;

Joining two tables on a key and then left outer joining a table on a number of criteria

I'm attempting to join 3 tables together in a single query. The first two have a key so each entry has a matching entry. This joined table will then be joined by a third table that could produce multiple entries for each entry from the first table (the joined ones).
select * from
(select a.bidentifier, a.bsession, a.symbol, b.jidentifier, b.JSession
from trade_monthly a, trade_monthly_second b
where
a.bidentifier = b.jidentifier AND
a.bsession = b.JSession)
left outer join
trade c
on c.symbol = a.symbol
order by a.bidentifier, a.bsession, a.symbol, b.jidentifier, b.JSession, c.symbol
There will be more criteria (not just c.symbol = a.symbol) on the left outer join but for now this should be useful. How can I nest the queries this way? I'm gettin gan SQL command not properly ended error.
Any help is appreciated.
Thanks
For what I know every derived table must be given a name; so try something like this:
SELECT * FROM
(SELECT a.bidentifier, ....
...
a.bsession = b.JSession) t
LEFT JOIN trade c
ON c.symbol = t.symbol
ORDER BY t.bidentifier, ...
Anyway I think you could use a simpler query:
SELECT a.bidentifier, a.bsession, a.symbol, b.jidentifier, b.JSession, c.*
FROM trade_monthly a
INNER JOIN trade_monthly_second b
ON a.bidentifier = b.jidentifier
AND a.bsession = b.JSession
LEFT JOIN trade c
ON c.symbol = a.symbol
ORDER BY a.bidentifier, a.bsession, a.symbol, b.jidentifier, b.JSession, c.symbol
Try this:
SELECT
`trade_monthly`.`bidentifier` AS `bidentifier`,
`trade_monthly`.`bsession` AS `bsession`,
`trade_monthly`.`symbol` AS `symbol`,
`trade_monthly_second`.`jidentifier` AS `jidentifier`,
`trade_monthly_second`.`jsession` AS `jsession`
FROM
(
(
`trade_monthly`
JOIN `trade_monthly_second` ON(
(
(
`trade_monthly`.`bidentifier` = `trade_monthly_second`.`jidentifier`
)
AND(
`trade_monthly`.`bsession` = `trade_monthly_second`.`jsession`
)
)
)
)
JOIN `trade` ON(
(
`trade`.`symbol` = `trade_monthly`.`symbol`
)
)
)
ORDER BY
`trade_monthly`.`bidentifier`,
`trade_monthly`.`bsession`,
`trade_monthly`.`symbol`,
`trade_monthly_second`.`jidentifier`,
`trade_monthly_second`.`jsession`,
`trade`.`symbol`
Why don't you just create a view of the two inner joined tables. Then you can build a query that joins this view to the trade table using the left outer join matching criteria.
In my opinion, views are one of the most overlooked solutions to a lot of complex queries.