在MS SQL中遇到问题

时间:2018-09-10 12:55:11

标签: sql sql-server tsql

我有一个表,其成员号,姓名,full_desc和activity_type如下所示

MEM_NO  Name    full_desc                 Activity_type
1233    John    LM Application received     9097
1233    John    Retired                     9630
1234    Mark    LM Application received     9097
3325    Steve   LM Application received     9097
3325    Steve   Retired                     9630
6566    Gareth  Retired                     9630
7088    Daniel  Resign                      9629

我希望输出为

MEM_NO  Name    full_desc           Activity_type     Status
1233    John    LM Application received 9097       LM Application received
1234    Mark    LM Application received 9097       LM Application received
3325    Steve   LM Application received 9097       LM Application received
6566    Gareth  Retired                 9630        No LM Recevied
7088    Daniel  Resign                  9629        No LM Recevied

注意:-

1)我想显示附加了activity_type 9097和9630或9629的不同成员,其状态应为“收到LM应用程序”

2)仅附加了activity_type 9097的成员也应具有“已收到LM应用程序”状态

3)未附加activity_type 9097但具有9630或9326的成员,然后将其状态显示为“未收到任何LM”。

SELECT MBR.MEMBERSHIP_NO,IND.TITLE,IND.FORENAMES,IND.DATE_OF_BIRTH,DATEADD(dd, 0, DATEDIFF(dd, 0, AL.ACTION_DATE)) as 'Date',LU1.FULL_DESC,COUNT(mbr.MEMBERSHIP_NO) as Total_Retired, null as Life_app_received,null as Total_Resign,
CASE WHEN AL.ACTIVITY_TYPE in (9630,9097) THEN  'LM Application received' ELSE 'NO LIFE' END AS STATUS
 ---AL.ACTIVITY_DATE,
FROM INDIVIDUAL IND
JOIN ACTIVITY_LOG AL ON AL.INDIVIDUAL_REF=IND.INDIVIDUAL_REF
JOIN LOOKUP_TABLE LU1 ON LU1.LOOKUP_TABLE_REF=AL.ACTIVITY_TYPE-----------------------MEM - 0 - Future Cancellation - Retired
JOIN MEMBER MBR ON MBR.INDIVIDUAL_REF=IND.INDIVIDUAL_REF
WHERE  AL.ACTIVITY_TYPE IN (9630,9097) AND  AL.ACTION_DATE BETWEEN '2017-01-01' AND '2020-12-31' --AND MBR.MEMBER_STATUS=33
GROUP BY MBR.MEMBERSHIP_NO,IND.TITLE,IND.FORENAMES,IND.DATE_OF_BIRTH,DATEADD(dd, 0, DATEDIFF(dd, 0, AL.ACTION_DATE)),LU1.FULL_DESC,AL.ACTIVITY_TYPE

以上是我的查询,未能带来我想要的东西。任何有帮助的求助

3 个答案:

答案 0 :(得分:2)

这建议我row_number()使用条件排序功能:

select top (1) with ties t.*, (case when Activity_type =  9097 
                                    then t.full_desc else 'No LM Received' 
                               end) as Status
from table t
order by row_number() over (partition by mem_no 
                                order by (case when Activity_type =  9097 
                                               then 0 else 1 
                                          end)
                           );

答案 1 :(得分:0)

尝试使用row_number()

select *,case when full_desc='LM Application received' then 'LM Application received' else 'No LM Recevied' end as status from 
(select mem_no,name,full_desc,activity_type,row_number() over(partition by mem_no,name order by case when full_desc='LM Application received' then 2 when full_desc='Retired' then 1
when full_desc='Resign' then 0 end desc) as rn
from tablename) a where rn=1

答案 2 :(得分:0)

请检查以下查询并尝试理解。 我将其与临时表一起使用,可以将其替换为原始表的名称。

IF OBJECT_ID('tempdb..#Tempdata') IS NOT NULL
    DROP TABLE #Tempdata

create table #Tempdata
(
    Mem_no int,
    Name varchar(250),
    full_desc varchar(max),
    activity_type int
)

INSERT INTO #Tempdata
select '1233','John','LM Application received','9097' UNION
select '1233','John','Retired','9630'  UNION
select '1234','Mark','LM Application received','9097'   UNION
select '3325','Steve','LM Application received','9097' UNION
select '3325','Steve','Retired','9630' UNION
select '6566','Gareth','Retired','9630' UNION
select '7088','Daniel','Resign','9629'

select *, CASE WHEN activity_type = '9097' THEN 'LM Application received' ELSE 'No LM Recevied' END  as Status from (
select ROW_NUMBER() OVER(PARTITION BY Mem_no,Name ORDER BY Mem_no,Name) as Rnk,
Mem_no , Name ,full_desc ,activity_type  from #Tempdata
) as D
where D.Rnk = 1