I have a long query which is basically consist of two sections as follows,
SELECT
cph.clientPollId,
cph.clientPollSeqNo,
cph.clientId,
cph.pollDate,
d.deviceId,
allPollData_A.HEALTHY,
batteryPollData_A.BATTERY_CHARGE_CYCLES,
batteryPollData_A.BATTERY_CURRENT,
batteryPollData_A.BATTERY_CURRENT_MAX,
batteryPollData_A.BATTERY_EFFECTIVE_SOC,
batteryPollData_A.BATTERY_EFFECTIVE_SOC_EXPLANATION,
batteryPollData_A.BATTERY_ENERGY,
batteryPollData_A.BATTERY_POWER,
batteryPollData_A.BATTERY_POWER_MAX,
batteryPollData_A.BATTERY_REAL_SOC,
batteryPollData_A.BATTERY_STORED_ENERGY,
batteryPollData_A.BATTERY_TEMP,
batteryPollData_A.BATTERY_VOLTAGE,
batteryPollData_A.BATTERY_VOLTAGE_MAX,
batteryPollData_A.BATTERY_VOLTAGE_MIN,
batteryPollData_A.BMS_VERSON,
batteryPollData_A.CELL_TEMPERATURE_MAX,
batteryPollData_A.CELL_TEMPERATURE_MIN,
batteryPollData_A.CELL_VOLTAGE_MAX,
batteryPollData_A.CELL_VOLTAGE_MIN,
.
.
.
FROM clientPollHeader_A cph
JOIN device d on cph.clientId = d.clientId
JOIN client c on d.clientId = c.clientId
JOIN latestClientPoll lcp on cph.clientPollSeqNo = lcp.clientPollSeqNo
LEFT OUTER JOIN allPollData_A on cph.clientPollSeqNo = allPollData_A.clientPollSeqNo
and d.deviceId = allPollData_A.deviceId
LEFT OUTER JOIN batteryPollData_A on cph.clientPollSeqNo = batteryPollData_A.clientPollSeqNo
and d.deviceId = batteryPollData_A.deviceId
LEFT OUTER JOIN deltaPollData_A on cph.clientPollSeqNo = deltaPollData_A.clientPollSeqNo
and d.deviceId = deltaPollData_A.deviceId
LEFT OUTER JOIN dessPollData_A on cph.clientPollSeqNo = dessPollData_A.clientPollSeqNo
and d.deviceId = dessPollData_A.deviceId
LEFT OUTER JOIN programPollData_A on cph.clientPollSeqNo = programPollData_A.clientPollSeqNo
and d.deviceId = programPollData_A.deviceId
LEFT OUTER JOIN emersonthermostat1PollData_A on cph.clientPollSeqNo = emersonthermostat1PollData_A.clientPollSeqNo
and d.deviceId = emersonthermostat1PollData_A.deviceId
LEFT OUTER JOIN emersonthermostat2PollData_A on cph.clientPollSeqNo = emersonthermostat2PollData_A.clientPollSeqNo
and d.deviceId = emersonthermostat2PollData_A.deviceId
LEFT OUTER JOIN emersonthermostat3PollData_A on cph.clientPollSeqNo = emersonthermostat3PollData_A.clientPollSeqNo
and d.deviceId = emersonthermostat3PollData_A.deviceId
LEFT OUTER JOIN emersonthermostat4PollData_A on cph.clientPollSeqNo = emersonthermostat4PollData_A.clientPollSeqNo
and d.deviceId = emersonthermostat4PollData_A.deviceId
LEFT OUTER JOIN thermostatPollData_A on cph.clientPollSeqNo = thermostatPollData_A.clientPollSeqNo
and d.deviceId = thermostatPollData_A.deviceId
LEFT OUTER JOIN inverterPollData_A on cph.clientPollSeqNo = inverterPollData_A.clientPollSeqNo
and d.deviceId = inverterPollData_A.deviceId
LEFT OUTER JOIN ioboardPollData_A on cph.clientPollSeqNo = ioboardPollData_A.clientPollSeqNo
and d.deviceId = ioboardPollData_A.deviceId
LEFT OUTER JOIN kokambatteryPollData_A on cph.clientPollSeqNo = kokambatteryPollData_A.clientPollSeqNo
and d.deviceId = kokambatteryPollData_A.deviceId
LEFT OUTER JOIN maggiePollData_A on cph.clientPollSeqNo = maggiePollData_A.clientPollSeqNo
and d.deviceId = maggiePollData_A.deviceId
LEFT OUTER JOIN mercedesbatteryPollData_A on cph.clientPollSeqNo = mercedesbatteryPollData_A.clientPollSeqNo
and d.deviceId = mercedesbatteryPollData_A.deviceId
LEFT OUTER JOIN omronPollData_A on cph.clientPollSeqNo = omronPollData_A.clientPollSeqNo
and d.deviceId = omronPollData_A.deviceId
LEFT OUTER JOIN daikinacPollData_A on cph.clientPollSeqNo = daikinacPollData_A.clientPollSeqNo
and d.deviceId = daikinacPollData_A.deviceId
LEFT OUTER JOIN outbackradianPollData_A on cph.clientPollSeqNo = outbackradianPollData_A.clientPollSeqNo
and d.deviceId = outbackradianPollData_A.deviceId
LEFT OUTER JOIN pvgenPollData_A on cph.clientPollSeqNo = pvgenPollData_A.clientPollSeqNo
and d.deviceId = pvgenPollData_A.deviceId
LEFT OUTER JOIN safeplugPollData_A on cph.clientPollSeqNo = safeplugPollData_A.clientPollSeqNo
and d.deviceId = safeplugPollData_A.deviceId
LEFT OUTER JOIN schneiderinverterPollData_A on cph.clientPollSeqNo = schneiderinverterPollData_A.clientPollSeqNo
and d.deviceId = schneiderinverterPollData_A.deviceId
LEFT OUTER JOIN solarchargecontrollerPollData_A on cph.clientPollSeqNo = solarchargecontrollerPollData_A.clientPollSeqNo
and d.deviceId = solarchargecontrollerPollData_A.deviceId
LEFT OUTER JOIN owonPollData_A on cph.clientPollSeqNo = owonPollData_A.clientPollSeqNo
and d.deviceId = owonPollData_A.deviceId
LEFT OUTER JOIN ecobeePollData_A on cph.clientPollSeqNo = ecobeePollData_A.clientPollSeqNo
and d.deviceId = ecobeePollData_A.deviceId
LEFT OUTER JOIN lgPollData_A on cph.clientPollSeqNo = lgPollData_A.clientPollSeqNo
and d.deviceId = lgPollData_A.deviceId
LEFT OUTER JOIN eatonemcb1PollData_A on cph.clientPollSeqNo = eatonemcb1PollData_A.clientPollSeqNo
and d.deviceId = eatonemcb1PollData_A.deviceId
LEFT OUTER JOIN eyedroPollData_A on cph.clientPollSeqNo = eyedroPollData_A.clientPollSeqNo
and d.deviceId = eyedroPollData_A.deviceId
LEFT OUTER JOIN iflowPollData_A on cph.clientPollSeqNo = iflowPollData_A.clientPollSeqNo
and d.deviceId = iflowPollData_A.deviceId
WHERE
(
lcp.clientId IN
(
'01886fc9-3442-473a-85fa-3325f2e6a66d',
'94711a81-76eb-4865-9fc9-95c622071c24'
)
)
union
SELECT
cph.clientPollId,
cph.clientPollSeqNo,
cph.clientId,
cph.pollDate,
d.deviceId,
allPollData_B.HEALTHY,
batteryPollData_B.BATTERY_CHARGE_CYCLES,
batteryPollData_B.BATTERY_CURRENT,
batteryPollData_B.BATTERY_CURRENT_MAX,
batteryPollData_B.BATTERY_EFFECTIVE_SOC,
batteryPollData_B.BATTERY_EFFECTIVE_SOC_EXPLANATION,
batteryPollData_B.BATTERY_ENERGY,
batteryPollData_B.BATTERY_POWER,
batteryPollData_B.BATTERY_POWER_MAX,
batteryPollData_B.BATTERY_REAL_SOC,
batteryPollData_B.BATTERY_STORED_ENERGY,
batteryPollData_B.BATTERY_TEMP,
batteryPollData_B.BATTERY_VOLTAGE,
batteryPollData_B.BATTERY_VOLTAGE_MAX,
batteryPollData_B.BATTERY_VOLTAGE_MIN,
batteryPollData_B.BMS_VERSON,
batteryPollData_B.CELL_TEMPERATURE_MAX,
batteryPollData_B.CELL_TEMPERATURE_MIN,
batteryPollData_B.CELL_VOLTAGE_MAX,
batteryPollData_B.CELL_VOLTAGE_MIN,
.
.
.
FROM clientPollHeader_B cph
JOIN device d on cph.clientId = d.clientId
JOIN client c on d.clientId = c.clientId
JOIN latestClientPoll lcp on cph.clientPollSeqNo = lcp.clientPollSeqNo
LEFT OUTER JOIN allPollData_B on cph.clientPollSeqNo = allPollData_B.clientPollSeqNo
and d.deviceId = allPollData_B.deviceId
LEFT OUTER JOIN batteryPollData_B on cph.clientPollSeqNo = batteryPollData_B.clientPollSeqNo
and d.deviceId = batteryPollData_B.deviceId
LEFT OUTER JOIN deltaPollData_B on cph.clientPollSeqNo = deltaPollData_B.clientPollSeqNo
and d.deviceId = deltaPollData_B.deviceId
LEFT OUTER JOIN dessPollData_B on cph.clientPollSeqNo = dessPollData_B.clientPollSeqNo
and d.deviceId = dessPollData_B.deviceId
LEFT OUTER JOIN programPollData_B on cph.clientPollSeqNo = programPollData_B.clientPollSeqNo
and d.deviceId = programPollData_B.deviceId
LEFT OUTER JOIN emersonthermostat1PollData_B on cph.clientPollSeqNo = emersonthermostat1PollData_B.clientPollSeqNo
and d.deviceId = emersonthermostat1PollData_B.deviceId
LEFT OUTER JOIN emersonthermostat2PollData_B on cph.clientPollSeqNo = emersonthermostat2PollData_B.clientPollSeqNo
and d.deviceId = emersonthermostat2PollData_B.deviceId
LEFT OUTER JOIN emersonthermostat3PollData_B on cph.clientPollSeqNo = emersonthermostat3PollData_B.clientPollSeqNo
and d.deviceId = emersonthermostat3PollData_B.deviceId
LEFT OUTER JOIN emersonthermostat4PollData_B on cph.clientPollSeqNo = emersonthermostat4PollData_B.clientPollSeqNo
and d.deviceId = emersonthermostat4PollData_B.deviceId
LEFT OUTER JOIN thermostatPollData_B on cph.clientPollSeqNo = thermostatPollData_B.clientPollSeqNo
and d.deviceId = thermostatPollData_B.deviceId
LEFT OUTER JOIN inverterPollData_B on cph.clientPollSeqNo = inverterPollData_B.clientPollSeqNo
and d.deviceId = inverterPollData_B.deviceId
LEFT OUTER JOIN ioboardPollData_B on cph.clientPollSeqNo = ioboardPollData_B.clientPollSeqNo
and d.deviceId = ioboardPollData_B.deviceId
LEFT OUTER JOIN kokambatteryPollData_B on cph.clientPollSeqNo = kokambatteryPollData_B.clientPollSeqNo
and d.deviceId = kokambatteryPollData_B.deviceId
LEFT OUTER JOIN maggiePollData_B on cph.clientPollSeqNo = maggiePollData_B.clientPollSeqNo
and d.deviceId = maggiePollData_B.deviceId
LEFT OUTER JOIN mercedesbatteryPollData_B on cph.clientPollSeqNo = mercedesbatteryPollData_B.clientPollSeqNo
and d.deviceId = mercedesbatteryPollData_B.deviceId
LEFT OUTER JOIN omronPollData_B on cph.clientPollSeqNo = omronPollData_B.clientPollSeqNo
and d.deviceId = omronPollData_B.deviceId
LEFT OUTER JOIN daikinacPollData_B on cph.clientPollSeqNo = daikinacPollData_B.clientPollSeqNo
and d.deviceId = daikinacPollData_B.deviceId
LEFT OUTER JOIN outbackradianPollData_B on cph.clientPollSeqNo = outbackradianPollData_B.clientPollSeqNo
and d.deviceId = outbackradianPollData_B.deviceId
LEFT OUTER JOIN pvgenPollData_B on cph.clientPollSeqNo = pvgenPollData_B.clientPollSeqNo
and d.deviceId = pvgenPollData_B.deviceId
LEFT OUTER JOIN safeplugPollData_B on cph.clientPollSeqNo = safeplugPollData_B.clientPollSeqNo
and d.deviceId = safeplugPollData_B.deviceId
LEFT OUTER JOIN schneiderinverterPollData_B on cph.clientPollSeqNo = schneiderinverterPollData_B.clientPollSeqNo
and d.deviceId = schneiderinverterPollData_B.deviceId
LEFT OUTER JOIN solarchargecontrollerPollData_B on cph.clientPollSeqNo = solarchargecontrollerPollData_B.clientPollSeqNo
and d.deviceId = solarchargecontrollerPollData_B.deviceId
LEFT OUTER JOIN owonPollData_B on cph.clientPollSeqNo = owonPollData_B.clientPollSeqNo
and d.deviceId = owonPollData_B.deviceId
LEFT OUTER JOIN ecobeePollData_B on cph.clientPollSeqNo = ecobeePollData_B.clientPollSeqNo
and d.deviceId = ecobeePollData_B.deviceId
LEFT OUTER JOIN lgPollData_B on cph.clientPollSeqNo = lgPollData_B.clientPollSeqNo
and d.deviceId = lgPollData_B.deviceId
LEFT OUTER JOIN eatonemcb1PollData_B on cph.clientPollSeqNo = eatonemcb1PollData_B.clientPollSeqNo
and d.deviceId = eatonemcb1PollData_B.deviceId
LEFT OUTER JOIN eyedroPollData_B on cph.clientPollSeqNo = eyedroPollData_B.clientPollSeqNo
and d.deviceId = eyedroPollData_B.deviceId
LEFT OUTER JOIN iflowPollData_B on cph.clientPollSeqNo = iflowPollData_B.clientPollSeqNo
and d.deviceId = iflowPollData_B.deviceId
WHERE
(
lcp.clientId IN
(
'01886fc9-3442-473a-85fa-3325f2e6a66d',
'94711a81-76eb-4865-9fc9-95c622071c24'
)
)
the thing is the _A tables have value but _B tables are all empty (In this case they might get values). So the first part before the union which is for _A takes less than a second to run however the second part for_B tables (empty tables) takes 31 seconds. So my question is does outer join on empty tables create a bottleneck or what? Any help appreciated.
Related
I have a view:
CREATE VIEW [SZV].[ViewMembership]
AS
SELECT DISTINCT
a.ID, a.RIZIVNr, a.Denomination, a.FirstInsDate, c.ID AS HcwId, e.BCEKBO AS CbeEstablishmentNr, d.ID AS TypeCodeId, d.Code AS TypeCodeCode, d.DescriptionNL,
d.DescriptionFR, g.ID AS TypeSitCodeId, g.Code AS SitCode, g.DescriptionFR AS SitDescriptionFR, g.DescriptionNL AS SitDescriptionNL, f.ID AS SitCodeId,
f.SitBegDate, f.SitEndDate, c.RIZIVNr AS LidRizivNr, a.LastSitID, c.LastSitID AS MemberLastSitID, n.Code AS MemberSitCode,
n.DescriptionFR AS MemberSitDescriptionFR, n.DescriptionNL AS MemberSitDescriptionNL, j.FirstName1, j.FirstName2, j.LastName, j.INSS,
h.FromDate AS StartDatum, k.Code AS RolCode, k.DescriptionFR AS RolDescriptionFR, k.DescriptionNL AS RolDescriptionNL, h.ToDate AS EindDatum,
m.ID AS AddressId, m.Street, m.StreetNr, m.Box, m.Locality, m.ZipCode, m.INSCode, p.AttributeValueID AS AttributValueId, p.AttributeID, r.Code AS AttributeCode,
q.AttributeValue, s.Code AS ProfCodeCode, s.DescriptionNL AS ProfDescriptionNL, s.DescriptionFR AS ProfDescriptionFR, h.ID AS HcwSitId, u.ID AS RequestId,
u.HCWID AS RequestForId, x.RIZIVNr AS RequestedForRizivNr, u.ToDate AS RequestToDate, u.FromDate AS RequestFromDate, u.RequestedBy,
v.Code AS RequestRolCode, v.DescriptionFR AS RequestRolDescriptionFR, v.DescriptionNL AS RequestRolDescriptionNL,
w.FirstName1 + ' ' + w.LastName AS RequestedByName, y.FirstName1 AS RequestedForFirstName1, y.FirstName2 AS RequestedForFirstName2,
y.LastName AS RequestedForLastName, y.INSS AS RequestedForINSS, z.ID AS HciAttributeRequestId, z.RequestedBy AS HciAttributeRequestedBy
FROM SZV.HCI AS a INNER JOIN
SZV.HCWHCI AS b ON a.ID = b.HCIID INNER JOIN
SZV.HCW AS c ON b.HCWID = c.ID INNER JOIN
Param.TypeCode AS d ON d.ID = a.TypeCodeID INNER JOIN
SZV.Org AS e ON e.ID = a.OrgID INNER JOIN
SZV.HCISit AS f ON f.ID IN
(SELECT MAX(ID) AS Expr1
FROM SZV.HCISit
WHERE (HCIID = a.ID)) INNER JOIN
Param.TypeSitCode AS g ON g.ID = f.SitCodeID INNER JOIN
SZV.HCWHCI AS h ON h.HCIID = a.ID AND h.HCWID = c.ID INNER JOIN
SZV.HCWSit AS o ON o.HCWID = c.ID INNER JOIN
Param.TypeSitCode AS n ON n.ID = o.SitCodeID INNER JOIN
SZV.Person AS j ON j.ID = c.PersonID INNER JOIN
Param.HCWRole AS k ON h.RoleID = k.ID INNER JOIN
SZV.HCIAddress AS l ON l.HCIID = a.ID INNER JOIN
Param.ProfCode AS s ON s.ID = c.ProfCodeID INNER JOIN
SZV.Address AS m ON m.ID = l.AddressID LEFT OUTER JOIN
SZV.HCIAttribute AS p ON p.HCIID = a.ID LEFT OUTER JOIN
Param.AttributeValue AS q ON p.AttributeValueID = q.ID LEFT OUTER JOIN
Param.Attribute AS r ON r.ID = p.AttributeID AND r.Code = 'TherapeuticLink' LEFT OUTER JOIN
SZV.HCWHCIRequest AS u ON u.HCIID = a.ID AND u.HCWID = c.ID AND u.Validated = 0 AND u.Canceled = 0 LEFT OUTER JOIN
Param.HCWRole AS v ON u.RoleID = v.ID LEFT OUTER JOIN
SZV.Person AS w ON w.INSS = u.RequestedBy LEFT OUTER JOIN
SZV.HCW AS x ON u.HCWID = x.ID LEFT OUTER JOIN
SZV.Person AS y ON x.PersonID = y.ID LEFT OUTER JOIN
SZV.HCIAttributeRequest AS z ON z.HCIID = a.ID AND z.Validated = 0 AND z.Canceled = 0
WHERE (d.Code IN ('805', '212', '214'))
Now when I use NHibernate to get my result I get the following error :
{"View or function 'SZV.ViewMembership' is not updatable because the modification affects multiple base tables."}
{"View or function 'SZV.ViewMembership' is not updatable because the modification affects multiple base tables."}
The code I use is the following :
ICriteria selectCriteria = this.CreateCriteria();
var or = Restrictions.Disjunction();
if (groupId != 0)
{
var restrHciId = Restrictions.Eq("Id", groupId);
or.Add(restrHciId);
}
selectCriteria.Add(or);
IList<Group> groupsList = this.Find(selectCriteria).ToList();
I am not trying to update anything in the view.
Can anyone help me?
You can only update One table in a "Update Query" for a View;
for two Col from multiple Tables You have to Use multiple queries :)
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
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
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
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