如何从SQL Server表中选择不同的行

时间:2016-01-06 19:55:56

标签: mysql sql-server

我正在将现有的工作查询从MySQL移动到MS SQL Server(版本11),我无法转换其中一个在MySQL上工作正常的查询。

它基本上只从表中选择不同的行,考虑4列值。

表是(具有最小的数据子集。对于不同的名称,sub_name和用户,存在相同类型的数据):

+-------+-------+-------+-------+-----------------+----------+--------+
| col_1 | col_2 | col_3 | col_4 | name            | sub_name | user   |
+-------+-------+-------+-------+-----------------+----------+--------+
|    50 |    40 |    20 |    30 | one_four_eleven | One      | user_1 |
|    50 |    40 |    20 |    30 | one_four_eleven | Two      | user_1 |
|    50 |    40 |    20 |    30 | one_four_eleven | Three    | user_1 |
|    50 |    40 |    20 |    30 | one_four_eleven | Four     | user_1 |
|    50 |    40 |    20 |    30 | one_four_eleven | Five     | user_1 |
|    50 |    40 |    20 |    30 | one_four_eleven | Six      | user_1 |
|    50 |    40 |    20 |    30 | one_four_eleven | Seven    | user_1 |
|    50 |    40 |    20 |    30 | one_four_eleven | Eight    | user_1 |
|    50 |    40 |    20 |    30 | one_four_eleven | Nine     | user_1 |
|    50 |    40 |    15 |    30 | one_four_eleven | Ten      | user_1 |
+-------+-------+-------+-------+-----------------+----------+--------+

我需要的是,如果所有col_1,col_2,col_3和col_4与其他行相比在一行中具有相同的值,则应该只选择其中一个相同的行。

与给定数据一样,只有最后一行在col_3中具有不同的数据,因此结果应该只有两行。一行显示col_1,col_2,col_3,col_4和name中的相同值。第二行应该是最后一行。

我可以使用MySQL查询执行此操作,如下所示,结果如下:

mysql> SELECT DISTINCT col_1, col_2, col_3, col_4, name FROM table_name 
WHERE (user = 'user_1' OR user = 'GLOBAL') AND name = 'one_four_eleven' AND sub_name != 'ALL' GROUP BY sub_name;
+-------+-------+-------+-------+-----------------+
| col_1 | col_2 | col_3 | col_4 | name            |
+-------+-------+-------+-------+-----------------+
|    50 |    40 |    20 |    30 | one_four_eleven |
|    50 |    40 |    15 |    30 | one_four_eleven |
+-------+-------+-------+-------+-----------------+

但是当我尝试将此查询转换为在MS SQL Server上运行时,它会出错:

Column 'table_name.col_1' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

如何为SQL Server编写一个查询,该查询将提供与上述MySQL查询相同的结果。

2 个答案:

答案 0 :(得分:1)

根据给出的示例数据,这将起作用:

SELECT col_1, col_2, col_3, col_4, name 
FROM table_name 
WHERE (user = 'user_1' OR user = 'GLOBAL') 
  AND name = 'one_four_eleven' 
  AND sub_name != 'ALL' 
GROUP BY col_1, col_2, col_3, col_4, name

或者

SELECT distinct col_1, col_2, col_3, col_4, name 
FROM table_name 
WHERE (user = 'user_1' OR user = 'GLOBAL') 
  AND name = 'one_four_eleven' 
  AND sub_name != 'ALL' 
哦,如果你想知道mysql基本上是这样的:

SELECT max(col_1), max(col_2), max(col_3), max(col_4), max(name) 
FROM table_name 
WHERE (user = 'user_1' OR user = 'GLOBAL') 
  AND name = 'one_four_eleven' 
  AND sub_name != 'ALL' 
GROUP BY sub_name

但这不如前两个好。

答案 1 :(得分:0)

我认为这可以帮到你:

;WITH cte  AS (
SELECT * 
, ROW_NUMBER() OVER( PARTITION BY col_1, col_2, col_3, col_4, name ORDER BY (SELECT NULL) ) AS rn
FROM yourTable
WHERE (user = 'user_1' OR user = 'GLOBAL') AND name = 'one_four_eleven' AND sub_name != 'ALL'
)

SELECT * FROM cte
WHERE rn = 1