Click here to Skip to main content
15,889,830 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Posted
Comments
Ankur\m/ 7-Feb-13 2:18am    
Filter the null value in the where clause if you don't want it.
And why can't you use 'Order By' clause?
Himanshu Yadav 7-Feb-13 2:26am    
original order of row
dddd
ffff
gg
null
ss
null

after using DISTINCT

null
dddd
ffff
gg
ss

row order is changed...
Chris Reynolds (UK) 7-Feb-13 9:21am    
I think we need to know why you can't use ORDER BY as this is the only way that you can be sure what order your result set will appear in.
RedDk 7-Feb-13 14:14pm    
What he's intimating is that the duplicate has been eliminated because he's used DISTINCT however the fact that it's NULL means that he wants to position that NULL between gg and ss.
Chris Reynolds (UK) 8-Feb-13 4:02am    
If what he's saying is that he wants null (and I assume that this is a database null rather than the literal) to be treated as starting with the letter n and being sorted as though it were the literal 'null' then he could cheat and do something like:

SELECT Col1 from Table order by ISNULL(Col1, 'null')

which would fool the sort order.

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'

That last SELECT clause' results:
etches
~~~~~~
dddd
ffff
gg
ss
 
Share this answer
 
v2
Hi i got the solution it will be like this..Thanks all to help me

SQL
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900