Question : add select count in query




;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

Answer : add select count in query

You could try
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
select *
   , CASE rn2
        WHEN 1
           THEN convert(varchar(20), Loyal)
        ELSE '' end as Loyal3
   ,CASE when    Loyalt>'0'
   ,count(*) over (partition by Dept)
        
from tempPaged
where Dept = 'Grocery' And totrow >= @Lower  AND totrow <= @Upper
Random Solutions  
 
programming4us programming4us