Click here to Skip to main content
15,894,362 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
able Product

PID(PK) --- Name --- ImagUrl

1 - Mobile --- something
2 - Laptop --- something
3 - Fashion --- something



Tabe Coupons

ID PID City

1 - 2 - Bangalore
2 - 3 - Pune
3 - 2 - Mumbai
4 - 1 - Bangalore
5 - 2 - Mumbai


I want to select distinct City name records joining two tables
(if i have five different city then i want to select one one record from each city).
Pl'z suggest me, how can i fetch this records.
I trired using distinct keyword to fetch records but i am unable to find desire output....
Posted
Comments
Herman<T>.Instance 15-Jan-13 6:24am    
In fact your determantion of tables is incorrect. Create A table City.
Suvabrata Roy 16-Jan-13 5:04am    
which columns are required in your output ...?
Sumit_Kumar_Sinha 17-Jan-13 0:15am    
for distinct city PID and Name

1 solution

SQL
select distinct Sumit.City, Sumit.Name  -- or Sumit.PID, depends on what you want to show
from
(
    select Row_Number() over(Partition By c.ID, c.City Order BY c.ID desc, City) as rowno,
	c.ID, c.City, p.Name, p.PID
    From Coupons  c
    JOIN Product p on c.pid = p.pid
) as Sumit
where Sumit.rowno = 1


1 assumption is that you use Sql Server by Microsoft
 
Share this answer
 
v4
Comments
Sumit_Kumar_Sinha 15-Jan-13 6:36am    
hey....i want to select city name and last Inserted PID for each city ........
Herman<T>.Instance 15-Jan-13 6:57am    
Do you need PID or NAME in the final result?
Sumit_Kumar_Sinha 15-Jan-13 9:34am    
i need city and pid in the final result
Herman<T>.Instance 15-Jan-13 9:42am    
the query above is already changed. in stead of sumit.Name you stat sumit.PID. Have you tested the query?
Sumit_Kumar_Sinha 16-Jan-13 1:25am    
it's throwing error :- Invalid column name 'City'.

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