sql - How to merge two or more queries with different where conditions? I have to reuse the code which is being used in 1st where code -


---below query gives customers fact irrespective of condition

select     count( dbo.fact_promotion.customerid) mailquantity         dbo.fact_promotion    inner join dbo.dim_promotion       on dbo.fact_promotion.promotionid = dbo.dim_promotion.promotionid  

---below query gives customers condition

select     count(distinct fact_loan.customerid) [new loans] ,avg(fact_loan.financeamount) [avg new loan amount]         dbo.fact_promotion   <condition>        ,  dbo.fact_loan.loantypeid = 6        , dbo.fact_loan.accountstatusid = 1 

----below query gives customers different condition

select     count(distinct fact_loan.customerid) [total loans],avg(fact_loan.financeamount) [avg total loan amount]         dbo.fact_promotion   <condition>     , dbo.fact_loan.accountstatusid = 1 

i'm not sure question trying achieve.

the where clause in second query appears deliver subset of data where clause in third query. both where statements identical exception second query (new loans) includes condition loantypeid (presumably financial product have taken) 6. guess latest loan product or campaign.

without knowing you're trying it's difficult give answer if want show total number of customers loantypeid aggregate count adding loantypeid column select statement , adding group dbo.fact_loan.loantypeid end of statement.

this may not straight forward as you're doing other stuff in select (such distinct , avg) without knowing end goal is, it's difficult answer question.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -