我怎样才能找到所有在另一家公司所有城市都有分公司的公司

时间:2016-10-20 16:05:37

标签: sql-server tsql sql-server-2014

我使用的是SQL Server 2014.我有一个这样的表:

from django.contrib import admin
from django import forms
from django.core.files.images import get_image_dimensions

from .models import ModelWithImageField


class ModelWithImageFieldForm(forms.ModelForm):
    class Meta: 
        model = ModelWithImageField
        fields = '__all__'

    def clean_photo(self):
        photo = self.cleaned_data["photo"] 
        width, height = get_image_dimensions(photo.file)
        if width < 900 or height < 900:
            raise form.ValidationError("Improper size.")
        return photo


@admin.register(models.ModelWithImageField)
class ModelWithImageFieldAdmin(admin.ModelAdmin):
    form = ModelWithImageFieldForm

我在桌子上有这些记录

CREATE Table Comp
(
  ID int identity(1,1),
  City varchar(50),
  Name varchar(50)
)

如何查询公司的名称,该名称在其他公司的所有城市中都有引用

例如,如果我的主公司是&#39; ABC&#39;,它应该查询QQQ&#39;和&#39; dd&#39;

4 个答案:

答案 0 :(得分:2)

这是一种方式

SELECT NAME
FROM   comp c1
WHERE  City IN (SELECT city
                FROM   comp
                WHERE  NAME = 'ABC')
       AND NAME <> 'ABC'
GROUP  BY NAME
HAVING Count(DISTINCT City) = (SELECT Count(DISTINCT city)
                               FROM   comp
                               WHERE  NAME = 'ABC') 

如果每个city没有重复 name,那么更好的方法

SELECT c1.NAME
FROM   comp c1
       JOIN (SELECT city,
                    Count(1)OVER() AS cnt
             FROM   comp
             WHERE  NAME = 'ABC') c2
         ON c1.City = c2.City
WHERE  c1.NAME <> 'ABC'
GROUP  BY c1.NAME
HAVING Count(c1.city) = Max(cnt) 

答案 1 :(得分:1)

可替换地,

declare @selectedcompany nvarchar(10) = 'abc'

select distinct name
from comp rsDistinct
where not exists
(
    select city from comp where name = @selectedcompany
    except
    select city from comp where name = rsDistinct.name
)
and name != 'ABC'

答案 2 :(得分:0)

在此期间,我也写了这个查询:

SELECT 
  DISTINCT
  S.name  
FROM Comp AS S 
WHERE NOT EXISTS 
    (   
        (
            SELECT 
              city 
            FROM Comp 
            WHERE name = 'ABC'
        ) 
        EXCEPT 
        (   SELECT 
            T.city 
            FROM Comp AS T 
            WHERE  T.name = S.Name 
        )
    )
    AND S.name <> 'ABC'

答案 3 :(得分:0)

我的解决方案是下面的查询1。

我还测试了您的查询(查询2),并尝试根据“实际执行计划”找出两者中哪一个更有效率。结果如下:

查询1:查询成本(相对于批次):25%

select Name, COUNT(*) as cities 
from (select city from Comp where name = 'ABC') m
join (select name, city from Comp where Name <> 'ABC') c on m.city = c.city 
group by Name
having COUNT(*) = (select COUNT(*) from Comp where Name = 'ABC')

查询2:查询成本(相对于批次):75%

SELECT DISTINCT S.name  
FROM Comp AS S 
WHERE NOT EXISTS 
(   
    (SELECT city FROM Comp WHERE name = 'ABC') 
    EXCEPT 
    (SELECT T.city FROM Comp AS T WHERE  T.name = S.Name 
    )
)
AND S.name <> 'ABC'

注意:在以后的评估中,我发现@Prdp的解决方案效率更高。

对于重复的城市场景

select Name
from (select city from Comp where name = 'ABC') m
join (select name, city from Comp where Name <> 'ABC') c on m.city = c.city 
group by Name
having COUNT_BIG(DISTINCT c.City) = (select COUNT_BIG(DISTINCT City) from Comp where Name = 'ABC')