Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple SQL

Status
Not open for further replies.

Steve95

MIS
Nov 3, 2004
265
US
Hi All

Iam new to SQL espcially sql 2005, I have a select statement where Iam selecting to fields from a table and then at the same same adding the 2 fields to and outputting as a new column denoted with a t.

I want to refine the select by the new the results that have been generated ie.

where t = '200604'

Anyway I have posted my whole statement,please can you help

SELECT procesyr, ((procesyr*100) + procesqtr) t, procesqtr
where t = '200604'
from ReporRates

Please can you help

Mnay Thanks
 
Code:
SELECT procesyr, ((procesyr*100) + procesqtr) t, procesqtr  
HAVING ((procesyr*100) + procesqtr) = 200604
from ReporRates

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
boris, HAVING should come after FROM ;-)

what i think you meant to write was:
Code:
SELECT procesyr
     , ((procesyr*100) + procesqtr) t
     , procesqtr  
  FROM ReporRates
 WHERE ((procesyr*100) + procesqtr) = 200604

if you're going to use the ugly HAVING without a GROUP BY, you might as well use the column alias
Code:
SELECT procesyr
     , ((procesyr*100) + procesqtr) t
     , procesqtr  
  FROM ReporRates
HAVING t = 200604
alternatively,
Code:
SELECT *
  FROM (
       SELECT procesyr
            , ((procesyr*100) + procesqtr) t
            , procesqtr  
         FROM ReporRates
       ) as d
 WHERE t = 200604
you could also do this, which will be more efficient, assuming that there is an index on procesyr and procesqtr --
Code:
SELECT procesyr
     , ((procesyr*100) + procesqtr) t
     , procesqtr  
  FROM ReporRates
 WHERE procesyr = 2006
   AND procesqtr = 04

r937.com | rudy.ca
 
You are right of course, all other statements except field list must come after FROM clause.

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top