根据位标志计数频率

时间:2017-12-09 03:36:16

标签: sql

SELECT    

              ROW_NUMBER() OVER (PARTITION BY dicei.IsLocked ORDER BY DocumentInstanceChapterExpanded.PK_DocumentInstanceChapterExpanded)                 
             ,DocumentInstance.PK_DocumentInstance
             ,DocumentInstanceChapterExpanded.PK_DocumentInstanceChapterExpanded 
             ,dicei.IsLocked

FROM         DocumentInstance INNER JOIN
                      DocumentInstanceChapter ON DocumentInstance.PK_DocumentInstance = DocumentInstanceChapter.FK_DocumentInstance INNER JOIN
                      DocumentInstanceChapter AS DocumentInstanceChapter_1 ON 
                      DocumentInstanceChapter.PK_DocumentInstanceChapter = DocumentInstanceChapter_1.FK_DocumentInstanceChapter INNER JOIN
                      DocumentInstanceChapterExpanded ON 
                      DocumentInstanceChapter_1.PK_DocumentInstanceChapter = DocumentInstanceChapterExpanded.FK_DocumentInstanceChapter INNER JOIN
                      DocumentInstanceChapterExpanded AS DocumentInstanceChapterExpanded_1 ON 
                      DocumentInstanceChapter.PK_DocumentInstanceChapter = DocumentInstanceChapterExpanded_1.FK_DocumentInstanceChapter INNER JOIN
                      DocumentInstanceChapterExpandedItem AS dicei ON 
                      DocumentInstanceChapterExpanded.PK_DocumentInstanceChapterExpanded = dicei.FK_DocumentInstanceChapterExpanded
WHERE     (DocumentInstance.PK_DocumentInstance = 455) 
 AND DocumentInstanceChapterExpanded_1.PK_DocumentInstanceChapterExpanded = 50730

enter image description here

正如您所看到的,我想要做的是添加一个表示

的列
**Result Expected**
ExpandeditemKey IsLocked     StatusColumn
50797               0         Mixed
50797               0         Mixed 
50797               1         Mixed
50797               1         Mixed
50797               1         Mixed
50798               1         Lock
50798               1         Lock
50798               1         Lock

If it contains 0 and 1 'Mixed'
If it contains 1 only  'Lock'
If it contains 0 only  'Unlock'

它不一定需要是字符串列,我尝试使用OverBy子句如果我可以使用Partition by for Islock Bit Field但是不能 谢谢你看看。

1 个答案:

答案 0 :(得分:0)

我建议使用MIN / MAX作为窗口函数,然后你可以添加一个跨分区的case表达式。 e.g。

SELECT
      ROW_NUMBER() OVER (PARTITION BY dicei.IsLocked ORDER BY DocumentInstanceChapterExpanded.PK_DocumentInstanceChapterExpanded)
    , DocumentInstance.PK_DocumentInstance
    , DocumentInstanceChapterExpanded.PK_DocumentInstanceChapterExpanded
    , dicei.IsLocked
    , case when dicei.isLockedMin <> dicei.isLockedMax then 'Mixed'
           when dicei.isLockedMax = 0 then 'Unlocked'
           else 'Locked'
      end StatusColumn
FROM DocumentInstance
INNER JOIN DocumentInstanceChapter ON DocumentInstance.PK_DocumentInstance = DocumentInstanceChapter.FK_DocumentInstance
INNER JOIN DocumentInstanceChapter AS documentinstancechapter_1 ON DocumentInstanceChapter.PK_DocumentInstanceChapter = documentinstancechapter_1.FK_DocumentInstanceChapter
INNER JOIN DocumentInstanceChapterExpanded ON documentinstancechapter_1.PK_DocumentInstanceChapter = DocumentInstanceChapterExpanded.FK_DocumentInstanceChapter
INNER JOIN DocumentInstanceChapterExpanded AS documentinstancechapterexpanded_1 ON DocumentInstanceChapter.PK_DocumentInstanceChapter = documentinstancechapterexpanded_1.FK_DocumentInstanceChapter
INNER JOIN (
      select d.*
            , min(d.IsLocked) over(partition by d.FK_DocumentInstanceChapterExpanded) isLockedMin
            , max(d.IsLocked) over(partition by d.FK_DocumentInstanceChapterExpanded) isLockedMax
      from DocumentInstanceChapterExpandedItem d
      ) AS dicei ON DocumentInstanceChapterExpanded.PK_DocumentInstanceChapterExpanded = dicei.FK_DocumentInstanceChapterExpanded
WHERE (DocumentInstance.PK_DocumentInstance = 455)
AND documentinstancechapterexpanded_1.PK_DocumentInstanceChapterExpanded = 50730

编辑,由于位列,需要大小写表达式:

            , min(case when d.IsLocked = 1 then 1 else 0 end) over(partition by d.FK_DocumentInstanceChapterExpanded) isLockedMin
            , max(case when d.IsLocked = 1 then 1 else 0 end) over(partition by d.FK_DocumentInstanceChapterExpanded) isLockedMax