Ask a Question related to Microsoft SQL / MS SQL Server, Design and Development.

  1. #1

    Default Re: add serial number

    Jane,

    If you have a unique identifier within each group, e.g., col2 in the following schema:

    CREATE TABLE T1
    (
    col1 CHAR(1) NOT NULL,
    col2 INT NOT NULL,
    PRIMARY KEY(col1, col2)
    )

    INSERT INTO T1 VALUES('A', 10)
    INSERT INTO T1 VALUES('A', 20)
    INSERT INTO T1 VALUES('A', 30)
    INSERT INTO T1 VALUES('B', 5)
    INSERT INTO T1 VALUES('B', 40)
    INSERT INTO T1 VALUES('C', 7)
    INSERT INTO T1 VALUES('C', 13)
    INSERT INTO T1 VALUES('C', 99)

    You can use the following query:

    SELECT *,
    (SELECT COUNT(*)
    FROM T1 AS B
    WHERE B.col1 = A.col1
    AND B.col2 <= A.col2) AS pos
    FROM T1 AS A

    If you don't, you can always add one (IDENTITY column):

    ALTER TABLE T1 ADD keycol INT NOT NULL IDENTITY
    CREATE INDEX idx1 ON T1(col1, keycol)

    SELECT *,
    (SELECT COUNT(*)
    FROM T1 AS B
    WHERE B.col1 = A.col1
    AND B.keycol <= A.keycol) AS pos
    FROM T1 AS A

    If you don't and you'd rather not add a new column, create an auxiliary table of numbers:

    CREATE TABLE Nums(n INT NOT NULL PRIMARY KEY)
    DECLARE @i AS INT
    SET @i = 1
    BEGIN TRAN
    WHILE @i <= 1000 -- make sure enough for max num of rows in group
    BEGIN
    INSERT INTO Nums VALUES(@i)
    SET @i = @i + 1
    END
    COMMIT

    And use the following query:

    SELECT col1, n
    FROM (SELECT col1, COUNT(*) AS cnt
    FROM T1
    GROUP BY col1) AS T
    JOIN Nums
    ON n <= cnt

    --
    BG, SQL Server MVP
    Solid Quality Learning
    [url]www.solidqualitylearning.com[/url]


    "Jane" <janek@123.com> wrote in message news:025301c34636$ecd0c8b0$a101280a@phx.gbl...
    > This is the 3rd time I tried to post the same ??? on this
    > board. I posted the first one for more than 1.5 hrs. ago,
    > it has not shown up on the board.
    >
    > Question: How can I add number(in sequential order) based
    > on the values on another column. Whenever the value
    > changes again, the number need to restart as '1' again. It
    > will be something look like this:
    > COLUMN1: A A A B B C C C
    > NEW COL: 1 2 3 1 2 1 2 3
    >
    Itzik Ben-Gan Guest

  2. Similar Questions and Discussions

    1. MM Serial Number Verification
      We are installing CF on a LAN. If the LAN is connected to an internet server, CF remains at Enterprise edition after entering our serial number....
    2. serial number
      got PS elements on a disc that came with my wacom tablet. i no longer have the serial number that came with it...and i dotn ever remember having one....
    3. Serial Number?
      James, When I bought a Wacom tablet, a copy of PE1 was included. The serial number was on the sleeve which contained the CD. Byron
    4. Serial number not accepted
      I've a brand new copy of Photo Elements 2.0. When trying to install the software I type in the Photoshop Elements registration number. The message...
    5. serial number calculation
      Hi Joseph There are (at least) 3 possibilities: If you only need these values to display in a portal, just type @@ on the first portal row in...
  3. #2

    Default add serial number

    Hi,
    Elaborate ur question!!!

    Thanks,
    Bush.
    >-----Original Message-----
    >This is the 3rd time I tried to post the same ??? on this
    >board. I posted the first one for more than 1.5 hrs. ago,
    >it has not shown up on the board.
    >
    >Question: How can I add number(in sequential order) based
    >on the values on another column. Whenever the value
    >changes again, the number need to restart as '1' again.
    It
    >will be something look like this:
    >COLUMN1: A A A B B C C C
    >NEW COL: 1 2 3 1 2 1 2 3
    >
    >.
    >
    Bush Guest

  4. #3

    Default Re: add serial number

    Thank you very much!

    Since I don't have a primary key. I use your second method
    and IT WORKS!!!

    I wish MS can make this simple process as less 'painful'
    as it can be.
    >-----Original Message-----
    >Do you have a Primary Key on this table? If so:
    >
    >ALTER TABLE Sometable ADD newcol INTEGER NULL
    >
    >GO
    >UPDATE Sometable
    > SET newcol =
    > (SELECT COUNT(*)
    > FROM Sometable AS S
    > WHERE column1=Sometable.column1
    > AND keycol<=Sometable.keycol)
    >
    >
    >If not, I think you will have to create a new table as
    follows.
    >
    >Create a numbers table at least as big as the largest
    group of Column1
    >values:
    >
    >CREATE TABLE Numbers (num INTEGER PRIMARY KEY)
    >
    >WHILE (SELECT COALESCE(MAX(num),0) FROM Numbers) < 1000
    > INSERT INTO Numbers
    > SELECT COALESCE(MAX(num),0)+1
    > FROM Numbers
    >
    >
    >Then create and populate a new table like this:
    >
    >CREATE TABLE Newtable (column1 CHAR(1) NOT NULL, newcol
    INTEGER NOT NULL,
    >PRIMARY KEY (column1,newcol))
    >
    >INSERT INTO newtable
    > SELECT C.column1, N.num
    > FROM
    > (SELECT column1, COUNT(*) AS cnt
    > FROM Sometable
    > GROUP BY column1) AS C
    > JOIN Numbers AS N
    > ON N.num <= C.cnt
    >
    >
    >--
    >David Portas
    >------------
    >Please reply only to the newsgroup
    >--
    >
    >
    >
    >.
    >
    Jane Guest

  5. #4

    Default Re: add serial number

    > I wish MS can make this simple process as less 'painful'
    > as it can be.
    The least painful solution is to ensure every table has a primary key,
    without fail. Time spent on good design will always pay off in the end.

    --
    David Portas
    ------------
    Please reply only to the newsgroup
    --



    David Portas Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139