美好的一天,我正在尝试选择数据。请先查看我的查询和表格。
我有这个查询
select * from
(
SELECT * from
(
select distinct TOP 201233 row, b.OutletCode as 'Kode Otlet',
i.Description as 'Area',
c.Nilai,a.Nip, b.Fullname 'Nama Lengkap', f.positioncode as 'Posisi Sebelumnya',
case when a.[status]=0
then j.ApprovedDate else p.ApprovedDate
end as 'Tanggal Upgrade/Demosi Sebelumnya',
d.positioncode as 'Posisi Baru', a.tanggal as 'Tanggal Upgrade/Demosi'
from penilaian_header a
left join Employee b on a.Nip = b.Nip
left join Position f on b.PositionCode = f.PositionCode
left join Position d on a.PositionCode = d.PositionCode
left join arealeader g on g.OutletCode = b.OutletCode
left join outlet h on g.OutletCode = h.OutletCode
left join area i on i.areacode = h.areacode
left join cutoff k on a.periode = k.cutoffcode
left join
(select ROW_NUMBER() OVER(PARTITION BY KodePenilaianH ORDER BY idPenilaiand ASC) AS Row,
Nilai,KodePenilaianH from penilaian_Detail ) c on a.KodePenilaian = c.KodePenilaianH
left join
( SELECT ROW_NUMBER() OVER (PARTITION BY nip ORDER BY ApprovedDate desc) AS rownumber,
ApprovedDate, Nip FROM historyposition ) AS p on a.nip=p.nip and p.rownumber = 2
left join ( SELECT ROW_NUMBER() OVER (PARTITION BY nip ORDER BY ApprovedDate desc) AS rownumber, ApprovedDate, Nip
FROM historyposition ) AS j on a.nip=j.nip and j.rownumber = 1
where a.flag = 1 and h.AreaCode like '%%'
and Periode like '%CO-2016-9-16-15%' and a.nip = '1004863'
--and tanggal <= k.[to] and tanggal >= k.[from]
order by i.Description asc) nilai pivot ( sum(nilai) for row in ([1],[2],[3],[4],[5]) ) piv)A order by Area;
以上查询我得到了这个结果
KodeMutasiP OldPosition NewPosition ApprovedBy ApprovedDate Nip KodePenilaian
HP0000514 P007 P007 0802678 2016-09-15 1004863 PE0000787
HP0000513 P007 P007 0802678 2016-04-04 1004863 PE0000130
NULL NULL P007 NULL 2016-04-04 1004863 NULL
NULL NULL P041 NULL 2016-01-20 1004863 NULL
NULL NULL P007 NULL 2015-02-12 1004863 NULL
确定。所以我要做的是在第一张图片中设置Posisi Sebelumnya
和Tanggal Upgrade/Demosi Sebelumnya
,并在第二张图片中设置newposition
和Approveddate
。
但是有了这个条件
如果newposition
和nip
col在第二张图片中与第二行相同则跳转到第二行,如果第二张与第三张相同则跳转到第四行,如果第四张与第三张相同则选择第三。
我已经编写了查询选择它,这是我的查询
select * INTO #tMP from historyposition where nip = '1004863' order by approveddate desc
declare @NewPosition varchar(50), @NewPositionLast varchar(50), @ApproveDate datetime, @ApproveDateLast datetime
select top 1 @NewPositionLast = NewPosition, @ApproveDateLast=ApprovedDate from #tmp
WHILE EXISTS (SELECT * FROM #tMP)
BEGIN
select top 1 @NewPosition = NewPosition, @ApproveDate=ApprovedDate from #tmp
if (@NewPosition = @NewPositionLast)
begin
set @NewPositionLast = @NewPosition
set @ApproveDateLast = @ApproveDate
end
else
begin
break
end
delete top(1) #tMP
END
select @NewPositionLast , @ApproveDateLast
drop table #tMP
我得到了这个结果
| P007 | 2016-04-04 00:00:00.000 |
结果就像我期望的那样。
所以我的问题是如何设置Posisi Sebelumnya
和Tanggal Upgrade/Demosi Sebelumnya
与我上次查询的结果。
抱歉我的英语不好。
这是我的预期结果。
我知道第一张照片也是如此。但它不对。因为在我的第一个查询中我选择了第二行。
select * from
(
SELECT * from
(
select distinct TOP 201233 row, b.OutletCode as 'Kode Otlet', i.Description as 'Area',
c.Nilai,a.Nip, b.Fullname 'Nama Lengkap',
case when
l.ShortDesc IS NULL then f.ShortDesc else l.ShortDesc
end
as 'PosisiSebelumnya',
-----
z.approveddate,
------
d.ShortDesc as 'Posisi Baru', a.tanggal as 'Tanggal Upgrade/Demosi'
from penilaian_header a
left join Employee b on a.Nip = b.Nip
left join Position f on b.PositionCode = f.PositionCode
left join Position d on a.PositionCode = d.PositionCode
left join position l on a.posisisaatini = l.positioncode
left join arealeader g on g.OutletCode = b.OutletCode
left join outlet h on g.OutletCode = h.OutletCode
left join area i on i.areacode = h.areacode
left join cutoff k on a.periode = k.cutoffcode
left join
(select ROW_NUMBER() OVER(PARTITION BY KodePenilaianH ORDER BY idPenilaiand ASC) AS Row,
Nilai,KodePenilaianH from penilaian_Detail ) c on a.KodePenilaian = c.KodePenilaianH
left join (
select top(1) Nip,NewPosition, ApprovedDate
from (
select Nip,NewPosition,ApprovedDate
, grp = row_number() over(order by ApprovedDate desc) - row_number() over(partition by NewPosition order by ApprovedDate desc)
from HistoryPosition
) t
where grp = 0
order by ApprovedDate
)z on z.Nip = a.Nip
where a.flag = 1
and h.AreaCode like '%%'
and Periode like '%CO-2016-9-16-15%'
and tanggal <= k.[to] and tanggal >= k.[from] order by i.Description asc) nilai
pivot ( sum(nilai) for row in ([1],[2],[3],[4],[5]) ) piv)A order by Area;
我明白了。
答案 0 :(得分:1)
您的上一个脚本会检索具有最小ApprovedDate的行。它应该与
实际相同SELECT ApprovedDate, NewPosition
FROM (
SELECT ROW_NUMBER() OVER (PARTITION BY NewPosition ORDER BY ApprovedDate asc) AS rownumber, ApprovedDate, NewPosition
FROM historyposition
WHERE nip = '1004863') t
WHERE rownumber = 1
尝试在更大的查询中使用此代码段。
修改强>
当NewPosition
被historyposition
点亮时,会检测第一组ApprovedDate desc
,然后在第一行中找到min ApprovedDate
组。
with HistoryPosition as(
-- sample data
select sKodeMutasiP,OldPosition,NewPosition,ApprovedBy
,cast(ApprovedDate as DATE) ApprovedDate, Nip,KodePenilaian
from (
values
('HP0000514','P007','P007',0802678,'2016-09-15',1004863,'PE0000787')
,('HP0000513','P007','P007',0802678,'2016-04-04',1004863,'PE0000130')
,(NULL,NULL,'P007',NULL,'2016-04-04',1004863,NULL)
,(NULL,NULL,'P041',NULL,'2016-01-20',1004863,NULL)
,(NULL,NULL,'P007',NULL,'2015-02-12',1004863,NULL)
) t (sKodeMutasiP,OldPosition,NewPosition,ApprovedBy,ApprovedDate,Nip,KodePenilaian)
)
select top(1) NewPosition, ApprovedDate
from (
select NewPosition,ApprovedDate
, grp = row_number() over(order by ApprovedDate desc) - row_number() over(partition by NewPosition order by ApprovedDate desc)
from HistoryPosition
where nip = 1004863
) t
where grp = 0
order by ApprovedDate;
<强> EDIT2 强> 在OUTER APPLY中使用此代码段而不是LEFT JOIN,因为它取决于其他表数据:
...
outer apply(
select top(1) NewPosition, ApprovedDate
from (
select NewPosition,ApprovedDate
, grp = row_number() over(order by ApprovedDate desc) - row_number() over(partition by NewPosition order by ApprovedDate desc)
from HistoryPosition hp
where hp.nip = a.nip -- other table
) t
where grp = 0
order by ApprovedDate
) npa
...
答案 1 :(得分:0)
如果您的问题是,我如何组合两个结果集以使它们出现在一行中,可以使用以下选择:
-- first select
select 1 one, 2 two, 3 three, 0 joinCondition
-- second select
select 'a' a, 'b' b, 'c' c, 0 joinCondition
-- combined
select one, two, three, a, b, c
from (select 1 one, 2 two, 3 three, 0 joinCondition) first
inner join (select 'a' a, 'b' b, 'c' c, 0 joinCondition) second
on first.joinCondition = second.joinCondition
我们的想法是,我们为具有相同值的两个选择添加一个虚拟列,以便可以使用它来加入它们