How to get rows with No values in SQL Server? -


i have table holds financial transactions data per account.

**fintransmaster table:** ------------------------------------ |acctid  |fintrnscode|businessday  | ------------------------------------ |1234567 |intrst     |2017-09-09   | ------------------------------------ |1234567 |charge     |2017-09-08   | ------------------------------------ |1234567 |pymnt      |2017-09-01   | ------------------------------------ |1234567 |intrst     |2017-08-19   | ------------------------------------ |1234567 |intrst     |2017-08-09   | ------------------------------------ |1234567 |charge     |2017-08-04   | ------------------------------------ |1234567 |pymnt      |2017-08-01   | ------------------------------------ |1234567 |intrst     |2017-07-19   | ------------------------------------ 

i want select last time payment made on each account.

so code follows:

select acctid       ,[fintranscode]       --,max([businessday])  --tried line, doesn't work       --,isnull(max([businessday]), 'never paid') last_paid --tried line too, doesn't work       ,case when isnull(max([businessday]), 0) = 0 'never paid'             else max([businessday])        end last_paid    [geb_dwh].[dbo].[fintransmaster]    acctid = '1234567'     , fintranstypecode = 'pymt'    group acctid,[fintranstypecode] 

this code doesn't work because, accounts have never paid, ever. hence accounts don't have 'pymnt' code @ in field. have other codes in field no pymnt code in field. so, when run code acctid, headers , no results.

how show 'never paid' in field acctid can join results table?

what i'm trying get, joining acctid:

----------------------------------------------------------------- |acctid  |contactname|phone number |email address |last payment | ----------------------------------------------------------------- |1234567 |john doe   |123-123-1234 |test@123.com  |never paid   | ----------------------------------------------------------------- 

please try below:

--summary stats transaction type     select     acctid,     fintrnscode,     lastactivitydt = max(businessday)     #temp     fintransmaster      group acctid,fintrnscode      --find out customers pmts     select      distinct     acctid,     lastpmtdate = lastactivitydt     #temp2     #temp fintrnscode = 'pymnt'      select acctid,     lastpmtdate = cast(lastpmtdate varchar(50))  #temp2     union     --get customers no pmt records     select distinct acctid, 'never paid' #temp acctid not in (select distinct acctid #temp2)   

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -