来自sql中多个列的DISTINCT值

时间:2015-10-22 00:23:27

标签: mysql sql-server

我有以下任务:

  

编写一个SELECT语句,为每个与另一个客户具有相同姓氏和帐单地址的客户返回一行。按姓氏和名字对结果集进行排序。

我尝试使用DISTINCT关键字,但这不符合我的目的。

由于某种原因,每次我使用GROUP BY时都会收到以下错误:

SELECT FirstName, LastName, BillingAddressID
    FROM Customers
    GROUP BY LastName;
  

专栏' Customers.FirstName'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。

我尝试使用UNION并出现以下错误:

SELECT LastName FROM Customers

UNION 

SELECT BillingAddressID FROM Customers
  

转换varchar值时转换失败' Sherwood'数据类型int。

有人可以指出我正确的方向吗?

这是我正在处理的数据集的示例

firstname   lastname    billingaddressid  
Allan       Sherwood        2  
Barry       Zimmer          3  
Christine   Brown           4  
David       Goldstein       6  
Erin        Sherwood        7  
Frank Lee   Wilson          8  
Gary        Brown           4  
Heather     Esway           12  

因此,查询应删除姓氏

的重复条目

2 个答案:

答案 0 :(得分:0)

尝试 -

SELECT FirstName,
       Customers.LastName,
       Customers.billingAddressID
FROM Customers INNER JOIN
     ( SELECT LastName,
              billingAddressID
       FROM Customers
       GROUP BY LastName,
                billingAddressID
       HAVING COUNT( LastName ) >= 2 ) lastNameQuery
  ON Customers.LastName = lastNameQuery.LastName AND
     Customers.billingAddressID = lastNameQuery.billingAddressID
ORDER BY Customers.LastName,
         FirstName;

我针对使用以下脚本创建的数据库进行了测试...

CREATE DATABASE Cust;

USE Cust;

CREATE TABLE Customers
(
    fldID               INT              NOT NULL    AUTO_INCREMENT,
    firstName           VARCHAR( 50 )    NOT NULL,
    lastName            VARCHAR( 50 ),
    billingAddressID    INT              NOT NULL,
    PRIMARY KEY ( fldID )
);

我使用 -

输入了提问者的样本数据
INSERT INTO Customers
SET firstName        = "Allan",
    lastName         = "Sherwood",
    billingAddressID = 2;

INSERT INTO Customers
SET firstName        = "Barry",
    lastName         = "Zimmer",
    billingAddressID = 3;

INSERT INTO Customers
SET firstName        = "Christine",
    lastName         = "Brown",
    billingAddressID = 4;

INSERT INTO Customers
SET firstName        = "David",
    lastName         = "Goldstein",
    billingAddressID = 6;

INSERT INTO Customers
SET firstName        = "Erin",
    lastName         = "Sherwood",
    billingAddressID = 7;

INSERT INTO Customers
SET firstName        = "Frank Lee",
    lastName         = "Wilson",
    billingAddressID = 8;

INSERT INTO Customers
SET firstName        = "Gary",
    lastName         = "Brown",
    billingAddressID = 10;

INSERT INTO Customers
SET firstName        = "Heather",
    lastName         = "Esway",
    billingAddressID = 12;

我还添加了以下内容以确保重复BOTH lastName和billingAddressID ...

INSERT INTO Customers
SET firstName        = "Don",
    lastName         = "Sherwood",
    billingAddressID = 22;

INSERT INTO Customers
SET firstName        = "Timmy",
    lastName         = "Sherwood",
    billingAddressID = 22;

INSERT INTO Customers
SET firstName        = "James",
    lastName         = "Brown",
    billingAddressID = 22;

INSERT INTO Customers
SET firstName        = "James",
    lastName         = "Esway",
    billingAddressID = 22;

询问我们的提问者的问题,我认为提问者正在寻求帮助的问题是 -

Write a SELECT statement that returns one row for each customer that has the same last name and billing address as another customer. Sort the result set by last name then first name.

我对此的解释是,我们应该返回EACH客户的记录,这些记录符合具有特定组合的标准,包括与至少一个其他客户共享的姓氏和BillingAddressID,并且返回记录的列表应按最后排序名字并在名字上转储。

我的答案的核心是细分 -

SELECT LastName,
       billingAddressID
FROM Customers

仅从Customers中选择两个条件字段。

对此我添加了 -

GROUP BY LastName,
         billingAddressID

这会将核心段结果细化为两个条件字段的每个唯一组合的列表。

然后我将此列表限制为通过添加 -

至少出现两次的独特组合
HAVING COUNT( LastName ) >= 2

然后,我为结果查询提供了lastNameQuery的别名。

然后我在两个条件字段上与Customers一起加入lastNameQuery,将我们的客户列表限制为使用 -

共享条件值对的人
     Customers INNER JOIN
     ( SELECT LastName,
              billingAddressID
       FROM Customers
       GROUP BY LastName,
                billingAddressID
       HAVING COUNT( LastName ) >= 2 ) lastNameQuery
  ON Customers.LastName = lastNameQuery.LastName AND
     Customers.billingAddressID = lastNameQuery.billingAddressID

由此我使用 -

选择了所需的字段
SELECT FirstName,
       Customers.LastName,
       Customers.billingAddressID
FROM

需要使用Customers.来澄清对CustomerslastNameQuery中出现的字段名称的引用。如果没有这个澄清,MySQL无法确定它应该使用哪些字段。

通过添加 -

将此列表分类到指定的顺序
ORDER BY Customers.LastName,
         FirstName;

我从测试完整陈述中得到的结果是 -

+-----------+----------+------------------+
| FirstName | LastName | billingAddressID |
+-----------+----------+------------------+
| Gary      | Brown    |               10 |
| Tom       | Brown    |               10 |
| Don       | Sherwood |               22 |
| Timmy     | Sherwood |               22 |
+-----------+----------+------------------+

这些是我的扩展样本数据集中唯一拥有条件字段中值的共享组合的客户。

答案 1 :(得分:0)

试试这个

SQL Fiddle

   SELECT * FROM 
    (
    SELECT *,ROW_NUMBER() OVER(PARTITION BY LASTNAME ORDER BY FIRSTNAME) AS RN
    FROM YOURTABLE
    )T
    WHERE RN=2

如果您想要返回所有唯一记录。

SELECT * FROM 
    (
    SELECT *,ROW_NUMBER() OVER(PARTITION BY LASTNAME ORDER BY FIRSTNAME) AS RN
    FROM YOURTABLE
    )T
    WHERE RN=1