If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > How to return only 1 row

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-18-09, 07:28
niths_86 niths_86 is offline
Registered User
 
Join Date: Jul 2009
Posts: 9
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.
Reply With Quote
  #2 (permalink)  
Old 07-18-09, 07:51
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,511
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)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 07-18-09, 13:53
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,595
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.
Reply With Quote
  #4 (permalink)  
Old 07-18-09, 14:12
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,511
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

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 07-20-09, 00:23
niths_86 niths_86 is offline
Registered User
 
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.
Reply With Quote
  #6 (permalink)  
Old 07-20-09, 06:09
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,511
did you try my suggestion?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On