Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,
Please help to solve Below Query

SQL
select SUM(currenttotalreps) totalreps
from (
Select isnull(dateadd(day,-day(coalesce(c.originaldate,c.reconciliationdaterun))+1,
CAST(DATEDIFF(DAY, 0, cast(COALESCE (c.OriginalDate,c.ReconciliationDateRun) AS datetime)) AS datetime)),'1/1/1900')AS AsOfDate
, Territory = (
SELECT DISTINCT RepID
                ,  HR.TerritoryNum TerritoryID
                ,   HR.DateFrom DateFrom
                , DateTo = MAX( CASE WHEN HR.DateTo = '1/1/1900' THEN GETDATE() ELSE ISNULL(HR.DateTo, GETDATE()) END )
             FROM HistoryRepTerritory HR
             WHERE EXISTS (SELECT 1 FROM TerritoryHierarchy inner join TerritoryHierarchy t on HR.TerritoryNum= t.Id)
             group BY HR.TerritoryNum, RepID, HR.DateFrom)
,r.active,currenttotalreps = 1
            , RepsWithNoResponse =CASE WHEN C.RepID IS NULL THEN 1 ELSE 0 end
, ReconciledToZero  = CASE WHEN  c.FinalStatus = 'Reconciled to Zero' AND C.RepID IS NOT NULL  THEN 1 ELSE 0 END
, ReconciledToThreshold = CASE WHEN  c.FinalStatus = 'Reconciled Within Allowable Threshold' AND C.RepID IS NOT NULL THEN 1 ELSE 0 END
, ReconciledToProductGroup= CASE WHEN  c.FinalStatus = 'Reconciled By Product Group' AND C.RepID IS NOT NULL THEN 1 ELSE 0 END
, NotReconciled = CASE WHEN  c.FinalStatus = 'UnReconciled' AND C.RepID IS NOT NULL THEN 1 ELSE 0 END
, Negative = CASE WHEN  c.FinalStatus = 'UnReconciled with Negative Variance' THEN 1 ELSE 0 END
, Incomplete = CASE WHEN c.FinalStatusCompliance = 0 AND c.FinalStatus IS NULL  AND C.RepID IS NOT NULL THEN 1 ELSE 0 END
FROM dbo.Reps r (NOLOCK)
INNER JOIN dbo.Auxiliary_SARoster SR (NOLOCK) ON R.RepID = SR.RepID
inner JOIN  dbo.Auxiliary_ReconciliationCheckList C (NOLOCK) ON R.RepID = C.RepID
where CAST(DATEDIFF(DAY, 0, cast(COALESCE (c.OriginalDate,c.ReconciliationDateRun,'1/1/1900') AS datetime)) AS datetime)
between '1/1/2014' and '12/31/2014') sq
where exists(SELECT 1 FROM TerritoryHierarchy inner join TerritoryHierarchy t on sq.territory= t.Id)


Error Message: Msg 116, Level 16, State 1, Line 13
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
Posted
Comments
Corporal Agarn 24-Feb-15 9:17am    
I would suggest breaking this down to the various parts and seeing what you get. RyanDev is correct that you cannot define a single value as a list. You would need to rewrite your query to use a rollup of the value.

1 solution

It's hard to read your code but this part is a problem:

SQL
Territory = (
SELECT DISTINCT RepID
                ,  HR.TerritoryNum TerritoryID
                ,   HR.DateFrom DateFrom
                , DateTo = MAX(


You are saying Territory is equal to a long list of columns.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900