のための最初のadjustment.idを返します。この
WITH CTE
AS
(
select
SeqNo = ROW_NUMBER() OVER(PARTITON BY BILL_DETAIL.id,BILL_HEADER.id ORDER BY adjustment.id DESC),
BILL_DETAIL.id as BILLDETAILID,
BILL_HEADER.id as BILLHEADERID,
adjustment.id adjustmentID
from BILLHEADER
Inner JOIN BILL_DETAIL ON BILL_DETAIL.billdetail2billhdr=BILLHEADER.id
left join adjustment on adjustment.adjustment2bill_detail=BILLDETAIL.id
inner join incident on incident.case2billhdr=BILLHEADER.id
)
SELECT
*
FROM CTE
WHERE SeqNo = 1
をお試しくださいもう一度試してみてください:
select
BILL_DETAIL.id as BILLDETAILID,
BILL_HEADER.id as BILLHEADERID,
MAX(adjustment.id) as adjustmentID
from BILLHEADER
Inner JOIN BILL_DETAIL ON BILL_DETAIL.billdetail2billhdr=BILLHEADER.id
left join adjustment on adjustment.adjustment2bill_detail=BILLDETAIL.id
inner join incident on incident.case2billhdr=BILLHEADER.id
group by BILL_DETAIL.id,BILL_HEADER.id
select billdetailid,
billheadid,
adjustmentid
from
(SELECT
BILL_DETAIL.id AS BILLDETAILID,
BILL_HEADER.id AS BILLHEADERID,
adjustment.id adjustmentID,
row_number() over(partition by bill_detail.id, bill_header.id order by adjustment.id desc) rn
FROM
BILLHEADER
INNER JOIN
BILL_DETAIL ON BILL_DETAIL.billdetail2billhdr = BILLHEADER.id
LEFT JOIN
adjustment ON adjustment.adjustment2bill_detail = BILLDETAIL.id
INNER JOIN
incident ON incident.case2billhdr = BILLHEADER.id) t1
where t1.rn = 1
ここでは、必要なデータベースのみをタグ付けします。 mysql <> sql-server。そして、2つは、 'bill_detailid'と' bill_headid'が同じであるように見えるので、より大きいか小さいadjustIDが必要ですか? – Simon
いいえ、私はちょうど "Bill_DetailID"ごとに1つのレコードを取得したいと思っています。 – Shilpi
'BILL_DETAIL_ID = 697843'には' adjustmentID = 39842,39843'があります。これらの2つの値のどちらを返すのですか? –