My SQL query within a query - sql

I have 2 tables that I am trying to combine in a specific way
Table 1: ‘part_defs’ Table 2 Items_part_values
in value_defs:
ID | Name
-------------
1 | color
2 | size
3 | weight
in Items_part_values
ItemID | valueID | Value
-------------------------
10 | 1 | red
11 | 1 | blue
What I need is a query where for a given item all the rows from value_defs appear and if they have a value in Items_part_values the value.
So for Item 11 I want
ID | Name | Value
--------------------
1 | color | red
2 | size | NULL
3 | weight | NULL
I’m new to MySQL, in access I would have created a subquery with the ItemID as a parameter and then done a Left Join with value_defs on the result.
Is there a way of doing something similar in MySQL?
Thanks

Use:
SELECT p.id,
p.name,
ipv.value
FROM PART_DEFS p
LEFT JOIN ITEMS_PART_VALUES ipv ON ipv.valueid = p.id
AND ipv.itemid = ?
Replace the "?" with the itemid you want to search for.
This means all the PARTS_DEF rows will be returned, and if the ITEMS_PART_VALUES.valueid matches the PART_DEFS.id value, then the ITEMS_PART_VALUES.value value will be displayed for the item you are looking for. If there's no match, the value column will be NULL for that record.
There's a difference in OUTER JOINs, when specifying criteria in the JOIN vs the WHERE clause. In the JOIN, the criteria is applied before the JOIN occurs while in the WHERE clause the criteria is applied after the JOIN.

Use a left join:
SELECT * FROM Table1 LEFT JOIN Table2 USING (ID);
Edit:
SELECT * FROM part_defs LEFT JOIN Items_part_values ON part_defs.ID = Items_part_values.valueID;

Related

how to get a record based on max date for each user in different multiple tables

I'm trying to make a query that can select the names, DateOfSpecimenResult,SwabResult,DateOfReleaseOfResult. Heres the Visual Representation:
Table: ContactTracingHeader
| AutoID | Name |
1 Jason
2 Chris
Table: Swab
| PatientNo | DateOfSpecimenCollection | SwabResultID | DateOfReleaseOfResult |
1 05/02/2020 1 05/10/2020
1 06/08/2020 1 06/11/2020
1 07/16/2020 2 07/20/2020
Note: ContactTracingHeader.AutoID = Swab.PatientNo
Table: SwabResult
| AutoID | SwabResult |
1 POSITIVE
2 NEGATIVE
Query Output that I'm trying to make
| AutoID | Name | DateOfSpecimenCollection | SwabResult | DateOfReleaseOfResult |
1 Jason 07/16/2020 NEGATIVE 07/20/2020
2 Chris (BLANK) (BLANK) (BLANK)
Note: Swab.ResultID = SwabResult.AutoID
Here, I'm only trying to show a Name with the latest DateOfSpecimenCollection and then use it as a reference for the 2 other column which is SwabResult and DateOfReleaseOfResult, And Since "Chris" doesn't have a input on the Swab table, his records are blank but his name and AutoID Still appears on the table. The SwabResult shows the NEGATIVE, or POSITIVE depending on the ID from table Swab.
What i have done so far is this:
SELECT
CTH.AutoID,
CTH.Firstname,
CTH.Lastname,
(SELECT MAX(DateOfSpecimenCollection)
FROM Swab
WHERE Swab.PatientNo = CTH.AutoID
) AS DateOfSpecimenCollection,
Swab.SwabResultID,
Swab.DateOfReleaseOfResult
FROM ContactTracingHeader AS CTH LEFT JOIN Swab ON Swab.PatientNo = CTH.AutoID;
This Query gets the latest DateOfSpecimenCollection and Output those who aren't in the Swab table which is correct, however it duplicates the record depending how many SwabResult or DateOfReleaseOfResult it has. Also I tried INNER JOIN the table SwabResult so i can output the SwabResult instead of the ID, However it gave me JOIN Expression not supported Error.
I apologize for the long and confusing explanation and duplicate question as I'm trying to strugge to find some answers on the internet. Thank you!
Need a unique identifier in Swab table. If not already there, autonumber should serve
Consider:
Query1:
SELECT Swab.* FROM Swab WHERE ID IN
(SELECT TOP 1 ID FROM Swab AS Dupe WHERE Dupe.PatientNo=Swab.PatientNo
ORDER BY Dupe.DateOfSpecimenCollection DESC);
Query2
SELECT ContactTracingHeader.AutoID, ContactTracingHeader.Name, Query1.DateOfSpecimenCollection,
SwabResult.SwabResult, Query1.DateOfReleaseOfResult
FROM ContactTracingHeader LEFT JOIN (SwabResult RIGHT JOIN Query1
ON SwabResult.AutoID = Query1.SwabResultID)
ON ContactTracingHeader.AutoID = Query1.PatientNo;
Or this sequence:
Query1:
SELECT Swab.PatientNo, Max(Swab.DateOfSpecimenCollection) AS MaxOfDateOfSpecimenCollection
FROM Swab
GROUP BY Swab.PatientNo;
Query2:
SELECT Swab.PatientNo, Swab.DateOfSpecimenCollection, Swab.SwabResultID,
Swab.DateOfReleaseOfResult
FROM Query1 INNER JOIN Swab
ON (Query1.MaxOfDateOfSpecimenCollection = Swab.DateOfSpecimenCollection)
AND (Query1.PatientNo = Swab.PatientNo);
Query3:
SELECT ContactTracingHeader.AutoID, ContactTracingHeader.Name,
Query2.DateOfSpecimenCollection, SwabResult.SwabResult, Query2.DateOfReleaseOfResult
FROM SwabResult RIGHT JOIN (ContactTracingHeader LEFT JOIN Query2
ON ContactTracingHeader.AutoID = Query2.PatientNo)
ON SwabResult.AutoID = Query2.SwabResultID;

Why does using a subquery inside a left join give a completely different answer than an equivalent table?

I'm using a left join in an Access query to get a table with a column added that only applies to a few rows. When I use a subquery as the table being joined it gives me a final table with the new column having the same value for all the rows. When I build a table that gives the same exact output as the subquery it works how I want it to. Why does a subquery and a table give me different results if they look the same.
I've looked over other questions about the difference between a table and a derived table(what I think I get using the subquery) and there doesn't seem to be a difference between the two.
SELECT *
FROM Table1
LEFT JOIN
(SELECT *, "P" AS PColumn FROM TableX, TableY WHERE TableX.x = TableY.y) AS Table2
ON (Table1.x = Table2.x)
WHERE Table1.X > 2
Every value in PColumn is "P". When I build a table that looks exactly like the subquery result and use that in place of the subquery, only row 5 has a PColumn value of "P" and the rest are null, which is what I want. The subquery and the table have the exact same values but different outputs in the outer SELECT statement
As inferred by the comments, this looks to be another occurrence of a bug present in the query optimiser of the JET database engine used by MS Access when evaluating outer joins, as described by Allen Browne in his article here.
Consider the following MCVE demonstrating the bug:
Table1
+----+
| ID |
+----+
| 1 |
| 2 |
+----+
Table2
+----+
| ID |
+----+
| 1 |
+----+
SQL
select * from table1 left join (select table2.id, "x" as X from table2) q on table1.id = q.id
Should return:
+-----------+------+---+
| table1.id | q.id | X |
+-----------+------+---+
| 1 | 1 | x |
| 2 | | |
+-----------+------+---+
Actually returns:
+-----------+------+---+
| table1.id | q.id | X |
+-----------+------+---+
| 1 | 1 | x |
| 2 | | x |
+-----------+------+---+
After confirming that my previous suggestions do not resolve the bug, instead leverage the bug to obtain expected results:
SELECT *
FROM Table1
LEFT JOIN
(SELECT TableX.x, Iif(TableX.x Is Null, Null, "P") AS PColumn
FROM TableX INNER JOIN TableY
ON TableX.x = TableY.y) AS Table2
ON (Table1.x = Table2.x)
WHERE Table1.X > 2
If Allen Brown is correct, the Access query optimizer executes a calculated field (i.e. static value) AFTER the join, so just make the field consider the existence of a normally non-null to reproduce correct outer join (i.e. left join) results.

GROUP BY shows the same group more than once when using CASE

I'm having an issue with a CASE Statement in T-SQL
Here is the query:
Select
CASE WHEN cri.ChartRetrievalMethodID IS NULL THEN wfseg.SiteEventGroupID
ELSE cri.ChartRetrievalMethodID END as Type,
count(distinct c.chartid) TotalCharts
From Sites s LEFT JOIN Charts c ON s.SiteID=c.SiteID
LEFT JOIN ChartRetrievalInformation cri ON c.ChartID=cri.ChartID
LEFT JOIN WFSiteEvents wfse ON wfse.SiteID=s.siteid
LEFT JOIN WFSiteEventTypes wfset ON wfset.EventTypeID=wfse.EventTypeID
LEFT JOIN WFSiteEventGroups wfseg ON wfset.SiteEventGroupID=wfseg.SiteEventGroupID
Where
wfse.EventStatusID in (1,2)
and s.ProjectID=110
group by
cri.ChartRetrievalMethodID, wfseg.SiteEventGroupID
I'm getting a lot of multiple rows instead of them combining into one - example:
+------+--------------+
| Type | Total Charts |
+------+--------------+
| 3 | 28 |
| 3 | 3 |
+------+--------------+
Ideally I would like these two rows mashed together to be just one:
+------+--------------+
| Type | Total Charts |
+------+--------------+
| 3 | 31 |
+------+--------------+
I'm sure there is nothing I'm writing incorrectly but I can't seem to see what it is.
If you include the fields cri.ChartRetrievalMethodID, wfseg.SiteEventGroupID in the column list for your select statement, it will become clear to you why these are shown in multiple rows with that grouping.
What you want to do is group by the value you're calling Type. In another DBMS this would be as simple as GROUP BY Type, but in SQL Server you must repeat the full expression in the GROUP BY clause.

SQL many-to-many select help needed

I have 2 tables
Bid_customer
|bidkey | customerkey
| 1 | 1
| 1 | 2
| 1 | 3
customer_groups
| groupkey | customerkey
| 1 | 1
| 1 | 2
| 1 | 3
What I'm trying to get is a result that will look like
| bidkey | groupkey
| 1 | 1
I've tried a cursor and joins but just don't seem to be able to get what i need any ideas or suggestions
EDIT: customers can belong to more that one group also
I am not sure who meaningful your sample data is. However following is a simple example.
Query:
select distinct b.bidkey, g.gkey
from bidcus b
inner join cusgroup g
on
b.cuskey = g.cuskey
and g.gkey = 10;
Results:
BIDKEY GKEY
1 10
Reference: SQLFIDDLE
In order to have a working Many-to-Many relationship in a database you need to have an intermediary table that defines the relationship so you do not get duplicates or mismatched values.
This select statement will join all bids with all groups because the customer matches.
Select bidkey, groupkey
From customer_groups
Inner Join bid_customer
Where customer_groups.customerkey = Bid_customer.customerkey
Hers is a sample Many to Many Relationship:
For your question:
You will need another table that joins the data. For example, GroupBids
customer_groups and bid_customer would have a one-to-many relationship with GroupBids
You would then do the following select to get your data.
Select bidkey, groupkey
From bid_customer
inner join GroupBids
ON bid_customer.primarykey = GroupBids.idBidKey
inner join customer_groups
ON customer_groups.primarykey = GroupBids.idCustomerGroupkey
This would make sure only related groups and bids are returned

SQL query Help with OUTER JOIN?

I have two tables like this.
Table1
Column | Type |
---------+------------------+
cod | text |
value99 | double precision |
Table2
Column | Type |
---------+------------------+
cod | text |
value06 | double precision |
and i'd like to join them so i'd have something like
Column | Type |
---------+------------------+
cod | text |
value99 | double precision |
value06 | double precision |
the problem is that not all the codes are present in both tables, so if a code is not present in one of the tables it's value should be null.. In the end i'd like something like this
cod | value99 | value06 |
---------+------------------+------------------+
1 | 10 | 20 |
2 | 13 | NULL |
3 | NULL | 15 |
I think that its not possible using LEFT or RIGHT JOIN.. or maybe it is... any ideas? Thx=)
EDITED:
I've tried the FULL OUTER JOIN but the result is something like
code value code value
1 10 1 4
2 15 NULL NULL
NULL NULL 3 36
ANSWER!!!:
i found the answer thx to #Tobiasopdenbrouw :
SELECT test1.code,test1.value,test2.value FROM public.test1 LEFT OUTER JOIN public.test2 ON test1.code=test2.code
UNION
SELECT test2.code,test1.value,test2.value FROM public.test1 RIGHT OUTER JOIN public.test2 ON test1.code=test2.code
I'm guessing a bit, because your question doesn't describe the required output in great detail, but what you probably need is a helper query that will create a table with all the codes for you (a UNION of 2 SELECT querys). This helper table can then be LEFT JOINED to your 2 source tables).
Edit: I thought of the (FULL) OUTER JOIN answer myself, but in reading between the lines, I don't think that's what the OP really needs. But I can be wrong, of course.
Use a FULL OUTER JOIN.
Using Full Outer Joins
To retain the
nonmatching information by including
nonmatching rows in the results of a
join, use a full outer join. SQL
Server provides the full outer join
operator, FULL OUTER JOIN, which
includes all rows from both tables,
regardless of whether or not the other
table has a matching value.
Consider a join of the Product table
and the SalesOrderDetail table on
their ProductID columns. The results
show only the Products that have sales
orders on them. The ISO FULL OUTER
JOIN operator indicates that all rows
from both tables are to be included in
the results, regardless of whether
there is matching data in the tables.
You can include a WHERE clause with a
full outer join to return only the
rows where there is no matching data
between the tables. The following
query returns only those products that
have no matching sales orders, as well
as those sales orders that are not
matched to a product (although all
sales orders, in this case, are
matched to a product).
select
coalesce(t1.cod, t2.cod)
,t1.value99
,t2.value06
from
table1 t1
full outer join table2 t2 on t1.cod= t2.cod