简体   繁体   English

我怎么能用'and'和'not'组成一个tsql查询结果

[英]How could I form a tsql query with 'and' and 'not' for a result

I have the following question: 我有以下问题:

For each city display the number of clients who only rented cars of type 'Toyota' or 'BMW' and never rented 'Mercedes' 对于每个城市,显示仅租用“ Toyota”或“ BMW”类型的汽车而从未租用“ Mercedes”的客户数量

The tables are as follows: 下表如下:

Car [CarId int, type varchar(30)]

Client [ClientId int, CityId int, Name varchar(30)]

City [CityId int, CityName varchar(30)]

Rent [CarId int, ClientId int, days_number int]

I don't know how would I formulate this query I tried hard but nothing worked until now. 我不知道该如何制定这个查询,但我一直没有尝试。

select city.cityname, count(*) as cnt
from client
join city
on client.cityId = city.cityId
where exists(select * from rent join car on rent.carid = car.carid 
             where client.clientid = rent.clientid 
                   and car.[type] in ('Toyota', 'BMW'))
and not exists(select * from rent join car on rent.carid = car.carid 
               where client.clientid = rent.clientid 
                 and car.[type] = 'Mercedes')
group by city.cityname

Declare @count1 int, @count2 int 声明@ count1 int,@ count2 int

Select @count1 = Count(*) 选择@ count1 = Count(*)

From Client inner join Rent 从客户内部加入租金

on Client.ClientId = Rent.ClientId 在Client.ClientId = Rent.ClientId上

inner join Car 内联车

on Car.CarId = Rent.CarId 在Car.CarId = Rent.CarId上

Where Car.type In( 'Toyota','BMW') 其中Car.type In('Toyota','BMW')

--except - 除了

Select @count2 = Count(*) 选择@ count2 = Count(*)

From Client inner join Rent 从客户内部加入租金

on Client.ClientId = Rent.ClientId 在Client.ClientId = Rent.ClientId上

inner join Car 内联车

on Car.CarId = Rent.CarId 在Car.CarId = Rent.CarId上

Where Car.type = 'Merccedes' 其中Car.type ='Merccedes'

Select (@count1 - @count2) 选择(@ count1-@ count2)

Please take a look at this query: 请看一下这个查询:

select
  City.CityName,
  count(1) as NumberOfClients
from
  City
    inner join Client on City.CityID = Client.CityID
      inner join
      (
      select Rent.ClientID
      from
        Rent
          inner join Car on Rent.CarID = Car.CarID
              and Car.type in ('Toyota','BMW','Mercedes')
      group by Rent.ClientID
      having sum(case when Car.type in ('Toyota','BMW') then 1 else 0 end) > 0
      and sum(case when Car.type in ('Mercedes') then 1 else 0 end) = 0
      ) as Summary
        on Client.ClientID = Summary.ClientID
group by
  City.CityID,
  City.CityName

What it does is: 它的作用是:

  1. In subquery (named Summary ) all distinct clients are selected, basing on a condition that those clients have rented 'Toyota' or 'BMW' at least once, while never have rented 'Mercedes'. 在子查询(名为Summary )中,选择所有不同的客户,条件是这些客户至少租了一次“ Toyota”或“ BMW”,而从未租过“ Mercedes”。

  2. The outer query takes the results of the subquery and calculates totals for each city. 外部查询采用子查询的结果,并计算每个城市的总数。

This can probably be done more efficiently - but this is first that comes to my mind. 这可能可以更有效地完成-但这是我首先想到的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM