0

我将不胜感激任何帮助。我正在尝试对 2 个子查询执行连接,但它一直说“IntEncTracking.EncounterList.ClaimId”在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中,但我已经尝试使用 el 和 q2 对其进行限定,但无济于事。任何建议将不胜感激。

将@SweepId 声明为 int = 10160,@RunType 声明为 varchar = 'Initial'

Select * from (Select distinct ClaimId
     , LineNum
     , 0 as EncounterLineNum
     , EncounterType
     , InternalICN
     , PreviousDpwICN
     , 0 as ClaimFrequencyCd
     , EncounterPeriod
     , 2 as StatusCd
     , BypassReason
     , null as EncounterFileNm
     ,@SweepId as SweepId
  from IntEncTracking.EncounterList
 where bypassflag = 1) as q1

 join

 ( Select  ClaimId
     , 0 as LineNum
     , 0 as EncounterLineNum
     , EncounterType
     , InternalICN
     , PreviousDpwICN
     , max(ClaimFreqCd) as ClaimFreqCd
     , max(EncounterPeriod) as EncounterPeriod
     , case when exists (select 'x' 
                           from IntEncTracking.EncounterList el1 
                          where el1.claimid = claimid
                            and BypassFlag = 0) then 1 
            else 2 
         end stscd
     , case when @RunType = 'Initial' then 100 else 300 end as [StatusReasonCd]
     , null as EncounterFileNm
     , @SweepId as SweepId
  from IntEncTracking.EncounterList el
 where BypassFlag = 0) as q2

  on q1.ClaimId = q2.ClaimId and
     q1.LineNum = q2.LineNum and
     q1.EncounterLineNum = q2.EncounterLineNum and
     q1.EncounterType = q2.EncounterType and
     q1.InternalICN = q2.InternalICN  
     group by q1.ClaimId, q1.EncounterType, q1.InternalICN, q1.PreviousDpwICN      
     order by q2.ClaimId, q2.LineNum, q2.EncounterLineNum, q2.EncounterType, q2.InternalICN
4

2 回答 2

0

这只是一个语法..

Declare @SweepId as int = 10160, @RunType as varchar = 'Initial' 

select *

from   (Select distinct ClaimId
     , LineNum
     , EncounterLineNum
     , 0
     , EncounterType
     , InternalICN
     , PreviousDpwICN
     , 0
     , EncounterPeriod
     , 2
     , BypassReason
     , null
     ,@SweepId
  from IntEncTracking.EncounterList
 where bypassflag = 1) as q1

join    
     (Select  ClaimId
     , 0 as LineNum
     , 0 as EncounterLineNum
     , EncounterType
     , InternalICN
     , PreviousDpwICN
     , max(ClaimFreqCd) as ClaimFreqCd
     , max(EncounterPeriod) as EncounterPeriod
     , case when exists (select 'x' 
                           from IntEncTracking.EncounterList el1 
                          where el1.claimid = claimid
                            and BypassFlag = 0) then 1 
            else 2 
         end stscd
     , case when @RunType = 'Initial' then 100 else 300 end as [StatusReasonCd]
     , null as EncounterFileNm
     , @SweepId
  from IntEncTracking.EncounterList el
 where BypassFlag = 0) as q2


 on q1.ClaimId = q2.ClaimId and
 on q1.LineNum = q2.LineNum and
 on q1.EncounterLineNum = q2.EncounterLineNum and
 on q1.EncounterType = q2.EncounterType and
 on q1.InternalICN = q2.InternalICN 
 group by ClaimId, encountertype, internalicn, PreviousDpwICN
  order by ClaimId, LineNum, EncounterLineNum,EncounterType,InternalICN
于 2013-07-16T19:43:51.103 回答
0

我总是做这样的事情

    ;with EncounterValues as(
    SELECT
    CommonValueOrId,
    ect..
    FROM
    IntEntTracking.EncountrList
    )

    , EncounterFileNums as(
    SELECT
    CommonValueOrId,
    ect..
    FROM
    IntEncTracking.EncounterList
    )

    SELECT
    ev.AnyField,
    efn.AnyField
    FROM
    EncounterValues ev
    LEFT JOIN EncounterFileNums efn
    ON ev.CommonValueOrId = evn.CommonValueOrId

不确定以这种方式执行此操作的含义是什么,但如果您遇到困难,可能值得一试。

于 2013-07-16T19:54:01.190 回答