यहां एक ओरेकल स्टेटमेंट है जिसे मैंने पोस्ट करने से पहले तैयार किया था कि आप SQL-Server का उपयोग कर रहे थे, लेकिन यह आपको कुछ विचार दे सकता है, हालांकि आपको सीटीई और स्वयं-जुड़ने का उपयोग करके अपना खुद का अनुपात_to_ रिपोर्ट विश्लेषणात्मक कार्य करें। हम उत्पादों और क्लाइंट रूट टेबल में प्रत्येक प्रकार के संचयी अनुपात की गणना करते हैं और मेल खाने वाले अनुपात बैंड पर एक गैर इक्वि-जॉइन करते हैं। मेरे द्वारा उपयोग किए गए नमूना डेटा में कुछ राउंड-ऑफ हैं लेकिन ये बड़े डेटा सेट के लिए कम हो जाएंगे।
यहाँ सेटअप है:
create table products (id int not null primary key, "type" int not null, route varchar (20) null);
create table clienttable ("type" int not null, percent number (10, 2) not null, route varchar (20) not null);
insert into clienttable ("type", percent, route) values (1, 0.4, 'A');
insert into clienttable ("type", percent, route) values (1, 0.4, 'B');
insert into clienttable ("type", percent, route) values (1, 0.2, 'C');
insert into clienttable ("type", percent, route) values (2, 0.5, 'A');
insert into clienttable ("type", percent, route) values (2, 0.5, 'B');
insert into clienttable ("type", percent, route) values (3, 1.0, 'C');
insert into products (id, "type", route) values (1, 1, null);
insert into products (id, "type", route) values (2, 1, null);
insert into products (id, "type", route) values (3, 1, null);
insert into products (id, "type", route) values (4, 1, null);
insert into products (id, "type", route) values (5, 1, null);
insert into products (id, "type", route) values (6, 1, null);
insert into products (id, "type", route) values (7, 1, null);
-- 7 rows for product type 1 so we will expect 3 of route A, 3 of route B, 1 of route C (rounded)
insert into products (id, "type", route) values (8, 2, null);
insert into products (id, "type", route) values (9, 2, null);
insert into products (id, "type", route) values (10, 2, null);
insert into products (id, "type", route) values (11, 2, null);
insert into products (id, "type", route) values (12, 2, null);
-- 5 rows for product type 2 so we will expect 3 of route A and 2 of route B (rounded)
insert into products (id, "type", route) values (13, 3, null);
insert into products (id, "type", route) values (14, 3, null);
-- 2 rows for product type 3 so we will expect 2 of route C
और यहाँ बयान
select prods.id, prods."type", client.route cr from
(
select
p.id,
p."type",
row_number() over (partition by p."type" order by p.id)/count (*) over (partition by p."type") cum_ratio
from
products p
) prods
inner join
(
select "type", route, nvl (lag (cum_ratio, 1) over (partition by "type" order by route), 0) ratio_start, cum_ratio ratio_end from
(select "type", route, sum (rr) over (partition by "type" order by route) cum_ratio
from (select c."type", c.route, ratio_to_report (c.percent) over (partition by "type") rr from clienttable c))) client
on prods."type" = client."type"
and prods.cum_ratio >= client.ratio_start and prods.cum_ratio < client.ratio_end
यह निम्न परिणाम देता है: -
+----+------+----+
| ID | type | CR |
+----+------+----+
| 1 | 1 | A |
| 2 | 1 | A |
| 3 | 1 | B |
| 4 | 1 | B |
| 5 | 1 | B |
| 6 | 1 | C |
| 8 | 2 | A |
| 9 | 2 | A |
| 10 | 2 | B |
| 11 | 2 | B |
| 13 | 3 | C |
+----+------+----+
"प्रतिशत" अपनी सूची में नहीं बल्कि है भ्रामक अगर संख्या जो निम्नानुसार है वह वास्तव में प्रतिशत नहीं है । –
आप किस डीबीएमएस का उपयोग कर रहे हैं? साथ ही, क्या आप अपने वर्तमान समाधान के कोड या छद्म कोड पोस्ट कर सकते हैं? –
एसक्यूएल सर्वर 2008. और 0.4 प्रतिशत कैसे नहीं है? 40.0 बेहतर है? मुझे लगता है कि 0.4 बेहतर है क्योंकि 0.4 * गिनती (*) अद्यतन करने के लिए पंक्तियों की संख्या है। – powlette