SQL 获取唯一记录并维护订单
SQL get unique records and Maintain the Order
我的 table 中有如下记录。
我想要不同的记录,当我按它分组时,它失去了顺序。我要维持秩序。我想要如下结果:
这是我的查询:
select route_id,fixcode,fixdescription
from route_fixcodes
group by route_id,fixcode,fixdescription
having route_id = 995063
只需添加不同的:
select distinct route_id,fixcode,fixdescription
from route_fixcodes
group by route_id,fixcode,fixdescription
having route_id = 995063
试试下面的代码。我猜这就是你要找的
CREATE TABLE [dbo].[route_fixcodes](
[route_id] [INT] NULL,
[fixcode] [INT] NULL,
[fixdescription] [NVARCHAR](50) NULL
) ON [PRIMARY]
GO
INSERT INTO [route_fixcodes]
([route_id] ,[fixcode],[fixdescription])
VALUES
(995063,100,'Issue Observed'),
(995063,137,'Swap Altice One Pack'),
(995063,249,'Defective CPE Equip.'),
(995063,112,'Outside coax repair'),
(995063,258,'Preventative Maint'),
(995063,100,'Issue Observed'),
(995063,137,'Swap Altice One Pack'),
(995063,249,'Defective CPE Equip.'),
(995063,112,'Outside coax repair'),
(995063,258,'Preventative Maint'),
(995063,100,'Issue Observed'),
(995063,137,'Swap Altice One Pack'),
(995063,249,'Defective CPE Equip.'),
(995063,112,'Outside coax repair'),
(995063,258,'Preventative Maint')
GO
SELECT * INTO #route_seq FROM dbo.route_fixcodes
ALTER TABLE #route_seq ADD seq [INT] IDENTITY(1,1)
SELECT MIN(seq) AS newseq, route_id,fixcode,fixdescription
FROM #route_seq
group by route_id,fixcode,fixdescription
ORDER BY MIN(seq)
SQL 表表示 无序 集。没有顺序,除非列指定顺序。
让我假设有一个。 . .我就叫它 id
。然后你可以这样做:
select route_id, fixcode, fixdescription
from route_fixcodes
where route_id = 995063
group by route_id, fixcode, fixdescription
order by min(id);
请注意,我已将 having
子句更改为 where
子句。通常,这将减少聚合的数据量——这对性能非常有利。
如果您没有排序列,但特别希望结果按 100、137、249、112、258 的顺序排列,那么您可以使用 case
表达式或类似的逻辑:
select route_id, fixcode, fixdescription
from route_fixcodes
where route_id = 995063
group by route_id, fixcode, fixdescription
order by charindex(id, '100,137,249,112,258');
(这只是一个方便的 shorthand,并非在所有情况下都有效,但适用于您提供的数据。)
下面的查询给了我想要的解决方案。
select route_id,fixcode,fixdescription, MIN(id) as minid from route_fixcodes where route_id = @RouteId GROUP BY route_id,fixcode ,fixdescription Order by minid
我的 table 中有如下记录。
我想要不同的记录,当我按它分组时,它失去了顺序。我要维持秩序。我想要如下结果:
这是我的查询:
select route_id,fixcode,fixdescription
from route_fixcodes
group by route_id,fixcode,fixdescription
having route_id = 995063
只需添加不同的:
select distinct route_id,fixcode,fixdescription
from route_fixcodes
group by route_id,fixcode,fixdescription
having route_id = 995063
试试下面的代码。我猜这就是你要找的
CREATE TABLE [dbo].[route_fixcodes](
[route_id] [INT] NULL,
[fixcode] [INT] NULL,
[fixdescription] [NVARCHAR](50) NULL
) ON [PRIMARY]
GO
INSERT INTO [route_fixcodes]
([route_id] ,[fixcode],[fixdescription])
VALUES
(995063,100,'Issue Observed'),
(995063,137,'Swap Altice One Pack'),
(995063,249,'Defective CPE Equip.'),
(995063,112,'Outside coax repair'),
(995063,258,'Preventative Maint'),
(995063,100,'Issue Observed'),
(995063,137,'Swap Altice One Pack'),
(995063,249,'Defective CPE Equip.'),
(995063,112,'Outside coax repair'),
(995063,258,'Preventative Maint'),
(995063,100,'Issue Observed'),
(995063,137,'Swap Altice One Pack'),
(995063,249,'Defective CPE Equip.'),
(995063,112,'Outside coax repair'),
(995063,258,'Preventative Maint')
GO
SELECT * INTO #route_seq FROM dbo.route_fixcodes
ALTER TABLE #route_seq ADD seq [INT] IDENTITY(1,1)
SELECT MIN(seq) AS newseq, route_id,fixcode,fixdescription
FROM #route_seq
group by route_id,fixcode,fixdescription
ORDER BY MIN(seq)
SQL 表表示 无序 集。没有顺序,除非列指定顺序。
让我假设有一个。 . .我就叫它 id
。然后你可以这样做:
select route_id, fixcode, fixdescription
from route_fixcodes
where route_id = 995063
group by route_id, fixcode, fixdescription
order by min(id);
请注意,我已将 having
子句更改为 where
子句。通常,这将减少聚合的数据量——这对性能非常有利。
如果您没有排序列,但特别希望结果按 100、137、249、112、258 的顺序排列,那么您可以使用 case
表达式或类似的逻辑:
select route_id, fixcode, fixdescription
from route_fixcodes
where route_id = 995063
group by route_id, fixcode, fixdescription
order by charindex(id, '100,137,249,112,258');
(这只是一个方便的 shorthand,并非在所有情况下都有效,但适用于您提供的数据。)
下面的查询给了我想要的解决方案。
select route_id,fixcode,fixdescription, MIN(id) as minid from route_fixcodes where route_id = @RouteId GROUP BY route_id,fixcode ,fixdescription Order by minid