我正在获取重复记录。我想知道如何以最高的" docdate-最后收到的日期"任何人都可以指出我正确的方向。我是sql查询的新手。我正在使用MS sql sever 2016。
(
select a.vendorid,
max(a.DOCDATE) as [Last Date Received],
max(b.VENDNAME) as [Vendor Name] --, b.VNDCLSID
-- ,a.*
from PM20000 a
inner join PM00200 b on a.VENDORID = b.VENDORID
where b.VNDCLSID in ('cb', '6')
group by a.VENDORID
)
union
(
select a.VENDORID,
max(a.DOCDATE) as [Last Date Received],
max(b.VENDNAME) as [Vendor Name] --, b.VNDCLSID
--,a.*
from PM30200 a
inner join PM00200 b on a.VENDORID = b.VENDORID
where b.VNDCLSID in ('cb', '6')
group by a.VENDORID
)
答案 0 :(得分:1)
对于包含最新JSON value of the Name field
{ } nil
{name: ""} pointer to ""
的单行:
[Last Date Received]
对于使用top with ties
row_number()
的每个select top 1 *
from (
select a.vendorid,
a.DOCDATE as [Last Date Received],
b.VENDNAME as [Vendor Name]
from PM20000 a
inner join PM00200 b on a.VENDORID = b.VENDORID
where b.VNDCLSID in ('cb', '6')
union all
select a.VENDORID,
a.DOCDATE as [Last Date Received],
b.VENDNAME as [Vendor Name]
from PM30200 a
inner join PM00200 b on a.VENDORID = b.VENDORID
where b.VNDCLSID in ('cb', '6')
) u
order by [Last Date Received] desc
的最新[Last Date Received]
:
VendorId
使用公共表表达式并按select top 1 with ties *
from (
select a.vendorid,
a.DOCDATE as [Last Date Received],
b.VENDNAME as [Vendor Name]
from PM20000 a
inner join PM00200 b on a.VENDORID = b.VENDORID
where b.VNDCLSID in ('cb', '6')
union all
select a.VENDORID,
a.DOCDATE as [Last Date Received],
b.VENDNAME as [Vendor Name]
from PM30200 a
inner join PM00200 b on a.VENDORID = b.VENDORID
where b.VNDCLSID in ('cb', '6')
) u
order by row_number() over (partition by VendorId order by [Last Date Received] desc)
排序:
[Last Date Received] desc
<小时/> 没有公用表表达式...
;with cte as (
select top 1 with ties *
from (
select a.vendorid,
a.DOCDATE as [Last Date Received],
b.VENDNAME as [Vendor Name]
from PM20000 a
inner join PM00200 b on a.VENDORID = b.VENDORID
where b.VNDCLSID in ('cb', '6')
union all
select a.VENDORID,
a.DOCDATE as [Last Date Received],
b.VENDNAME as [Vendor Name]
from PM30200 a
inner join PM00200 b on a.VENDORID = b.VENDORID
where b.VNDCLSID in ('cb', '6')
) u
order by row_number() over (partition by VendorId order by [Last Date Received] desc)
)
select
VendorId
, VendorName
, [Last Date Received]
, Days_LastInvoiced = datediff(day,[Last Date Received],getdate())
from cte
order by [Last Date Received] desc
答案 1 :(得分:0)
出于性能原因,我建议您在执行TOP
之前执行UNION
。 。 。并使用UNION ALL
:
select top 1 *
from ((select top 1 a.vendorid, max(a.DOCDATE) as [Last Date Received],
max(b.VENDNAME) as [Vendor Name]
from PM20000 a inner join
PM00200 b
on a.VENDORID = b.VENDORID
where b.VNDCLSID in ('cb', '6')
group by a.VENDORID
order by [Last Date Received] desc
)
union all
(select top 1 a.VENDORID, max(a.DOCDATE) as [Last Date Received],
max(b.VENDNAME) as [Vendor Name]
from PM30200 a inner join
PM00200 b
on a.VENDORID = b.VENDORID
where b.VNDCLSID in ('cb', '6')
group by a.VENDORID
order by [Last Date Received] desc
)
) tt
order by [Last Date Received] desc;
正如SQLZim指出的那样,如果你重复,请使用with ties
。