Case statement-How to achieve the target format in MYSQL? - mysql

My source data looks something like this:
enter image description here
Now, for every distinct PK, I want to extract only data related to Z99 code, which has closest date with either Z10 or Z39 Codes and additionally calculate the difference of the z99 and z10/39 date too.
Expected result:enter image description here
Can someone guide me on how to achieve this using case statement or any other better way?
Thanks.

I tried the below code and it worked for me.
Note:
I have created intermediate tables to get the results. If you don't have permission, you can go ahead with creating temptable or CTE
If diff is same for z39 and z10, then both will be taken as you did not specify any priority
/* tbl refers to your input table */
/* daten refers to date */
create table ads as
select
a.daten,
b.daten as z99_dt,
a.pk, a.code as codea,
b.code as codeb,
(a.daten - b.daten) as diff
from tbl as a
inner join (select distinct code, pk, daten
from tbl
where code='z99') as b
on a.code<>b.code and a.pk = b.pk
where codea IN ('z39', 'z10');
In the above snippet, an ads would be created with necessary calculations.
Following snippet will find minimum of diff
create table ref as
select pk, min(diff) as min_diff
from ads
group by pk;
Following snippet joins ref table and ads table to get the final result
select a.pk, codea as st_code, daten as st_dt, codeb as end_code, z99_dt as end_dt, diff from ads as a
inner join ref as b
on a.pk=b.pk and a.diff=b.min_diff;
Please try this out and let me know if this works!

Related

Show values from a column, using values from another column as conditions

I have a table called "fisketur" that looks like this:
I have a table called "plads" that looks like this:
How can I make it so that I only get the values from "plads.navn" that have a "plads.id" that corresponds to a specific month of the year (as found in the "fisketur" table)?
This is what I tried (getting all "plads.navn" that correspond to a "fisketur" taking place in october (month=10):
The output is correct, BUT I would like to set the the whole output column from line 92 (select plads_id from lystfisker.fisketur where month(dato)=10;) as a condition, and not have to set the output values manually as done in line 93.
Any help is greatly appreciated,
Best regards.
You can do it using subquery :
select navn
from lystfisker.plads
where plads_id in ( select plads_id
from lystfisker.fisketur
where month(dato) = 10
);
Or using an inner join
select navn
from lystfisker.plads p
inner join lystfisker.fisketur f on f.plads_id = p.plads_id
where month(f.dato) = 10

MySQL: How to get Information in Header , Subheader sort of format

I am trying to do a SQL query to give me information in the following structure:
enter image description here
I am basically trying to create a MySQL Select Query with a sort of subset.
If anyone has any ideas on this please let me know, this is the logic I has in mind
SELECT
device_type
COUNT(devices)
device_type_class
COUNT(device_type_class)
COUNT(Total Devices)
FROM device_type
INNER JOIN dev_type classification
It is not clear what scheme you have.
Let me take a wild guess:
You have 'devecie' table with FK to type.id
And you have device_type table with id, type and subtype.
SELECT
dt.type,
dt.subtype,
COUNT(ds.id)
FROM
device_type dt
LEFT JOIN
device ds ON ds.type_id = dt.id
GROUP BY
dt.type, dt.subtype
;

Putting a working union query into a subquery causes JOIN syntax error

When I try to run the following query in Access 2016, I get a "Syntax error in JOIN operation" message.
Tables in use:
VISIB_BOMS is basically a self referencing hierarchy of part numbers in a Bill of Materials. (Similar to the classic example of an employee table where the employee can manager other employees.)
ztbl_IFSParts is just 1 column table of part numbers that I want to get the sub-component part numbers for.
The end goal is to have a single column table of all component parts from the parts from ztbl_IFSParts. I'll then use this list to get further info from JOINs to other tables.
SELECT *
FROM
(
/* CODE BETWEEN HERE WORKS */
(
SELECT DISTINCT aa0.PartNo AS PartNo
FROM ztbl_IFSParts AS aa0
)
UNION ALL
(
SELECT DISTINCT b0.COMPPARTNO AS PartNo
FROM
(
SELECT DISTINCT aa0.PartNo AS PartNo
FROM ztbl_IFSParts AS aa0
) AS ab0
LEFT JOIN VISIB_BOMS AS b0
ON ab0.PartNo = b0.ASSYPARTNO
)
/* CODE BETWEEN HERE WORKS */
)
If I run the code between the comments on its own it works fine, only when I try to do any SELECT operations on it, it throws an error.
Hope it's enough info to run with.
Removing the brackets around the two queries either side of the UNION ALL solved this.

How can I compare the sum of one field to another single field?

My data set is like so:
Total_Order_Table
Order_no (unique)
Shipped_quantity
Order_Detail_Table
Order_number (not unique)
Quantity_per_bundle
I need to take the sum of Quantity_per_bundle for each order_number from Order_Detail_Table and compare it to the Shipped_quantity.
My idea is an outer join so that my data will look like so:
I need to be able to see quantity discrepancies and if the order number exists in both tables.
Thanks in advance!
Normaly with a FULL OUTER JOIN in sql:
SELECT to.Order_no AS Order_no_Total_Order_Table, od.Order_number AS Order_No_Ordr_detail_Table, SUM(od.Order_number) AS sum_Quanitty_Per_Bundle, od.Order_number
FROM Total_Order_Table AS to
FULL OUTER JOIN Order_Detail_Table AS od ON to.Order_no = od.Order_number
GROUP BY to.Order_no
But FULL OUTER JOIN don't exist in mysql. But you can simulate it : http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/
Unless I'm missing something subtle in the question, isn't this just as simple as a SELECT query with a join between the tables.
Something along the lines of this should achieve the result:
SELECT tot.Order_no,
odt.Order_no,
SUM(odt.Quantity_per_bundle),
tot.Shipped_quantity
FROM Total_Order_Table tot
LEFT JOIN Order_Detail_Table odt ON odt.Order_Number = tot.Order_Number
GROUP BY tot.Order_no, odt.Order_no, tot.shipped_quantity
(Code not tested in MySQL so forgive errors)

NOT EXISTS Clause

I have a Table called "contas" and another table called "cartoes" I need to verify what "IDCARTAO" doesn't exists in table contas, like that: "If I have one conta with cartoes.IDCARTAO = 1, the result needs to be 2 and 3";
SELECT cartoes.IDCARTAO
from cartoes
WHERE NOT EXISTS(SELECT *
from cartoes
LEFT OUTER JOIN contas ON (cartoes.IDCARTAO = contas.IDCARTAO)
WHERE contas.IDCARTAO = cartoes.IDCARTAO)
Why this sql code doesn't work?
Are you looking for this?
SELECT IDCARTAO
FROM cartoes c
WHERE NOT EXISTS
(
SELECT *
FROM contas
WHERE IDCARTAO = c.IDCARTAO
)
Tiny tweak: Instead of using not exists, try not in. As in...
SELECT ct.IDCARTAO
from cartoes ct
WHERE ct.idcartao not in
(SELECT c.idcartao from contas c)
I don't think that's how it works.
In your case, that query would return all records from the cartoes table where there are no records in the cartas table for the given IDCARTAO.
You already have 3 options up there, If you want to filter records with some specific IDCARTAO(means if you have a static list of IDCARTAO), use in. Otherwise I would use the answer from #peterm, because it would be faster when the subquery results is large.
for more reference, please hit EXISTS Condition