How to get Count or Total Record with CTE (Common Table Exression)

Anonymous
2022-10-24T21:42:07.743+00:00

i have a query with a cte and many subqueries i need to check and verify that the total records of the whole query and verify the results are correct.

with cte_x1 as (  
       select cs1.intProp, cs1.intCustNumb, cs1.strSpace,cs1.dtMoveIn,cs1.dtEffStartDW,cs1.dtEffEndDW,cs1.file_ingest_date  
                ,cs1.SK -- renaming sk to dimcustomerspacessk is the reason why line 22 is throwing an error  
                ,concat(coalesce(c1.strFName,""),coalesce(c1.strLName, "")) as fullName  
               
                ,c1.SK as dimCustomerSK  
                ,c1.strFName, c1.strLName  
        from gold.dimCustomerSpacesscd2 cs1  
         
        inner join gold.dimCustomer c1  
          on cs1.intProp = c1.intProp  
         and cs1.intCustNumb = c1.intCustNumb   
        -- and cs1.active = true   
         and c1.active = true  
           
    )      
    select a1.*  
        ,dd.DateKey as dimDateSK  
        --,s1.SK as dimPropSK   
        ,coalesce(cds1.SK,fds1.SK)as dimPropertiesSK  
        ,coalesce(ca2.SK,fa2.SK) as dimCustomerSpacesSK  
        ,ca2.dimCustomerSK  
         
    from  
    (  
        select  va2.*  
            ,concat(case va2.VisitorFirstName when 'na' then "" else coalesce(va2.VisitorFirstName, "") end  
            ,coalesce(va2.VisitorLastName, " ")) as visitorName  
             
        from silver.VisitorActivity va2  
    ) a1  
    --left outer join cte_x1 a2  
    --on a1.intProp = a2.intProp   
    --and a1.unitAccessAreaName = a2.strSpace    
    --and a1.VisitorName = a2.fullName  
      
     left outer join cte_x1 ca2  
    on a1.intProp = ca2.intProp   
    and a1.unitAccessAreaName = ca2.strSpace    
    and a1.VisitorName = ca2.fullName  
    and a1.createdOn between ca2.dtEffStartDw and coalesce(ca2.dtEffEndDW,ca2.file_ingest_date)  
      
     left outer join cte_x1 fa2  
    on a1.intProp = fa2.intProp   
    and a1.unitAccessAreaName = fa2.strSpace    
    and a1.VisitorName = fa2.fullName  
    and a1.file_ingest_date between fa2.dtEffStartDw and coalesce(fa2.dtEffEndDW,fa2.file_ingest_date)  
     
    --left outer join gold.dimproperties s1   
    --on s1.intProp = a1.intProp  
   --- and s1.active = true  
   left outer join gold.dimpropertiesscd2 cds1   
    on cds1.intProp = a1.intProp  
   and a1.createdOn between cds1.dtEffStartDw and coalesce(cds1.dtEffEndDW, cds1.file_ingest_date)  
     
   left outer join gold.dimpropertiesscd2 fds1   
    on fds1.intProp = a1.intProp  
   and a1.file_ingest_date between fds1.dtEffStartDw and coalesce(fds1.dtEffEndDW, fds1.file_ingest_date)  
       
    left outer join gold.dimdate dd  
    on a1.CreatedOn_fixed = dd.Date  
      
  
      
Azure SQL Database
Developer technologies | Transact-SQL
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.