SQL JOIN only on specific CASE - sql

I have a stored procedure where I have to join a table only on rows where a bit field for that record is 0. Is this possible? Here's what I have/need
SELECT TOP 100
SR.ServiceRecordID,
dbo.fn_ServiceRecordGetServiceName(SR.ServiceRecordID,''),
SR.LocationStd,
SR.AreaServedStd,
SR.RegionalLimited,
R.Region,
CASE WHEN (ISNULL(R_SR.RegionID,0) = 0 AND ISNULL(R_SR_Serv.RegionID,0) = 0)
THEN 0 ELSE 1 END AS Visible,
NULL
FROM ServiceRecord SR
INNER JOIN Sites S
ON SR.SiteID = S.SiteID
INNER JOIN Regions R
ON R.SiteID = S.SiteID
LEFT OUTER JOIN lkup_Region_ServiceRecord R_SR
ON R_SR.RegionID = R.RegionID
AND R_SR.ServiceRecordID = SR.ServiceRecordID
--and this is the additional join I need ONLY if RegionalLimited = 0
CASE WHEN SR.RegionalLimited = 0 THEN
LEFT OUTER JOIN lkup_Region_ServiceRecord_Serv R_SR_Serv
ON R_SR_Serv.RegionID = R.RegionID
AND R_SR_Serv.ServiceRecordID = SR.ServiceRecordID
END
WHERE SR.SiteID = #SiteID
AND R.RegionID % 100 != 0

This should work
SELECT TOP 100 SR.ServiceRecordID, dbo.fn_ServiceRecordGetServiceName(SR.ServiceRecordID,''),
SR.LocationStd, SR.AreaServedStd,
SR.RegionalLimited, R.Region,
CASE WHEN (ISNULL(R_SR.RegionID,0) = 0 AND ISNULL(R_SR_Serv.RegionID,0) = 0) THEN 0 ELSE 1 END AS Visible,NULL
FROM ServiceRecord SR
INNER JOIN Sites S ON SR.SiteID = S.SiteID
INNER JOIN Regions R ON R.SiteID = S.SiteID
LEFT OUTER JOIN lkup_Region_ServiceRecord R_SR ON R_SR.RegionID = R.RegionID
AND R_SR.ServiceRecordID = SR.ServiceRecordID
LEFT OUTER JOIN lkup_Region_ServiceRecord_Serv R_SR_Serv
ON R_SR_Serv.RegionID = R.RegionID
AND R_SR_Serv.ServiceRecordID = SR.ServiceRecordID
AND SR.RegionalLimited = 0 -- this is the trick
WHERE SR.SiteID = #SiteID
AND R.RegionID % 100 != 0
I have added this condition AND SR.RegionalLimited = 0 to the left outer join with lkup_Region_ServiceRecord

Related

A new row for each record

I would like the results to produce a new row in each instance where a condition is met. I'm using a CASE statement but this isn't the way to go since once the first condition is met it stops evaluating the field.
SELECT
Reviews.ReviewID,
CASE
WHEN Score_CorrectID = 0 THEN 'Correct ID Right Party Authentication'
WHEN Score_ProperlyIdentified = 0 THEN 'PCA Properly Identified Itself'
WHEN Score_MiniMiranda = 0 THEN 'Mini-Miranda'
END AS [Error Type]
FROM Reviews INNER JOIN PCAs ON Reviews.PCAID = PCAs.PCAID LEFT JOIN
PCARebuttal ON Reviews.ReviewID = PCARebuttal.ReviewID
WHERE
(Score_CorrectID = 0 OR Score_ProperlyIdentified = 0 OR Score_MiniMiranda =
0)
This produces this:
I would like this:
Use apply:
SELECT r.ReviewID, v.error_type
FROM Reviews r INNER JOIN
PCAs
ON r.PCAID = PCAs.PCAID LEFT JOIN
PCARebuttal pr
ON r.ReviewID = pr.ReviewID OUTER APPLY
(SELECT *
FROM (VALUES (Score_CorrectID, 'Correct ID Right Party Authentication'),
(Score_ProperlyIdentified, 'PCA Properly Identified Itself'),
(Score_MiniMiranda, 'Mini-Miranda')
) v(score, error_type)
WHERE score = 0
) v
WHERE (Score_CorrectID = 0 OR Score_ProperlyIdentified = 0 OR Score_MiniMiranda = 0);
That said, I would probably just concatenate the values into a single column:
SELECT r.ReviewID,
( (CASE WHEN Score_CorrectID = 0 THEN 'Correct ID Right Party Authentication; ' ELSE '' END) +
(CASE WHEN Score_ProperlyIdentified = 0 THEN 'PCA Properly Identified Itself; ' ELSE '' END) +
(CASE WHEN Score_MiniMiranda = 0 THEN 'Mini-Miranda;' ELSE '' END)
) as error_types
FROM Reviews r INNER JOIN
PCAs
ON r.PCAID = PCAs.PCAID LEFT JOIN
PCARebuttal pr
ON r.ReviewID = pr.ReviewID OUTER APPLY
(SELECT *
FROM (VALUES ()
) v(score, error_type)
WHERE score = 0
) v
WHERE (Score_CorrectID = 0 OR Score_ProperlyIdentified = 0 OR Score_MiniMiranda = 0);
Instead of using case, you can use union all instead:
SELECT
Reviews.ReviewID,
'Correct ID Right Party Authentication' AS [Error Type]
FROM Reviews
INNER JOIN PCAs ON Reviews.PCAID = PCAs.PCAID
LEFT JOIN PCARebuttal ON Reviews.ReviewID = PCARebuttal.ReviewID
WHERE Score_CorrectID = 0
UNION ALL
SELECT
Reviews.ReviewID,
'PCA Properly Identified Itself' AS [Error Type]
FROM Reviews
INNER JOIN PCAs ON Reviews.PCAID = PCAs.PCAID
LEFT JOIN PCARebuttal ON Reviews.ReviewID = PCARebuttal.ReviewID
WHERE Score_ProperlyIdentified = 0
UNION ALL
SELECT
Reviews.ReviewID,
'Mini-Miranda' AS [Error Type]
FROM Reviews
INNER JOIN PCAs ON Reviews.PCAID = PCAs.PCAID
LEFT JOIN PCARebuttal ON Reviews.ReviewID = PCARebuttal.ReviewID
WHERE Score_MiniMiranda = 0

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

Optimize query with sql server

I have the following query .its execution took 15 min .It is too much slowly .
Is there a way to optimize it ?
Query
SELECT
Id,Fees,WeekOfMonth,CONVERT(NVARCHAR(MAX), StartDate, 103) AS StartDate,CONVERT(NVARCHAR(MAX), EndDate, 103) AS EndDate,'Temp Fees ' AS FeesName
,#MonthName AS [MonthName]
INTO ##TempFeesMonthly
from
(
SELECT DISTINCT
1 as Id,sum(((CNTI_THPAYE *(CNTI_THFACT / CNTI_THPAYE)) *
(CASE WHEN CNTI_DURHEBDO IS NULL THEN 1 ELSE CNTI_DURHEBDO/5 END)*#NumberOfDays)) AS Fees,WeekOfMonth,StartDate,EndDate
FROM SCHHAYS.dbo.WTVTAT TAT
LEFT JOIN
SCHHAYS.dbo.WTTIEINT INT
ON (
TAT.TIE_ID = INT.TIE_ID
)
AND (
TAT.VTAT_IORDRE = INT.TIEI_ORDRE
)
LEFT JOIN
SCHHAYS.dbo.PYCONTRAT CC
ON TAT.PER_ID = CC.PER_ID
AND TAT.CNT_ID = CC.CNT_ID
LEFT JOIN
SCHHAYS.dbo.CMTIERS T
ON TAT.TIE_ID = T.TIE_ID
LEFT JOIN
SCHHAYS.dbo.WTMISS M
ON CC.PER_ID = M.PER_ID
AND CC.CNT_ID = M.CNT_ID
LEFT JOIN
##WTCNTIWeek COT1
ON M.PER_ID = COT1.PER_ID
AND M.CNT_ID = COT1.CNT_ID
INNER JOIN
SCHHAYS.dbo.WTPRH AS PRH
ON M.PER_ID = PRH.PER_ID
AND M.CNT_ID = PRH.CNT_ID
AND M.TIE_ID = PRH.TIE_ID
INNER JOIN
##tempStartEndWeekDates AS Tsed
ON PRH_DTEDEBSEM>=Tsed.StartDate
AND PRH_DTEFINSEM<=Tsed.EndDate
LEFT JOIN
SCHHAYS.dbo.WTSCCT C
ON CC.RGPCNT_ID = C.RGPCNT_ID
AND CC.PER_ID = C.PER_ID
AND CC.CNT_ID = C.CNT_ID
INNER JOIN
##TempHaysStaffWeek HF
ON C.VAPO_CODE = HF.onetouch COLLATE Latin1_General_CI_AS
group by
WeekOfMonth,StartDate,EndDate)t
--CREATE INDEX IDX_TempFeesMonthly ON ##TempFeesMonthly(WeekOfMonth)
--Calcul Temp Margin
UNION ALL
--INSERT INTO ##TempFeesMonthly(Id,Fees,WeekOfMonth,StartDate,EndDate,FeesName,[MonthName])
SELECT
2, sum(Fees) AS Fees ,WeekOfMonth,CONVERT(NVARCHAR(MAX), StartDate, 103) AS StartDate,CONVERT(NVARCHAR(MAX), EndDate, 103) AS EndDate,'Temp Margin ' AS FeesName
,#MonthName AS [MonthName]
from
(
SELECT DISTINCT
sum((CASE WHEN CNTI_DURHEBDO IS NULL THEN 1 ELSE CNTI_DURHEBDO/5 END)*#NumberOfDays)-(CNTI_THPAYE *(CASE WHEN CNTI_DURHEBDO IS NULL THEN 1 ELSE CNTI_DURHEBDO/5 END)*1.453*1.21*#NumberOfDays) AS Fees,WeekOfMonth,StartDate,EndDate
FROM SCHHAYS.dbo.WTVTAT TAT
LEFT JOIN
SCHHAYS.dbo.WTTIEINT INT
ON (
TAT.TIE_ID = INT.TIE_ID
)
AND (
TAT.VTAT_IORDRE = INT.TIEI_ORDRE
)
LEFT JOIN
SCHHAYS.dbo.PYCONTRAT CC
ON TAT.PER_ID = CC.PER_ID
AND TAT.CNT_ID = CC.CNT_ID
LEFT JOIN
SCHHAYS.dbo.CMTIERS T
ON TAT.TIE_ID = T.TIE_ID
LEFT JOIN
SCHHAYS.dbo.WTMISS M
ON CC.PER_ID = M.PER_ID
AND CC.CNT_ID = M.CNT_ID
LEFT JOIN
##WTCNTIWeek COT1
ON M.PER_ID = COT1.PER_ID
AND M.CNT_ID = COT1.CNT_ID
INNER JOIN
SCHHAYS.dbo.WTPRH AS PRH
ON M.PER_ID = PRH.PER_ID
AND M.CNT_ID = PRH.CNT_ID
AND M.TIE_ID = PRH.TIE_ID
INNER JOIN
##tempStartEndWeekDates AS Tsed
ON PRH_DTEDEBSEM>=Tsed.StartDate
AND PRH_DTEFINSEM<=Tsed.EndDate
LEFT JOIN
SCHHAYS.dbo.WTSCCT C
ON CC.RGPCNT_ID = C.RGPCNT_ID
AND CC.PER_ID = C.PER_ID
AND CC.CNT_ID = C.CNT_ID
INNER JOIN
##TempHaysStaffWeek HF
ON C.VAPO_CODE = HF.onetouch COLLATE Latin1_General_CI_AS
group by
WeekOfMonth,StartDate,EndDate,CNTI_THPAYE,CNTI_DURHEBDO)t
GROUP BY WeekOfMonth,StartDate,EndDate

Update Statement using Join and Group By

I have written the below Update Statement, but it shows the error such as "Incorrect syntax near the keyword 'GROUP'."
UPDATE
J
SET
J.StatusID = CASE WHEN SUM(DUV.VendorDUQuantity) = SUM(RD.InvoiceQuantity) THEN 1 ELSE J.StatusID END
FROM
PLN_DU_Vendor DUV
INNER JOIN ENG_Release R ON R.ReleaseID = DUV.ReleaseID
INNER JOIN ENG_DU_Header H ON H.ReleaseID = R.ReleaseID AND DUV.DUID = H.DUID
INNER JOIN MKT_JobOrder J ON J.JobOrderID = R.JobOrderID
INNER JOIN MKT_CustomerOrder CO ON CO.OrderID = J.OrderID
LEFT JOIN PMT_RFDHeader RH ON RH.JobOrderID = J.JobOrderID
LEFT JOIN PMT_RFDDetail RD ON RD.RFDID = RH.RFDID AND RD.DUID = DUV.DUID
WHERE
CO.OrderID = 100
GROUP BY
J.JobOrderID
Instead of Update, Select is working perfectly for the above Query. What would be the problem and How to write the Query based on Join and Group By clause.
You can try putting the group by inside of a subquery, then join by the "JobOrderID", like this:
UPDATE J
SET J.StatusID = A.statusId
FROM MKT_JobOrder J
INNER JOIN (
SELECT J.JobOrderID
, CASE
WHEN SUM(DUV.VendorDUQuantity) = SUM(RD.InvoiceQuantity)
THEN 1
ELSE J.StatusID
END AS statusId
FROM PLN_DU_Vendor DUV
INNER JOIN ENG_Release R ON R.ReleaseID = DUV.ReleaseID
INNER JOIN ENG_DU_Header H ON H.ReleaseID = R.ReleaseID
AND DUV.DUID = H.DUID
INNER JOIN MKT_JobOrder J ON J.JobOrderID = R.JobOrderID
INNER JOIN MKT_CustomerOrder CO ON CO.OrderID = J.OrderID
LEFT JOIN PMT_RFDHeader RH ON RH.JobOrderID = J.JobOrderID
LEFT JOIN PMT_RFDDetail RD ON RD.RFDID = RH.RFDID
AND RD.DUID = DUV.DUID
WHERE CO.OrderID = 100
GROUP BY J.JobOrderID
, J.StatusID
) A ON J.JobOrderID = A.JobOrderID
User sub query instead to perform this operation
UPDATE
J
SET StatusID = x.Status
FROM
(select CASE WHEN SUM(DUV.VendorDUQuantity) = SUM(RD.InvoiceQuantity) THEN 1
ELSE J.StatusID END as Status
JobOrderID
FROM
PLN_DU_Vendor DUV
INNER JOIN ENG_Release R ON R.ReleaseID = DUV.ReleaseID
INNER JOIN ENG_DU_Header H ON H.ReleaseID = R.ReleaseID AND DUV.DUID = H.DUID
INNER JOIN MKT_JobOrder J ON J.JobOrderID = R.JobOrderID
INNER JOIN MKT_CustomerOrder CO ON CO.OrderID = J.OrderID
LEFT JOIN PMT_RFDHeader RH ON RH.JobOrderID = J.JobOrderID
LEFT JOIN PMT_RFDDetail RD ON RD.RFDID = RH.RFDID AND RD.DUID = DUV.DUID
WHERE
CO.OrderID = 100
GROUP BY
J.JobOrderID) X
INNER JOIN MKT_JobOrder J On x.JobOrderID = J.JobOrderID