Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have one query in sql server 2008 for 2 table that display record like that here is my query :-

SQL
select U.FirstName+' '+U.LastName as 'Full Name',U.City,U.Email,U.Mobile,O.ID as 'Order ID'
from [User] U
inner join [Order] O on U.ID=O.UserID
where U.City like '%puna%'
order by O.ID desc


it will select all the record from table like this :-

SQL
Fullname	City	   Email                 Mobile       OrderID
irfan jesani	Rajkot	 abc@ymail.com	        12345678	1560
hitesh soni	Rajkot	 adminr@gmail.com	12345678	1559
Priyam Shah	Rajkot	 priyam_shah@yahoo.com	12345678	1558
Priyam Shah	Rajkot	 priyam_shah@yahoo.com	12345678	1556



now i want to need a unique record from that. like record of priyam shah select only one time.


Help me ....
Posted
Updated 18-Jul-12 4:51am
v2
Comments
[no name] 18-Jul-12 10:53am    
select distinct
Yatin chauhan 18-Jul-12 11:17am    
i try Distinct too but same record display. no change.

Try:
SQL
SELECT
    DISTINCT U.Email,
    U.FirstName+' '+U.LastName as 'Full Name',
    U.City,
    U.Mobile,
    O.ID as 'Order ID'
FROM 
    [User] U
INNER JOIN 
    [Order] O ON U.ID=O.UserID
WHERE 
    U.City like '%puna%'
ORDER BY 
    O.ID desc
 
Share this answer
 
Comments
Yatin chauhan 19-Jul-12 1:48am    
Hello Sandeep Sir,

i try the same but same record display. still it display all the record of Priyam Shah.

Thank You.
Hi ..

U can use query like below one.

SQL
select distinct name,product from sales 
 
Share this answer
 
Comments
Yatin chauhan 19-Jul-12 1:50am    
Hello Arul
i have distinct but it still display all the records.
Arul R Ece 19-Jul-12 2:04am    
If u use select distinct Fullname City Email Mobile OrderID from tblname .

It will check all the colums.if any matching 2 rows ,it will display one row.otherwise display all the rows.
hey in your query remove o.id because
for Priyam Shah you have 2 order id. hence it is giving to records.
try below query :-

SQL
select distinct U.FirstName+' '+U.LastName as 'Full Name',U.City,U.Email,U.Mobile
from [User] U
inner join [Order] O on U.ID=O.UserID
where U.City like '%puna%'
order by O.ID desc
 
Share this answer
 
Comments
Yatin chauhan 19-Jul-12 7:38am    
I try to this too but it give the error like that :-

"ORDER BY items must appear in the select list if SELECT DISTINCT is specified."

this error because of we don't select order id in select statement. so it give error....
ssd_coolguy 19-Jul-12 8:24am    
so remove order by clouse from the query...
Your query returns two rows because the same person have different order ID
 
Share this answer
 
Comments
Yatin chauhan 19-Jul-12 7:39am    
Yes i know that. so what i have to do for that.
dimpledevani 19-Jul-12 9:10am    
If you remove your orderId column it will work fine i guess.And if you remove it dont forget to remove the order by clause
Your query returns two rows other data same except Different OrderID because the same person have different order ID
So if u get unike record then remove OrderID...........


SELECT
DISTINCT U.Email,
U.FirstName+' '+U.LastName as 'Full Name',
U.City,
U.Mobile
FROM
[User] U
INNER JOIN
[Order] O ON U.ID=O.UserID
WHERE
U.City like '%puna%'
 
Share this answer
 
v4
Comments
Yatin chauhan 19-Jul-12 7:40am    
I try to this too but it give the error like that :-

"ORDER BY items must appear in the select list if SELECT DISTINCT is specified."

this error because of we don't select order id in select statement. so it give error....
Try this..





create table tbl_User(
ID int primary key,
FirstName varchar(500),
LastName varchar(500),
City varchar(500),
Email varchar(500),
Mobile int)

create table tbl_Order(
UserId int,
OrderId int primary key)

insert into tbl_User values (1,'priyam','shah','Rajkot','f@gmail.com',12345678)
insert into tbl_order values (1,5001)
insert into tbl_order values (1,5002)


With Cte_test as
(select U.FirstName+' '+U.LastName as 'Full Name',U.City,U.Email,U.Mobile,O.orderID as 'Order ID',
ROW_NUMBER() over (Partition by Id order by orderId desc) as Rownumber
from [tbl_User] U
inner join [tbl_Order] O on U.ID=O.UserID)
select [Full name],City,Email,Mobile,[Order Id] from Cte_test where Rownumber=1
 
Share this answer
 
Comments
Yatin chauhan 21-Jul-12 2:01am    
I have table like this.
table name [Order]
ID int Primary key
UserID int Forien Key


Table Name [User]
ID int Primary Key
FirstName varchar
LastName varchar
Email varchar
City varchar
Mobile varchar
Santhosh Kumar Jayaraman 23-Jul-12 0:15am    
It wouldnt be difficult.

With Cte_test as
(select U.FirstName+' '+U.LastName as 'Full Name',U.City,U.Email,U.Mobile,O.ID as 'Order ID',
ROW_NUMBER() over (Partition by U.Id order by O.Id desc) as Rownumber
from [tbl_User] U
inner join [tbl_Order] O on U.ID=O.UserID)
select [Full name],City,Email,Mobile,[Order Id] from Cte_test where Rownumber=1

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