troubleshooting Question

add select count in query

Avatar of Seven price
Seven priceFlag for United States of America asked on
Microsoft SQL ServerMicrosoft SQL Server 2005
9 Comments6 Solutions321 ViewsLast Modified:



;with tempPaged AS

(
 select *
      , row_number()
           over(partition by Grouper order by ItemID desc)AS rn2  
,ROW_NUMBER() over (order by itemid) as totrow      
 from DistributionCart_Temp A
where  Dept = Dept  And A.UserId = @UserId
)
, t
as
(
select *
   , CASE rn2
        WHEN 1
           THEN convert(varchar(20), Loyal)
        ELSE '' end as Loyal3
   ,CASE when    Loyalt>'0'
       
from tempPaged
where Dept = 'Grocery' And totrow >= @Lower  AND totrow <= @Upper
)
select *
    , coalesce(nullif(a.Loyal3, '0'), coalesce(b.Loyalt3, '0')) as Loyal3
from t as a
left outer join t as b on b.Grouper = a.Grouper and b.rn2 = a.rn2

order by a.ItemID, a.rn2 asc

#################################################
I would like to add
(SELECT COUNT(*) As to3 FROM Distrib where Dept = Dept  )
to

select *
   , CASE rn2
        WHEN 1
           THEN convert(varchar(20), Loyal)
        ELSE '' end as Loyal3
   ,CASE when    Loyalt>'0'
       
from tempPaged
where Dept = 'Grocery' And totrow >= @Lower  AND totrow <= @Upper

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 6 Answers and 9 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 6 Answers and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros