查询以计算每列的类似值

时间:2013-08-02 05:49:41

标签: sql sql-server sql-server-2012

请帮助您查询下表

╔══════════════╗
║ A  B   C   D ║
╠══════════════╣
║ N  E   1  -5 ║
║ N  N   4  -3 ║
║ N  -4  6   E ║
║ 3  E   E  -8 ║
║ 1  N   N   N ║
╚══════════════╝

我需要输出如下

╔═════════════════════════════════╗
║    N    E     NegativeValues    ║
╠═════════════════════════════════╣
║ A  3    0     0                 ║
║ B  2    2     1                 ║
║ C  1    1     0                 ║
║ D  1    1     3                 ║
╚═════════════════════════════════╝

我需要得到N和E的计数以及所有列的负值。请帮忙。提前致谢

更新 这里N是NULL,E以字符串“-1E + 70”

开头

3 个答案:

答案 0 :(得分:7)

尝试取消旋转,然后有条件地计算行数 像这样:

SELECT ABC, 
       Sum(CASE 
             WHEN VALS = 'n' THEN 1 
             ELSE 0 
           END) n, 
       Sum(CASE 
             WHEN VALS = 'e' THEN 1 
             ELSE 0 
           END) E, 
       Sum(CASE 
             WHEN LEFT(VALS, 1) = '-' THEN 1 
             ELSE 0 
           END) NegativeValues 
FROM   TABLE1 
       UNPIVOT (VALS 
               FOR ABC IN ([A], 
                           [B], 
                           [C], 
                           [D]))t 
GROUP  BY ABC 

查看SQL Fiddle上的工作示例 祝你好运!


考虑到'n'值实际上是NULL并且UNPIVOT函数丢弃NULL值并且'e'值实际上是'-1e',这里是查询的更新版本:

SELECT ABC, 
       Sum(CASE 
             WHEN VALS = 'NULL' THEN 1 
             ELSE 0 
           END) n, 
       Sum(CASE 
             WHEN VALS = '-1e' THEN 1 
             ELSE 0 
           END) E, 
       Sum(CASE 
             WHEN LEFT(VALS, 1) = '-' 
                  AND Charindex('e', VALS) = 0 THEN 1 
             ELSE 0 
           END) NegativeValues 
FROM   (SELECT Isnull(A, 'NULL')A, 
               Isnull(B, 'NULL')B, 
               Isnull(C, 'NULL')C, 
               Isnull(D, 'NULL')D 
        FROM   TABLE1)T 
       UNPIVOT (VALS 
               FOR ABC IN ([A], 
                           [B], 
                           [C], 
                           [D]))t 
GROUP  BY ABC 

这是SQL Fiddle的更新链接。


修改 使用新信息-1e实际上是科学数字,并且所有字段都是浮点类型,这是我更新的解决方案(希望最终):

SELECT ABC, 
       Sum(CASE 
             WHEN VALS = 'NULL' THEN 1 
             ELSE 0 
           END) n, 
       Sum(CASE 
             WHEN VALS LIKE '-1e+070%' THEN 1 
             ELSE 0 
           END) E, 
       Sum(CASE 
             WHEN LEFT(VALS, 1) = '-' 
                  AND Charindex('e', VALS) = 0 THEN 1 
             ELSE 0 
           END) NegativeValues 
FROM   (SELECT Isnull(Cast(A AS VARCHAR(10)), 'NULL')A, 
               Isnull(Cast(B AS VARCHAR(10)), 'NULL')B, 
               Isnull(Cast(C AS VARCHAR(10)), 'NULL')C, 
               Isnull(Cast(D AS VARCHAR(10)), 'NULL')D 
        FROM   TABLE1)T 
       UNPIVOT (VALS 
               FOR ABC IN ([A], 
                           [B], 
                           [C], 
                           [D]))t 
GROUP  BY ABC 

当然是SQL Fiddle的新链接。

答案 1 :(得分:2)

试试这个 -

DECLARE @temp TABLE (A CHAR(2), B CHAR(2), C CHAR(2), D CHAR(2))

INSERT INTO @temp (A, B, C, D)
VALUES 
    (NULL, 'E'  , '1', '-5'),
    (NULL, NULL  , '4', '-3'),
    (NULL, '-4' , '6', 'E'),
    ('3', 'E'  , 'E', '-8'),
    ('1', NULL  , NULL, NULL)

SELECT 
      Code
    , N = COUNT(CASE WHEN Value = '' THEN 1 END)
    , E = COUNT(CASE WHEN Value LIKE '%[A-Z]%' THEN 1 END)
    , NegativeValues = COUNT(CASE WHEN LEFT(Value, 1) = '-' THEN 1 END)
FROM (
    SELECT 
          A = ISNULL(A, '')
        , B = ISNULL(B, '')
        , C = ISNULL(C, '')
        , D = ISNULL(D, '')
    FROM @temp
) t
UNPIVOT 
(
    Value FOR Code IN (A, B, C, D)
) unpvt
GROUP BY Code

答案 2 :(得分:1)

对于更通用的解决方案,您可以参考以下查询:

  SELECT "A", 
       Sum(N) N, 
       Sum(E) E, 
       Sum(M) "Negative" 
FROM   (SELECT CASE 
                 WHEN A = "N" THEN Count(A) 
                 ELSE 0 
               END N, 
               CASE 
                 WHEN A = "E" THEN Count(A) 
                 ELSE 0 
               END E, 
               CASE 
                 WHEN A LIKE "%-%" THEN Count(A) 
                 ELSE 0 
               END "M" 
        FROM   #TAB2 
        GROUP  BY A) aa 
UNION 
SELECT "B", 
       Sum(N) N, 
       Sum(E) E, 
       Sum(M) "Negative" 
FROM   (SELECT CASE 
                 WHEN B = "N" THEN Count(B) 
                 ELSE 0 
               END N, 
               CASE 
                 WHEN B = "E" THEN Count(B) 
                 ELSE 0 
               END E, 
               CASE 
                 WHEN B LIKE "%-%" THEN Count(B) 
                 ELSE 0 
               END "M" 
        FROM   #TAB2 
        GROUP  BY B) bb 
UNION 
SELECT "C", 
       Sum(N) N, 
       Sum(E) E, 
       Sum(M) "Negative" 
FROM   (SELECT CASE 
                 WHEN C = "N" THEN Count(A) 
                 ELSE 0 
               END N, 
               CASE 
                 WHEN C = "E" THEN Count(A) 
                 ELSE 0 
               END E, 
               CASE 
                 WHEN C LIKE "%-%" THEN Count(C) 
                 ELSE 0 
               END "M" 
        FROM   #TAB2 
        GROUP  BY C) cc 
UNION 
SELECT "D", 
       Sum(N) N, 
       Sum(E) E, 
       Sum(M) "Negative" 
FROM   (SELECT CASE 
                 WHEN D = "N" THEN Count(D) 
                 ELSE 0 
               END N, 
               CASE 
                 WHEN D = "E" THEN Count(D) 
                 ELSE 0 
               END E, 
               CASE 
                 WHEN D LIKE "%-%" THEN Count(D) 
                 ELSE 0 
               END "M" 
        FROM   #TAB2 
        GROUP  BY D) dd