April 25, 2014 at 1:19 pm
I have a table that has field called total amount. And they have different amounts ranging from 1 mil to 30 mil….all u have to do is output in such a way that not two subsequent lines can have one line with over 10 mil
So for example if u have data like this: 30 mil, 20mil, 2 mil, 3 mil
U should out put like this
30mil, 2 mil, 20 mil, 3 mil (note that for every line that has a exceeding 10 mil, the next line is less than 10 mil)
How can I accomplish this?
April 25, 2014 at 1:31 pm
Please post DDL and sample data in a consumable format.
April 25, 2014 at 1:35 pm
Just attached.
April 25, 2014 at 4:06 pm
Maybe you could use something like this:
SELECT TotalAmt
FROM (
SELECT TotalAmt
,CAST( ROW_NUMBER() OVER(ORDER BY TotalAmt) AS decimal(8,1)) rn
FROM #temp
WHERE TotalAmt >= 10
UNION ALL
SELECT TotalAmt
,CAST( ROW_NUMBER() OVER(ORDER BY TotalAmt) AS decimal(8,1)) + .5 rn
FROM #temp
WHERE TotalAmt < 10)x
ORDER BY rn
April 25, 2014 at 7:35 pm
What would you want in the following case?
drop table #temp
CREATE TABLE #temp
(
TotalAmt DECIMAL(10,0)
)
INSERT INTO #temp(TotalAmt) VALUES(30);
INSERT INTO #temp(TotalAmt) VALUES(40);
INSERT INTO #temp(TotalAmt) VALUES(60);
INSERT INTO #temp(TotalAmt) VALUES(30);
INSERT INTO #temp(TotalAmt) VALUES(40);
INSERT INTO #temp(TotalAmt) VALUES(60);
SELECT * FROM #temp
It will happen. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply