Access SQL Sum Duplicating Rows - sql

I'm trying to write an Access SQL Query to get some values with a sub-select and sum a bunch of data but when I run the query the data is getting duplicated.
HereĀ“s my query
SELECT
tt.TransportType,
rp.duns AS Duns,
rp.part AS Part,
rp.plant AS Plant,
rr.Route AS Route,
rr.RouteComp,
tt.TransLength*tt.TransWidth*tt.TransHeight AS [Capacidade],
len(rr.CurrentFrequency) AS [Frequencia],
Capacidade*Frequencia AS Cap_Disp,
s.Cap_Disp*s.FrequenciaTotal AS Cap_Total,
s.FrequenciaTotal,
Cap_Disp/Cap_Total AS Rateio,
FROM ((((tblRoutesParts rp
INNER JOIN tbl20week w ON rp.duns = w.duns
AND rp.part = w.prt
AND rp.plant = w.plant)
INNER JOIN tblRoutesRoutes rr ON rp.Route = rr.Route)
INNER JOIN tblTransportTypes tt ON tt.TransportType = rr.TransportType)
INNER JOIN (SELECT tt.TransportType, rp.duns, rp.part, rp.plant,
sum(len(rr.CurrentFrequency)) as FrequenciaTotal,
sum((tt.TransLength*tt.TransWidth*tt.TransHeight)*(len(rr.CurrentFrequency))) AS Cap_Disp
from ( tblRoutesParts rp
INNER JOIN tblRoutesRoutes rr ON rp.Route = rr.Route)
INNER JOIN tblTransportTypes tt ON tt.TransportType = rr.TransportType
GROUP BY tt.TransportType,
rp.duns,
rp.part,
rp.plant) s ON s.duns= rp.duns
AND s.part = rp.part
AND s.plant = rp.plant)
WHERE left(rp.Route, 1) <> 'L'
and rp.duns = '903323939'
and rp.part = '24584938'
and rp.plant = 'BE'
and rr.Route = 'FRW.A0001'
And here's the output:
Like you see the data is duplicated only at the sum fields!
Can anyone help me?

Try joining your 's' ON s.TransportType = tt.TransportType. You may be getting multiple rows when you inner join s.
SELECT
tt.TransportType,
rp.duns AS Duns,
rp.part AS Part,
rp.plant AS Plant,
rr.Route AS Route,
rr.RouteComp,
tt.TransLength*tt.TransWidth*tt.TransHeight AS [Capacidade],
len(rr.CurrentFrequency) AS [Frequencia],
Capacidade*Frequencia AS Cap_Disp,
s.Cap_Disp*s.FrequenciaTotal AS Cap_Total,
s.FrequenciaTotal,
Cap_Disp/Cap_Total AS Rateio,
FROM ((((tblRoutesParts rp
INNER JOIN tbl20week w ON rp.duns = w.duns
AND rp.part = w.prt
INNER JOIN tblRoutesRoutes rr ON rp.Route = rr.Route)
INNER JOIN tblTransportTypes tt ON tt.TransportType = rr.TransportType)
INNER JOIN (SELECT tt.TransportType, rp.duns, rp.part, rp.plant,
sum(len(rr.CurrentFrequency)) as FrequenciaTotal,
sum((tt.TransLength*tt.TransWidth*tt.TransHeight)*(len(rr.CurrentFrequency))) AS Cap_Disp
from ( tblRoutesParts rp
INNER JOIN tblRoutesRoutes rr ON rp.Route = rr.Route)
INNER JOIN tblTransportTypes tt ON tt.TransportType = rr.TransportType
GROUP BY tt.TransportType,
rp.duns,
rp.part,
rp.plant) s ON s.duns= rp.duns
AND s.part = rp.part
AND s.plant = rp.plant
AND s.TransportType = tt.TransportType)
WHERE left(rp.Route, 1) <> 'L'
and rp.duns = '903323939'
and rp.part = '24584938'
and rp.plant = 'BE'
and rr.Route = 'FRW.A0001'

Related

Column 'h.Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause

I need to return all of these columns but only where EmployeeID occurs once. I tried using group by having COUNT but get the error in the title. I have seen some examples doing this using only one or two columns in the select field but I am not sure how to structure this with more columns and do the group by on only EmployeeID.
SELECT DISTINCT i.EmployeeID, h.Name, h.AliasLastName, pp.Active, pp.DeptID, d.Name AS DepartmentName,
dpp.Title, hpp.Status, hpp.StatusDateTime, i.HealthRecordID, i.ImmunizationDateTime
FROM [liveNdb].dbo.HrEmpAuditHealthRecordImmunizations i
INNER JOIN liveNdb.dbo.HrEmployees h
ON h.SourceID = i.SourceID
AND h.EmployeeID = i.EmployeeID
INNER JOIN [liveNdb].dbo.HrEmployeePayrolls pp
ON pp.SourceID = i.SourceID
AND pp.EmployeeID = i.EmployeeID
LEFT JOIN [liveATdb].dbo.MisGlDept_Main d
ON d.SourceID = i.SourceID
and d.MisGlDeptID = pp.DeptID
LEFT JOIN liveNdb.dbo.DMisGlComponentValue cv
ON cv.SourceID = i.SourceID
and cv.ValueID = pp.DeptID
LEFT JOIN liveNdb.dbo.DPpPositions dpp
ON dpp.SourceID = pp.SourceID
AND (dpp.PositionID = pp.PositionNumberID OR
dpp.JobCodeID = pp.JobCodeID)
LEFT JOIN [liveNdb].dbo.HrEmployeePersonelPositions hpp
ON hpp.SourceID = pp.SourceID
AND hpp.EmployeeID = pp.EmployeeID
WHERE i.HealthRecordID LIKE 'COVID%'
AND (hpp.Status IN ('ACTIVE', 'LEAVE', 'NEW') OR d.Name = 'CHQ Administration')
GROUP BY i.EmployeeID having COUNT(*) = 1
When you use GROUP BY every column in the SELECT must either be an agregate or be named in the GROUP BY.
Do have the same results as DISTINCT we put everything into GROUP BY:
SELECT i.EmployeeID, h.Name, h.AliasLastName, pp.Active, pp.DeptID, d.Name AS DepartmentName,
dpp.Title, hpp.Status, hpp.StatusDateTime, i.HealthRecordID, i.ImmunizationDateTime
FROM [liveNdb].dbo.HrEmpAuditHealthRecordImmunizations i
INNER JOIN liveNdb.dbo.HrEmployees h
ON h.SourceID = i.SourceID
AND h.EmployeeID = i.EmployeeID
INNER JOIN [liveNdb].dbo.HrEmployeePayrolls pp
ON pp.SourceID = i.SourceID
AND pp.EmployeeID = i.EmployeeID
LEFT JOIN [liveATdb].dbo.MisGlDept_Main d
ON d.SourceID = i.SourceID
and d.MisGlDeptID = pp.DeptID
LEFT JOIN liveNdb.dbo.DMisGlComponentValue cv
ON cv.SourceID = i.SourceID
and cv.ValueID = pp.DeptID
LEFT JOIN liveNdb.dbo.DPpPositions dpp
ON dpp.SourceID = pp.SourceID
AND (dpp.PositionID = pp.PositionNumberID OR
dpp.JobCodeID = pp.JobCodeID)
LEFT JOIN [liveNdb].dbo.HrEmployeePersonelPositions hpp
ON hpp.SourceID = pp.SourceID
AND hpp.EmployeeID = pp.EmployeeID
WHERE i.HealthRecordID LIKE 'COVID%'
AND (hpp.Status IN ('ACTIVE', 'LEAVE', 'NEW') OR d.Name = 'CHQ Administration')
GROUP BY i.EmployeeID, h.Name, h.AliasLastName, pp.Active, pp.DeptID, d.Name AS DepartmentName,
dpp.Title, hpp.Status, hpp.StatusDateTime, i.HealthRecordID, i.ImmunizationDateTime
having COUNT(*) = 1;
If you want to only group by i.EmployeeID in the GROUP BY use the following query, but check because it may not return what you're expecting. We use the agregation function MAX which works with all data types.
SELECT i.EmployeeID,
max(h.Name) AS "name",
max(h.AliasLastName) aliasLastName,
max(pp.Active) AS "Active",
max(pp.DeptID) AS DeptID,
max(d.Name) AS AS DepartmentName,
max(dpp.Title) AS Title,
max(hpp.Status) AS Status,
max(hpp.StatusDateTime) AS StatusDateTime,
max(i.HealthRecordID) AS HealthRecordID,
max(i.ImmunizationDateTime) AS ImmunizationDateTime
FROM [liveNdb].dbo.HrEmpAuditHealthRecordImmunizations i
INNER JOIN liveNdb.dbo.HrEmployees h
ON h.SourceID = i.SourceID
AND h.EmployeeID = i.EmployeeID
INNER JOIN [liveNdb].dbo.HrEmployeePayrolls pp
ON pp.SourceID = i.SourceID
AND pp.EmployeeID = i.EmployeeID
LEFT JOIN [liveATdb].dbo.MisGlDept_Main d
ON d.SourceID = i.SourceID
and d.MisGlDeptID = pp.DeptID
LEFT JOIN liveNdb.dbo.DMisGlComponentValue cv
ON cv.SourceID = i.SourceID
and cv.ValueID = pp.DeptID
LEFT JOIN liveNdb.dbo.DPpPositions dpp
ON dpp.SourceID = pp.SourceID
AND (dpp.PositionID = pp.PositionNumberID OR
dpp.JobCodeID = pp.JobCodeID)
LEFT JOIN [liveNdb].dbo.HrEmployeePersonelPositions hpp
ON hpp.SourceID = pp.SourceID
AND hpp.EmployeeID = pp.EmployeeID
WHERE i.HealthRecordID LIKE 'COVID%'
AND (hpp.Status IN ('ACTIVE', 'LEAVE', 'NEW') OR d.Name = 'CHQ Administration')
GROUP BY i.EmployeeID
having COUNT(*) = 1;

how to solve the error "Cannot perform an aggregate function on an expression containing an aggregate or a subquery"

how to solve this error "Cannot perform an aggregate function on an expression containing an aggregate or a subquery"
SELECT tblPR.PRNO, tblPRMaterial.PRMaterialCode, ISNULL(SUM(DISTINCT tblPRMaterial.PRReqdQty - SUM(tblPOFromPR.Qty)), tblPRMaterial.PRReqdQty) AS PR_Qty
FROM tblPR INNER JOIN
tblPRMaterial ON tblPR.PRNO = tblPRMaterial.PRNO INNER JOIN
tblMaterial ON tblPRMaterial.PRMaterialCode = tblMaterial.MaterialCode LEFT OUTER JOIN
tblPOFromPR ON tblPRMaterial.PRNO = tblPOFromPR.PRNO AND tblPRMaterial.PRMaterialCode = tblPOFromPR.MaterialCode
WHERE (tblPR.PRStatus = 1) AND (tblPRMaterial.PRItemStatus = 0) AND (tblPR.PRType = 'PR') AND (tblPR.PRNO = 56548)
GROUP BY tblPR.PRNO, tblPRMaterial.PRMaterialCode, tblPRMaterial.PRReqdQty
ORDER BY tblPR.PRNO
You can try below - you need to add sum(tblPRMaterial.PRReqdQty) in your isnull function
SELECT tblPR.PRNO, tblPRMaterial.PRMaterialCode,
coalesce(SUM(DISTINCT tblPRMaterial.PRReqdQty) - SUM(tblPOFromPR.Qty),
sum(tblPRMaterial.PRReqdQty)) AS PR_Qty
FROM tblPR INNER JOIN
tblPRMaterial ON tblPR.PRNO = tblPRMaterial.PRNO INNER JOIN
tblMaterial ON tblPRMaterial.PRMaterialCode = tblMaterial.MaterialCode LEFT OUTER JOIN
tblPOFromPR ON tblPRMaterial.PRNO = tblPOFromPR.PRNO AND tblPRMaterial.PRMaterialCode = tblPOFromPR.MaterialCode
WHERE (tblPR.PRStatus = 1) AND (tblPRMaterial.PRItemStatus = 0) AND (tblPR.PRType = 'PR') AND (tblPR.PRNO = 56548)
GROUP BY tblPR.PRNO, tblPRMaterial.PRMaterialCode
ORDER BY tblPR.PRNO
number of column in selection and group by must be same
SELECT tblPR.PRNO,
tblPRMaterial.PRMaterialCode,
ISNULL(SUM(tblPRMaterial.PRReqdQty) - SUM(tblPOFromPR.Qty), sum(tblPRMaterial.PRReqdQty)) AS PR_Qty
FROM tblPR INNER JOIN
tblPRMaterial ON tblPR.PRNO = tblPRMaterial.PRNO INNER JOIN
tblMaterial ON tblPRMaterial.PRMaterialCode = tblMaterial.MaterialCode LEFT OUTER JOIN
tblPOFromPR ON tblPRMaterial.PRNO = tblPOFromPR.PRNO AND tblPRMaterial.PRMaterialCode = tblPOFromPR.MaterialCode
WHERE (tblPR.PRStatus = 1) AND (tblPRMaterial.PRItemStatus = 0) AND (tblPR.PRType = 'PR') AND (tblPR.PRNO = 56548)
GROUP BY tblPR.PRNO, tblPRMaterial.PRMaterialCode
ORDER BY tblPR.PRNO,tblPRMaterial.PRMaterialCode
Because Aggregate cannot contain another Aggregate expression on it.. Maybe you can use explicit to do Sum Operation..
SELECT
PRNO,
PRMaterialCode,
ISNULL(SUM(SUM_PR), PRReqdQty) AS PR_QTY
FROM
(SELECT
tblPR.PRNO,
tblPRMaterial.PRMaterialCode,
tblPRMaterial.PRReqdQty,
DISTINCT tblPRMaterial.PRReqdQty - SUM(tblPOFromPR.Qty) AS SUM_PR
FROM
tblPR
INNER JOIN tblPRMaterial ON tblPR.PRNO = tblPRMaterial.PRNO
AND (tblPRMaterial.PRItemStatus = 0)
INNER JOIN tblMaterial ON tblPRMaterial.PRMaterialCode = tblMaterial.MaterialCode
LEFT OUTER JOIN tblPOFromPR ON tblPRMaterial.PRNO = tblPOFromPR.PRNO
AND tblPRMaterial.PRMaterialCode = tblPOFromPR.MaterialCode
WHERE
(tblPR.PRStatus = 1)
AND (tblPR.PRType = 'PR')
AND (tblPR.PRNO = 56548)
GROUP BY
tblPR.PRNO,
tblPRMaterial.PRMaterialCode,
tblPRMaterial.PRReqdQty
ORDER BY
tblPR.PRNO) AS Derived_Table
GROUP BY
PRNO,
PRMaterialCode,
PRReqdQty
ORDER BY
PRNO

SQL Query to provide a count of rows on sales order

Hoping you can help.
We have an app that displays a grid. We can add custom fields to the grid using subqueries with which I am struggling. The main grid query looks like this.
SELECT TOP 300000
'' AS alloc_status
,'' AS stock_status
,wo_description
,wo_quantity
,wo_number
,wo_pwos_id
,vad_variant_code
,oh_order_number
,oh_datetime
,ohd_dm_id
,ohd_customer_name
,vad_description AS vad_description_Condition
,vad_variant_code AS vad_variant_code_Condition
,wo_id AS key_id
FROM works_order
LEFT OUTER JOIN works_order_analysis
ON works_order_analysis.woa_wo_id = works_order.wo_id
LEFT OUTER JOIN works_order_process
ON works_order_process.wop_id = works_order.wo_current_wop_id
LEFT OUTER JOIN works_order_process_analysis
ON works_order_process_analysis.wopa_wop_id = works_order_process.wop_id
INNER JOIN variant_detail
ON variant_detail.vad_id = works_order.wo_vad_id
LEFT OUTER JOIN works_order_total
ON works_order.wo_id = works_order_total.wot_wo_id
LEFT JOIN order_line_item
ON oli_id = wo_oli_id
LEFT JOIN order_header
ON oh_id = oli_oh_id
LEFT JOIN job_number
ON jn_id = wo_jn_id
LEFT JOIN works_order_process_subcontract_analysis
ON wopsa_wop_id = wop_id
LEFT JOIN order_header_detail
ON ohd_oh_id = oh_id
LEFT JOIN customer_detail
ON cd_id = oh_cd_id
WHERE ((cd_ow_account = 'NOTHS')
AND (wo_pwos_id = 1)
OR (cd_ow_account = 'EBAY')
AND (wo_pwos_id = 1)
OR (cd_ow_account = '4008')
AND (wo_pwos_id = 1)
OR (cd_ow_account = 'TRUSCA')
AND (wo_pwos_id = 1))
AND ((wo_required_datetime <= '2016-12-24 23:59:59'
OR wo_required_datetime IS NULL)
AND (wo_wos_id <> 4)
AND (wo_kit = 0))
The subquery is to basically provide a column that looks at the sales order and returns a count of row within that order. When I do this I get the following:
There was a problem retrieving the data:
subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, < etc
or when the subquery is used as an expression
Below is the query complete with subquery. If anyone can help that would be amazing
SELECT TOP 300000
'' AS alloc_status
,'' AS stock_status
,wo_description
,wo_quantity
,wo_number
,wo_pwos_id
,vad_variant_code
,oh_order_number
,oh_datetime
,ohd_dm_id
,ohd_customer_name
,(SELECT
COUNT(order_line_item.oli_id) AS 'Count'
FROM dbo.order_line_item
INNER JOIN dbo.order_header
ON order_line_item.oli_oh_id = order_header.oh_id
INNER JOIN dbo.variant_detail
ON order_line_item.oli_vad_id = variant_detail.vad_id
INNER JOIN dbo.variant_setting
ON variant_setting.vas_vad_id = variant_detail.vad_id
WHERE variant_setting.vas_manufactured_variant = 1
GROUP BY order_header.oh_order_number)
AS CustomLineCount
,vad_description AS vad_description_Condition
,vad_variant_code AS vad_variant_code_Condition
,wo_id AS key_id
FROM works_order
LEFT OUTER JOIN works_order_analysis
ON works_order_analysis.woa_wo_id = works_order.wo_id
LEFT OUTER JOIN works_order_process
ON works_order_process.wop_id = works_order.wo_current_wop_id
LEFT OUTER JOIN works_order_process_analysis
ON works_order_process_analysis.wopa_wop_id = works_order_process.wop_id
INNER JOIN variant_detail
ON variant_detail.vad_id = works_order.wo_vad_id
LEFT OUTER JOIN works_order_total
ON works_order.wo_id = works_order_total.wot_wo_id
LEFT JOIN order_line_item
ON oli_id = wo_oli_id
LEFT JOIN order_header
ON oh_id = oli_oh_id
LEFT JOIN job_number
ON jn_id = wo_jn_id
LEFT JOIN works_order_process_subcontract_analysis
ON wopsa_wop_id = wop_id
LEFT JOIN order_header_detail
ON ohd_oh_id = oh_id
LEFT JOIN customer_detail
ON cd_id = oh_cd_id
WHERE ((cd_ow_account = 'NOTHS')
AND (wo_pwos_id = 1)
OR (cd_ow_account = 'EBAY')
AND (wo_pwos_id = 1)
OR (cd_ow_account = '4008')
AND (wo_pwos_id = 1)
OR (cd_ow_account = 'TRUSCA')
AND (wo_pwos_id = 1))
AND ((wo_required_datetime <= '2016-12-24 23:59:59'
OR wo_required_datetime IS NULL)
AND (wo_wos_id <> 4)
AND (wo_kit = 0))
Try the below query, it may helps you
SELECT TOP 300000
'' AS alloc_status
,'' AS stock_status
,wo_description
,wo_quantity
,wo_number
,wo_pwos_id
,vad_variant_code
,oh_order_number
,oh_datetime
,ohd_dm_id
,ohd_customer_name
,(SELECT
COUNT(order_line_item.oli_id) AS 'Count'
FROM dbo.order_line_item
INNER JOIN dbo.order_header
ON order_line_item.oli_oh_id = order_header.oh_id
INNER JOIN dbo.variant_detail
ON order_line_item.oli_vad_id = variant_detail.vad_id
INNER JOIN dbo.variant_setting
ON variant_setting.vas_vad_id = variant_detail.vad_id
WHERE variant_setting.vas_manufactured_variant = 1
and order_header.oh_order_number=ordhead.oh_order_number)
AS CustomLineCount
,vad_description AS vad_description_Condition
,vad_variant_code AS vad_variant_code_Condition
,wo_id AS key_id
FROM works_order
LEFT OUTER JOIN works_order_analysis
ON works_order_analysis.woa_wo_id = works_order.wo_id
LEFT OUTER JOIN works_order_process
ON works_order_process.wop_id = works_order.wo_current_wop_id
LEFT OUTER JOIN works_order_process_analysis
ON works_order_process_analysis.wopa_wop_id = works_order_process.wop_id
INNER JOIN variant_detail
ON variant_detail.vad_id = works_order.wo_vad_id
LEFT OUTER JOIN works_order_total
ON works_order.wo_id = works_order_total.wot_wo_id
LEFT JOIN order_line_item
ON oli_id = wo_oli_id
LEFT JOIN order_header ordhead
ON oh_id = oli_oh_id
LEFT JOIN job_number
ON jn_id = wo_jn_id
LEFT JOIN works_order_process_subcontract_analysis
ON wopsa_wop_id = wop_id
LEFT JOIN order_header_detail
ON ohd_oh_id = oh_id
LEFT JOIN customer_detail
ON cd_id = oh_cd_id
WHERE ((cd_ow_account = 'NOTHS')
AND (wo_pwos_id = 1)
OR (cd_ow_account = 'EBAY')
AND (wo_pwos_id = 1)
OR (cd_ow_account = '4008')
AND (wo_pwos_id = 1)
OR (cd_ow_account = 'TRUSCA')
AND (wo_pwos_id = 1))
AND ((wo_required_datetime <= '2016-12-24 23:59:59'
OR wo_required_datetime IS NULL)
AND (wo_wos_id <> 4)
AND (wo_kit = 0))
Change your sub query to this:
(SELECT
COUNT(order_line_item.oli_id) AS 'Count'
FROM dbo.order_line_item
INNER JOIN dbo.order_header
ON order_line_item.oli_oh_id = order_header.oh_id
INNER JOIN dbo.variant_detail
ON order_line_item.oli_vad_id = variant_detail.vad_id
INNER JOIN dbo.variant_setting
ON variant_setting.vas_vad_id = variant_detail.vad_id
WHERE variant_setting.vas_manufactured_variant = 1
AND order_header.oh_order_number = OUTER_QUERY_TABLE.SAME_FIELD )
I've added this line:
AND order_header.oh_order_number = OUTER_QUERY_TABLE.SAME_FIELD
You have to make this query correlated , so add the condition to match the record to the outer query . I didn't know which field it is.
You could also optimize your WHERE and make it more readable with IN() :
WHERE wo_pwos_id = 1
AND cd_ow_account IN('NOTHS','EBAY','4008','TRUSCA')
AND (wo_required_datetime <= '2016-12-24 23:59:59' OR wo_required_datetime IS NULL)
AND wo_wos_id <> 4
AND wo_kit = 0

Getting a second query as a field in a first one

I'm fairly new to writing queries and I'm struggling with getting the next to queries combined:
SELECT od.ODCode
, c.Description as 'Line Position'
, ps.Description as 'Pallet Stacking'
, pt.Description as 'Pallet Type'
, odex.PalletRows
, odex.PalletTypeID
,CONCAT(( SELECT sp.SpecPropertyValue FROM SpecificationProperty sp
INNER JOIN specificationclass sc ON sc.specclassid = sp.specclassid
INNER JOIN specificationcode sco ON sco.specclassid = sc.specclassid and sco.speccodeid = pt.pallettypeid
INNER JOIN SpecificationClassProperty scp ON scp.specClassid = sp.specclassid and scp.specpropertyid = sp.specpropertyid and sco.speccodeid = sp.speccodeid
WHERE scp.specpropertyname = 'DocDir'and sc.specclassname = 'Pallettypedoc'
),
(SELECT sp.SpecPropertyValue FROM SpecificationProperty sp
INNER JOIN specificationclass sc ON sc.specclassid = sp.specclassid
INNER JOIN specificationcode sco ON sco.specclassid = sc.specclassid and sco.speccodeid = pt.pallettypeid
INNER JOIN SpecificationClassProperty scp ON scp.specClassid = sp.specclassid and scp.specpropertyid = sp.specpropertyid and sco.speccodeid = sp.speccodeid
WHERE scp.specpropertyname = 'Documentname'and sc.specclassname = 'Pallettypedoc' )) AS Spec
FROM ODExitPallet odex
INNER JOIN OperationDescr od
ON od.OperationDescrID=odex.OperationDescrID
INNER JOIN PalletStacking ps
ON ps.PalletStackingID=odex.PalletStackingID
INNER JOIN PalletType pt
ON pt.PalletTypeID=odex.PalletTypeID
INNER JOIN Code c
on c.CodeNumber=odex.LinePosition and c.CodeTypeID=72
INNER JOIN WorkOrder wo
on wo.OperationDescrID=odex.OperationDescrID
WHERE wo.WorkOrderID = #WorkOrderID
and
SELECT SUM(PlannedBatch) / SUM(NominalBlanks * Stacks) as NrOfPallets
FROM (SELECT WO.PlannedBatch, WO.NominalBlanks, ODEP.OperationDescrID, LinePosition,
Case when COUNT(*) = 0 then 1
else COUNT(*)
end as Stacks
FROM WorkOrder Wo
INNER JOIN ODExitPallet ODEX
ON WO.operationDescrID = ODEX.OperationDescrID
INNER JOIN Stacking ST
ON ST.PalletStackingID = ODEX.PalletStackingID
WHERE WO.WorkOrderID = #WorkOrderID
GROUP BY PlannedBatch, NominalBlanks, ODEX.OperationDescrID, LinePosition
) TOTAL
So I want to add the second query as a field in my first one. I've tried it with numerous thing like parentheses and stuff but I keep getting errors. Can someone enlighten me?
insert ( ) in whole second Query but make sure you second query return only 1 row
SELECT od.ODCode
, c.Description as 'Line Position'
, ps.Description as 'Pallet Stacking'
, pt.Description as 'Pallet Type'
, odex.PalletRows
, odex.PalletTypeID
,CONCAT(
(
SELECT sp.SpecPropertyValue
FROM SpecificationProperty sp
INNER JOIN specificationclass sc ON sc.specclassid = sp.specclassid
INNER JOIN specificationcode sco ON sco.specclassid = sc.specclassid and sco.speccodeid = pt.pallettypeid
INNER JOIN SpecificationClassProperty scp ON scp.specClassid = sp.specclassid and scp.specpropertyid = sp.specpropertyid and sco.speccodeid = sp.speccodeid
WHERE scp.specpropertyname = 'DocDir'and sc.specclassname = 'Pallettypedoc'
)
,
(
SELECT sp.SpecPropertyValue FROM SpecificationProperty sp
INNER JOIN specificationclass sc ON sc.specclassid = sp.specclassid
INNER JOIN specificationcode sco ON sco.specclassid = sc.specclassid and sco.speccodeid = pt.pallettypeid
INNER JOIN SpecificationClassProperty scp ON scp.specClassid = sp.specclassid and scp.specpropertyid = sp.specpropertyid and sco.speccodeid = sp.speccodeid
WHERE scp.specpropertyname = 'Documentname'and sc.specclassname = 'Pallettypedoc'
)
) AS Spec
,(
SELECT SUM(PlannedBatch) / SUM(NominalBlanks * Stacks)
FROM (
SELECT WO.PlannedBatch, WO.NominalBlanks, ODEP.OperationDescrID, LinePosition,
Case when COUNT(*) = 0 then 1
else COUNT(*)
end as Stacks
FROM WorkOrder Wo
INNER JOIN ODExitPallet ODEX
ON WO.operationDescrID = ODEX.OperationDescrID
INNER JOIN Stacking ST
ON ST.PalletStackingID = ODEX.PalletStackingID
WHERE WO.WorkOrderID = #WorkOrderID
GROUP BY PlannedBatch, NominalBlanks, ODEX.OperationDescrID, LinePosition
) TOTAL
) AS [NrOfPallets]
FROM ODExitPallet odex
INNER JOIN OperationDescr od ON od.OperationDescrID=odex.OperationDescrID
INNER JOIN PalletStacking ps ON ps.PalletStackingID=odex.PalletStackingID
INNER JOIN PalletType pt ON pt.PalletTypeID=odex.PalletTypeID
INNER JOIN Code c on c.CodeNumber=odex.LinePosition and c.CodeTypeID=72
INNER JOIN WorkOrder wo on wo.OperationDescrID=odex.OperationDescrID
WHERE wo.WorkOrderID = #WorkOrderID

Select Last Record in SSRS report Designer 3.0

Trying to get the last/lastest record from ShipHead.Shipdate instead of all the records How do i Do last record with this query?
SELECT
OrderRel.ReqDate
,OrderHed.EntryPerson
,ShipHead.ShipDate
,Customer.Name
,ShipDtl.OrderNum
,ShipDtl.OrderLine
,ShipDtl.OrderRelNum
FROM
OrderHed
INNER JOIN OrderDtl
ON OrderHed.Company = OrderDtl.Company AND OrderHed.OrderNum = OrderDtl.OrderNum
INNER JOIN OrderRel
ON OrderDtl.Company = OrderRel.Company AND OrderDtl.OrderNum = OrderRel.OrderNum AND OrderDtl.OrderLine = OrderRel.OrderLine
INNER JOIN ShipDtl
ON OrderRel.Company = ShipDtl.Company AND OrderRel.OrderNum = ShipDtl.OrderNum AND OrderRel.OrderLine = ShipDtl.OrderLine AND OrderRel.OrderRelNum = ShipDtl.OrderRelNum
INNER JOIN ShipHead
ON ShipDtl.Company = ShipHead.Company AND ShipDtl.PackNum = ShipHead.PackNum
INNER JOIN Customer
ON ShipHead.Company = Customer.Company AND ShipHead.CustNum = Customer.CustNum
WHERE
OrderRel.OrderNum = 603205
You could try the following. If you wanted the last dated record for each entry person then change to the B:
SELECT
X.ReqDate,
X.EntryPerson,
X.ShipDate,
X.Name,
X.OrderNum,
X.OrderLine,
X.OrderRelNum
FROM
(
SELECT
ORl.ReqDate,
OH.EntryPerson,
SH.ShipDate,
rn = row_number() OVER(ORDER BY SH.ShipDate DESC),
--rn = row_number() OVER(PARTITION BY OH.EntryPerson ORDER BY SH.ShipDate DESC) --<<B
C.Name,
SD.OrderNum,
SD.OrderLine,
SD.OrderRelNum
FROM
OrderHed OH
INNER JOIN OrderDtl OD ON
OH.Company = OD.Company AND
OH.OrderNum = OD.OrderNum
INNER JOIN OrderRel ORl ON
OD.Company = ORl.Company AND
OD.OrderNum = ORl.OrderNum AND
OD.OrderLine = ORl.OrderLine
INNER JOIN ShipDtl SD ON
ORl.Company = SD.Company AND
ORl.OrderNum = SD.OrderNum AND
ORl.OrderLine = SD.OrderLine AND
ORl.OrderRelNum = SD.OrderRelNum
INNER JOIN ShipHead SH ON
SD.Company = SH.Company AND
SD.PackNum = SH.PackNum
INNER JOIN Customer C ON
SH.Company = C.Company AND
SH.CustNum = C.CustNum
WHERE
ORl.OrderNum = 603205
) X
WHERE X.rn = 1
P.S. why don't you use shorter aliases - that is part of the point of aliases