Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Need Help for the following SQL Query

$
0
0

Hi,

I hope all of you are fine.

I am looking for a solution generate report in C# Crystal Reports, and for that I am using SQL Server Stored Procedure. Query gives me duplicate result, if this can be handled Crystal Reports then please also tell me how? and if query has to be modified then please help.

Following picture is showing the result of a query, here there are three tables, Bookings, Cargo_Details and Expense_Details, Booking table is the Parent table and other two are child tables. There is a 1 to many relation between Parents and child table. Although I have not set the relations as I am doing it by C# Code. Now the query gives me result that for each record of child tables it repeats booking record and also both child tables repeat there own records. Please see the picture.

Following pictures is showing the result for which I am looking for.

here is the query which I have written and showing me result as of 1st picture

SELECT 
	BK.[Booking_ID],
	BK.[Date],
	--[Bookings].[POD],
	--[Bookings].[POL],
	(select Ports.Port_Name From Ports where Ports.Port_ID = BK.POD) AS POD ,
	(select Ports.Port_Name From Ports where Ports.Port_ID = BK.POL) AS POL ,
	(select Countries.Country_Name From Countries where Countries.Country_ID = BK.Origin) AS Origin ,
	(select Countries.Country_Name From Countries where Countries.Country_ID = BK.Destination) AS Destination ,
	BK.[House_Bill_No],
	BK.[Master_Bill_No],
	BK.[Vessel_Flight_No],
	BK.[Vessel_Flight_Date],
	BK.[Export],
	BK.[Import],
	SHPNG.[Shipping_Type],
	CON.[Name],
	CGO.[Container_No],
	CGO.[Size],
	CGO.Gross_Weight,
	CGO.Net_Weight,
	CGO.Package_Dimention,
	CGO.[CBM],
	CGO.[Quantity],
	PKG.Package_Type,
	CU.Currency_Name,
	EXPD.[Selling],
	EXPD.Buying,
	EXPD.[Exchange_Rate],
	EXPD.[Description]
	--[User_Accounts].[First_Name],
	--[User_Accounts].[Last_Name]
FROM 
    Bookings BK left outer join
	Cargo_Detail CGO on CGO.Booking_ID = BK.Booking_ID left outer join
	Expense_Detail EXPD on EXPD.Booking_ID = BK.Booking_ID left outer join
	Currency CU on CU.Currency_ID=EXPD.Currency left outer join
	Contact_Detail CON on CON.Contact_ID = BK.Consignee_Importer left outer join
	Shipping_Type SHPNG on SHPNG.Shipping_Type_ID = BK.ShippingType left outer join
	Packages PKG on PKG.Package_ID = CGO.Package_ID
	--User_Accounts on Payments.User_ID=User_Accounts.User_ID
WHERE 
	BK.[Booking_ID]= 18

Waiting for earliest replies.

Thanks and Regards

Faheem Riaz Awan


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>