【问题标题】:Select only the records with same values仅选择具有相同值的记录
【发布时间】:2021-03-22 07:44:27
【问题描述】:

我正在编写将成为视图一部分的 SQL 语句。我需要的是只提取两次具有相同唯一键的记录。查询现在如下所示。

select distinct 
    rscmaster_no_in, rsc_no_in, calendar_year, calendar_month, 
    Wstat_Abrv_Ch,
    h.Wstat_no_in, Staffing_Calendar_Date, payhours,
    l.OTStatus
from 
    vw_all_ts_hire h
left join 
    MCFRS_OTStatus_Lookup l on l.wstat_no_in = h.Wstat_no_in
where 
    rscmaster_no_in in (select rscmaster_no_in from vw_rsc_ECC_splty) 
    and Wstat_Abrv_Ch <> ''
    and h.Wstat_no_in in (103, 107)
    and l.OTStatus in ('ECCOTRemove', 'ECCOTSignup')
    and Staffing_Calendar_Date = '2020-11-01' -- only for the testing purposes. Will be removed later.
order by 
    RscMaster_no_in

我从上面的查询得到的结果是:

我需要修改 SQL 语句,使最终结果如下所示:

我怎样才能修改上面的语句以吐出这样的最终结果?

【问题讨论】:

  • 请阅读this,了解一些改进问题的技巧。数据图片让您更难为您提供帮助。

标签: sql sql-server tsql count


【解决方案1】:

这可能对你有帮助:

select * from (
select distinct 
    rscmaster_no_in, rsc_no_in, calendar_year, calendar_month, 
    Wstat_Abrv_Ch,
    h.Wstat_no_in, Staffing_Calendar_Date, payhours,
    l.OTStatus,
    SELECT COUNT(*) OVER (PARTITION BY rscmaster_no_in) AS uinqueCount
from 
    vw_all_ts_hire h
left join 
    MCFRS_OTStatus_Lookup l on l.wstat_no_in = h.Wstat_no_in
where 
    rscmaster_no_in in (select rscmaster_no_in from vw_rsc_ECC_splty) 
    and Wstat_Abrv_Ch <> ''
    and h.Wstat_no_in in (103, 107)
    and l.OTStatus in ('ECCOTRemove', 'ECCOTSignup')
    and Staffing_Calendar_Date = '2020-11-01' -- only for the testing purposes. Will be removed later.
) innerReult
where uinqueCount=2 --Or uinqueCount>1 base on your business
order by 
    RscMaster_no_in

【讨论】:

  • 这与现有的 3 个答案有何不同/更好?
  • 这是基于问题的完整答案。其他答案讨论了如何处理,但这个答案显示了如何在当前问题中使用它
【解决方案2】:

使用解析count(*) over () function

with cte as (
  select
      count(*) over (partition by YourUniqueKey) as MyRowCount
  {rest of your query}
)
select *
from cte
where MyRowCount = 2;

【讨论】:

    【解决方案3】:

    可以使用COUNT(*) OVER ()等窗口函数

    SELECT *
      FROM
      (
       SELECT COUNT(*) OVER (PARTITION BY rscmaster_no_in) AS cnt,
              t.*
         FROM tab t
       ) t
     WHERE cnt>1
       AND OTStatus = 'ECCOTRemove'
    

    【讨论】:

      【解决方案4】:

      这应该会给你想要的结果(性能取决于索引/表设计)。

      这会将您的核心逻辑放入子选择中,该子选择仅返回计数 > 1 的记录。

      然后使用这些 ID 选择您需要的所有数据,但仅适用于 count > 1 的子选择中的那些 ID

      select distinct rscmaster_no_in,rsc_no_in, calendar_year, calendar_month, 
      Wstat_Abrv_Ch, h.Wstat_no_in, Staffing_Calendar_Date, payhours ,l.OTStatus
      from vw_all_ts_hire h
      left join MCFRS_OTStatus_Lookup l on l.wstat_no_in = h.Wstat_no_in
      WHERE rscmaster_no_in IN (
          SELECT rscmaster_no_in
          from vw_all_ts_hire h
          left join MCFRS_OTStatus_Lookup l on l.wstat_no_in = h.Wstat_no_in
          where rscmaster_no_in in (select rscmaster_no_in from vw_rsc_ECC_splty) 
          and Wstat_Abrv_Ch <> ''
          and h.Wstat_no_in in (103, 107)
          and l.OTStatus in ('ECCOTRemove', 'ECCOTSignup')
          and Staffing_Calendar_Date = '2020-11-01' -- only for the testing purposes. Will be removed later.
          GROUP BY rscmaster_no_in
          HAVING COUNT(*) > 1
      ) 
      order by RscMaster_no_in
      

      【讨论】:

        猜你喜欢
        • 2018-12-06
        • 2019-01-28
        • 2021-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-10
        • 2016-01-26
        • 1970-01-01
        相关资源
        最近更新 更多