简体   繁体   中英

SQL Server 2012 - Best way to assign incrementing number for differing sets of records, from a specific value

Any code warriors out there? :) I am kindly requesting for expertise in the best way (elegant?) of doing this. Cheers!

Booking system scenario. I basically need to add incoming new records to a Master bookings table. The Master bookings table has a incrementing number ( Sequence_Number ), per set of bookings, per customer.

The Incoming New Bookings, if they match a customer already in the MasterBookings , need to have this Sequence_Number start from the last available number +1 and increment for all the new incoming bookings in that set. And this is per customer. There could new customers too in the new bookings which the sequence_number would have to start at 1.

eg:-

[MasterBookings] :

CustomerNo   Sequence_Number     ExternalBookingID
---------------------------------------------------
0000001      1                   1234
0000001      2                   1285
0000001      3                   1324

0000002      1                   1534
0000002      2                   1585
0000002      3                   1524
0000002      4                   1534

0000003      1                   1734
0000003      13                  1785

[NewBookings] :

CustomerNo    ExternalBookingID
--------------------------------
0000001       8776
0000001       8787
0000002       8888
0000003       8998
0000004       9000
0000004       9001
0000004       9002
0000005       9211

I need to add the [NewBookings] into the [MasterBookings] , and they require the sequence_number field to be calculated and incremented

The final [MasterBookings] table after processing should contain this:

[MasterBookings] :

CustomerNo   Sequence_Number     ExternalBookingID
---------------------------------------------------
0000001      1                   1234
0000001      2                   1285
0000001      3                   1324
0000001      4                   8776
0000001      5                   8787

0000002      1                   1534
0000002      2                   1585
0000002      3                   1524
0000002      4                   1534
0000002      5                   8888

0000003      1                   1734
0000003      13                  1785
0000003      14                  8998

0000004      1                   9000
0000004      2                   9001
0000004      3                   9002

0000005      1                   9211

Thanks in advance.

Hello Guys, can I request a revision to the answer to to this scenario, as I have now been informed that the [NewBookingsTable] can contain previous bookings! So in essence, all bookings including historical and new ones can be in there.

Also, the [MasterBookings] can contain records that were not entered from the [NewBookingsTable] table.

Example, using the first Customer:-

eg:-

[MasterBookings] :

CustomerNo   Sequence_Number     ExternalBookingID
---------------------------------------------------
0000001      1                   1234
0000001      2                   1285
0000001      3                   1324
0000001      4                   1367  <-- booking made from another source, not in the [NewBookings]
0000001      5                   1377  <-- booking made from another source ,not in the [NewBookings]

[NewBookings] :

CustomerNo    ExternalBookingID
--------------------------------

0000001       1234
0000001       1285
0000001       1324

0000001       8776      <-- a new booking not in the [MasterBookings]
0000001       8787      <-- a new booking not in the [MasterBookings]

I need to add the [NewBookings] into the [MasterBookings] , and they require the sequence_number field to be calculated and incremented

The final [MasterBookings] table after processing should contain this:

[MasterBookings] :

CustomerNo   Sequence_Number     ExternalBookingID
---------------------------------------------------
0000001      1                   1234
0000001      2                   1285
0000001      3                   1324

0000001      4                   1367
0000001      5                   1377

0000001      6                   8776
0000001      7                   8787

It still has to calculate per Set, per Customer. I have only used the first CustomerNo as an example of this expanded scenario.

Thanks as always!

Use the row_number analytic function:

insert into MasterBookings (CustomerNo, Sequence_Number, ExternalBookingID)
select 
  CustomerNo,
  ExternalBookingID,
  row_number() over (partition by CustomerNo order by ExternalBookingID) +
  (
    select coalesce(max(Sequence_Number), 0)
    from MasterBookings
    where MasterBookings.CustomerNo = NewBookings.CustomerNo
  ) as Sequence_Number
from NewBookings;

The SQL fiddle: http://www.sqlfiddle.com/#!6/9aa9ef/7 .

EDIT: You changed your request such as that NewBookings can contain bookins already present in MasterBookings. To solve this is very easy: Simply exclude records that exist in MasterBookings, using the EXISTS clause:

insert into MasterBookings (CustomerNo, ExternalBookingID, Sequence_Number)
select 
  CustomerNo,
  ExternalBookingID,
  row_number() over (partition by CustomerNo order by ExternalBookingID) +
  (
    select coalesce(max(Sequence_Number), 0)
    from MasterBookings
    where MasterBookings.CustomerNo = NewBookings.CustomerNo
  ) as Sequence_Number
from NewBookings
where not exists
(
  select *
  from MasterBookings
  where MasterBookings.CustomerNo = NewBookings.CustomerNo 
  and MasterBookings.ExternalBookingID = NewBookings.ExternalBookingID
);

Fiddle: http://www.sqlfiddle.com/#!6/ede49/2 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM