Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2009
    Posts
    9

    Unanswered: How to return only 1 row

    Hi ,
    I need to return only 1 row from a select statement since anyway all the rows return the same values. I tried with TOP 1 but it doesnt seem to work. The query that i used is
    select
    (select sum(Convert(int,marks)) from table1 where Grade = 'A' and Requirement = 'A+'
    and Status='Active' ) as colmn1,
    (select sum(int,target) from table1 where Grade = 'B' and Requirement = 'B'
    and Status='Active' ) as colmn2
    from table1;
    Can some one please tell me what to do. The problem is it return the same result for every row ( How many ever rows that matches this criteria).
    Please help me resolve this issue.

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by niths_86
    Can some one please tell me what to do.
    first of all, you could identify which database system you're using

    sybase? oracle? informix? db2? firebird? postgresql?

    i'm certain you don't really want an ANSI SQL solution (which is the forum you posted in)
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Your code isn't clear enough that I can reliably figure out what you want to do. Can you explain in English what you want without worrying about the table or tables for the moment?

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    the reason he's getting the same numbers on every row is the FROM clause in the outer query -- he really only wants the two scalars in the SELECT clause, i.e. one row with two number

    in SQL Server, remove the FROM clause

    in Oracle, select from DUAL

    etc.

    which is why i axed him which dbms, eh

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    Jul 2009
    Posts
    9
    yes i am using SQL server 2005 only .. i am actually trying to find out the sum of a column when few conditions are satisified . here it is returning the sum of the expected column only but rather than just returning one row , it returns many rows which is equivalent to the no.of rows that match the where criteria.

  6. #6
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    did you try my suggestion?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

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