我终于在最后对数据进行分组时写了一篇文章。基本上,我已经设计了查询,现在我正在获取我想要查看的所有数据,但现在我需要按清单编号(man_no列)对结果进行分组。基本上,如果结果只显示唯一清单编号的第一个实例,我会很高兴,因为后续实例实际上是相同的数据。
有什么建议吗?正如您从数据中看到的那样,这些记录实际上是相同的数据,但在"托运"专栏结果......我可能只是写得不好?
QUERY:
SELECT landing_jobs.man_no, ltrim(rtrim(brun.veh_code)) as Run_Vehicle,
ltrim(rtrim(landing_jobs.col_zone)) + ' - ' + ltrim(rtrim(landing_jobs.del_zone)) as coldel,
CASE
WHEN con2.long_no <> ' ' and landing_jh.est_time<=job_history2.est_time and jmaster.col_state='SA' THEN dateadd(ss,1800,job_history2.est_time)
WHEN (con2.long_no <> ' ' and landing_jh.est_time>job_history2.est_time and jmaster.col_state='SA') OR (con2.long_no IS null and jmaster.col_state='SA') then dateadd(ss,1800,Landing_JH.est_time)
WHEN con2.long_no <> ' ' and landing_jh.est_time<=job_history2.est_time and jmaster.col_state='WA' THEN dateadd(ss,7200,job_history2.est_time)
WHEN (con2.long_no <> ' ' and landing_jh.est_time>job_history2.est_time and jmaster.col_state='WA') OR (con2.long_no IS null and jmaster.col_state='WA') then dateadd(ss,7200,Landing_JH.est_time)
WHEN con2.long_no <> ' ' and landing_jh.est_time<=job_history2.est_time and jmaster.col_state='QLD' THEN dateadd(ss,0,job_history2.est_time)
WHEN (con2.long_no <> ' ' and landing_jh.est_time>job_history2.est_time and jmaster.col_state='QLD') OR (con2.long_no IS null and jmaster.col_state='QLD') then dateadd(ss,0,Landing_JH.est_time)
ELSE landing_jh.est_time
END AS dep_mel_time,
CASE
WHEN con2.del_code<>' ' and con2.del_code<>landing_jobs.del_code then LTRIM(rtrim(landing_jobs.del_code)) + ' / ' + LTRIM(rtrim(con2.del_code))
ELSE ltrim(rtrim(landing_jobs.del_code))
END AS deliveries,
CASE
WHEN con3.long_no<>' ' THEN LTRIM(RTRIM(landing_jobs.long_no)) + '-' + LTRIM(rtrim(landing_jobs.cus_code)) + ' and ' + LTRIM(rtrim(con2.long_no)) + '-' + LTRIM(rtrim(con2.cus_code)) + ' and ' + LTRIM(rtrim(con3.long_no)) + '-' + LTRIM(rtrim(con3.cus_code))
WHEN con2.long_no<>' ' THEN LTRIM(RTRIM(landing_jobs.long_no)) + '-' + LTRIM(rtrim(landing_jobs.cus_code)) + ' and ' + LTRIM(rtrim(con2.long_no)) + '-' + LTRIM(rtrim(con2.cus_code))
ELSE LTRIM(rtrim(landing_jobs.long_no)) + '-' + ltrim(rtrim(Landing_Jobs.cus_code))
END AS consignments,
ltrim(rtrim(landing_jobs.trl_code1)) as con1trl1,
ltrim(rtrim(landing_jobs.trl_code2)) as con1trl2,
ltrim(rtrim(con2.trl_code1)) as con2trl1,
ltrim(rtrim(con2.trl_code2)) as con2trl2,
ltrim(rtrim(driver2.name))
FROM landing_jobs left outer join landing_runs brun on landing_jobs.man_no=brun.job_no left outer join landing_driver driver2 on brun.drv_code=driver2.code left outer join landing_equipment on landing_equipment.code=brun.veh_code left outer join landing_driver on landing_driver.code=brun.drv_code left outer join landing_JH on landing_jobs.job_no=landing_JH.job_no and landing_JH.job_status='C' left outer join landing_jobs con2 on landing_jobs.man_no=con2.man_no and landing_jobs.job_no<>con2.job_no left outer join landing_JH job_history2 on job_history2.job_no=con2.job_no and job_history2.job_no<>landing_JH.job_no and job_history2.job_status='C' left outer join landing_jobs con3 on landing_jobs.man_no=con3.man_no and landing_jobs.job_no<>con3.job_no and con3.job_no<>con2.job_no LEFT join landing_jobs jmaster on landing_jobs.mas_no=jmaster.job_no
WHERE landing_jobs.type='JL' and brun.work_code<>'LOCAL' and brun.veh_code<>'SC' and brun.veh_code<>' ' AND landing_jobs.col_zone<>landing_jobs.del_zone AND jmaster.srv_code<>'B/T' and landing_jobs.leg_item=1
AND landing_jh.est_time >= '2013-07-01'
order by dep_mel_time
返回值:
man_no Run_Vehicle coldel dep_mel_time deliveries consignments con1trl1 con1trl2 con2trl1 con2trl2 Driver
349802 091V ADE - WAGG 7/05/2014 18:00 WAGGA 349835JL#1-COPE and 349832JL#1-BFL 445 2541 EY Peter
349802 091V ADE - WAGG 7/05/2014 18:00 WAGGA 349832JL#1-BFL and 349835JL#1-COPE 2541 445 EY Peter
349792 010V ADE - MEL 7/05/2014 18:00 COPEMEL 349808JL#1-BFL and 349806JL#1-COPE 2005 430 THIELE Laura
349792 010V ADE - MEL 7/05/2014 18:00 COPEMEL 349806JL#1-COPE and 349808JL#1-BFL 430 2005 THIELE Laura
349799 117V ADE - MEL 7/05/2014 18:00 BFLMEL / POLYDAN 349825JL#1-BFL and 349823JL#1-POLYAIRE 2611 5580 DESTRO Bill
349799 117V ADE - MEL 7/05/2014 18:00 POLYDAN / BFLMEL 349823JL#1-POLYAIRE and 349825JL#1-BFL 5580 2611 DESTRO Bill
349516 259 MEL - SYD 7/05/2014 19:00 CAMSYD 349455JL#1-CAMERON 2590 688 NULL NULL WAIN Sarah
349794 113V ADE - NHIL 7/05/2014 19:00 NHILL 349820JL#1-WA FR 2608 643 NULL NULL STEPNELL William
349766 047K SYD - MEL 7/05/2014 19:00 COSRIN 349686JL#1-COSTCO CC2622 CC5504 NULL NULL WINTERTON Mike
349753 039K SYD - MEL 7/05/2014 19:00 WAFGMELB 349661JL#1-WA FR 2619 716 NULL NULL SIDHU Marpreet
答案 0 :(得分:0)
您可以使用row_number()
执行所需操作。我建议:
with cte as (<your query here (without the `order by`)>)
select t.*
from (select cte.*, row_number() over (partition by man_no order by dep_mel_time desc) as seqnum
from cte
) t
where seqnum = 1;
作为一个注释,给定man_no
的记录对我来说看起来不像重复。我看到列中有很多不同之处。