January 11, 2006 at 1:27 pm
IF # records returned is > 1 then
SUM(rmstranamt) AS rmstranamt10
ELSE
rmstranamt AS rmstranamt10
rmstranamt AS rmstranamt10 <-----If statement goes here base on if the amount of records found in select is >1 or not
FROM RFINANL
WHERE RMSTRANCDE = '10'
GROUP BY RMSFILENUM, rmstranamt
) AS rf10 ON rf10.RMSFILENUM = rm.RMSFILENUM
In other words if this:
SELECT RMSFILENUM,
rmstranamt AS rmstranamt10
FROM RFINANL
WHERE RMSTRANCDE = '10'
GROUP BY RMSFILENUM, rmstranamt
produces > 1 records, then I don't want to return rmstranamt AS rmstranamt10, I want to return sum(rmstranamt) AS rmstranamt10
January 11, 2006 at 2:25 pm
If number of records = 1 then sum(rmstranamt) = rmstranamt.
So just use sum(rmstranamt).
_____________
Code for TallyGenerator
January 11, 2006 at 2:50 pm
try this in place of your code.
SELECT RMSFILENUM,
sum(rmstranamt) AS rmstranamt10 1 or not
FROM RFINANL
WHERE RMSTRANCDE = '10'
GROUP BY RMSFILENUM
January 12, 2006 at 5:27 am
Hi, I can´t understand what are you trying to do... if your query returns just a single record sum(x) = x because there is JUST ONE record... so, you doesn´t need to check for the number of records... also, to return just de field and not the sum, you have to put it in the group clause...
Checking conditions into a query is well done using CASE clause... see BOL for info on that.
Good Luck
Nicolas Donadio
Software Team Leader
Mitrol SRL
Argentina
January 12, 2006 at 6:42 am
I'm also not sure what you are trying to do but if you have several records where RMSTRANCDE = '10' and you want to return 0 for zero records and return 1 for more than 1 record then try this
SELECT
RMSFILENUM,
CASE WHEN COUNT(*) > 1 THEN 1 ELSE 0 END AS rmstranamt10
FROM RFINANL
WHERE RMSTRANCDE = '10'
GROUP BY RMSFILENUM
January 12, 2006 at 6:47 am
SELECT RMSFILENUM, SUM(rmstranamt) AS [rmstranamt10]
FROM (SELECT RMSFILENUM, rmstranamt AS rmstranamt10
FROM RFINANL
WHERE RMSTRANCDE = '10'
GROUP BY RMSFILENUM, rmstranamt) x
GROUP BY RMSFILENUM
Far away is close at hand in the images of elsewhere.
Anon.
January 12, 2006 at 9:42 am
For my money I'd go with Sergiy - why bother to count # of rows - just sum(rmstranamt) should be adequate...
**ASCII stupid question, get a stupid ANSI !!!**
January 12, 2006 at 9:53 am
Unless it it the sum of unique rmstranamt's that is required
Far away is close at hand in the images of elsewhere.
Anon.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply