Combine 2 rows to the same column - sql

Hey so my query right now is
ALTER PROCEDURE [SSRS].[VolumeCustomers]
#UserID int
AS
select
CaseTypeName,
COUNT(CaseNo) as CaseCount,
'Open' as indicator
FROM ORDERS.ApCase AC with (NOLOCK)
join ORDERS.CaseType CT (NOLOCK) on CT.CaseTypeID = AC.CaseTypeID
join WORKFLOW.WorkflowHistory WH (NOLOCK) on WH.EntityID = AC.CaseID and TableID = dbo.GetTableID('ApCase', 'ORDERS') and WH.Active = 1
inner join WORKFLOW.WorkflowStep WS (NOLOCK) on WS.WorkflowStepID = WH.WorkflowStepID and WS.NextStepID is null
where (AC.Active =1 and AC.CreatedDate >= DATEADD(day,-7,getdate()) and AC.CreatedDate < GETDATE())
Group By CaseTypeName
union
select
CaseTypeName,
COUNT(Caseno) as CaseCount,
'Closed' as indicator
FROM ORDERS.ApCase AC with (NOLOCK)
join ORDERS.CaseType CT (NOLOCK) on CT.CaseTypeID = AC.CaseTypeID
join WORKFLOW.WorkflowHistory WH (NOLOCK) on WH.EntityID = AC.CaseID and TableID = dbo.GetTableID('ApCase', 'ORDERS') and WH.Active = 1
join WORKFLOW.WorkflowStep WS (NOLOCK) on WS.WorkflowStepID = WH.WorkflowStepID and WS.NextStepID is not null
where (AC.Active =1 and AC.CreatedDate >= DATEADD(day,-7,getdate()) and AC.CreatedDate < GETDATE())
GROUP BY CaseTypeName
Order by CaseCount desc
and the out put is
Cytogenetics 2 All
Cytogenetics 1 Open
Flow Tech 1 All
Flow Tech 1 Open
Surgical 1 All
Surgical 1 Open
But i want the cytogenetics, flow tech, and surgical to all appear on the same row
example:
Cytogenetics 2 All 1 Open
Flow Tech 1 All 1 Open
Surgical 1 All 1 Open
How do I edit my query to reflect this?

Does this work?
SELECT A.*, B.CaseCount, B.indicator
FROM (<First Part of Union in Question>) AS A INNER JOIN
(<Second Part of Union in Question>) AS B ON A.CaseTypeName = B.CaseTypeName

SELECT
CaseTypeName,
COUNT(CASE WHEN WS.NextStepID IS NULL THEN Caseno END) AS CaseCountOpen,
COUNT(CASE WHEN WS.NextStepID IS NOT NULL THEN Caseno END) AS CaseCountClosed,
COUNT(CaseNo) AS CaseCountAll
FROM ORDERS.ApCase AC with (NOLOCK)
JOIN ORDERS.CaseType CT (NOLOCK)
ON CT.CaseTypeID = AC.CaseTypeID
JOIN WORKFLOW.WorkflowHistory WH (NOLOCK)
ON WH.EntityID = AC.CaseID
AND TableID = dbo.GetTableID('ApCase', 'ORDERS')
AND WH.Active = 1
JOIN WORKFLOW.WorkflowStep WS (NOLOCK)
ON WS.WorkflowStepID = WH.WorkflowStepID
WHERE AC.Active = 1
AND AC.CreatedDate >= DATEADD(day,-7,getdate())
AND AC.CreatedDate < GETDATE()
GROUP BY CaseTypeName
ORDER BY CaseCountAll DESC

Related

The SQL Server query is taking too long to load the data

The below query is actually a view which is being used to display the cash payment report. But it is taking too much of time to load the data
SELECT
Billing_AccountPaymentDate.DueDate,
SUM(Billing_AccountCharge.NetAmount) AS NetCharges,
ISNULL(SUM(AccountChargePayment.SchoolPayments + AccountChargePayment.SchoolRemittances + AccountChargePayment.SchoolRemittancesPending), 0) / SUM(Billing_AccountCharge.NetAmount) AS PercentCollected,
SUM(Billing_AccountCharge.NetAmount - ISNULL(AccountChargePayment.SchoolPayments + AccountChargePayment.SchoolRemittances + AccountChargePayment.SchoolRemittancesPending, 0)) AS RemainingBalance,
Billing_AccountPaymentDate.RemittanceEffectiveDate,
Billing_Account.SchoolId,
ISNULL(SUM(AccountChargePayment.SchoolPayments), 0) AS SchoolPayments,
ISNULL(SUM(AccountChargePayment.SchoolRemittances), 0) AS SchoolRemittances,
ISNULL(SUM(AccountChargePayment.SchoolRemittancesPending), 0) AS SchoolRemittancesPending,
Billing_Account.SchoolYearId,
ISNULL(SUM(AccountChargePayment.SchoolPayments + AccountChargePayment.SchoolRemittances), 0) AS TotalReceipts
FROM
Billing_AccountCharge
INNER JOIN
Billing_AccountInvoice ON
Billing_AccountInvoice.AccountInvoiceId = Billing_AccountCharge.AccountInvoiceId
INNER JOIN
Billing_Account ON
Billing_Account.AccountId = Billing_AccountInvoice.AccountId
INNER JOIN
Billing_PaymentMethod ON
Billing_PaymentMethod.PaymentMethodId = CASE WHEN Billing_AccountInvoice.AutomaticPaymentEligible = 1 THEN Billing_Account.PaymentMethodId ELSE 3 END -- Send Statements
INNER JOIN
Billing_AccountPaymentDate ON
Billing_AccountPaymentDate.AccountPaymentMethodId = Billing_PaymentMethod.AnticipatedAccountPaymentMethodId AND
Billing_AccountPaymentDate.DueDate = Billing_AccountInvoice.DueDate AND
Billing_AccountPaymentDate.HoldForFee = Billing_Account.HoldPaymentForFee
INNER JOIN
Billing_ChargeItem ON
Billing_ChargeItem.ChargeItemId = Billing_AccountCharge.ChargeItemId
LEFT OUTER JOIN
(
SELECT
Billing_AccountChargePayment.AccountChargeId,
SUM(CASE WHEN Billing_AccountPayment.AccountPaymentTypeId = 9 THEN Billing_AccountChargePayment.Amount ELSE 0 END) AS SchoolPayments,
SUM(CASE WHEN Billing_AccountChargePayment.SchoolRemittanceId IS NOT NULL THEN Billing_AccountChargePayment.Amount ELSE 0 END) AS SchoolRemittances,
SUM(CASE WHEN Billing_AccountChargePayment.SchoolRemittanceId IS NULL AND Billing_AccountPayment.AccountPaymentTypeId <> 9 THEN Billing_AccountChargePayment.Amount ELSE 0 END) AS SchoolRemittancesPending
FROM
Billing_AccountChargePayment
INNER JOIN
Billing_AccountPayment ON
Billing_AccountPayment.AccountPaymentId = Billing_AccountChargePayment.AccountPaymentId
GROUP BY
Billing_AccountChargePayment.AccountChargeId
) AccountChargePayment ON
AccountChargePayment.AccountChargeId = Billing_AccountCharge.AccountChargeId
WHERE
Billing_AccountInvoice.AccountInvoiceStatusId <> 4 AND -- Voided
Billing_ChargeItem.RemitToSchool = 1
AND Billing_Account.[SchoolId] = 6 --hard code in a school with data
AND Billing_Account.[SchoolYearId] = 12 --hard code in a school year with data
GROUP BY
Billing_AccountPaymentDate.DueDate,
Billing_AccountPaymentDate.RemittanceEffectiveDate,
Billing_Account.SchoolId,
Billing_Account.SchoolYearId
HAVING
SUM(Billing_AccountCharge.NetAmount) <> 0
order by Billing_AccountPaymentDate.DueDate ASC
It looks like the inner query in the left join is taking too much of time, both the tables already have non clustered index, I tried taking those tables outside but the data is not accurate
Use CTE instead of subquery and do all calculation there instead of in Left outer join. Moreover, use with(nolock) whenever you fetch data. Still your query is taking much more time then You should implement proper indexing.
WITH PaymentData AS (
SELECT
acp.AccountChargeId,
SUM(
CASE
WHEN ap.AccountPaymentTypeId = 9 THEN acp.Amount
ELSE 0
END
) AS SchoolPayments,
SUM(
CASE
WHEN acp.SchoolRemittanceId IS NOT NULL THEN acp.Amount
ELSE 0
END
) AS SchoolRemittances,
SUM(
CASE
WHEN acp.SchoolRemittanceId IS NULL AND ap.AccountPaymentTypeId <> 9 THEN acp.Amount
ELSE 0
END
) AS SchoolRemittancesPending
FROM
Billing_AccountChargePayment acp WITH(NOLOCK)
INNER JOIN Billing_AccountPayment ap WITH(NOLOCK) ON acp.AccountPaymentId = ap.AccountPaymentId
GROUP BY
acp.AccountChargeId
)
SELECT
apd.DueDate,
SUM(ac.NetAmount) AS NetCharges,
COALESCE(
SUM(pd.SchoolPayments + pd.SchoolRemittances + pd.SchoolRemittancesPending),
0
) / SUM(ac.NetAmount) AS PercentCollected,
SUM(ac.NetAmount - COALESCE(pd.SchoolPayments, 0) - COALESCE(pd.SchoolRemittances, 0) - COALESCE(pd.SchoolRemittancesPending, 0)) AS RemainingBalance,
apd.RemittanceEffectiveDate,
a.SchoolId,
COALESCE(SUM(pd.SchoolPayments), 0) AS SchoolPayments,
COALESCE(SUM(pd.SchoolRemittances), 0) AS SchoolRemittances,
COALESCE(SUM(pd.SchoolRemittancesPending), 0) AS SchoolRemittancesPending,
a.SchoolYearId,
COALESCE(SUM(pd.SchoolPayments + pd.SchoolRemittances), 0) AS TotalReceipts
FROM
Billing_AccountCharge ac WITH(NOLOCK)
INNER JOIN Billing_AccountInvoice ai WITH(NOLOCK) ON ac.AccountInvoiceId = ai.AccountInvoiceId
INNER JOIN Billing_Account a WITH(NOLOCK) ON ai.AccountId = a.AccountId
INNER JOIN Billing_PaymentMethod pm WITH(NOLOCK) ON pm.PaymentMethodId = CASE
WHEN ai.AutomaticPaymentEligible = 1 THEN a.PaymentMethodId
ELSE 3
END
INNER JOIN Billing_AccountPaymentDate apd WITH(NOLOCK) ON
apd.AccountPaymentMethodId = pm.AnticipatedAccountPaymentMethodId AND
apd.DueDate = ai.DueDate AND
apd.HoldForFee = a.HoldPaymentForFee
INNER JOIN Billing_ChargeItem ci WITH(NOLOCK) ON ac.ChargeItemId = ci.ChargeItemId
LEFT OUTER JOIN PaymentData pd WITH(NOLOCK) ON ac.AccountChargeId = pd.AccountChargeId
WHERE
ai.AccountInvoiceStatusId <> 4 AND
ci.RemitToSchool = 1 AND
a.SchoolId = 6 AND
a.SchoolYearId = 12
GROUP BY
apd.DueDate,
apd.RemittanceEffectiveDate,
a.SchoolId,
a.SchoolYearId
HAVING
SUM(ac.NetAmount) <> 0
ORDER BY
apd.DueDate ASC;

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= orwhen the subquery is used as an expression

I am having this error message. I have tried many other ways but can't resolve the issue.
I would be really grateful if someone could look at this :
;WITH progression
AS
(
SELECT
w.CustomerID,
CASE WHEN SUM(CASE WHEN w.workflowoutcomeid = 8 THEN 1 ELSE 0 END) > 0 THEN 'Yes' ELSE 'No' END AS [PG01],
CASE WHEN SUM(CASE WHEN w.workflowoutcomeid = 11 THEN 1 ELSE 0 END) > 0 THEN 'Yes' ELSE 'No' END AS [PG03],
CASE WHEN SUM(CASE WHEN w.workflowoutcomeid = 14 THEN 1 ELSE 0 END) > 0 THEN 'Yes' ELSE 'No' END AS [PG04],
CASE WHEN SUM(CASE WHEN w.workflowoutcomeid = 17 THEN 1 ELSE 0 END) > 0 THEN 'Yes' ELSE 'No' END AS [PG05],
CASE WHEN SUM(CASE WHEN w.workflowoutcomeid = 9 THEN 1 ELSE 0 END) > 0 THEN 'Yes' ELSE 'No' END AS [in_work_review]
FROM
ABC.dbo.tblWorkflow w
WHERE
w.Deleted IS NULL
AND
w.workflowqueueid = 4
AND
w.workflowoutcomeid IN (8, 11, 14, 17, 9)
GROUP BY
w.CustomerID
),
contact_data
AS
(
SELECT
CustomerID, [Email], [Mobile], [Home]
FROM
(
SELECT
CustomerID,
CASE
WHEN ContactTypeID = 1 THEN 'Home'
WHEN ContactTypeID = 2 THEN 'Mobile'
WHEN ContactTypeID = 5 THEN 'Email'
END AS ContactTypeDescription,
ContactValue
FROM ABC.dbo.tblCustomerContact
WHERE ContactTypeID IN (1,2,5)
) base
PIVOT
(
MAX(ContactValue) FOR ContactTypeDescription IN ([Home],[Mobile],[Email])
) pvt
),
appointment
AS
(
SELECT
n.[Key] AS CustomerID,
MAX(CASE WHEN n.AppointmentStatusID IN (2,4) THEN n.ActionDate ELSE NULL END) AS [Last_Completed_Attended_Appointment],
MAX(CASE WHEN n.AppointmentStatusID = 1 THEN n.ActionDate ELSE NULL END) AS [Next Pending Appointment]
FROM ABC.dbo.tblNote n
WHERE n.AppointmentTypeID = 6 AND n.AppointmentStatusID IN (1,2,4)
GROUP BY n.[Key]
)
SELECT
m.Firstname + ' ' + m.Lastname AS [Manager],
u.Firstname + ' ' + u.Lastname AS [Adviser],
c.CustomerID,
c.GivenName + ' ' + c.FamilyName AS [Customer Name],
g.Gender,
e.EthnicityName AS Ethnicity,
com.CompanyName,
NULL AS DeliverySite,
d.[Name] AS District,
css.ServiceStatus,
contact_data.Email,
uad.Line1 AS AddressLine1,
uad.Line2 AS AddressLine2,
uad.Line3 AS AddressLine3,
uad.Postcode,
uad.AlternativePostcode,
uad.Town,
ISNULL(contact_data.mobile, contact_data.home) AS Phone,
c.StartDate,
(SELECT MIN(wh.MGCReviewerModifiedDate) AS [ST01_Approved_Date]
FROM ABC.dbo.tblWorkflow w
INNER JOIN ABC.dbo.tblWorkflowHistory wh
ON w.WorkflowID = wh.WorkflowID
WHERE wh.WorkflowQueueID = 4 AND wh.MGCReviewerModifiedDate IS NOT NULL
AND wh.Deleted IS NULL AND w.WorkflowOutcomeID = 1 AND w.WorkflowQueueID = 4
AND w.Deleted IS NULL AND w.CustomerID = c.CustomerID
GROUP BY w.WorkflowID, w.CustomerID) AS ST01_Approved_Date,
qual.LearningAimTitle,
qual.LearningAimCompletionStatus,
qual.LearningAimOutcomeStatus,
mwp.WorkExperienceTitle ,
mwp.WorkExperienceStartDate,
mwp.WorkExperienceEndDate,
mwp.learningaimcompletionstatus as [Work_Ex_CompletionStatus],
mwp.learningaimoutcomestatus as [Work_Ex_OutcomeStatus],
p.PG01 AS [PG01_Start(Job Start Approved/Claimed ?)],
p.in_work_review AS [PG01_In_work_review(Job Outcome Approved/Claimed ?)],
p.PG03 AS [PG03 – Education Approved / Claimed?],
p.PG04 AS [PG04 – Apprenticeship Approved / Claimed?],
p.PG05 AS [PG05– Apprenticeship Approved / Claimed?],
a.Last_Completed_Attended_Appointment,
a.[Next Pending Appointment],
FLOOR(DATEDIFF(day, c.DateOfBirth, GETDATE()) / 365.25) as CustomerAge
FROM ABC.dbo.tblCustomer c
LEFT JOIN ABC.dbo.tblUser u ON c.OwnerID = u.UserID
LEFT JOIN ABC.dbo.tblUser m ON u.ManagerID = m.UserID
LEFT JOIN ABC.dbo.tlkpGender g ON c.GenderID = g.GenderID
LEFT JOIN ABC.dbo.tlkpEthnicity e ON c.EthnicityID = e.EthnicityID
LEFT JOIN ABC.dbo.tblCompany com ON c.CompanyId = com.[CompanyID]
LEFT JOIN ABC.dbo.tblCustomerAddress cad ON c.CustomerID = cad.CustomerID AND cad.EffectiveTo IS NULL
LEFT JOIN ABC.dbo.tblUKAddress uad ON uad.UKaddressID = cad.UKaddressID
LEFT JOIN PostcodeESyNCS.dbo.tblPostcode po ON uad.Postcode = po.Postcode
LEFT JOIN PostcodeESyNCS.dbo.tlkpDistrict d ON po.DistrictId = d.DistrictId
LEFT JOIN ABC.dbo.tlkpCustomerServiceStatus css ON c.CustomerServiceStatusID = css.CustomerServiceStatusID
LEFT JOIN progression p ON c.CustomerID = p.CustomerID
LEFT JOIN contact_data ON c.CustomerID = contact_data.CustomerID
OUTER APPLY
(
SELECT TOP 1
l.learningaimid,
aim.LearningAimTitle,
aim.isfullqualification,
st.LearningAimCompletionStatus,
aos.LearningAimOutcomeStatus
FROM ABC.dbo.tbllearning l
LEFT JOIN ABC.dbo.tlkplearningaim aim on aim.learningaimid = l.learningaimid
LEFT JOIN ABC.dbo.tlkplearningaimcompletionstatus st on st.learningaimcompletionstatusid = l.learningaimcompletionstatusid
LEFT JOIN ABC.dbo.tlkpLearningAimOutcomeStatus aos on aos.learningaimoutcomestatusid = l.learningaimoutcomestatusid
WHERE aim.learningaimtypeid = 1 AND l.customerid = c.customerid
ORDER BY l.StartDate DESC
) qual
OUTER APPLY
(
SELECT TOP 1
l.learningaimid,
REPLACE(aim.LearningAimTitle,',',' ') as WorkExperienceTitle,
aim.isfullqualification,
st.LearningAimCompletionStatus,
outcome.LearningAimOutcomeStatus,
l.StartDate as WorkExperienceStartDate,
l.ActualEndDate as WorkExperienceEndDate
FROM ABC.dbo.tbllearning l
LEFT JOIN ABC.dbo.tlkplearningaim aim ON aim.learningaimid=l.learningaimid
LEFT JOIN ABC.dbo.tlkplearningaimcompletionstatus st ON st.learningaimcompletionstatusid=l.learningaimcompletionstatusid
LEFT JOIN ABC.dbo.tlkpLearningAimOutcomeStatus outcome ON outcome.learningaimoutcomestatusid= l.learningaimoutcomestatusid
WHERE aim.LearningAimID IN (SELECT la.LearningAimID
FROM ABC.dbo.tlkpLearningAim la
WHERE la.LearningAimTypeID = 2
and la.LearningAimTitle like '%Work%')
AND l.customerid = c.customerid
ORDER BY StartDate DESC
) AS mwp
LEFT JOIN appointment a ON c.CustomerID = a.CustomerID
WHERE
c.Deleted IS NULL
I am getting the following error:
Msg 512, Level 16, State 1, Line 2
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Warning: Null value is eliminated by an aggregate or other SET operation.
The only place where I see that this could be happening is the definition of ST01_Approved_Date:
(SELECT MIN(wh.MGCReviewerModifiedDate) AS [ST01_Approved_Date]
FROM ABC.dbo.tblWorkflow w JOIN
ABC.dbo.tblWorkflowHistory wh
ON w.WorkflowID = wh.WorkflowID
WHERE wh.WorkflowQueueID = 4 AND wh.MGCReviewerModifiedDate IS NOT NULL AND
wh.Deleted IS NULL AND w.WorkflowOutcomeID = 1 AND w.WorkflowQueueID = 4 AND
w.Deleted IS NULL AND w.CustomerID = c.CustomerID
GROUP BY w.WorkflowID, w.CustomerID
) AS ST01_Approved_Date,
A correlated subquery should not contain GROUP By for exactly this reason. I don't know what the logic would be, but there should probably be a condition on w.WorkFlowId to the outer query, something like:
(SELECT MIN(wh.MGCReviewerModifiedDate) AS [ST01_Approved_Date]
FROM ABC.dbo.tblWorkflow w JOIN
ABC.dbo.tblWorkflowHistory wh
ON w.WorkflowID = wh.WorkflowID
WHERE wh.WorkflowQueueID = 4 AND wh.MGCReviewerModifiedDate IS NOT NULL AND
wh.Deleted IS NULL AND w.WorkflowOutcomeID = 1 AND w.WorkflowQueueID = 4 AND
w.Deleted IS NULL AND w.CustomerID = c.CustomerID AND
w.workflowID = <outer query reference>.WorkflowId
) AS ST01_Approved_Date,

SQL query very slow and need to be improved

I have a select query that I need to improve to make it fast.
SELECT
st.SigHistUID, st.RelationType, st2.RelationType,
case
when st.RelationType <> st2.RelationType
then 'N'
else st.RelationType
end as [NewRelationType],
st.ToSigUID , sh.FullDescription, sh.SigHistUID AS ToSigHistUID
FROM
[Stackability] (nolock) st
INNER JOIN
[SignatureHistory] (nolock) sh ON sh.SigUID = st.ToSigUID
AND sh.SigHistUID = (SELECT TOP 1 SigHistUID FROM [SignatureHistory] (nolock) tmp where tmp.SigUID = sh.SigUID ORDER BY SigHistUID DESC)
INNER JOIN
[SignatureHistory] (nolock) sh2 ON st.SigHistUID = sh2.SigHistUID
INNER JOIN
Stackability (nolock) st2 on st2.ToSigUID = sh2.SigUID and st2.SigHistUID = sh.SigHistUID
WHERE
st.SigHistUID < 1000 and
st.RelationType <> st2.RelationType
ORDER BY
st.SigHistUID
Try having taken out the top 1 condition:
SELECT
st.SigHistUID, st.RelationType, st2.RelationType,
case
when st.RelationType <> st2.RelationType
then 'N'
else st.RelationType
end as [NewRelationType],
st.ToSigUID , sh.FullDescription, sh.SigHistUID AS ToSigHistUID
FROM
[Stackability] (nolock) st
INNER JOIN
(select distinct siguid, max(SigHistUID) OVER(PARTITION BY siguid ) as SigHistUID from [SignatureHistory] (nolock)) sh ON sh.SigUID = st.ToSigUID
INNER JOIN
[SignatureHistory] (nolock) sh2 ON st.SigHistUID = sh2.SigHistUID
INNER JOIN
Stackability (nolock) st2 on st2.ToSigUID = sh2.SigUID and st2.SigHistUID = sh.SigHistUID
WHERE
st.SigHistUID < 1000 and
st.RelationType <> st2.RelationType
ORDER BY
st.SigHistUID

SQl Error : Each GROUP BY expression must contain at least one column that is not an outer reference [duplicate]

This question already has answers here:
Each GROUP BY expression must contain at least one column that is not an outer reference
(8 answers)
Closed 6 years ago.
I get this error
Each GROUP BY expression must contain at least one column that is not an outer reference
while running this query:
SELECT TOP 1
SUM(mla.total_current_attribute_value)
FROM
partstrack_machine_location_attributes mla (NOLOCK)
INNER JOIN
#tmpInstallParts_Temp installpartdetails ON mla.machine_sequence_id = installpartdetails.InstallKitToMachineSequenceId
AND (CASE WHEN mla.machine_side_id IS NULL THEN 1
WHEN mla.machine_side_id = installpartdetails.InstallKitToMachineSideId THEN 1 END
) = 1
INNER JOIN
partstrack_mes_attribute_mapping mam (NOLOCK) ON mla.mes_attribute = mam.mes_attribute_name
INNER JOIN
partstrack_attribute_type at (NOLOCK) ON mam.pt_attribute_id = at.pt_attribute_id
INNER JOIN
partstrack_ipp_mes_attributes ima(NOLOCK) ON at.pt_attribute_id = ima.pt_attribute_id
WHERE
mla.active_ind = 'Y' AND
ima.ipp_ID IN (SELECT ipp.ipp_id
FROM partstrack_individual_physical_part ipp
INNER JOIN #tmpInstallParts_Temp tmp ON (ipp.ipp_id = tmp.InstallingPartIPPId OR
(CASE WHEN tmp.InstallingPartIPKId = '-1' THEN 1 END) = 1
)
GROUP BY
ima.ipp_id
Can someone help me?
This is the text of the query from the first revision of the question.
In later revisions you removed the last closing bracket ) and the query became syntactically incorrect. You'd better check and fix the text of the question and format the text of the query, so it is readable.
SELECT TOP 1
SUM(mla.total_current_attribute_value)
FROM
partstrack_machine_location_attributes mla (NOLOCK)
INNER JOIN #tmpInstallParts_Temp installpartdetails
ON mla.machine_sequence_id = installpartdetails.InstallKitToMachineSequenceId
AND (CASE WHEN mla.machine_side_id IS NULL THEN 1
WHEN mla.machine_side_id = installpartdetails.InstallKitToMachineSideId THEN 1 END) = 1
INNER JOIN partstrack_mes_attribute_mapping mam (NOLOCK) ON mla.mes_attribute = mam.mes_attribute_name
INNER JOIN partstrack_attribute_type at (NOLOCK) ON mam.pt_attribute_id = at.pt_attribute_id
INNER JOIN partstrack_ipp_mes_attributes ima(NOLOCK) ON at.pt_attribute_id = ima.pt_attribute_id
WHERE
mla.active_ind = 'Y'
AND ima.ipp_ID IN
(
Select
ipp.ipp_id
FROM
partstrack_individual_physical_part ipp
INNER JOIN #tmpInstallParts_Temp tmp
ON (ipp.ipp_id = tmp.InstallingPartIPPId
OR (CASE WHEN tmp.InstallingPartIPKId = '-1' THEN 1 END) = 1)
GROUP BY
ima.ipp_id
)
With this formatting it is clear now that there is a subquery with GROUP BY.
Most likely it is just a typo: you meant to write GROUP BY ipp.ipp_id instead of GROUP BY ima.ipp_id.
If you really wanted to have the GROUP BY not in a subquery, but in the main SELECT, then you misplaced the closing bracket ) and the query should look like this:
SELECT TOP 1
SUM(mla.total_current_attribute_value)
FROM
partstrack_machine_location_attributes mla (NOLOCK)
INNER JOIN #tmpInstallParts_Temp installpartdetails
ON mla.machine_sequence_id = installpartdetails.InstallKitToMachineSequenceId
AND (CASE WHEN mla.machine_side_id IS NULL THEN 1
WHEN mla.machine_side_id = installpartdetails.InstallKitToMachineSideId THEN 1 END) = 1
INNER JOIN partstrack_mes_attribute_mapping mam (NOLOCK) ON mla.mes_attribute = mam.mes_attribute_name
INNER JOIN partstrack_attribute_type at (NOLOCK) ON mam.pt_attribute_id = at.pt_attribute_id
INNER JOIN partstrack_ipp_mes_attributes ima(NOLOCK) ON at.pt_attribute_id = ima.pt_attribute_id
WHERE
mla.active_ind = 'Y'
AND ima.ipp_ID IN
(
Select
ipp.ipp_id
FROM
partstrack_individual_physical_part ipp
INNER JOIN #tmpInstallParts_Temp tmp
ON (ipp.ipp_id = tmp.InstallingPartIPPId
OR (CASE WHEN tmp.InstallingPartIPKId = '-1' THEN 1 END) = 1)
)
GROUP BY
ima.ipp_id
In any case, proper formatting of the source code can really help.
Group By ima.ipp_id
should be applicable to outer query. Because of incorrect placement of '(' it was applying to inner query.
Now after correcting the query,it's working fine without any issues.
Final Query is :
SELECT TOP 1
SUM(mla.total_current_attribute_value)
FROM
partstrack_machine_location_attributes mla (NOLOCK)
INNER JOIN #tmpInstallParts_Temp installpartdetails
ON mla.machine_sequence_id = installpartdetails.InstallKitToMachineSequenceId
AND (CASE WHEN mla.machine_side_id IS NULL THEN 1
WHEN mla.machine_side_id = installpartdetails.InstallKitToMachineSideId THEN 1 END ) = 1
INNER JOIN partstrack_mes_attribute_mapping mam (NOLOCK) ON mla.mes_attribute = mam.mes_attribute_name
INNER JOIN partstrack_attribute_type at (NOLOCK) ON mam.pt_attribute_id = at.pt_attribute_id
INNER JOIN partstrack_ipp_mes_attributes ima(NOLOCK) ON at.pt_attribute_id = ima.pt_attribute_id
WHERE
mla.active_ind = 'Y'
AND ima.ipp_ID IN
(
Select
ipp.ipp_id
FROM
partstrack_individual_physical_part ipp
INNER JOIN #tmpInstallParts_Temp tmp
ON (ipp.ipp_id = tmp.InstallingPartIPPId
OR (CASE WHEN tmp.InstallingPartIPKId = '-1' THEN 1 END ) =1)
)
GROUP BY ima.ipp_id
Thank you all.

TSQL query is running slow, how to speed it up?

I have a sql query for a report, it includes a few sub queries. it runs very slow. I tried a few ways (like use join instead of subquery, add a few more index). but none of them worked. Here is the query:
declare #time_from datetime
declare #time_to datetime
set #time_from ='2012-01-01'
set #time_to = '2014-01-01'
select a.a_id, c.c_id, c.c_chat_line_id, a.a_first_name, a.a_last_name
,(select isnull(SUM(ac.ac_amount),0) from t_actress_credit ac join t_order o on o.o_id = ac.order_id where o.o_status = 1 and ac.actress_id = a.a_id and ac.ac_time>=#time_from and ac.ac_time<=#time_to) as credit
,(select isnull(SUM(ac.ac_amount),0) from t_actress_credit ac join t_order o on o.o_id = ac.order_id where o.o_status = 1 and ac.ac_is_paid = 1 and ac.actress_id = a.a_id and ac.ac_time>=#time_from and ac.ac_time<=#time_to) as paid_credit
,(select COUNT(1) from t_message pm join t_call_log l1 on pm.call_log_id = l1.c_id where pm.m_type = 2 and l1.caller_id = c.c_id and pm.m_time>=#time_from and pm.m_time<=#time_to) as pmsg_sent
,(select COUNT(1) from t_message pm join t_call_log l2 on pm.m_to_call_log_id = l2.c_id where pm.m_type = 2 and l2.caller_id = c.c_id and pm.m_time>=#time_from and pm.m_time<=#time_to) as pmsg_received
,(select COUNT(1) from t_message pm join t_call_log l3 on pm.call_log_id = l3.c_id where pm.m_type = 1 and l3.caller_id = c.c_id and pm.m_time>=#time_from and pm.m_time<=#time_to) as lcmsg_sent
,(select COUNT(1) from t_message pm join t_call_log l4 on pm.m_to_call_log_id = l4.c_id where pm.m_type = 1 and l4.caller_id = c.c_id and pm.m_time>=#time_from and pm.m_time<=#time_to) as lcmsg_received
,(select COUNT(1) from t_actress_live_minute where actress_id = a.a_id and alm_time>=#time_from and alm_time<=#time_to ) as live_calls
,(select isnull(SUM(alm_minutes),0) from t_actress_live_minute where actress_id = a.a_id and alm_time>=#time_from and alm_time<=#time_to) as live_call_minutes
,(select isnull(count(1),0) from t_call_log l where l.caller_id = c.c_id and l.c_time_out is not null and c_time_in >=#time_from and c_time_in <= #time_to) as total_calls
,(select isnull(SUM(DATEDIFF(minute, l.c_time_in, l.c_time_out)),0) from t_call_log l where c_time_in >=#time_from and c_time_in <= #time_to and l.caller_id = c.c_id and l.c_time_out is not null ) as total_call_minutes
from t_actress a
join t_caller c on c.c_id = a.caller_id
group by a.a_id,c.c_id, c.c_chat_line_id, a.a_first_name, a.a_last_name
Can any one give some suggestions?
Thanks a lot!
Alan
You could try combining subqueries that pull from the same table or set of tables into a single subquery. To account for variations in the conditions, you could use conditional aggregation (employing CASE expressions).
I can see four, possibly five, such groups in your query. Here it is rewritten to use four subqueries:
SELECT
a.a_id,
c.c_id,
c.c_chat_line_id,
a.a_first_name,
a.a_last_name,
ISNULL(cr.credit , 0) AS credit
ISNULL(cr.paid_credit , 0) AS paid_credit
ISNULL(m.pmsg_sent , 0) AS pmsg_sent,
ISNULL(m.pmsg_received , 0) AS pmsg_received,
ISNULL(m.lcmsg_sent , 0) AS lcmsg_sent,
ISNULL(m.lcmsg_received , 0) AS lcmsg_received,
ISNULL(alm.live_calls , 0) AS live_calls,
ISNULL(alm.live_call_minutes, 0) AS live_call_minutes,
ISNULL(l.total_calls , 0) AS total_calls,
ISNULL(l.total_call_minutes , 0) AS total_call_minutes,
FROM t_actress AS a
INNER JOIN t_caller AS c
ON c.c_id = a.caller_id
LEFT JOIN (
SELECT
ac.actress_id,
SUM( ac.ac_amount ) AS credit,
SUM(CASE ac.ac_is_paid WHEN 1 THEN ac.ac_amount END) AS paid_credit
FROM t_actress_credit AS ac
JOIN t_order o ON o.o_id = ac.order_id
WHERE o.o_status = 1
AND ac.ac_time BETWEEN #time_from AND #time_to
GROUP BY ac.actress_id
) AS ac
ON ac.actress_id = a.a_id
LEFT JOIN (
SELECT
l.caller_id,
COUNT(CASE WHEN m. call_log_id = l1.c_id AND m.m_type = 2 THEN 1 END) AS pmsg_sent,
COUNT(CASE WHEN m.m_to_call_log_id = l2.c_id AND m.m_type = 2 THEN 1 END) AS pmsg_received,
COUNT(CASE WHEN m. call_log_id = l3.c_id AND m.m_type = 1 THEN 1 END) AS lcmsg_sent,
COUNT(CASE WHEN m.m_to_call_log_id = l4.c_id AND m.m_type = 1 THEN 1 END) AS lcmsg_received
FROM t_message AS m
JOIN t_call_log AS l ON l.c_id IN (m.call_log_id, m.m_to_call_log_id)
WHERE m.m_type IN (1, 2)
AND m.m_time BETWEEN #time_from AND #time_to
GROUP BY l.caller_id
) AS m
ON m.caller_id = c.c_id
LEFT JOIN (
SELECT
actress_id,
COUNT(*) AS live_calls,
SUM(alm_minutes) AS live_call_minutes
FROM t_actress_live_minute
WHERE alm_time BETWEEN #time_from AND #time_to
GROUP BY actress_id
) AS alm
ON alm.actress_id = a.a_id
LEFT JOIN (
SELECT
caller_id,
COUNT(*) AS total_calls,
SUM(DATEDIFF(MINUTE, c_time_in, c_time_out)) AS total_call_minutes
FROM t_call_log
WHERE c_time_out IS NOT NULL
AND c_time_in BETWEEN #time_from AND #time_to
GROUP BY caller_id
) AS l
ON l.actress_id = a.a_id
;
It could be five subqueries if you split the m subquery into two by joining separately on call_log_id and on m_to_call_log_id (and thus potentially giving the query planner more room for optimisation), i.e. instead of
LEFT JOIN (
SELECT
l.caller_id,
COUNT(CASE WHEN m. call_log_id = l1.c_id AND m.m_type = 2 THEN 1 END) AS pmsg_sent,
COUNT(CASE WHEN m.m_to_call_log_id = l2.c_id AND m.m_type = 2 THEN 1 END) AS pmsg_received,
COUNT(CASE WHEN m. call_log_id = l3.c_id AND m.m_type = 1 THEN 1 END) AS lcmsg_sent,
COUNT(CASE WHEN m.m_to_call_log_id = l4.c_id AND m.m_type = 1 THEN 1 END) AS lcmsg_received
FROM t_message AS m
JOIN t_call_log AS l ON l.c_id IN (m.call_log_id, m.m_to_call_log_id)
WHERE m.m_type IN (1, 2)
AND m.m_time BETWEEN #time_from AND #time_to
GROUP BY l.caller_id
) AS m
ON m.caller_id = c.c_id
it would be
LEFT JOIN (
SELECT
l.caller_id,
COUNT(CASE WHEN m.call_log_id = l1.c_id AND m.m_type = 2 THEN 1 END) AS pmsg_sent,
COUNT(CASE WHEN m.call_log_id = l3.c_id AND m.m_type = 1 THEN 1 END) AS lcmsg_sent,
FROM t_message AS m
JOIN t_call_log AS l ON l.c_id = m.call_log_id
WHERE m.m_type IN (1, 2)
AND m.m_time BETWEEN #time_from AND #time_to
GROUP BY l.caller_id
) AS mf
ON mf.caller_id = c.c_id
LEFT JOIN (
SELECT
l.caller_id,
COUNT(CASE WHEN m.m_to_call_log_id = l2.c_id AND m.m_type = 2 THEN 1 END) AS pmsg_received,
COUNT(CASE WHEN m.m_to_call_log_id = l4.c_id AND m.m_type = 1 THEN 1 END) AS lcmsg_received
FROM t_message AS m
JOIN t_call_log AS l ON l.c_id = m.m_to_call_log_id
WHERE m.m_type IN (1, 2)
AND m.m_time BETWEEN #time_from AND #time_to
GROUP BY l.caller_id
) AS mt
ON mt.caller_id = c.c_id
changing also the corresponding references in the main SELECT clause.
I'm not sure whether which of the variations is better, you'll need to test both to find out.
Note that I've omitted the main query's GROUP BY clause. It seems unnecessary both in your query and in mine, because, as far as I can see, it includes primary keys from both t_actress and t_caller and those combinations would be unique anyway. I assume that the GROUP BY is a leftover from your previous attempts at rewriting the query using joins.
thanks for your reply. I tried your way, it is still slow. here is what I did and finally works. I basically put all the sub queries into table, and then join the table at the end. not sure why, but it is faster: here is the code;
-- total calls
declare #t_call table(
a_id bigint,
total_calls bigint,
total_call_minutes bigint
);
insert into #t_call
SELECT a_id, COUNT(1) AS total_calls, isnull(SUM(DATEDIFF(MINUTE, c_time_in, c_time_out)),0) AS total_call_minutes
FROM t_actress aa
join t_call_log l on aa.caller_id = l.caller_id and c_time_in BETWEEN #time_from AND #time_to and c_time_out IS NOT NULL
GROUP BY a_id;
-- total live minutes
declare #t_live table(
a_id bigint,
live_calls bigint,
live_call_minutes bigint
);
insert into #t_live
SELECT a_id, COUNT(*) AS live_calls, isnull(SUM(alm_minutes),0) AS live_call_minutes
FROM t_actress a
join t_actress_live_minute alm on alm.actress_id = a.a_id and alm_time BETWEEN #time_from AND #time_to
GROUP BY a_id
-- total message by caller
declare #t_cm table(
caller_id bigint,
pmsg_sent bigint,
pmsg_received bigint,
lcmsg_sent bigint,
lcmsg_received bigint
)
insert into #t_cm
SELECT l.caller_id,
COUNT(CASE WHEN m.call_log_id = l.c_id AND m.m_type = 2 THEN 1 END) AS _pmsg_sent,
COUNT(CASE WHEN m.m_to_call_log_id = l.c_id AND m.m_type = 2 THEN 1 END) AS _pmsg_received,
COUNT(CASE WHEN m.call_log_id = l.c_id AND m.m_type = 1 THEN 1 END) AS _lcmsg_sent,
COUNT(CASE WHEN m.m_to_call_log_id = l.c_id AND m.m_type = 1 THEN 1 END) AS _lcmsg_received
FROM t_message m
join t_call_log l on l.c_id in (m.call_log_id, m.m_to_call_log_id)
where m.m_time BETWEEN #time_from AND #time_to
GROUP BY l.caller_id
-- total message by actress
declare #t_msg table(
a_id bigint,
pmsg_sent bigint,
pmsg_received bigint,
lcmsg_sent bigint,
lcmsg_received bigint
)
insert into #t_msg
select a_id, isnull(SUM(cm.pmsg_sent),0), isnull(SUM(cm.pmsg_received),0), isnull(SUM(cm.lcmsg_sent),0), isnull(SUM(cm.lcmsg_received),0)
from t_actress a
join #t_cm cm on a.caller_id = cm.caller_id
group by a_id
-- total credit
declare #t_credit table(
a_id bigint,
credit money,
paid_credit money
)
insert into #t_credit
SELECT a_id, isnull(SUM(ac.ac_amount),0) AS credit, isnull(SUM(CASE ac.ac_is_paid WHEN 1 THEN ac.ac_amount else 0 END),0) AS paid_credit
FROM t_actress a
join t_actress_credit ac on ac.actress_id = a.a_id AND ac.ac_time BETWEEN #time_from AND #time_to
JOIN t_order o ON o.o_id = ac.order_id and o_status = 1
GROUP BY a_id
-- the report
select a.a_id, cl.c_id, cl.c_chat_line_id, a.a_first_name, a.a_last_name,
isnull(ac.credit,0) credit, isnull(ac.paid_credit,0) paid_credit,
isnull(m.pmsg_sent,0) pmsg_sent, isnull(m.pmsg_received,0) pmsg_received, isnull(m.lcmsg_sent,0) lcmsg_sent, isnull(m.lcmsg_received,0) lcmsg_received,
isnull(l.live_calls,0) live_calls, isnull(l.live_call_minutes,0) live_call_minutes,
isnull(c.total_calls,0) total_calls, isnull(c.total_call_minutes,0) total_call_minutes
from t_actress a
join t_caller cl on cl.c_id = a.caller_id
left outer join #t_call c on c.a_id = a.a_id
left outer join #t_live l on l.a_id = a.a_id
left outer join #t_msg m on m.a_id = a.a_id
left outer join #t_credit ac on ac.a_id = a.a_id
order by a_id