Grouping numbers out from a larger group

122 views Asked by At

I'm running into some problems trying to understand how I can do this. Let's say I'm running a lottery. Some tickets, or bunches of tickets will sometimes turn out void, which will be marked by a void flag in my database. Each ticket also has an individual number, along with a transaction id.

For a report I would like to have these voided tickets split up into groups. These groups can be any size, be it numbers 1-2, or 100-560. Having these groups makes discarding these tickets much easier, rather than sifting through for individual numbers. For example, lets say I sell 1000 tickets, from 1-1000. Bunches 10-36, 100-164, and 276-340 are all void.

How can I get my report to display that as follows:

Lottery Name| Voided Ticket Series
Lucky 7     | 10-36
Lucky 7     | 100-164
Lucky 7     | 276-340

I have some tables in place, they have the following data structures. The void tickets table is as follows:

Ticket # | Transaction_ID | Seller_ID | 

and the transaction table:

Transaction_ID | Seller_ID | Asset_ID | Lottery_name
2

There are 2 answers

0
Tebbe On BEST ANSWER

One way to do it in Oracle, cribbed from here:

SQL> CREATE TABLE voided_tix (
  2      ticket_#        NUMBER
  3  ,   transaction_id  NUMBER
  4  ,   seller_id       NUMBER
  5  );

Table created.

SQL> CREATE TABLE transactions (
  2      transaction_id  NUMBER
  3  ,   seller_id       NUMBER
  4  ,   lottery_name    VARCHAR2(20)
  5  );

Table created.

SQL> INSERT INTO voided_tix
  2  SELECT CASE
  3         WHEN ROWNUM BETWEEN  1 AND  27 THEN ROWNUM + 9
  4         WHEN ROWNUM BETWEEN 28 AND  92 THEN ROWNUM + 72
  5         WHEN ROWNUM BETWEEN 93 AND 157 THEN ROWNUM + 183
  6         END
  7  ,      1000 + ROWNUM
  8  ,      12345678
  9  FROM   DUAL
 10  CONNECT BY LEVEL <= 157
 11  ;

157 rows created.

SQL> INSERT INTO transactions
  2  SELECT
  3      1000 + ROWNUM
  4  ,   12345678
  5  ,   'Lucky 7'
  6  FROM DUAL
  7  CONNECT BY LEVEL <= 200;

200 rows created.

SQL> COL n            FOR 99
SQL> COL lottery_name FOR A12
SQL> COL range        FOR A20
SQL> SELECT   ROW_NUMBER() OVER (ORDER BY b.grping) n
  2  ,        b.lottery_name
  3  ,        TO_CHAR(MIN(b.ticket_#))
  4           || DECODE(MIN(b.ticket_#)
  5              ,      MAX(b.ticket_#), NULL
  6              ,      '-' || MAX(b.ticket_#)) range
  7  FROM    (SELECT  a.ticket_#
  8           ,       MAX(a.grp)
  9                   OVER (PARTITION BY a.lottery_name
 10                         ,            a.seller_id
 11                         ORDER BY     a.ticket_#) grping
 12           ,       a.lottery_name
 13           FROM   (SELECT  vt.ticket_#
 14                   ,       CASE
 15                           WHEN vt.ticket_# - 1 <> NVL(LAG(vt.ticket_#)
 16                                                       OVER (PARTITION BY t.lottery_name
 17                                                             ,            vt.seller_id
 18                                                             ORDER BY     vt.ticket_#)
 19                                                   ,   vt.ticket_#)
 20                           THEN vt.ticket_#
 21                           END             grp
 22           ,       vt.seller_id
 23           ,       t.lottery_name
 24           FROM    voided_tix      vt
 25           ,       transactions    t
 26           WHERE   vt.seller_id      = t.seller_id
 27           AND     vt.transaction_id = t.transaction_id) a ) b
 28  GROUP BY b.grping
 29  ,        b.lottery_name
 30  ORDER BY b.grping
 31  ;

  N LOTTERY_NAME RANGE
--- ------------ --------------------
  1 Lucky 7      10-36
  2 Lucky 7      100-164
  3 Lucky 7      276-340

SQL>
1
kaj On

There is an example of how to find contiguous groups at https://stackoverflow.com/questions/5424095/efficiently-select-beginning-and-end-of-multiple-contiguous-ranges-in-

For a similar grouping idea using Oracle (but dates) - see http://code.cheesydesign.com/?p=695

I don't know specifically about Oracle syntax so as well as a CTE approach there is a more laborious way to get to the same result (SQL Server syntax) - hopefully one of these will give you enough to work on.

declare @low table
(
  groupId int identity(1,1),
  lowRangeId int,
  lowTicketNumber int
)

declare @high table
(
  groupId int identity(1,1),
  highRangeId int,
  highTicketNumber int
)


insert into @low (lowRangeId, lowTicketNumber)
select vdLow.transactionId, vdLow.ticketNumber
from @voidTickets vdLow
where not exists (select * from @voidTickets ml where ml.ticketNumber = vdLow.ticketNumber - 1)

insert into @high (highRangeId, highTicketNumber)
select vdHigh.transactionId, vdHigh.ticketNumber
from @voidTickets vdHigh
where not exists (select * from @voidTickets mh where mh.ticketNumber = vdHigh.ticketNumber + 1)

select tr.lotteryName, low.lowTicketNumber, high.highTicketNumber
from @transaction tr
  inner join @low low on low.lowRangeId = tr.transactionId
  inner join @high high on high.groupId = low.groupId