0
ID  GROUP       NAME
1   1,2,      Supreeth
2   1,2,5,      Aishu
3   3,         Arvi
4   4,5,       Gani
5   4,3,2,      Jyo
6   3,2,1,      Upi
7   2,3,4,1,5,  Savi

我有这样的表,我正在尝试获取组数

    DECODE    Count
1   A         4
2   B         5
3   C         4
4   D         3
5   E         3

我想将 group 的值解码为 DECODE 值,例如 1 到 A 、 2 到 B 、3 到 C 、 4 到 D 、 5 到 E ..

我尝试这样但得到错误

select  count(*) from s_c where age like '%1%' and '%2%' and '%3%';
4

3 回答 3

3

一种简单(不一定有效)的方法是获取每个组值的单独计数。请注意,列名Group使用的是保留字,因此您必须使用适当的分隔标识符表示法。当您使用可移植(SQL 标准)双引号时,您必须使标识符的大小写正确;我假设小写 - YMMV。

SELECT 1 AS GroupID, 'A' AS Decode, COUNT(*) AS GroupCount
  FROM AnonymousTable
 WHERE "group" LIKE '%1%'
 GROUP BY GroupID, Decode
UNION
SELECT 2 AS GroupID, 'B' AS Decode, COUNT(*) AS GroupCount
  FROM AnonymousTable
 WHERE "group" LIKE '%1%'
 GROUP BY GroupID, Decode
UNION
SELECT 3 AS GroupID, 'C' AS Decode, COUNT(*) AS GroupCount
  FROM AnonymousTable
 WHERE "group" LIKE '%1%'
 GROUP BY GroupID, Decode
UNION
SELECT 4 AS GroupID, 'D' AS Decode, COUNT(*) AS GroupCount
  FROM AnonymousTable
 WHERE "group" LIKE '%1%'
 GROUP BY GroupID, Decode
UNION
SELECT 5 AS GroupID, 'E' AS Decode, COUNT(*) AS GroupCount
  FROM AnonymousTable
 WHERE "group" LIKE '%1%'
 GROUP BY GroupID, Decode

但这不能很好地扩展。再加5组,非常不爽;加 500 是无法管理的。您最好将数据存储在正确规范化的表中,然后可以使用更简单的 SQL 进行分析。


另一种模式设计和查询

Users                     UserGroups          Groups
ID        Name            UserID GroupID      ID         Decode
1         Supreeth        1      1            1          A
2         Aishu           1      2            2          B
3         Arvi            2      1            3          C
4         Gani            2      2            4          D
5         Jyo             2      5            5          E
6         Savi            3      3
                          4      4
                          4      5
                          ...

这是简化的查询,它的性能可能会比原始查询好很多,并且可以扩展到任意数量的组(如果需要,可以扩展到数百万组):

SELECT u.GroupID, g.Decode, COUNT(*) AS Count
  FROM UserGroups AS u
  JOIN Groups     AS g ON u.GroupID = g.ID
 GROUP BY u.GroupID, g.Decode

标准化让生活更轻松——这是这样做的原因之一!

于 2013-07-24T16:02:28.463 回答
1

此解决方案可能不好或不灵活,因为它取决于硬编码值,但它实现了问题中描述的所需输出:

SELECT 1 AS [Num], 'A' AS Decode, COUNT(*) AS [Count] FROM my_table WHERE [Group] LIKE '%1%'
UNION
SELECT 2 AS [Num], 'B' AS Decode, COUNT(*) AS [Count] FROM my_table WHERE [Group] LIKE '%2%'
UNION
SELECT 3 AS [Num], 'C' AS Decode, COUNT(*) AS [Count] FROM my_table WHERE [Group] LIKE '%3%'
UNION
SELECT 4 AS [Num], 'D' AS Decode, COUNT(*) AS [Count] FROM my_table WHERE [Group] LIKE '%4%'
UNION
SELECT 5 AS [Num], 'E' AS Decode, COUNT(*) AS [Count] FROM my_table WHERE [Group] LIKE '%5%'
于 2013-07-24T16:03:16.817 回答
0

我知道这已经得到了回答,但我想分享我的观点。

我所做的实际上是将逗号分隔的列拆分为行,然后我只是应用计数。至于解码,我没有手动完成这项工作,而是将数字添加到 64 并应用 CHAR 函数,该函数将返回 ASCII 表的大写字母......

SELECT Decode,
       COUNT(*)
FROM
(
    SELECT ID,
           CHAR(64+CONVERT(INT,String)) AS Decode,
           string
    FROM
    (
           SELECT A.[id],  
                Split.a.value('.', 'VARCHAR(100)') AS String  
           FROM  
               (
               SELECT id,
                      CAST ('<M>' + REPLACE(SUBSTRING([GROUP],0,LEN([GROUP])), ',', '</M><M>') + '</M>' AS XML) AS String  
               FROM  Table1
               ) AS A CROSS APPLY String.nodes ('/M') AS Split(a)
     ) T1
  ) T2
 GROUP BY Decode
 ORDER BY Decode

只是为了让你知道我到了Column To Rows

我还在这里创建了一个小提琴

我昨天会给出我的答案,但我全神贯注......对不起

扬尼斯

于 2013-07-25T07:56:48.800 回答