Left Join not working - sql

I am a SQL Server beginner and so I have been using MS Access to create queries and then have been amending them for SQL Server.
I have created a query which includes a left join, which works perfectly in Access but when I copy the SQL code across to SQL Server it does not show the null values and I cannot work out why.
I want to show all of the attribute values whether they include a value or not. Can anyone help? The code is as follows:
SELECT DISTINCT
tbLease.LeaseTitle
, tbAttributeValue.AttributeTemplateDefinitionLinkID
, tbAttributeValue.Value
FROM
((((tbBusinessUnit
LEFT JOIN
tbAttributeValue ON tbBusinessUnit.BusinessUnitID = tbAttributeValue.ParentID)
INNER JOIN
tbBuildingLinkBusinessUnit ON tbBusinessUnit.BusinessUnitID = tbBuildingLinkBusinessUnit.BusinessUnitID)
INNER JOIN
tbBuilding ON tbBuildingLinkBusinessUnit.BuildingID = tbBuilding.BuildingID)
INNER JOIN
(tbUnitLocation
INNER JOIN
tbUnit ON tbUnitLocation.UnitID = tbUnit.UnitID)
ON tbBuilding.BuildingID = tbUnitLocation.LocationID)
INNER JOIN
tbLease ON tbUnit.UnitID = tbLease.UnitID
WHERE
(((tbAttributeValue.AttributeTemplateDefinitionLinkID) = 30
Or (tbAttributeValue.AttributeTemplateDefinitionLinkID) = 31
Or (tbAttributeValue.AttributeTemplateDefinitionLinkID) = 32));

Hard to say what you want without some examples but you might want this:
WHERE coalesce(tbAttributeValue.AttributeTemplateDefinitionLinkID,30) in (30,31,32);
This will give you items where AttributeTemplateDefinitionLinkID is null
(that is it did not join on the left join) OR is one of those 3 values.
Right now if you don't join with the left join it will not display that row because of the where condition, so your left join is the same as an inner join.

The NULL values you would normally see with a left join are getting filtered out by your WHERE clause.
Any time you add filters to a WHERE clause that apply to an outer joined table, it will effectively make it the same as an inner join unless you include NULL values as an option in your where clause as well.
SELECT DISTINCT
tbLease.LeaseTitle,
tbAttributeValue.AttributeTemplateDefinitionLinkID,
tbAttributeValue.Value
FROM
tbBusinessUnit
LEFT JOIN tbAttributeValue ON tbBusinessUnit.BusinessUnitID = tbAttributeValue.ParentID
INNER JOIN tbBuildingLinkBusinessUnit ON tbBusinessUnit.BusinessUnitID = tbBuildingLinkBusinessUnit.BusinessUnitID
INNER JOIN tbBuilding ON tbBuildingLinkBusinessUnit.BuildingID = tbBuilding.BuildingID
INNER JOIN tbUnitLocation ON tbBuilding.BuildingID = tbUnitLocation.LocationID
INNER JOIN tbUnit ON tbUnitLocation.UnitID = tbUnit.UnitID
INNER JOIN tbLease ON tbUnit.UnitID = tbLease.UnitID
WHERE
tbAttributeValue.AttributeTemplateDefinitionLinkID in (30, 31, 32)
or tbAttributeValue.AttributeTemplateDefinitionLinkID is null

Move the right table filters from where clause to ON condition when you are using Left Outer Join else Left join will be implicitly converted to INNER JOIN. Try this
SELECT DISTINCT tblease.leasetitle,
tbattributevalue.attributetemplatedefinitionlinkid,
tbattributevalue.value
FROM tbbusinessunit
LEFT JOIN tbattributevalue
ON tbbusinessunit.businessunitid = tbattributevalue.parentid
AND ( tbattributevalue.attributetemplatedefinitionlinkid IN
( 30, 31, 32 )
OR tbattributevalue.attributetemplatedefinitionlinkid IS
NULL )
INNER JOIN tbbuildinglinkbusinessunit
ON tbbusinessunit.businessunitid =
tbbuildinglinkbusinessunit.businessunitid
INNER JOIN tbbuilding
ON tbbuildinglinkbusinessunit.buildingid = tbbuilding.buildingid
INNER JOIN tbunitlocation
ON tbbuilding.buildingid = tbunitlocation.locationid
INNER JOIN tbunit
ON tbunitlocation.unitid = tbunit.unitid
INNER JOIN tblease
ON tbunit.unitid = tblease.unitid

Related

Big Query : LEFT OUTER JOIN cannot be used without a condition that is an equality of fields from both sides of the join

I am writing an equivalent logic in BQ as in my source system. In source SQL server side it is working fine. But in Big query it is failing with the OR condition in the last left outer join condition. If I am moving the OR condition in the where clause it is giving wrong count. Need help to fix this issue. How can I re write the below query ?
SELECT count(*)
FROM stprof PRO
INNER JOIN stdim DIM
ON (DIM.diSet = PRO.diSet)
INNER JOIN DQConfig CFG
ON (CFG.ConSet = PRO.ConSet)
LEFT OUTER JOIN AgSt CCT
ON (CCT.StSet = PRO.StSet)
INNER JOIN stprof SummPRO
ON (SummPRO.diSet = DIM.SummdiSet AND
SummPRO.dIntervalStart = PRO.dIntervalStart AND
SummPRO.SiteId = PRO.SiteId AND
SummPRO.nDuration = PRO.nDuration)
LEFT OUTER JOIN AgSt SummCCT
ON (SummCCT.StSet = SummPRO.StSet)
LEFT OUTER JOIN AgentStatus SummSTS
ON (
SummSTS.StSet = SummPRO.StSet
OR
SummSTS.StSet = PRO.StSet)
WHERE DIM.cType = 'A'
You can replace LEFT JOIN with CROSS JOIN and move condition from ON clause to WHERE clause as in below example
#standardSQL
SELECT COUNT(*)
FROM stprof PRO
INNER JOIN stdim DIM
ON DIM.diSet = PRO.diSet
INNER JOIN DQConfig CFG
ON CFG.ConSet = PRO.ConSet
LEFT OUTER JOIN AgSt CCT
ON CCT.StSet = PRO.StSet
INNER JOIN stprof SummPRO
ON SummPRO.diSet = DIM.SummdiSet
AND SummPRO.dIntervalStart = PRO.dIntervalStart
AND SummPRO.SiteId = PRO.SiteId
AND SummPRO.nDuration = PRO.nDuration
LEFT OUTER JOIN AgSt SummCCT
ON SummCCT.StSet = SummPRO.StSet
CROSS JOIN AgentStatus SummSTS
WHERE DIM.cType = 'A'
AND (
SummSTS.StSet = SummPRO.StSet
OR SummSTS.StSet = PRO.StSet
)

Convert fractions into decimal in sql using a function and select statements

Every time I try to run the following in Microsoft SQL Server Management Studio, I get an error message
A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations
Is there anyway at all to run this in one statement?
The reason why I'm calling a function in this statement (dbo.ufn_ConvertJambDepthFractionToNumber) is because the dbo.CDS_Shipments_Door_Overall_Jamb_Depth.InstructionValue AS [Jamb Depth] column spits out values like 4 1/6", 3 1/4", 6 9/16". I'm using that function to try to convert the varchar values (4 1/6", 3 1/4", 6 9/16") into decimals.
If there is a better way to do that, I'm all ears.
use BeechworthProdWTS
declare #jamb_depth as varchar
SELECT DISTINCT
dbo.QuoteShippingAddress.Name AS [CDS Location],
dbo.QuoteShippingAddress.Address1 AS [Quote Shipping Address1],
dbo.CDS_Shipments_Door_Overall_Jamb_Depth.InstructionValue AS [Jamb Depth],
dbo.CDS_Shipments_Door_Count_view.Doors,
dbo.CDS_Shipments_Screen_Count_view.Screens,
dbo.CDS_Shipments_Windows_Count_view.Windows,
dbo.LineItemMaster.LineNumber,
#jamb_depth = dbo.CDS_Shipments_Door_Overall_Jamb_Depth.InstructionValue,
dbo.ufn_ConvertJambDepthFractionToNumber(#jamb_depth)
FROM
dbo.Quotes
INNER JOIN
dbo.QuoteShippingAddress WITH (NOLOCK) ON dbo.Quotes.QuoteID = dbo.QuoteShippingAddress.QuoteID
INNER JOIN
dbo.Clients WITH (NOLOCK) ON dbo.Quotes.ClientID = dbo.Clients.ClientID
INNER JOIN
dbo.CustomerProjectInformation WITH (NOLOCK) ON dbo.Quotes.QuoteID = dbo.CustomerProjectInformation.QuoteID
INNER JOIN
dbo.LineItemMaster WITH (NOLOCK) ON dbo.Quotes.QuoteID = dbo.LineItemMaster.QuoteID
INNER JOIN
dbo.LineItems WITH (NOLOCK) ON dbo.LineItemMaster.LineItemMasterID = dbo.LineItems.LineItemMasterID
INNER JOIN
dbo.WorkOrders WITH (NOLOCK) ON dbo.LineItems.LineItemID = dbo.WorkOrders.LineItemID
LEFT OUTER JOIN
dbo.CDS_Shipments_Door_Count_view WITH (NOLOCK) ON dbo.LineItems.LineItemID = dbo.CDS_Shipments_Door_Count_view.LineItemID
LEFT OUTER JOIN
dbo.CDS_Shipments_Door_Overall_Jamb_Depth WITH (NOLOCK) ON dbo.LineItems.LineItemID = dbo.CDS_Shipments_Door_Overall_Jamb_Depth.LineItemID
LEFT OUTER JOIN
dbo.CDS_Shipments_Screen_Count_view WITH (NOLOCK) ON dbo.LineItems.LineItemID = dbo.CDS_Shipments_Screen_Count_view.LineItemID
LEFT OUTER JOIN
dbo.CDS_Shipments_Windows_Count_view WITH (NOLOCK) ON dbo.LineItems.LineItemID = dbo.CDS_Shipments_Windows_Count_view.LineItemID
Since you say you are pretty new to SQL I reformatted this using aliases so you can see how much difference it make visually. This doesn't affect performance but it does keep the developer sane. Also, not really sure what you are trying to do with your scalar but maybe trying to get the value for each row?
Cleaned up your query might look something like this.
SELECT DISTINCT
qsa.Name AS [CDS Location],
qsa.Address1 AS [Quote Shipping Address1],
sdojd.InstructionValue AS [Jamb Depth],
sdcv.Doors,
sscv.Screens,
swcv.Windows,
lim.LineNumber,
--#jamb_depth = sdojd.InstructionValue, --not really sure what you are trying to do here
--dbo.ufn_ConvertJambDepthFractionToNumber(#jamb_depth)
JambDepth = dbo.ufn_ConvertJambDepthFractionToNumber(sdojd.InstructionValue)
FROM dbo.Quotes q
INNER JOIN dbo.QuoteShippingAddress qsa ON q.QuoteID = qsa.QuoteID
INNER JOIN dbo.Clients c ON q.ClientID = c.ClientID
INNER JOIN dbo.CustomerProjectInformation cpi ON q.QuoteID = cpi.QuoteID
INNER JOIN dbo.LineItemMaster lim ON q.QuoteID = lim.QuoteID
INNER JOIN dbo.LineItems li ON lim.LineItemMasterID = li.LineItemMasterID
INNER JOIN dbo.WorkOrders wo ON li.LineItemID = wo.LineItemID
LEFT OUTER JOIN dbo.CDS_Shipments_Door_Count_view sdcv ON li.LineItemID = sdcv.LineItemID
LEFT OUTER JOIN dbo.CDS_Shipments_Door_Overall_Jamb_Depth sdojd ON li.LineItemID = sdojd.LineItemID
LEFT OUTER JOIN dbo.CDS_Shipments_Screen_Count_view sscv ON li.LineItemID = sscv.LineItemID
LEFT OUTER JOIN dbo.CDS_Shipments_Windows_Count_view swcv ON li.LineItemID = swcv.LineItemID

POSTGRESQL - UNNEST function not work in LINUX [duplicate]

When i run this query in window system behave correctly UNNSET
but when i run this query Linux behave different.unnset duplicate record list on different row
SELECT DISTINCT
"billing_billmanagement"."creation_date",
"billing_billmanagement"."bill_number",
unnest(array_agg(DISTINCT "inventory_product"."product_name")) AS "product",
unnest(array_agg(DISTINCT "services_service"."name")) AS "service"
FROM "billing_billmanagement"
INNER JOIN "users_staffuser" ON ("billing_billmanagement"."staff_id" = "users_staffuser"."id")
INNER JOIN "auth_user" ON ("users_staffuser"."user_id" = "auth_user"."id")
LEFT OUTER JOIN "billing_customerproductbill" ON ("billing_billmanagement"."id" = "billing_customerproductbill"."bill_id")
LEFT OUTER JOIN "inventory_product" ON ("billing_customerproductbill"."product_id" = "inventory_product"."id")
LEFT OUTER JOIN "billing_customerservicebill" ON ("billing_billmanagement"."id" = "billing_customerservicebill"."bill_id")
LEFT OUTER JOIN "services_service" ON ("billing_customerservicebill"."service_id" = "services_service"."id")
WHERE "billing_billmanagement"."creation_date" BETWEEN '2017-12-04' AND '2017-12-06'
GROUP BY billing_billmanagement.creation_date,
billing_billmanagement.bill_number
ORDER BY "billing_billmanagement"."creation_date" ASC
If getting duplicate rows is the problem, try this
SELECT billing_billmanagement.creation_date,
billing_billmanagement.bill_number,
inventory_product.product_name AS product,
services_service.name AS service
FROM billing_billmanagement
INNER JOIN users_staffuser ON (billing_billmanagement.staff_id = users_staffuser.id)
INNER JOIN auth_user ON (users_staffuser.user_id = auth_user.id)
LEFT OUTER JOIN billing_customerproductbill ON (billing_billmanagement.id = billing_customerproductbill.bill_id)
LEFT OUTER JOIN inventory_product ON (billing_customerproductbill.product_id = inventory_product.id)
LEFT OUTER JOIN billing_customerservicebill ON (billing_billmanagement.id = billing_customerservicebill.bill_id)
LEFT OUTER JOIN services_service ON (billing_customerservicebill.service_id = services_service.id)
WHERE billing_billmanagement.creation_date BETWEEN '2017-12-04' AND '2017-12-06'
GROUP BY 1,
2,
3,
4
ORDER BY 1 ASC;

IN Linux Distinct SQL is not working with UNNEST

When i run this query in window system behave correctly UNNSET
but when i run this query Linux behave different.unnset duplicate record list on different row
SELECT DISTINCT
"billing_billmanagement"."creation_date",
"billing_billmanagement"."bill_number",
unnest(array_agg(DISTINCT "inventory_product"."product_name")) AS "product",
unnest(array_agg(DISTINCT "services_service"."name")) AS "service"
FROM "billing_billmanagement"
INNER JOIN "users_staffuser" ON ("billing_billmanagement"."staff_id" = "users_staffuser"."id")
INNER JOIN "auth_user" ON ("users_staffuser"."user_id" = "auth_user"."id")
LEFT OUTER JOIN "billing_customerproductbill" ON ("billing_billmanagement"."id" = "billing_customerproductbill"."bill_id")
LEFT OUTER JOIN "inventory_product" ON ("billing_customerproductbill"."product_id" = "inventory_product"."id")
LEFT OUTER JOIN "billing_customerservicebill" ON ("billing_billmanagement"."id" = "billing_customerservicebill"."bill_id")
LEFT OUTER JOIN "services_service" ON ("billing_customerservicebill"."service_id" = "services_service"."id")
WHERE "billing_billmanagement"."creation_date" BETWEEN '2017-12-04' AND '2017-12-06'
GROUP BY billing_billmanagement.creation_date,
billing_billmanagement.bill_number
ORDER BY "billing_billmanagement"."creation_date" ASC
If getting duplicate rows is the problem, try this
SELECT billing_billmanagement.creation_date,
billing_billmanagement.bill_number,
inventory_product.product_name AS product,
services_service.name AS service
FROM billing_billmanagement
INNER JOIN users_staffuser ON (billing_billmanagement.staff_id = users_staffuser.id)
INNER JOIN auth_user ON (users_staffuser.user_id = auth_user.id)
LEFT OUTER JOIN billing_customerproductbill ON (billing_billmanagement.id = billing_customerproductbill.bill_id)
LEFT OUTER JOIN inventory_product ON (billing_customerproductbill.product_id = inventory_product.id)
LEFT OUTER JOIN billing_customerservicebill ON (billing_billmanagement.id = billing_customerservicebill.bill_id)
LEFT OUTER JOIN services_service ON (billing_customerservicebill.service_id = services_service.id)
WHERE billing_billmanagement.creation_date BETWEEN '2017-12-04' AND '2017-12-06'
GROUP BY 1,
2,
3,
4
ORDER BY 1 ASC;

sum of particular column getting error [duplicate]

This question already has answers here:
Reason for Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause [duplicate]
(4 answers)
Closed 4 years ago.
I have a query like this:
SELECT DISTINCT
dbo.T_Order_Header.F_Exhibitor,
dbo.T_Order_Header.F_Exhibition,
dbo.T_Exhibition.F_Exhibition_Name,
dbo.T_Exhibitor.F_Exhibitor_Name,
dbo.T_Order_Detail.F_ItemCode,
dbo.T_L2Category.F_L2Cat_Name,
SUM(dbo.T_Order_Detail.F_Qty-dbo.T_Order_Detail.F_CNQty) AS F_Qty,
dbo.T_L1Category.F_L1Cat_Name,
dbo.T_Order_Header.F_Stand,
dbo.T_Category.F_Cat_name,
dbo.T_ExStand.F_Bld_Code,
dbo.T_ExBuilding.F_Bld_name
FROM dbo.T_Order_Header
LEFT OUTER JOIN dbo.T_OrderAttachment
ON dbo.T_OrderAttachment.F_OrderNumber = dbo.T_Order_Header.F_OrderNumber
LEFT OUTER JOIN dbo.T_Order_Detail
ON dbo.T_Order_Detail.[Header_ID] = dbo.T_Order_Header.[ID]
LEFT OUTER JOIN dbo.T_L2Category
ON dbo.T_Order_Detail.F_ItemCode = dbo.T_L2Category.F_ItemCode
LEFT OUTER JOIN dbo.T_L1Category
ON dbo.T_L1Category.F_L1Cat_Code = dbo.T_L2Category.F_L1Cat_Code
LEFT OUTER JOIN dbo.T_Category
ON dbo.T_Category.F_Cat_Code = dbo.T_L2Category.F_Cat_Code
LEFT OUTER JOIN dbo.T_ExStand
ON dbo.T_ExStand.F_Stand_Code = dbo.T_Order_Header.F_Stand
LEFT OUTER JOIN dbo.T_ExBuilding
ON dbo.T_ExStand.F_Bld_Code = dbo.T_ExBuilding.F_Bld_Code
LEFT OUTER JOIN dbo.T_Exhibition
ON dbo.T_Order_Header.F_Exhibition = dbo.T_Exhibition.F_Exhibition_Code
LEFT OUTER JOIN dbo.T_Exhibitor
ON dbo.T_Order_Header.F_Exhibitor = dbo.T_Exhibitor.F_Exhibitor_Code
WHERE
F_Stand IN(
SELECT F_Stand_Code
FROM T_ExStand
WHERE
F_Site_Code ='DWTC'
AND F_Bld_Code = 'HALL1-4 & CONCOURSE'
)
AND T_Order_Header.F_Exhibition = '12004'
AND T_Order_Header.F_IsActive = 1
AND F_Exhibitor='2467'
I want to show sum of F_qty but I keep getting this error:
Column 'dbo.T_Order_Header.F_Exhibitor' is invalid in the select list
because it is not contained in either an aggregate function or the
GROUP BY clause
You need to group by with all the columns which you're selecting (other than the one which is in the aggregate function), something like below query:
SELECT
dbo.T_Order_Header.F_Exhibitor,
dbo.T_Order_Header.F_Exhibition,
dbo.T_Exhibition.F_Exhibition_Name,
dbo.T_Exhibitor.F_Exhibitor_Name,
dbo.T_Order_Detail.F_ItemCode,
dbo.T_L2Category.F_L2Cat_Name,
SUM(dbo.T_Order_Detail.F_Qty-dbo.T_Order_Detail.F_CNQty) AS F_Qty,
dbo.T_L1Category.F_L1Cat_Name,
dbo.T_Order_Header.F_Stand,
dbo.T_Category.F_Cat_name,
dbo.T_ExStand.F_Bld_Code,
dbo.T_ExBuilding.F_Bld_name
FROM dbo.T_Order_Header
LEFT OUTER JOIN dbo.T_OrderAttachment
ON dbo.T_OrderAttachment.F_OrderNumber = dbo.T_Order_Header.F_OrderNumber
LEFT OUTER JOIN dbo.T_Order_Detail
ON dbo.T_Order_Detail.[Header_ID] = dbo.T_Order_Header.[ID]
LEFT OUTER JOIN dbo.T_L2Category
ON dbo.T_Order_Detail.F_ItemCode = dbo.T_L2Category.F_ItemCode
LEFT OUTER JOIN dbo.T_L1Category
ON dbo.T_L1Category.F_L1Cat_Code = dbo.T_L2Category.F_L1Cat_Code
LEFT OUTER JOIN dbo.T_Category
ON dbo.T_Category.F_Cat_Code = dbo.T_L2Category.F_Cat_Code
LEFT OUTER JOIN dbo.T_ExStand
ON dbo.T_ExStand.F_Stand_Code = dbo.T_Order_Header.F_Stand
LEFT OUTER JOIN dbo.T_ExBuilding
ON dbo.T_ExStand.F_Bld_Code = dbo.T_ExBuilding.F_Bld_Code
LEFT OUTER JOIN dbo.T_Exhibition
ON dbo.T_Order_Header.F_Exhibition = dbo.T_Exhibition.F_Exhibition_Code
LEFT OUTER JOIN dbo.T_Exhibitor
ON dbo.T_Order_Header.F_Exhibitor = dbo.T_Exhibitor.F_Exhibitor_Code
WHERE
F_Stand IN(
SELECT F_Stand_Code
FROM T_ExStand
WHERE
F_Site_Code ='DWTC'
AND F_Bld_Code = 'HALL1-4 & CONCOURSE'
)
AND T_Order_Header.F_Exhibition = '12004'
AND T_Order_Header.F_IsActive = 1
AND F_Exhibitor='2467'
group by dbo.T_Order_Header.F_Exhibitor,
dbo.T_Order_Header.F_Exhibition,
dbo.T_Exhibition.F_Exhibition_Name,
dbo.T_Exhibitor.F_Exhibitor_Name,
dbo.T_Order_Detail.F_ItemCode,
dbo.T_L2Category.F_L2Cat_Name,
dbo.T_L1Category.F_L1Cat_Name,
dbo.T_Order_Header.F_Stand,
dbo.T_Category.F_Cat_name,
dbo.T_ExStand.F_Bld_Code,
dbo.T_ExBuilding.F_Bld_name
Use below query for your requirement.
SELECT DISTINCT
dbo.T_Order_Header.F_Exhibitor,
dbo.T_Order_Header.F_Exhibition,
dbo.T_Exhibition.F_Exhibition_Name,
dbo.T_Exhibitor.F_Exhibitor_Name,
dbo.T_Order.F_ItemCode,
dbo.T_L2Category.F_L2Cat_Name,
T_Order.F_Qty F_Qty,
dbo.T_L1Category.F_L1Cat_Name,
dbo.T_Order_Header.F_Stand,
dbo.T_Category.F_Cat_name,
dbo.T_ExStand.F_Bld_Code,
dbo.T_ExBuilding.F_Bld_name
FROM dbo.T_Order_Header
LEFT OUTER JOIN dbo.T_OrderAttachment
ON dbo.T_OrderAttachment.F_OrderNumber = dbo.T_Order_Header.F_OrderNumber
LEFT OUTER JOIN
(select SUM(dbo.T_Order_Detail.F_Qty)-sum(dbo.T_Order_Detail.F_CNQty) AS F_Qty,
[Header_ID],F_ItemCode from
dbo.T_Order_Detail
group by
[Header_ID]) T_Order ON dbo.T_Order.[Header_ID] = dbo.T_Order_Header.[ID]
LEFT OUTER JOIN dbo.T_L2Category
ON dbo.F_ItemCode.F_ItemCode = dbo.T_L2Category.F_ItemCode
LEFT OUTER JOIN dbo.T_L1Category
ON dbo.T_L1Category.F_L1Cat_Code = dbo.T_L2Category.F_L1Cat_Code
LEFT OUTER JOIN dbo.T_Category
ON dbo.T_Category.F_Cat_Code = dbo.T_L2Category.F_Cat_Code
LEFT OUTER JOIN dbo.T_ExStand
ON dbo.T_ExStand.F_Stand_Code = dbo.T_Order_Header.F_Stand
LEFT OUTER JOIN dbo.T_ExBuilding
ON dbo.T_ExStand.F_Bld_Code = dbo.T_ExBuilding.F_Bld_Code
LEFT OUTER JOIN dbo.T_Exhibition
ON dbo.T_Order_Header.F_Exhibition = dbo.T_Exhibition.F_Exhibition_Code
LEFT OUTER JOIN dbo.T_Exhibitor
ON dbo.T_Order_Header.F_Exhibitor = dbo.T_Exhibitor.F_Exhibitor_Code
WHERE
F_Stand IN(
SELECT F_Stand_Code
FROM T_ExStand
WHERE
F_Site_Code ='DWTC'
AND F_Bld_Code = 'HALL1-4 & CONCOURSE'
)
AND T_Order_Header.F_Exhibition = '12004'
AND T_Order_Header.F_IsActive = 1
AND F_Exhibitor='2467'
You are using SQL group function. In this case you have to add GROUP BY clause at the end of the sql-statement. Please add all generic columns to GROUP BY clause except F_Qty. And remove DISTINCT from your select statement.