Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
pvaru
Starting Member
5 Posts |
Posted - 2013-03-02 : 00:01:25
|
distcode YTD (YTD Desc order)D101 4000 D102 2000D103 50090% salesie., sum(YTD)*90% : 6500*90%=5850I want to find out for 5850, count(distcode) contribution (ie.d how many distributor summing YTD comes to 5850 from YTD desc order ,we have to consider)If i add 2 disttributor YTD then it comes near to 5850result is count(Distcode)=2is it possible write sql statement |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-03-05 : 00:56:25
|
DECLARE @tab TABLE(distcode VARCHAR(5), YTD INT)INSERT INTO @tab VALUES('D101', 4000 ), ('D102', 2000), ('D103', 500)SELECT COUNT(distcode)+1 DistCountFROM (SELECT a.distcode, a.YTD, (SELECT SUM(b.YTD) FROM @tab b WHERE b.distcode <= a.distcode) RunningTotal, 0.9*SUM(YTD) OVER() Sales FROM @tab a )tWHERE RunningTotal <= Sales--Chandu |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-05 : 01:03:19
|
quote: Originally posted by bandi DECLARE @tab TABLE(distcode VARCHAR(5), YTD INT)INSERT INTO @tab VALUES('D101', 4000 ), ('D102', 2000), ('D103', 500)SELECT COUNT(distcode)+1 DistCountFROM (SELECT a.distcode, a.YTD, (SELECT SUM(b.YTD) FROM @tab b WHERE b.distcode <= a.distcode) RunningTotal, 0.9*SUM(YTD) OVER() Sales FROM @tab a )tWHERE RunningTotal <= Sales--Chandu
will not work alwayssee this sample datadistcode YTD (YTD Desc order)D101 4000 D102 1850D103 300D104 350 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-03-05 : 01:22:08
|
[code]DECLARE @tab TABLE(distcode VARCHAR(5), YTD INT)INSERT INTO @tab --VALUES('D101', 4000 ), ('D102', 2000), ('D103', 500)SELECT 'D103', 4000 UNION ALLSELECT 'D102', 1850 UNION ALLSELECT 'D101', 300 UNION ALLSELECT 'D104', 350SELECT a.distcode, a.YTD, (SELECT SUM(b.YTD) FROM @tab b WHERE b.YTD >= a.YTD) RunningTotal, 0.9*SUM(YTD) OVER() SalesFROM @tab a;With CTE AS( SELECT a.distcode, a.YTD, (SELECT SUM(b.YTD) FROM @tab b WHERE b.YTD >= a.YTD) RunningTotal, 0.9*SUM(YTD) OVER() Sales FROM @tab a)SELECT COUNT(distcode)+1 DistCountFROM CTE tWHERE RunningTotal < Sales[/code]--Chandu |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-05 : 01:56:07
|
not again correct as its not considering the order of distcode DECLARE @tab TABLE(distcode VARCHAR(5), YTD INT)INSERT INTO @tab --VALUES('D101', 4000 ), ('D102', 2000), ('D103', 500)SELECT 'D103', 4000 UNION ALLSELECT 'D102', 1000 UNION ALLSELECT 'D101', 900 UNION ALLSELECT 'D106', 550 UNION ALLSELECT 'D107', 200 UNION ALLSELECT 'D105', 250 UNION ALLSELECT 'D104',150--Bandi solutionSELECT a.distcode, a.YTD, (SELECT SUM(b.YTD) FROM @tab b WHERE b.YTD >= a.YTD) RunningTotal, 0.9*SUM(YTD) OVER() SalesFROM @tab a;With CTE AS( SELECT a.distcode, a.YTD, (SELECT SUM(b.YTD) FROM @tab b WHERE b.YTD >= a.YTD) RunningTotal, 0.9*SUM(YTD) OVER() Sales FROM @tab a)SELECT COUNT(distcode)+1 DistCountFROM CTE tWHERE RunningTotal < Sales--Visakh Solution;With CTE AS(SELECT a.distcode, a.YTD, 0.9*SUM(YTD) OVER() AS [90PC],ROW_NUMBER() OVER (ORDER BY distcode) AS RN FROM @tab a )SELECT RN AS DistCountFROM CTE c1OUTER APPLY (SELECT SUM(YTD) AS RunningTotal FROM CTE WHERE RN < c1.RN) c2WHERE COALESCE(RunningTotal,0)<= [90PC]AND COALESCE(RunningTotal,0) + YTD > [90PC]output-----------------------------------------distcode YTD RunningTotal SalesD103 4000 4000 6345.0D102 1000 5000 6345.0D101 900 5900 6345.0D106 550 6450 6345.0D107 200 6900 6345.0D105 250 6700 6345.0D104 150 7050 6345.0Bandi output-------------------------------4Visakh Output-------------------------------6 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|
|
|