如何编写SQL语句以比较昨天的当前日期和日期。表具有相同的列。我需要检查选择的两个日期在哪一列中哪些数据不同,并显示两个表之间的变化。
这是我开始的存储过程
CREATE PROCEDURE [dbo].[usp_CompareNetworksandRatings]
@media_month_id smallint,
@rating_category_id tinyint,
@rate_date datetime
AS
BEGIN
SET NOCOUNT ON;
SELECT DISTINCT
nn.nielsen_network_id,
nn.code,
nn.name
FROM
daily_ratings dr (NOLOCK)
JOIN media_months mm (NOLOCK) ON mm.id=dr.media_month_id
AND mm.id=@media_month_id
JOIN uvw_nielsen_network_universes nn (NOLOCK) ON nn.nielsen_network_id=dr.nielsen_network_id
AND nn.start_date<=mm.start_date AND (nn.end_date>=mm.start_date OR nn.end_date IS NULL)
WHERE
dr.rating_category_id=@rating_category_id
AND dr.media_month_id=@media_month_id
AND dr.rating_date=@rate_date
ORDER BY
nn.code
答案 0 :(得分:0)
您需要为要检查的每个列创建一个案例构造。像这样:
select case when t2.f1 <> t1.f1 then 'yes' else 'no' end f1_changed