检索一列而不是另一列的不同SQL查询结果

时间:2022-05-15 08:02:23

I have a table that has the following format for the data. All I want is just one code and phone number. It does not matter which phone number I get, all I need it one phone number.

我有一个表格,其数据格式如下。我想要的只是一个代码和电话号码。我得到的电话号码无关紧要,我需要一个电话号码。

  • Code phoneNumber
  • 1000009 (123)752-0108
  • 1000257 (456)718-1229
  • 1000257 (789)750-1057
  • 1000259 (000)000-0001 1000259 (111)453-0522 1000259 (222)460-8947 1000270 (333)528-6468 1000276 (444)384-5571
  • 1000259 (000)000-0001 1000259(111)453-0522 1000259 (222)460-8947 1000270 (333)528-6468 1000276 (444)384-5571

The results I need would look like: Code phoneNumber 1000009 (123)752-0108 1000257 (456)718-1229 1000259 (000)000-0001 1000270 (333)528-6468 1000276 (444)384-5571

我需要的结果如下:代码phoneNumber 1000009 (123)752-0108 1000257 (456)718-1229 1000259 (000)000-0001 1000270 (333)528-6468 1000276 (444)384-5571

Any help with the SQL query would be appreciated. Thank you

任何有关SQL查询的帮助将不胜感激。谢谢

2 个解决方案

#1


2  

If any of the phoneNumbers for a given code is sufficient, you can use a GROUP BY with any of the aggregating functions to accomplish just that

如果给定代码的任何phoneNumbers足够,您可以使用GROUP BY与任何聚合函数来实现

This example uses the MAX aggregating function.

此示例使用MAX聚合函数。

SELECT  Code
        , PhoneNumber = MAX(phoneNumber)
FROM    Table
GROUP BY
        Code

#2


1  

WITH cteRowNum AS (
    SLEECT Code, phoneNumber,
           ROW_NUMBER() OVER(PARTITION BY Code ORDER BY phoneNumber) AS RowNum
        FROM YourTable
)
SELECT Code, phoneNumber
    FROM cteRowNum
    WHERE RowNum = 1;

#1


2  

If any of the phoneNumbers for a given code is sufficient, you can use a GROUP BY with any of the aggregating functions to accomplish just that

如果给定代码的任何phoneNumbers足够,您可以使用GROUP BY与任何聚合函数来实现

This example uses the MAX aggregating function.

此示例使用MAX聚合函数。

SELECT  Code
        , PhoneNumber = MAX(phoneNumber)
FROM    Table
GROUP BY
        Code

#2


1  

WITH cteRowNum AS (
    SLEECT Code, phoneNumber,
           ROW_NUMBER() OVER(PARTITION BY Code ORDER BY phoneNumber) AS RowNum
        FROM YourTable
)
SELECT Code, phoneNumber
    FROM cteRowNum
    WHERE RowNum = 1;