我正在处理SQL查询,该查询计算基于我正在使用的文本字段的重复记录:其中datasource =' Web'或者'内部'。我目前正在使用案例陈述来计算记录显示该值的次数。我的问题是如何返回值(我用一个指示符(1或0)来思考一个case语句),它显示了datasource =' Web'和日期> datasource ='内部'和约会?
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
OR
14008 Mitch Smith 1 1 2
想法?感谢。
答案 0 :(得分:0)
这可能会让你开始:
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
答案 1 :(得分:0)
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