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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Distributor count

Author  Topic 

pvaru
Starting Member

5 Posts

Posted - 2013-03-02 : 00:01:25
distcode YTD (YTD Desc order)
D101 4000
D102 2000
D103 500

90% sales
ie., sum(YTD)*90% : 6500*90%=5850

I 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 5850

result is count(Distcode)=2

is 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 DistCount
FROM (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
)t
WHERE RunningTotal <= Sales

--
Chandu
Go to Top of Page

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 DistCount
FROM (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
)t
WHERE RunningTotal <= Sales

--
Chandu



will not work always

see this sample data

distcode YTD (YTD Desc order)
D101 4000
D102 1850
D103 300
D104 350


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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 ALL
SELECT 'D102', 1850 UNION ALL
SELECT 'D101', 300 UNION ALL
SELECT 'D104', 350

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

;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 DistCount
FROM CTE t
WHERE RunningTotal < Sales[/code]

--
Chandu
Go to Top of Page

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 ALL
SELECT 'D102', 1000 UNION ALL
SELECT 'D101', 900 UNION ALL
SELECT 'D106', 550 UNION ALL
SELECT 'D107', 200 UNION ALL
SELECT 'D105', 250 UNION ALL
SELECT 'D104',150


--Bandi solution
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

;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 DistCount
FROM CTE t
WHERE 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 DistCount
FROM CTE c1
OUTER APPLY (SELECT SUM(YTD) AS RunningTotal FROM CTE WHERE RN < c1.RN) c2
WHERE COALESCE(RunningTotal,0)<= [90PC]
AND COALESCE(RunningTotal,0) + YTD > [90PC]

output
-----------------------------------------
distcode YTD RunningTotal Sales
D103 4000 4000 6345.0
D102 1000 5000 6345.0
D101 900 5900 6345.0
D106 550 6450 6345.0
D107 200 6900 6345.0
D105 250 6700 6345.0
D104 150 7050 6345.0

Bandi output
-------------------------------
4

Visakh Output
-------------------------------
6




------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -