交叉应用于sql与sum

时间:2012-11-06 08:38:40

标签: sql-server sum cross-apply

我有以下查询:

insert into [MyDB].[dbo].[Reports_ActivityStat] (ActivityID,TaskID,LS_HowOthersAnswered,LS_ImproveMyChances)
(
SELECT
ActivityID=tasks.ActivityID,
TaskID=tasks.ID,
CAST(
        CASE 
            WHEN CHARINDEX('stats',item.value('(UsedLifeSavers)[1]', 'NVARCHAR(MAX)'))>0
                THEN 1 
            ELSE 0 
        END AS bit) as LS_HowOthersAnswered,
CAST(
        CASE 
            WHEN CHARINDEX('FiftyFifty',item.value('(UsedLifeSavers)[1]', 'NVARCHAR(MAX)'))>0
                THEN 1 
            ELSE 0 
        END AS bit) as LS_ImproveMyChances

FROM  [MyDB].[dbo].[Tasks] as tasks CROSS APPLY [Progress].nodes ('//Progress/Steps/ProgressStep') Progress(item)
)  

作用于以下Tasks表:

 ID  |   ActivityID    |      Progress  
  1  |        1        |  [example below..]  
  2  |        1        |  [example below..]

Progress是xml的那种:

<Progress xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <Steps>
    <ProgressStep>
      <FinishedOn>2012-10-30T13:07:52.6374861+02:00</FinishedOn>
      <Score>0</Score>
      <StartedOn>2012-10-30T13:07:45.8234861+02:00</StartedOn>
      <Status>Finished</Status>
      <StepIndex>0</StepIndex>
      <StepType>Summary</StepType>
      <UsedLifeSavers xmlns:a="http://schemas.datacontract.org/2004/07/MindLab.Logic.Study" />
    </ProgressStep>
    <ProgressStep>
      <FinishedOn i:nil="true" />
      <PartNumber>1</PartNumber>
      <Score>0</Score>
      <StartedOn>2012-10-30T13:07:52.6374861+02:00</StartedOn>
      <Status>NotFinished</Status>
      <StepIndex>1</StepIndex>
      <StepType>Information</StepType>
      <SubmittedAnswersCount>0</SubmittedAnswersCount>
      <UsedLifeSavers xmlns:a="http://schemas.datacontract.org/2004/07/MindLab.Logic.Study">
        <a:LifeSavers>Stats</a:LifeSavers>
        <a:LifeSavers>FiftyFifty</a:LifeSavers>
      </UsedLifeSavers>
    </ProgressStep>  
  </Steps>
</Progress>  

(通常有两个以上的步骤......)

我的查询得出(不是实际数据,只是样本):

 ID  |   ActivityID    |   TaskID   |    LS_HowOthersAnswered   |   LS_ImproveMyChances
  1  |        1        |      1     |             0             |             0
  2  |        1        |      1     |             1             |             0  
  3  |        1        |      1     |             0             |             0    

这几乎是我的需要,但并不完全 我需要所有SUMLS_HowOthersAnswered LS_ImproveMyChances的每个唯一任务ID 我试图分组,但无法成功,因为它与常规的内部联接太不同了......

1 个答案:

答案 0 :(得分:0)

发现:

insert into [MyDB].[dbo].[Reports_ActivityStat] (ActivityID,TaskID,LS_HowOthersAnswered,LS_ImproveMyChances)
(
SELECT
ActivityID=tasks.ActivityID,
TaskID=tasks.ID,    
SUM (CAST(
        CASE 
            WHEN CHARINDEX('stats',item.value('(UsedLifeSavers)[1]', 'NVARCHAR(MAX)'))>0
                THEN 1 
            ELSE 0 
        END AS int)
        ),

SUM (CAST(
        CASE 
            WHEN CHARINDEX('FiftyFifty',item.value('(UsedLifeSavers)[1]', 'NVARCHAR(MAX)'))>0
                THEN 1 
            ELSE 0 
        END AS int)
        )  

FROM  [MyDB].[dbo].[Tasks] as tasks CROSS APPLY [Progress].nodes ('//Progress/Steps/ProgressStep') Progress(item)
group by tasks.ID
)