【问题标题】:SQL: Flag Duplicate Records Using Case Statement WHERE FIELD VALUE1 <>FIELD VALUE 2 & FIELD VALUE1 DATE > FIELD VALUE2SQL:使用案例语句标记重复记录 WHERE FIELD VALUE1 <>FIELD VALUE 2 & FIELD VALUE1 DATE > FIELD VALUE2
【发布时间】:2015-05-07 05:29:11
【问题描述】:

我正在处理基于我正在使用的文本字段计算重复记录的 SQL 查询:其中 datasource = 'Web' 或 'Internal'。我目前正在使用 case 语句来计算记录显示该值的次数。我的问题是如何返回值(我正在考虑带有指示符(1 或 0)的 case 语句)来显示 datasource = 'Web' 和 date > datasource = 'Internal' 和 date 的位置?

web.datasource 日期 > internal.datasource 日期

我正在附加一个关于我目前正在工作的内容、我的输出是什么以及我希望最终结果是什么样子的查询。

SELECT id
,lastname
,firstname
,datasource
,CASE
WHEN (
    (Datasource = 'Web')
    )THEN Count(Datasource)
ELSE 0
    END WebData
,CASE
WHEN (
    (Datasource = 'Internal')
    ) THEN Count(Datasource)
ELSE 0
    END InternalData
,count(id) as countid
,date

FROM Table


GROUP BY
id
,lastname
,firstname
,datasource
,date

目前返回:

12345   Jack    Boss    Internal    0   1   1   2015-03-25
12241   Eric    Graves  Internal    0   1   1   2015-04-01
13300   Su      Lynn    Web         1   0   1   2016-02-01
13300   Su      Lynn    Internal    0   1   1   2015-08-07
13914   Mark    Ross    Internal    0   2   2   2015-05-01
14008   Mitch   Smith   Web         1   0   1   2016-03-07
14008   Mitch   Smith   Internal    0   1   1   2015-06-02

这就是我希望最终结果的样子:

12345   Jack    Boss    Internal    0   1   1   2015-03-25   0
12241   Eric    Graves  Internal    0   1   1   2015-04-01   0
13300   Su      Lynn    Web         1   0   1   2016-02-01   0
13300   Su      Lynn    Internal    0   1   1   2015-08-07   0
13914   Mark    Ross    Internal    0   2   2   2015-05-01   0
14008   Mitch   Smith   Web         1   0   1   2016-03-07   1
14008   Mitch   Smith   Internal    0   1   1   2015-06-02   1

14008   Mitch   Smith    1   1   2

想法?谢谢。

【问题讨论】:

  • 我不明白为什么除了您的示例中的最后两条记录之外的所有记录都是0,您能解释一下吗?也许扩展您的样本数据以显示重复项会有所帮助。另外,您使用的是哪个数据库?
  • 假设您指的是datasource 列,您是否使用union 将所有数据放入一个结果集中?
  • 我只是把 0,1 作为指标。可能是 True、False 或其他。 1 表示网络记录的日期大于内部记录
  • 我可以使用联合。无论你们认为/应该使用什么来使最终结果看起来像我上面发布的两个选项之一。在这方面运气不佳...
  • 你使用的是哪个数据库?

标签: sql duplicates subquery case distinct


【解决方案1】:

这可能会让你开始:

SELECT id
,lastname
,firstname
,datasource
,CASE
WHEN (
    (Datasource = 'Web')
    )THEN Count(Datasource)
ELSE 0
    END WebData
,CASE
WHEN (
    (Datasource = 'Internal')
    ) THEN Count(Datasource)
ELSE 0
    END InternalData
,count(id) as countid
,date
, sub_table.an_indicator

FROM Table
,   (   select  t2.id as id
,               case when h_table.web_date > h_table.internal_date 
                then 1 
                else 0 end as an_indicator
        from( select t2.id as id
              ,      max( date ) as web_date
              ,      null as internal_date
              from table t2
              where t2.id=Table.id
              and t2.lastname = Table.lastname
              and t2.firstname = Table.firstname
              and t2.datasource = 'Web' 
              group by t2.id
              union
              select t2.id
              ,      null
              ,      max( date ) 
              from table t2
              where t2.id=Table.id
              and t2.lastname = Table.lastname
              and t2.firstname = Table.firstname
              and t2.datasource = 'Internal' 
              group by t2.id
            ) h_table       
    ) sub_table
where sub_table.id = Table.id

【讨论】:

  • 这是一个很好的起点,但我设法在 union distinct 处得到一个错误,我不知道为什么我会得到......?从逻辑上看查询是有道理的。不太清楚我的问题是什么......
  • 尝试忽略不同的...只是联合。只要确保你不使用联合所有。
【解决方案2】:
select
    id, lastname, firstname, datasource
    case when Datasource = 'Web' then count(Datasource) else 0 end as WebData,
    case when Datasource = 'Internal' then count(Datasource) else 0 end as InternalData,
    count(id) as CountId,
    "date",
    min(dups.flag) as dup
from
    <table> as t
    inner join
    (
        select
            id, lastname, firstname,
            case
                when max(case when datasource = 'web' then "date" end) >
                     max(case when datasource = 'internal' then "date" end)
                then 1 else 0
            end as flag
        from <table>
        group by
            id, lastname, firstname
    ) as dups
        on      dups.id = t.id
            and dups.lastname = t.lastname and dups.firstname = t.firstname
group by
    id, lastname, firstname, datasource

这个有可能会起作用吗?

select
    id, lastname, firstname, datasource
    case when Datasource = 'Web' then count(Datasource) else 0 end as WebData,
    case when Datasource = 'Internal' then count(Datasource) else 0 end as InternalData,
    count(id) as CountId,
    "date",
    case when max(case when datasource = 'web'      then "date" end)
                  over (partition by id, lastname, firstname) >
              max(case when datasource = 'internal' then "date" end)
                  over (partition by id, lastname, firstname)
         then 1
         else 0
    end as dup
from
    <table> as t
group by
    id, lastname, firstname, datasource

【讨论】:

    猜你喜欢
    • 2020-12-05
    • 2011-02-24
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2015-06-05
    相关资源
    最近更新 更多