SQL-如何识别多个键值的排列

SQL-如何识别多个键值的排列,sql,sql-server,count,permutation,Sql,Sql Server,Count,Permutation,感谢您的审阅。我非常需要帮助,甚至很难弄清楚下面的问题 使用SQL2008,我设置了一个包含五列的表 with t as ( select 'a' as a, 'b' as b, 'c' as c union all select 'a', 'b', 'd' union all select 'e', 'b', 'd' ), perms as ( select 1 as col1, 1 as col2, 1 as col3 un

感谢您的审阅。我非常需要帮助,甚至很难弄清楚下面的问题

使用SQL2008,我设置了一个包含五列的表

with t as (
      select 'a' as a, 'b' as b, 'c' as c union all
      select 'a', 'b', 'd' union all
      select 'e', 'b', 'd'
     ),
     perms as (
      select 1 as col1, 1 as col2, 1 as col3 union all
      select 1, 1, 0 union all
      select 1, 0, 0 union all
      select 0, 1, 1 union all
      select 0, 1, 0 union all
      select 0, 0, 1
     )
select (case when p.col1 = 1 then a end) as col1,
       (case when p.col2 = 1 then b end) as col2,
       (case when p.col3 = 1 then c end) as col3,
       count(*)
from t cross join
     perms p
group by (case when p.col1 = 1 then a end),
         (case when p.col2 = 1 then b end),
         (case when p.col3 = 1 then c end)
数据实际上如下所示:

column1 column2 column3 column4 column5
T1      N1      A1      L1      S1
T1      N2      A2      L2      S4
T1      N2      A3      L2      S2
T1      N2      A1      L2      S4
T2      N6      A3      L3      S2
T2      N7      A3      L3      S4
T2      N7      A3      L4      S4
...
对于具有相同
column1
值的记录,我想通过
column5
识别
column2
的每个唯一排列

我想回答这样的问题:

column1 column2 column3 column4 column5
T1      N1      A1      L1      S1
T1      N2      A2      L2      S4
T1      N2      A3      L2      S2
T1      N2      A1      L2      S4
T2      N6      A3      L3      S2
T2      N7      A3      L3      S4
T2      N7      A3      L4      S4
...
案例1

哪里有多少记录

column1 is the same value and
column2 is the same value and
column3 is the same value and
column4 is the same value and
column5 is different
然后

案例2

column1 is the same value and
column2 is the same value and
column3 is the same value and
column4 is different and
column5 is the same value
然后

案例3

column1 is the same value and
column2 is the same value and
column3 is the same value and
column4 is different and
column5 is different

等等。谢谢

嗯。如果你想用手来做的话,可以用多个键来做。例如,对于案例1,您应该执行以下操作:

select column1,column2,column3, column4, count(column5) group by column1,column2,column3, column4
这应该为第1列到第4列的每个唯一组合提供唯一的记录。如果您想要区分,那么您可能需要进行一些预处理


如果您想要一个更灵活的存储过程,那么执行分组的存储过程可能是一个解决方案。

嗯。如果您想手动执行,应该使用类似于多个键的方法。例如,对于案例1,您应该执行以下操作:

select column1,column2,column3, column4, count(column5) group by column1,column2,column3, column4
这应该为第1列到第4列的每个唯一组合提供唯一的记录。如果您想要区分,那么您可能需要进行一些预处理


如果您想要一个更灵活的存储过程,那么执行group by可能是一个解决方案。

您可以在一个查询中完成这项工作,方法是在查询中设置所需的排列表和一些
case
逻辑

下面是一个包含三列的示例

with t as (
      select 'a' as a, 'b' as b, 'c' as c union all
      select 'a', 'b', 'd' union all
      select 'e', 'b', 'd'
     ),
     perms as (
      select 1 as col1, 1 as col2, 1 as col3 union all
      select 1, 1, 0 union all
      select 1, 0, 0 union all
      select 0, 1, 1 union all
      select 0, 1, 0 union all
      select 0, 0, 1
     )
select (case when p.col1 = 1 then a end) as col1,
       (case when p.col2 = 1 then b end) as col2,
       (case when p.col3 = 1 then c end) as col3,
       count(*)
from t cross join
     perms p
group by (case when p.col1 = 1 then a end),
         (case when p.col2 = 1 then b end),
         (case when p.col3 = 1 then c end)

您可以在一个查询中实现这一点,方法是在查询中设置所需排列的表和一些
case
逻辑

下面是一个包含三列的示例

with t as (
      select 'a' as a, 'b' as b, 'c' as c union all
      select 'a', 'b', 'd' union all
      select 'e', 'b', 'd'
     ),
     perms as (
      select 1 as col1, 1 as col2, 1 as col3 union all
      select 1, 1, 0 union all
      select 1, 0, 0 union all
      select 0, 1, 1 union all
      select 0, 1, 0 union all
      select 0, 0, 1
     )
select (case when p.col1 = 1 then a end) as col1,
       (case when p.col2 = 1 then b end) as col2,
       (case when p.col3 = 1 then c end) as col3,
       count(*)
from t cross join
     perms p
group by (case when p.col1 = 1 then a end),
         (case when p.col2 = 1 then b end),
         (case when p.col3 = 1 then c end)

您可以使用
count(独立列5)
我将测试并报告。非常感谢。您可以使用
count(独立列5)
我将测试并报告。非常感谢。谢谢戈登,我也会试试这个,然后回来汇报。非常有帮助!谢谢Gordon,我也会尝试一下,然后回来汇报。非常有帮助!