select KeywordId,currentposition,PsnUpdateDate,PsnUpdateBy from seo.Tbl_KeywordPosition where psnupdatedate = '2015-01-22'
select KeywordId,currentposition,PsnUpdateDate,PsnUpdateBy from seo.Tbl_KeywordPosition where psnupdatedate = '2015-01-23'
1456 10 2015-01-22 00:00:00.000 Ananth
1467 8 2015-01-22 00:00:00.000 gangabhavani
1468 10 2015-01-22 00:00:00.000 admin
1456 9 2015-01-23 00:00:00.000 Ananth
1467 11 2015-01-23 00:00:00.000 gangabhavani
1468 9 2015-01-23 00:00:00.000 admin
输出需要=
KeywordId, oldPosition newposition PsnUpdateBy
1456 10 9 Ananth
1467 8 11 gangabhavani
我给两个输入旧日期和新日期。我想在明智和用户方面看到日期的差异。
答案 0 :(得分:2)
合并这两个语句的最简单方法是:
select old.KeywordId, oldposition, newposition, old.PsnUpdateBy
from
(select KeywordId,currentposition as oldposition, PsnUpdateBy
from seo.Tbl_KeywordPosition
where psnupdatedate = '2015-01-22' ) as old
inner join
(select KeywordId, currentposition as newposition, PsnUpdateBy
from seo.Tbl_KeywordPosition
where psnupdatedate = '2015-01-23' ) as new
on old.KeywordId = new.KeywordId
答案 1 :(得分:1)
使用select
Conditional Aggregate
语句
SELECT KeywordId,
Max(CASE
WHEN PsnUpdateDate = '2015-01-22' THEN currentposition
END) Old_Position,
Max(CASE
WHEN PsnUpdateDate = '2015-01-23' THEN currentposition
END) New_Position,
PsnUpdateBy
FROM seo.Tbl_KeywordPosition
WHERE psnupdatedate IN( '2015-01-22', '2015-01-23' )
GROUP BY KeywordId,
PsnUpdateBy
答案 2 :(得分:1)
您可以使用UNION operator
SELECT KeywordId,currentposition,PsnUpdateDate,PsnUpdateBy
FROM seo.Tbl_KeywordPosition
WHERE psnupdatedate = '2015-01-22'
UNION
SELECT KeywordId,currentposition,PsnUpdateDate,PsnUpdateBy
FROM seo.Tbl_KeywordPosition
WHERE psnupdatedate ='2015-01-23';