Notre groupe Wechat a un ami à poser un problème réel:
Statistiques du Centre de commerce sur la situation de chaque garage souterrain, montrant que certains centres ne font que faire des entrées, n'exporte ni l'entrée. Bien sûr, est de trouver des exportations sans surveillance ou des centres commerciaux, puis vérifiez à nouveau. Que dois-je faire si j'attrape le feu
La structure est la suivante:
create table dbo.Entrances(MallId int ,EntranceName nvarchar(50),EntranceFlag int)
Insert into dbo.Entrances(MallId,EntranceName,EntranceFlag)values(1,N'王府井东门',1),(1,N'王府井西门',2),(1,N'王府井南门',2),(1,N'王府井北门',3),(2,N'崇文门新世界东门',1),(2,N'崇文门新世界西门',2) ,(3,N'工人体育馆南门',1),(3,N'工人体育馆北门',3) ,(4,N'地安门公园东门',1),(4,N'地安门公园西门',1)
-- 1. Not ExistsSelect Distinct etr.MallId as MallIdFrom dbo.Entrances etr With(Nolock)Where etr.EntranceFlag = 1 and Not exists(Select top 1 1 From dbo.Entrances etr2 With(nolock) Where etr2.MallId = etr.MallId and etr2.EntranceFlag = 2) and Not exists(Select top 1 1 From dbo.Entrances etr2 With(nolock) Where etr2.MallId = etr.MallId and etr2.EntranceFlag = 3) Select Distinct etr.MallId as MallIdFrom dbo.Entrances etr With(Nolock)Where etr.EntranceFlag = 1 and Not exists(Select top 1 1 From dbo.Entrances etr2 With(nolock) Where etr2.MallId = etr.MallId and etr2.EntranceFlag IN(2,3)) -- 2. Left Join Select Distinct etr.MallId From dbo.Entrances etr With(Nolock)Left join dbo.Entrances etr2 With(Nolock) on etr.MallId = etr2.MallId and etr2.EntranceFlag in (2,3)Where etr.EntranceFlag = 1 and etr2.MallId is null -- 3. Rank; With base_query AS(Select MallId,DENSE_RANK()Over(Partition By MallId Order By EntranceFlag ASC) AS RnkFrom Entrances ) Select Distinct etr.MallId FROM Entrances etr With(nolock) Where etr.EntranceFlag = 1 and Not Exists(Select Top 1 1 from base_query bq Where bq.MallId = etr.MallId and bq.Rnk > 1 )
MALLID: ID de magasinage ENTRANCENAME: MALL Plus ] Entrée: 1 - est entrée; 2. Il est exporté; 3- est une entrée Notre objectif n'est que 1 (c'est-à-dire dans l'entrée) sans 2 et 3 (NOT Exportation ou entrée) Centre commercial Projet de 3 cas: