Join two SQL Queries to produce one table without headings - sql

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';

Related

Passing different column values to where clause

SELECT pims.icicimedicalexaminerreport.id,
pims.icicimerfemaleapplicant.adversemenstrualid,
pims.icicimerfemaleapplicant.pregnantid,
pims.icicimerfemaleapplicant.miscarriageabortionid,
pims.icicimerfemaleapplicant.breastdiseaseid,
pims.pimscase.tiannumber
FROM pims.pimscase
INNER JOIN pims.digitization
ON pims.pimscase.digitizationid = pims.digitization.id
INNER JOIN pims.medicalexaminerreport
ON pims.digitization.medicalexaminerreportid =
pims.medicalexaminerreport.id
INNER JOIN pims.icicimedicalexaminerreport
ON pims.medicalexaminerreport.id =
pims.icicimedicalexaminerreport.id
INNER JOIN pims.icicimerfemaleapplicant
ON pims.icicimedicalexaminerreport.id =
pims.icicimerfemaleapplicant.id
WHERE pims.pimscase.tiannumber = 'ICICI1234567890'
which gives me the following output
Now I want to use the above output values to select the rows from the table "YesNoAnswerWithObservation"
I imagine it should look something like this Select * from YesNoAnswerWithObservation Where Id in (22,27,26,...23)
Only instead of typing the values inside IN clause I want to use the values in each column resulting from above-mentioned query.
I tried the below code but it returns all the rows in the table rather than rows mentioned inside the In
SELECT pims.yesnoanswerwithobservation.observation,
graphitegtccore.yesnoquestion.description,
pims.yesnoanswerwithobservation.id ObservationId
FROM pims.yesnoanswerwithobservation
INNER JOIN graphitegtccore.yesnoquestion
ON pims.yesnoanswerwithobservation.yesnoanswerid =
graphitegtccore.yesnoquestion.id
WHERE EXISTS (SELECT pims.icicimedicalexaminerreport.id,
pims.icicimerfemaleapplicant.adversemenstrualid,
pims.icicimerfemaleapplicant.pregnantid,
pims.icicimerfemaleapplicant.pelvicorgandiseaseid,
pims.icicimerfemaleapplicant.miscarriageabortionid,
pims.icicimerfemaleapplicant.gynocologicalscanid,
pims.icicimerfemaleapplicant.breastdiseaseid,
pims.pimscase.tiannumber
FROM pims.pimscase
INNER JOIN pims.digitization
ON pims.pimscase.digitizationid =
pims.digitization.id
INNER JOIN pims.medicalexaminerreport
ON pims.digitization.medicalexaminerreportid =
pims.medicalexaminerreport.id
INNER JOIN pims.icicimedicalexaminerreport
ON pims.medicalexaminerreport.id =
pims.icicimedicalexaminerreport.id
INNER JOIN pims.icicimerfemaleapplicant
ON pims.icicimedicalexaminerreport.id =
pims.icicimerfemaleapplicant.id
WHERE pims.pimscase.tiannumber = 'ICICI1234567890')
Any help or a nudge in the right direction would be greatly appreciated
Presumably you want the ids from the first query:
SELECT awo.observation, ynq.description, ynq.id as ObservationId
FROM pims.yesnoanswerwithobservation awo JOIN
graphitegtccore.yesnoquestion ynq
ON awo.yesnoanswerid = ynq.id
WHERE ynq.id = (SELECT mer.id
FROM pims.pimscase c JOIN
pims.digitization d
ON c.digitizationid = d.id JOIN
pims.medicalexaminerreport mer
ON d.medicalexaminerreportid = mer.id JOIN
pims.icicimedicalexaminerreport imer
ON mer.id = imer.id JOIN
pims.icicimerfemaleapplicant ifa
ON imer.id = ifa.id
WHERE c.tiannumber = 'ICICI1234567890'
) ;
Notice that table aliases make the query much easier to write and to read.

SQL select results not appearing if a value is null

I am building a complex select statement, and when one of my values (pcf_auto_key) is null it will not disipaly any values for that header entry.
select c.company_name, h.prj_number, h.description, s.status_code, h.header_notes, h.cm_udf_001, h.cm_udf_002, h.cm_udf_008, l.classification_code
from project_header h, companies c, project_status s, project_classification l
where exists
(select company_name from companies where h.cmp_auto_key = c.cmp_auto_key)
and exists
(select status_code from project_status s where s.pjs_auto_key = h.pjs_auto_key)
and exists
(select classification_code from project_classification where h.pcf_auto_key = l.pcf_auto_key)
and pjm_auto_key = 11
--and pjt_auto_key = 10
and c.cmp_auto_key = h.cmp_auto_key
and h.pjs_auto_key = s.pjs_auto_key
and l.pcf_auto_key = h.pcf_auto_key
and s.status_type = 'O'
How does my select statement look? Is this an appropriate way of pulling info from other tables?
This is an oracle database, and I am using SQL Developer.
Assuming you want to show all the data that you can find but display the classification as blank when there is no match in that table, you can use a left outer join; which is much clearer with explicit join syntax:
select c.company_name, h.prj_number, h.description, s.status_code, h.header_notes,
h.cm_udf_001, h.cm_udf_002, h.cm_udf_008, l.classification_code
from project_header h
join companies c on c.cmp_auto_key = h.cmp_auto_key
join project_status s on s.pjs_auto_key = h.pjs_auto_key
left join project_classification l on l.pcf_auto_key = h.pcf_auto_key
where pjm_auto_key = 11
and s.status_type = 'O'
I've taken out the exists conditions as they just seem to be replicating the join conditions.
If you might not have matching data in any of the other tables you can make the other inner joins into outer joins in the same way, but be aware that if you outer join to project_status you will need to move the statatus_type check into the join condition as well, or Oracle will convert that back into an inner join.
Read more about the different kinds of joins.

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

Joining 5 tables in Access 2010

So here's a challenge. I have a webpage that needs to show data from 5 different tables in a database. Here's the query:
'SELECT ORGANIZATIONS.ORG_NAME, ORG_SECTIONS.SectionName, ATTORNEYS.NAME, ATTORNEYS.LASTNAME, ATTORNEYS.EMAIL, ATTORNEYS.TEL, ATTY_TITLES.ATT_TITLE, FIRM_PRACTICE_GRPS.PRACTICE_GRP
FROM (ORGANIZATIONS INNER JOIN (Org_Sec_Atty INNER JOIN ATTORNEYS ON Org_Sec_Atty.Atty_ID = ATTORNEYS.ATTY_ID) ON ORGANIZATIONS.ID = Org_Sec_Atty.OrgID) INNER JOIN ORG_SECTIONS ON Org_Sec_Atty.SecID = ORG_SECTIONS.ID
WHERE ATTORNEYS.LASTNAME LIKE #LASTNAME;'
The 5 tables are Org_Sec_Atty, ORGANIZATIONS, ORG_SECTIONS, ATTORNEYS, ATTY_TITLES, and FIRM_PRACTICE_GRPS. First, I didn't come up with the table names, lol.
Second, ATTORNEYS is the table that has ATTY_ID, TITLE_ID, PRACTICE_GRP_ID, which is what I'm sure I need to join to FIRM_PRACTICE_GRPS and ATTY_TITLE, and then it needs to connect with the Org_Sec_Atty and ORG_SECTIONS table.
I already have a page where the three tables ATTORNEYS, Org_Sec_Atty, and ORG_SECTIONS are joined by the JOIN statement you see up there. I'm trying to figure out where the other INNER JOIN statements would go and in what order.
It seems like I need to replace "ATTORNEYS" in the "Org_Sec_Atty INNER JOIN ATTORNEYS" with the subordinate INNER JOIN statements, but, again, I'm not sure of the order. Is this possible?
You didn't specified foreign keys on last two tables. Change appropriately if necessary:
SELECT ORGANIZATIONS.ORG_NAME ,
ORG_SECTIONS.SectionName ,
ATTORNEYS.NAME ,
ATTORNEYS.LASTNAME ,
ATTORNEYS.EMAIL ,
ATTORNEYS.TEL ,
ATTY_TITLES.ATT_TITLE ,
FIRM_PRACTICE_GRPS.PRACTICE_GRP
FROM (((( ORGANIZATIONS
INNER JOIN Org_Sec_Atty ON ORGANIZATIONS.ID = Org_Sec_Atty.OrgID)
INNER JOIN ATTORNEYS ON Org_Sec_Atty.Atty_ID = ATTORNEYS.ATTY_ID)
INNER JOIN ORG_SECTIONS ON Org_Sec_Atty.SecID = ORG_SECTIONS.ID)
INNER JOIN ATTY_TITLES ON ORG_SECTIONS.ATTY_ID = ATTY_TITLES.ID)
INNER JOIN FIRM_PRACTICE_GRPS ON ATTY_TITLES.FIRM_ID = FIRM_PRACTICE_GRPS.ID
WHERE ATTORNEYS.LASTNAME LIKE #LASTNAME;

Ambiguous outer join in MS Access

Trying to create an outer join on two other joined tables when recieving this error - I just dont see how to create two separate queries to make it work. Subqueries don't seem to work either, any help appreciated. I get errors for the below query, thanks.
SELECT
CardHeader.CardID, CardHeader.CardDescription, CardHeader.GloveSize,
CardHeader.GloveDescription, CardDetail.Bin, CardDetail.ItemID, Items.ItemDescription,
Items.VCatalogID, CardDetail.ChargeCode, CardDetail.Quantity, Items.Cost, CardColors.ColorID
FROM
((Items
INNER JOIN
(CardHeader INNER JOIN CardDetail ON CardHeader.CardID = CardDetail.CardID) ON Items.ItemID = CardDetail.ItemID)
LEFT JOIN
CardColors ON CardDetail.ItemID = CardColors.ItemID)
INNER JOIN
Colors ON CardColors.ColorID = Colors.ID
ORDER BY
CardHeader.CardID;
I tried the following which runs but asks for the following parameters (which it shouldnt)
CardHeader.ID, MainQry.CardID
SELECT
MainQry.ID, MainQry.CardDescription, MainQry.GloveSize,
MainQry.GloveDescription, MainQry.Bin, MainQry.ItemID,
MainQry.ItemDescription, MainQry.VCatalogID, MainQry.ChargeCode,
MainQry.Quantity, MainQry.Cost, SubQry.ColorID
FROM
(SELECT
CardHeader.ID, CardHeader.CardDescription, CardHeader.GloveSize,
CardHeader.GloveDescription, CardDetail.Bin,
CardDetail.ItemID, Items.ItemDescription, Items.VCatalogID,
CardDetail.ChargeCode, CardDetail.Quantity, Items.Cost
FROM
Items
INNER JOIN
(CardHeader
INNER JOIN
CardDetail ON CardHeader.CardID = CardDetail.CardID) ON Items.ItemID = CardDetail.ItemID
) AS MainQry
LEFT JOIN
(SELECT
CardColors.ItemID, CardColors.ColorID
FROM
CardColors
INNER JOIN
Colors ON CardColors.ColorID = Colors.ID) AS SubQry ON MainQry.ItemID = SubQry.ItemID
ORDER BY
MainQry.CardID;
The second SQL statement can be corrected by reference to the first statement and the error. The error is that both CardHeader.ID and MainQry.CardID are prompting for a parameter, which indicates that the inner statement should include CardHeader.CardID, rather than CardHeader.ID