sql Distinct正在改变表中行的顺序 [英] sql Distinct is changing the order of row in table
本文介绍了sql Distinct正在改变表中行的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我使用distinct时,总是将null值置于顶部。该行的顺序正在变化,请帮帮我。
如何避免这个
注意 - 我不能使用order by子句
When i am using distinct it is always bringing the null value on top.Means order of the row is getting changed please help me out.
How to avoid this
NOTE- i can not use order By clause
推荐答案
我没有看到行的顺序正在改变......
(我的意思是,从技术上来说,因为消除堆栈顶部)
[结束编辑]
I don''t see "the order of the row is getting changed ..."
[edit]
(I mean, not technically, because that NULL on the top of the stack is eliminated)
[end edit]
--CREATE SCHEMA [cpqa]
USE [cpqaAnswers]
GO
CREATE TABLE [cpqaAnswers].[cpqa].[tblHY](
[etches][nvarchar](48)
)
INSERT INTO [cpqaAnswers].[cpqa].[tblHY](etches)
VALUES(''dddd'')
INSERT INTO [cpqaAnswers].[cpqa].[tblHY](etches)
VALUES(''ffff'')
INSERT INTO [cpqaAnswers].[cpqa].[tblHY](etches)
VALUES(''gg'')
INSERT INTO [cpqaAnswers].[cpqa].[tblHY](etches)
VALUES(NULL)
INSERT INTO [cpqaAnswers].[cpqa].[tblHY](etches)
VALUES(''ss'')
INSERT INTO [cpqaAnswers].[cpqa].[tblHY](etches)
VALUES(NULL)
SELECT [etches] FROM [cpqaAnswers].[cpqa].[tblHY]
SELECT DISTINCT [etches] FROM [cpqaAnswers].[cpqa].[tblHY]
SELECT * FROM [cpqaAnswers].[cpqa].[tblHY] WHERE [etches] != ''NULL''
最后一个SELECT子句''结果:
That last SELECT clause'' results:
etches
~~~~~~
dddd
ffff
gg
ss
您好我得到了解决方案就像这样......谢谢所有人帮助我
Hi i got the solution it will be like this..Thanks all to help me
WITH CTE AS (
SELECT SUBSTRING(code,2,2) as c_code, ProdName, ROW_NUMBER() OVER(PARTITION BY SUBSTRING(code,2,2)ORDER BY SERIALNUMBER,PRODUCTNUMBER,SOURCE) AS RowNumber
FROM Productdetail
)
SELECT * INTO #ProdDetail FROM CTE WHERE RowNumber =1
这不会改变订单但会给出最高记录我需要
This will not change the order but gives the record on the top that what i was needed
这篇关于sql Distinct正在改变表中行的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文