Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server SQL GROUP BY,其列应包含镜像值_Sql Server_Group By_Aggregate - Fatal编程技术网

Sql server SQL GROUP BY,其列应包含镜像值

Sql server SQL GROUP BY,其列应包含镜像值,sql-server,group-by,aggregate,Sql Server,Group By,Aggregate,对不起,标题不好。我想不出更好的方式来描述我的问题 我有下表: Category | A | B A | 1 | 2 A | 2 | 2 B | 3 | 4 B | 4 | 3 类别A(第1行和第2行)的值错误,因为A列和B列应该镜像。对于B类(第3行和第4行),值是正确的 正确的表格应该是这样的: Category | A | B A | 1 | 2 A | 2 | 1 B | 3 | 4 B

对不起,标题不好。我想不出更好的方式来描述我的问题

我有下表:

Category | A | B
A        | 1 | 2
A        | 2 | 2
B        | 3 | 4
B        | 4 | 3
类别A(第1行和第2行)的值错误,因为A列和B列应该镜像。对于B类(第3行和第4行),值是正确的

正确的表格应该是这样的:

Category | A | B
A        | 1 | 2
A        | 2 | 1
B        | 3 | 4
B        | 4 | 3
然而,我手头有第一张桌子。对于这个表,我想输出一条错误消息,指出值1和2未正确镜像

要创建此输出消息,我使用以下查询

SELECT 'The values of category ' + category + ' are not correctly mirrored'
FROM table t1
INNER JOIN table t2 ON t1.category=t2.category AND t1.A!=t2.A AND (t1.A!=t2.B OR t1.B!=t2.A)
GROUP BY t1.category
这个查询有效。但是,我想用A和B的相应值来增强错误消息。类似于:

SELECT 'The values ' + a + ' and ' + b + ' of category ' + category + ' are not correctly mirrored'
FROM table t1
INNER JOIN table t2 ON t1.category=t2.category AND t1.A!=t2.A AND (t1.A!=t2.B OR t1.B!=t2.A)
GROUP BY t1.category
但很明显,我得到了以下错误:

列“a”在选择列表中无效,因为它未包含 在聚合函数或GROUP BY子句中

列“b”在选择列表中无效,因为它不包含在 聚合函数或GROUPBY子句


如何才能达到预期的效果?

试试这样的方法

SELECT Category,
       CASE
         WHEN Min(A) = Min(B)
              AND Max(A) = Max(B) THEN 'The values of category ' + category + ' are correctly mirrored'
         ELSE 'The values of category ' + category + ' are not correctly mirrored'
       END
FROM   yourtable
GROUP  BY Category 

您可以按条件取出group by condition并选择最上面的1行,以便显示a和b的值

SELECT TOP 1
        'The values ' + CONVERT(VARCHAR(8), t1.a) + ' and ' + CONVERT(VARCHAR(8), t1.b) + ' of category ' + t1.category + ' are not correctly mirrored' ErrMessage
    FROM [table] t1
        INNER JOIN [table] t2 ON t1.category=t2.category AND t1.A!=t2.A AND (t1.A!=t2.B OR t1.B!=t2.A)
    ORDER BY
        t1.category
        ,t1.a
        ,t1.b
或者,如果您希望有多个类别出现类似错误,则可以使用ROW_NUMBER对结果进行计数,并按类别进行分区

SELECT
        A.ErrMessage
    FROM
    (
        SELECT
                'The values ' + CONVERT(VARCHAR(8), t1.a) + ' and ' + CONVERT(VARCHAR(8), t1.b) + ' of category ' + t1.category + ' are not correctly mirrored' ErrMessage
                ,ROW_NUMBER()OVER(PARTITION BY t1.category ORDER BY t1.a, t1.B) Position
            FROM [table] t1
                INNER JOIN [table] t2 ON t1.category=t2.category AND t1.A!=t2.A AND (t1.A!=t2.B OR t1.B!=t2.A)
    ) A
    WHERE A.Position = 1
最后一个例子是这样一个表:

Category A           B
-------- ----------- -----------
A        1           2
A        2           2
B        3           4
B        4           3
C        5           6
C        6           6
将返回两个有错误的类别:

ErrMessage
-------------------------------------------------------------------------
The values 1 and 2 of category A are not correctly mirrored
The values 5 and 6 of category C are not correctly mirrored
可能重复的