Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
INSERT INTO #SRESULTS(ZZBAND,ZZTAXBAND,ZZRATE,ZZANNUAL,ZZMONTHLY,ZZMAXTAX)
SELECT DISTINCT ZBAND,
ZTAXBAND,
ZRATE,
ZANNUAL,
ZANNUAL/12 AS ZMONTHLY,
(ZANNUAL/12-(ZANNUAL-ZTAXBAND)/12)*ZRATE/100 AS ZMAXTAX
FROM #FRESULTS


---------------------------------------------------------------------------------------------------------

1. Want the first record computation of the undernoted 'ZMAXTAX' to equal ZERO how do I achieve it.



(ZANNUAL/12-(ZANNUAL-ZTAXBAND)/12)*ZRATE/100 AS ZMAXTAX

What I have tried:

This is an ongoing Select statement challenge
Posted
Updated 3-Aug-19 7:08am
Comments
OriginalGriff 29-Jul-19 6:51am    
And?
What have you tried?
Where are you stuck?
What help do you need?
Wendelius 29-Jul-19 7:19am    
How do you define the first record? First based on what?

1 solution

As @Wendelius has pointed out, there is no concept of a "first record" unless you have some means of identifying the ORDER in which the records should be interpreted.

As you have no obvious auto-increment, nor a definition of "first" I have assumed that the records will appear in ZBAND + ZTAXBAND order to generate a row number. I can then use that row number in a CASE statement similar to this
SQL
;WITH CTE AS
 (
	SELECT DISTINCT ROW_NUMBER() OVER (ORDER BY ZBAND, ZTAXBAND) AS RN,
	ZBAND,
	ZTAXBAND,
	ZRATE,
	ZANNUAL,
	ZANNUAL/12 AS ZMONTHLY,
	(ZANNUAL/12-(ZANNUAL-ZTAXBAND)/12)*ZRATE/100 AS ZMAXTAX 
	FROM #FRESULTS 
)
INSERT INTO #SRESULTS(ZZBAND,ZZTAXBAND,ZZRATE,ZZANNUAL,ZZMONTHLY,ZZMAXTAX)
SELECT 
	ZBAND,
	ZTAXBAND,
	ZRATE,
	ZANNUAL,
	ZANNUAL/12 AS ZMONTHLY,
	CASE WHEN RN = 1 THEN 0 ELSE (ZANNUAL/12-(ZANNUAL-ZTAXBAND)/12)*ZRATE/100 END AS ZMAXTAX 
	FROM CTE
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900