SQL结果显示在两个查询中

时间:2018-08-27 06:01:30

标签: sql sql-server

我正在尝试返回人们签署特定调查的结果。但是,“当他们以前曾经回答过调查1并同时显示在调查1和调查2中时,就会遇到无法返回调查答案的问题。

如何通过选择最新的调查结果确保调查答案仅出现一次,以使它们不会同时出现在两个调查中?

以斜体显示的结果代表一家商店的重复记录,该商店回答了两个调查,但我只希望显示最近答复的调查。在这种情况下,它们应仅出现在调查1中,因为它是最新的

代码

go
use [database]

--Select Outlets that have answers to Survey 1
(select distinct activityanswers.CustomerNumber as Outlet, 'Survey 1' as 
'Survey Program', max(answereddate) as 'Last Answered Date'
from dbo.activityanswers
where activityid in (select id from activitys where ActivityGroupId = 
'1061293')              
group by customernumber
)


--Select Outlets that have answers to Survey 2
(select distinct activityanswers.CustomerNumber as Outlet, 'Survey 2' as 
'Survey Program', max(answereddate) as 'Last Answered Date'
from dbo.activityanswers
where activityid in (select id from activitys where ActivityGroupId = 
'1061294')                                                              
group by customernumber
)

调查1个结果

Store   Survey      AnswerTime
1285939 Survey 1    2018-08-27 10:13:57.000
1348372 Survey 1    2018-08-27 09:21:18.000
2142522 Survey 1    2018-08-27 15:26:29.000
2147380   Survey 1    2018-08-24 22:26:49.000

调查2个结果

Store   Survey      AnswerTime
2147380   Survey 2    2018-08-24 21:58:59.000
2641188 Survey 2    2018-08-27 11:39:31.000

4 个答案:

答案 0 :(得分:0)

您可以使用一条SQL语句获得结果。尝试使用Row_Number或Rank函数。

类似的事情可能会起作用

;WITH CTE
AS
(
SELECT
    RN =ROW_NUMBER() OVER(PARTITION BY ANS.CustomerNumber ORDER BY ANS.answereddate DESC,
ACT.ActivityGroupId ASC),
    ANS.CustomerNumber as Outlet, 
    CASE ACT.ActivityGroupId
        WHEN '1061293' THEN 'Survey 1' 
        ELSE 'Survey 2' END as 'Survey Program', 
    ANS.answereddate as 'Last Answered Date'
    FROM dbo.ActivityAnswers ANS
        INNER JOIN Activitys ACT
            ON ANS.activityid = ACT.ID
        WHERE ACT.ActivityGroupId IN 
            (
                '1061293',
                '1061294'
            )
)
SELECT
    *
    FROM CTE
        WHERE RN = 1

答案 1 :(得分:0)

我认为您应该在分组后进行过滤,请尝试如下操作:

select a.CustomerNumber as Outlet, a.Last_Answered_Date from (
select CustomerNumber, max(answereddate) as 'Last_Answered_Date'
from activityanswers 
group by customernumber) a
join activityanswers b on a.CustomerNumber = b.CustomerNumber and a. 
[Last_Answered_Date] = b.answereddate
where b.activityid in (select id from activitys where ActivityGroupId = '1061294') 

答案 2 :(得分:0)

您可以通过检查特定客户的答案是否较新(不存在子查询)来做到这一点。这样,您还可以消除分组依据。

select
    CustomerNumber as Outlet, 
    'Survey 1' as 'Survey Program', 
    answereddate as 'Last Answered Date'
from dbo.activityanswers a
where activityid in (
    select id from activitys where ActivityGroupId = '1061293')
and not exists (
    select from dbo.activityanswers b 
    where b.CustomerNumber = a.CustomerNumber
    and b.answereddate > a.answereddate)

答案 3 :(得分:0)

也许子查询可以满足您的目的!

SELECT 
  Outlet
, SurveyProgram
, LastAnsweredDate  
FROM (
SELECT
    ans.CustomerNumber  Outlet
,   CASE WHEN ActivityGroupId = '1061293' THEN 'Survey 1' ELSE 'Survey 2' END   SurveyProgram
,   answereddate LastAnsweredDate
,   ROW_NUMBER() OVER(PARTITION BY ans.CustomerNumber ORDER BY answereddate DESC) RN 
FROM 
    activityanswers ans
LEFT JOIN activitys act ON act.ID = ans.activityid AND ActivityGroupId IN('1061293', '1061294') 
GROUP BY 
    ans.CustomerNumber
) D
WHERE 
    RN = 1

我已将IN()替换为LEFT JOIN,这在您的查询中是更好的方法。另外,从查询中可以得到调查1和2,因此我们使用ROW_NUMBER进行过滤。我在DESC中订购了它们,因此最近的日期时间将排在最前面。因此,从每个CustomerNumber中获取第一行将为您提供最近的记录。 。这将为您提供更大的查询灵活性。