SQL查询显示购买苹果但没有土豆的客户

发布于 2025-02-09 01:42:37 字数 1179 浏览 0 评论 0原文

不确定如何解释这一点。

我有一个类似的表,但是我已经用以下内容简化了它:

我有一张运送到不同的库司机的货物表。有些人只买了苹果,另一些人买了苹果和钳子。

我希望SQL查询仅返回“被收费”的客户=是,并且客户没有购买任何蔬菜。

因此,例如,如果表看起来像这样:

项目名称to_be_billcustomerno。
2000苹果水果1
2000苹果水果22000
水果苹果水果3
2000苹果水果4
2000苹果薯片2
540004000
薯条名称4希望

查询返回

to_be_bill项目custicerno
2000苹果水果1
2000苹果水果5

原因4的原因是要收取苹果的原因,但客户也买了土豆,因此也要忽略...

Not sure how to explain this..

I have a similar table, but i have simplified it with the following:

I have a table of goods shipped to different cusotmers. Some have bought apples only, others have bought apples and potates.

I want an SQL query to return only customers where "To be billed" = Yes AND the customer hasnt bought any vegetables.

So for example if the table looks like this:

ItemNameGroupTo_be_billedCustomerNo.
2000AppleFruitYes1
2000AppleFruitNo2
2000AppleFruitNo3
2000AppleFruitYes4
2000AppleFruitYes5
4000PotatoVegetableNo2
4000PotatoVegetableNo4

I want the query to return:

ItemNameGroupTo_be_billedCustomerNo.
2000AppleFruitYes1
2000AppleFruitYes5

The reason 4 has bought apples, and is to be billed, but the customer also bought Potatoes, so is to be ignored...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月竹挽风 2025-02-16 01:42:37

您可以创建一个CTE检查customerno

with bought_veg as
(
    select "CustomerNo."
    from tbl
    where tbl."Group" like 'Vegetable'
)
select tbl.*
from tbl
where not exists (select 1 from bought_veg where tbl."CustomerNo." = bought_veg."CustomerNo.")
    and tbl.To_be_billed = 'Yes'

select tbl.*
from tbl
where not exists (select "CustomerNo." from tbl t2 where tbl.[CustomerNo.] = t2.[CustomerNo.] and "Group" like 'Vegetable')
    and tbl.To_be_billed = 'Yes'

You can create a CTE to check for CustomerNo.s that you need to ignore, and then use not exists:

with bought_veg as
(
    select "CustomerNo."
    from tbl
    where tbl."Group" like 'Vegetable'
)
select tbl.*
from tbl
where not exists (select 1 from bought_veg where tbl."CustomerNo." = bought_veg."CustomerNo.")
    and tbl.To_be_billed = 'Yes'

Example without CTE:

select tbl.*
from tbl
where not exists (select "CustomerNo." from tbl t2 where tbl.[CustomerNo.] = t2.[CustomerNo.] and "Group" like 'Vegetable')
    and tbl.To_be_billed = 'Yes'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文