Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i am working with sql server. now i want to join two tables like below.

SQL
select 
bill.bill_no as BillNo,
bill.customer_name,
room_no_info.room_number,
isnull(convert(varchar(11),room_no_info.in_date,106),'01-Jan-99') as InDate,
isnull(convert(varchar(11),room_no_info.out_date,106),'01-Jan-99') as OutDate
 from bill inner join room_no_info on bill.bill_no=room_no_info.bill_no group by bill.bill_no,room_no_info.room_number,room_no_info.in_date,room_no_info.out_date,
bill.customer_name


it returns like below..

BillNo Name RoomNo InDate OutDate

20 Name:zzzz 315 02 Jun 2013 14 Jun 2013
21 Name:xxxx 309 02 Jun 2013 15 Jun 2013
21 Name:xxxx 310 02 Jun 2013 03 Jun 2013
22 Name:yyyy 206 01 Jun 2013 02 Jun 2013


it returns all rows which are satisfies join conditions but i want return only one row from the second table.

can u help me any one?
Posted

Instead INNER JOIN, use RIGHT or LEFT JOIN.
To understand the difference, please, read this article: Visual Representation of SQL Joins[^]
 
Share this answer
 
i solved using aggregate function.

SQL
select
bill.bill_no as BillNo,
bill.customer_name,
max(room_no_info.room_number),
isnull(convert(varchar(11),room_no_info.in_date,106),'01-Jan-99') as InDate,
isnull(convert(varchar(11),room_no_info.out_date,106),'01-Jan-99') as OutDate
 from bill inner join room_no_info on bill.bill_no=room_no_info.bill_no group by bill.bill_no,room_no_info.in_date,room_no_info.out_date,
bill.customer_name
 
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