Professional Web Applications Themes

Counter - Microsoft SQL / MS SQL Server

Good morning. Does anyone knows how to select a counter along with other fields from a table? For example, I have a table: Product ProdName Price Prod1 20.00 Prod2 25.00 Prod3 30.00 Prod4 12.00 I would like to query it and get the following output: ProdName Price Counter Prod1 20.00 1 Prod2 25.00 2 Prod3 30.00 3 Prod4 12.00 4 Thanks....

  1. #1

    Default Counter

    Good morning.
    Does anyone knows how to select a counter along with other
    fields from a table? For example, I have a table:
    Product
    ProdName Price
    Prod1 20.00
    Prod2 25.00
    Prod3 30.00
    Prod4 12.00

    I would like to query it and get the following output:
    ProdName Price Counter
    Prod1 20.00 1
    Prod2 25.00 2
    Prod3 30.00 3
    Prod4 12.00 4

    Thanks.

    Anya Guest

  2. #2

    Default Re: Counter

    select prodname,price
    ,(select count(distinct price) from table_name a where a.price <= b.price)
    counter
    from table_name b

    --
    -Vishal

    "Anya" <com> wrote in message
    news:147b01c351e8$f736c490$gbl... 


    Vishal Guest

  3. #3

    Default Re: Counter


    I think this solution fails when we have multiple products
    with same price.
    If we don't have any column to uniquely identify a row in
    this table one way is

    SELECT prodname
    , price
    , IDENTITY(INT,1,1) AS counter
    into #table
    from table_name

    select *
    from #table order by counter

    HTH,
    Srinivas Sampangi

     
    a.price <= b.price) [/ref]
    other 
    >
    >
    >.
    >[/ref]
    sampangi Guest

  4. #4

    Default Re: Counter

    yes, probably i didnt read the question carefully. I assumed he needs to
    know the RANK of the price.
    ie highest price will be 1 next 2 and so on.

    --
    -Vishal

    "sampangi" <com> wrote in message
    news:0a9501c351f0$145fa800$gbl... 
    > a.price <= b.price) [/ref]
    > other 
    > >
    > >
    > >.
    > >[/ref][/ref]


    Vishal Guest

  5. #5

    Default Re: Counter

    HOW TO: Dynamically Number Rows in a Select Statement
    http://support.microsoft.com/default.aspx?scid=KB;EN-US;q186133

    --
    -Vishal

    "Anya" <com> wrote in message
    news:147b01c351e8$f736c490$gbl... 


    Vishal Guest

Similar Threads

  1. counter
    By [XaToA] in forum PHP Development
    Replies: 18
    Last Post: July 20th, 03:27 PM
  2. RFC: DBIx::Counter - persistent counter class
    By Rhesa Rozendaal in forum PERL Modules
    Replies: 0
    Last Post: April 14th, 12:15 AM
  3. Help me! Counter
    By Chanond in forum Dreamweaver AppDev
    Replies: 1
    Last Post: April 4th, 10:17 PM
  4. Free Network Tools - Random Password Generator - Link Counter - Hit Counter
    By FreewareTown.com in forum Linux / Unix Administration
    Replies: 0
    Last Post: August 30th, 09:20 AM
  5. hit counter or page counter
    By Myothello in forum Web Design
    Replies: 3
    Last Post: April 23rd, 10:15 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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