Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MSSQL:使用不带GROUPBY子句的不同顶部选择_Sql_Sql Server_Count_Subquery_Left Join - Fatal编程技术网

MSSQL:使用不带GROUPBY子句的不同顶部选择

MSSQL:使用不带GROUPBY子句的不同顶部选择,sql,sql-server,count,subquery,left-join,Sql,Sql Server,Count,Subquery,Left Join,我想在不使用GROUPBY子句的情况下使用DISTINCT和TOP selection 我想从[location]中选择行,并根据与[location_photos]中每个位置相关联的行数对结果进行排序 我现在有这个(甚至不用TOP): 但是,这将为[location_photos](56000)中的每一行返回一行,其中这应该是[location_photos](12000)中行数的潜在最大值 我已经检查过了 如何使用distinct和TOP选择不同的行数 DDL和数据 CREATE TABLE

我想在不使用GROUPBY子句的情况下使用DISTINCT和TOP selection

我想从[location]中选择行,并根据与[location_photos]中每个位置相关联的行数对结果进行排序

我现在有这个(甚至不用TOP):

但是,这将为[location_photos](56000)中的每一行返回一行,其中这应该是[location_photos](12000)中行数的潜在最大值

我已经检查过了

如何使用distinct和TOP选择不同的行数

DDL和数据

CREATE TABLE [dbo].[locations](
    [id] [int] NOT NULL,
    [title] [nvarchar](500) NOT NULL,
    [createdate] [datetime] NULL,
 CONSTRAINT [PK_locs] PRIMARY KEY NONCLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

ALTER TABLE [dbo].[locations] ADD  CONSTRAINT [DF_homes_createdate]  DEFAULT (getdate()) FOR [createdate]
GO

CREATE TABLE [dbo].[location_photos](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [locationid] [int] NOT NULL,
    [locpath] [nvarchar](250) NOT NULL,
 CONSTRAINT [PK_location_photos] PRIMARY KEY NONCLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[location_photos]  WITH CHECK ADD  CONSTRAINT [FK_locs_photos_homes] FOREIGN KEY([locationid])
REFERENCES [dbo].[locations] ([id])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[location_photos] CHECK CONSTRAINT [FK_locs_photos_homes]
GO



INSERT INTO [locations](id,title) VALUES (1,'new york')
INSERT INTO [locations](id,title) VALUES (2,'boston')
INSERT INTO [locations](id,title) VALUES (3,'chicago')
INSERT INTO [locations](id,title) VALUES (4,'los angeles')


INSERT INTO [location_photos](locationid,locpath) VALUES (1,'nyc1')
INSERT INTO [location_photos](locationid,locpath) VALUES (1,'nyc2')
INSERT INTO [location_photos](locationid,locpath) VALUES (1,'nyc3')
INSERT INTO [location_photos](locationid,locpath) VALUES (1,'nyc4')
INSERT INTO [location_photos](locationid,locpath) VALUES (1,'nyc5')

INSERT INTO [location_photos](locationid,locpath) VALUES (2,'boston1')
INSERT INTO [location_photos](locationid,locpath) VALUES (2,'boston2')
INSERT INTO [location_photos](locationid,locpath) VALUES (2,'boston3')
INSERT INTO [location_photos](locationid,locpath) VALUES (2,'boston4')

--there are not photos for chicago on purpose

INSERT INTO [location_photos](locationid,locpath) VALUES (4,'la1')
INSERT INTO [location_photos](locationid,locpath) VALUES (4,'la2')

我想你想要:

SELECT l.id, count(lp.locationid) AS cnt_photos, l.title
FROM locations l
LEFT JOIN location_photos lp on lp.locationid = l.id
GROUP BY l.id, l.title
ORDER BY cnt_photos desc
或者,您可以使用子查询对匹配的照片进行计数。下面是一种使用横向连接的方法:

select l.*, lp.*
from locations l
cross apply (select count(*) cnt_photos from location_photos lp where lp.locationid = l.id) lp
ORDER BY lp.cnt_photos desc
我想从[location]中选择行,并根据与[location_photos]中每个位置相关联的行数对结果进行排序

您可以在
ORDER BY
子句中使用相关子查询:

select l.*
from locations l
order by (select count(*) 
          from location_photos lp
          where lp.location_id = l.id
         ) desc;
如果需要查询中的计数以及排序条件,只需将子查询移动到
选择

select l.*,
       (select count(*) 
        from location_photos lp
        where lp.location_id = l.id
       ) as num_photos
from locations l
order by num_photos desc;

请提供样本数据和预期结果。还可以通过
解释对
分组的厌恶。
select l.*,
       (select count(*) 
        from location_photos lp
        where lp.location_id = l.id
       ) as num_photos
from locations l
order by num_photos desc;