3

请您帮助获取下表的查询

╔══════════════╗
║ 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”开头

4

4 回答 4

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的新链接。

于 2013-08-02T06:07:08.550 回答
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
于 2013-08-02T06:08:35.613 回答
1
如果您只有四列,则可以尝试此解决方案。

这是 SQLFiddel 演示

以下是您可以尝试的查询:

select 'A' as ColumnName,
       (select count(*) 
          from Table1
         where A = 'N') as N,
       (select count(*) 
          from Table1
         where A = 'E') as E,
       (select count(*) 
          from Table1
         where substring(A,1,1) = '-') as Negative
union
select 'B' as ColumnName,
       (select count(*) 
          from Table1
         where B = 'N') as N,
       (select count(*) 
          from Table1
         where B = 'E') as E,
       (select count(*) 
          from Table1
         where substring(B,1,1) = '-') as Negative
union
select 'C' as ColumnName,
       (select count(*) 
          from Table1
         where C = 'N') as N,
       (select count(*) 
          from Table1
         where C = 'E') as E,
       (select count(*) 
          from Table1
         where substring(C,1,1) = '-') as Negative
union
select 'D' as ColumnName,
       (select count(*) 
          from Table1
         where D = 'N') as N,
       (select count(*) 
          from Table1
         where D = 'E') as E,
       (select count(*) 
          from Table1
         where substring(D,1,1) = '-') as Negative
于 2013-08-02T07:02:32.240 回答
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 
于 2013-08-02T06:32:27.433 回答