问题
I need a modification of my previous post regarding how to combine tables with 1 to many relationship into 1 line of record
how to combine tables with 1 to many relationship into 1 line of record
now my problem is my record has now 1 to many relationship. What I need to show is the last record only and combine it in a single line tables tbl_equipment and tbl_warranty
and here is the desired output
here is the code I'm trying to implement
SELECT
a.equipmentid,
a.codename,
a.name,
a.labelid,
a.ACQUISITIONDATE,
a.description,
a.partofid,
w1.warrantyid as serviceidwarranty,
w1.startdate,
w1.enddate,
w2.warrantyid as productidwarranty,
w2.startdate,
w2.enddate,
s.equipstatusid,
l.equiplocationid FROM TBL_EQUIPMENTMST a
left JOIN tbl_equipwarranty w1
ON w1.equipmentid=a.equipmentid and w1.serviceproduct = 'service'
left JOIN tbl_equipwarranty w2
ON w2.equipmentid=a.equipmentid and w2.serviceproduct = 'product'
left join tbl_equipstatus s
on a.equipmentid = s.equipmentid
left join tbl_equiplocation l
on a.equipmentid = l.equipmentid WHERE a.equipmentid = '112'
I only want to show 1 record with the last value of warranty product and warranty service in the output. Can anyone guide me how to modify my code so that when I try join all the tables listed above can produce 1 record only with the last record of warranty as an output. I am using firebird as a database. If you have a solution in mysql kindly tell me and ill try to find the counterpart in firebird.
回答1:
with summary as(
select e.equipmentid ,e.Codename,e.Name,w.warrantyid ,w.Satartdate ,w.Enddate,w.warrantytype
from Eqp e
join Warranty w
on(w.equipmentid =e.equipmentid )
where w.warrantyid =3)
select *,w.warrantyid,w.Satartdate ,w.Enddate,w.warrantytype
from summary s
join Warranty w
on s.Satartdate =w.Satartdate and s.Enddate =w.Enddate
where w.warrantyid =4
回答2:
after reading the comment of Barmar at the question for solution. I Figured out subquery can solve my problem. Subquery is a new word for me. I research on how to use subquery and came out with a solution below. you can correct me if my code is wrong or how to improve the performance of the query
SELECT
a.equipmentid,a.codename,a.name,a.labelid,a.ACQUISITIONDATE,a.description,a.partofid,
w1.warrantyid as serviceidwarranty,w1.startdate,w1.enddate,
w2.warrantyid as productidwarranty,w2.startdate,w2.enddate,
s.equipstatusid,
l.equiplocationid
FROM
TBL_EQUIPMENTMST a
left JOIN
(select first 1 *
from tbl_equipwarranty
where equipmentid='112' and serviceproduct = 'service'
order by warrantyid desc) w1 ON w1.equipmentid = a.equipmentid
and w1.serviceproduct = 'service'
left JOIN
(select first 1 *
from tbl_equipwarranty
where equipmentid = '112' and serviceproduct = 'product'
order by warrantyid desc) w2 ON w2.equipmentid = a.equipmentid
and w2.serviceproduct = 'product'
left join
(select first 1 *
from tbl_equipstatus
where equipmentid = '112'
order by equipstatusid desc) s on a.equipmentid = s.equipmentid
left join
(select first 1 *
from tbl_equiplocation
where equipmentid = '112'
order by equiplocationid desc) l on a.equipmentid = l.equipmentid
WHERE
a.equipmentid = '112'
来源:https://stackoverflow.com/questions/26882396/combine-tables-with-1-to-n-relationship-into-1-line-of-record-with-the-last-valu