我想编写一个MySQL脚本来完成以下操作,我有一个包含这样数据的Orders表 -
CustomerNumber | OrderNumber | Fullfilled
-----------------------------------------
001 10001 T
001 10002 T
002 10003 F
002 10004 T
003 10005 T
003 10006 T
003 10007 T
004 10008 T
我想创建一个查询,该查询只会列出订单数超过1的客户编号,并且所有的订单都有Fullfilled =“T”。
针对上述数据的查询将产生CustomerNumbers列表 - “001,003”。
答案 0 :(得分:4)
试试这个:
SELECT CustomerNumber
FROM mytable
GROUP BY CustomerNumber
HAVING COUNT(DISTINCT OrderNumber) > 1 AND SUM(Fullfilled <> 'T') = 0
查询在其HAVING
子句中使用两个谓词:
第二个过滤掉与至少一个 Fullfilled = 'T'
记录相关的客户编号。
答案 1 :(得分:1)
尝试这样:
select customerNumber from `table`
group by customerNumber
HAVING SUM(Fullfilled='T')>1
此代码的工作原理如下:
1-将客户编号与group by customerNumber
分组
2-使用HAVING
,聚合函数SUM (Fullfilled = 'T')> 1
按customerNumber
进行过滤,重复多次。
答案 2 :(得分:1)
SELECT CustomerNumber
FROM orders
GROUP BY Customernumber
HAVING Sum(Fullfilled = 'T') > 1
上述条款HAVING Sum(Fullfilled = 'T') > 1
将确保只会带来超过1 customerNUmber
的{{1}}。
使用Fullfilled = 'T',
子句以便我们可以使用GROUP BY
子句。
答案 3 :(得分:1)
您可以使用此选择查询来解决您的问题
SELECT CustomerNumber FROM `Orders` WHERE Fullfilled = 'T' GROUP BY CustomerNumber, Fullfilled HAVING COUNT(CustomerNumber)>1;
上述条款WHERE Fullfilled = 'T' GROUP BY CustomerNumber, Fullfilled
确保只有CustomerNumber
Fullfilled = 'T'
和HAVING COUNT(CustomerNumber)>1
才能确保OrderNumber > 1
答案 4 :(得分:0)
这就是你要找的东西:
Select the customernumber from the orders table where fulfilled is set to T only.
Group it by the customernumber
Having/Where the customernumber shows up more than once
然后SQL是:
SELECT customernumber FROM orders
WHERE fulfilled ='T'
GROUP BY customernumber
HAVING
COUNT(distinct customernumber) > 1);
答案 5 :(得分:0)
创建表/插入数据
CREATE TABLE t
(`CustomerNumber` CHAR(3), `OrderNumber` INT, `Fullfilled` VARCHAR(1))
;
INSERT INTO t
(`CustomerNumber`, `OrderNumber`, `Fullfilled`)
VALUES
('001', 10001, 'T'),
('001', 10002, 'T'),
('002', 10003, 'F'),
('002', 10004, 'T'),
('003', 10005, 'T'),
('003', 10006, 'T'),
('003', 10007, 'T'),
('004', 10008, 'T')
;
您可以通过GROUP BY,COUNT和SUM组合完成所需的操作。 我将发布一些SELECTS结果,这样你就可以理解想要的就是快乐。
<强>查询强>
SELECT
CustomerNumber
, COUNT(DISTINCT OrderNumber) > 1 #displays 1 if CustomerNummer has more than 1 unique OrderNumbers
, SUM(Fullfilled = 'T') #SUM up if CustomerNumber if Fullfilled = 'T' is 1 (true)
, SUM(Fullfilled = 'T') = COUNT(DISTINCT OrderNumber) #displays 1 (true) if All records are Fullfilled = 'T'
FROM
t
GROUP BY
CustomerNumber
<强>结果强>
CustomerNumber COUNT(DISTINCT OrderNumber) > 1 SUM(Fullfilled = 'T') SUM(Fullfilled = 'T') = COUNT(DISTINCT OrderNumber)
-------------- ------------------------------- --------------------- -----------------------------------------------------
001 1 2 1
002 1 1 0
003 1 3 1
004 0 1 1
现在确保只显示包含更多订单的记录。
我们使用HAVING COUNT(DISTINCT OrderNumber) > 1
<强>查询强>
SELECT
CustomerNumber
, COUNT(DISTINCT OrderNumber) > 1 #displays 1 if CustomerNummer has more than 1 unique OrderNumbers
, SUM(Fullfilled = 'T') #SUM up if CustomerNumber if Fullfilled = 'T' is 1 (true)
, SUM(Fullfilled = 'T') = COUNT(DISTINCT OrderNumber) #display 1 if All records are Fullfilled = 'T'
FROM
t
GROUP BY
CustomerNumber
HAVING
COUNT(DISTINCT OrderNumber) > 1
<强>结果强>
CustomerNumber COUNT(DISTINCT OrderNumber) > 1 SUM(Fullfilled = 'T') SUM(Fullfilled = 'T') = COUNT(DISTINCT OrderNumber)
-------------- ------------------------------- --------------------- -----------------------------------------------------
001 1 2 1
002 1 1 0
003 1 3 1
现在确保只显示包含更多订单的记录。
我们确保我们过滤掉所有记录应该具有Fullfilled =“T”的记录。
我们使用HAVING COUNT(DISTINCT OrderNumber) > 1 AND (SUM(Fullfilled = 'T') = COUNT(DISTINCT OrderNumber) = 1)
<强>查询强>
SELECT
CustomerNumber
, COUNT(DISTINCT OrderNumber) > 1 #displays 1 if CustomerNummer has more than 1 unique OrderNumbers
, SUM(Fullfilled = 'T') #SUM up if CustomerNumber if Fullfilled = 'T' is 1 (true)
, SUM(Fullfilled = 'T') = COUNT(DISTINCT OrderNumber) #display 1 if All records are Fullfilled = 'T'
FROM
t
GROUP BY
CustomerNumber
HAVING
COUNT(DISTINCT OrderNumber) > 1
AND
(SUM(Fullfilled = 'T') = COUNT(DISTINCT OrderNumber) = 1)
<强>结果强>
CustomerNumber COUNT(DISTINCT OrderNumber) > 1 SUM(Fullfilled = 'T') SUM(Fullfilled = 'T') = COUNT(DISTINCT OrderNumber)
-------------- ------------------------------- --------------------- -----------------------------------------------------
001 1 2 1
003 1 3 1
最终查询
SELECT
CustomerNumber
FROM
t
GROUP BY
CustomerNumber
HAVING
COUNT(DISTINCT OrderNumber) > 1
AND
(SUM(Fullfilled = 'T') = COUNT(DISTINCT OrderNumber) = 1)
<强>结果强>
CustomerNumber
----------------
001
003