0

次のクエリがあります。

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>  

(通常は 2 つ以上のステップがあります..)

私のクエリは(実際のデータではなく、単なるサンプルです):

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

これはほとんど必要なものですが、完全ではありません。
一意の TaskID ごとSUMに allLS_HowOthersAnsweredとが必要ですLS_ImproveMyChances
グループ化しようと思ったのですが、普通の内部結合とは違いすぎてできませんでした..

4

1 に答える 1

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
)
于 2012-11-06T11:31:12.350 に答える