SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SQL Query question

    Hello,

    I have a doubt with a query. I have a query that returns me lots of data and I want to add another column to the result that has in every line the total number of lines. Somehitng like:

    Code:
      fieldA | fieldB | .... | newColumn
    -------------------------------------
    1  valA1 |  valB1 | .... | n
    2  valA2 |  valB2 | .... | n
    3  valA3 |  valB3 | .... | n
       ...
    n  valAn |  valBn | .... | n
    Can anyone help me?

    I tried adding COUNT(*) as newColumn but that didn't work (result was 0 in everyline)

    Thanks in advance

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    It can't give 0 in every line. It'll give 0 if the query has no results, otherwise it should return at least 1 in every line.

    Why don't you post your query?
    And what exactly must that new column contain? The total number of lines returned by the query? Or do you want to group by something and get the number of lines for each grouped by value?

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,347
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by guido2004 View Post
    And what exactly must that new column contain?
    sounds to me like he wants a running count of rows, i.e. a row number
    Code:
      fieldA | fieldB | .... | newColumn
    -------------------------------------
    1  valA1 |  valB1 | .... | 1
    2  valA2 |  valB2 | .... | 2
    3  valA3 |  valB3 | .... | 3
       ...
    n  valAn |  valBn | .... | n
    prl, the best place to do this is in your application code
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •