Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Need help in writing a SELECT statement with CASE and SUM function

$
0
0

Dear Experts,

I am a newbie and trying to learn sql queries.

The following query does not work, I know there is something wrong with the syntax.

Please help me in writing this query better.

Thanks in advance.

SET @QDate = 'Jun' + ' ' + '2013'
SELECT st.ID, st.FirstName, st.LastName,
    CASE 
WHEN sf.SubFee <= 0 
THEN 
CASE 
WHEN ss.JoinDate < DATEADD(MONTH, DATEDIFF(MONTH, 0, @QDate),0)+10 
THEN su.DefaultSubFee 
WHEN ss.JoinDate > DATEADD(MONTH, DATEDIFF(MONTH, 0, @QDate),0)+10 AND ss.JoinDate < DATEADD(MONTH, DATEDIFF(MONTH, 0, @QDate),0)+20 
THEN su.DefaultSubFee*0.5
ELSE 0
END 
WHEN sf.SubFee > 0 
THEN 
CASE 
WHEN ss.JoinDate < DATEADD(MONTH, DATEDIFF(MONTH, 0, @QDate),0)+10 
THEN sf.SubFee 
WHEN ss.JoinDate > DATEADD(MONTH, DATEDIFF(MONTH, 0, @QDate),0)+10 AND ss.JoinDate < DATEADD(MONTH, DATEDIFF(MONTH, 0, @QDate),0)+20 
THEN sf.SubFee*0.5
ELSE 0
END
ELSE 0
END [dd], SUM(dd+sf.RegFee) as total


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>