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 > Return Values on conditions

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-10-07, 10:14
grooverinthesouth grooverinthesouth is offline
Registered User
 
Join Date: Mar 2006
Posts: 36
Question Return Values on conditions

Hi all
I'm trying to return an integer from a query.

I have a table which has a PK(int), Type(int), DateLastUpdated(DateTime) and some other columns...

My query is trying to return the PK integer value of the row which has the most recent date in (DateLastUpdated) defined by a certain type.

QUERY:
Select TOP 1 Table.PK from Table
WHERE Table.Type = @Type
ORDER BY Table.DateLastUpdated DESC

This query returns the correct result.
How do I formulate it so that it returns Table.PK as an int
OR returns -1 if no records are returned(because there are no records with a certain @Type)?
Reply With Quote
  #2 (permalink)  
Old 05-10-07, 12:09
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
Huh? PK is already an INT in your table.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #3 (permalink)  
Old 05-10-07, 12:21
grooverinthesouth grooverinthesouth is offline
Registered User
 
Join Date: Mar 2006
Posts: 36
yeah, i Got that

How do I return -1 if the record doesn't exist?
Reply With Quote
  #4 (permalink)  
Old 05-10-07, 13:37
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
first of all, TOP is proprietary to Microsoft, so perhaps you might want this thread moved from the SQL forum (SQL is a language) to the Microsoft SQL Server forum?

in SQL, your query would be:
Code:
SELECT PK 
     , Other
     , Columns 
  FROM Table
 WHERE Type = @Type
   AND DateLastUpdated =
       ( SELECT MAX(DateLastUpdated)
           FROM Table
          WHERE Type = @Type )
regarding returning -1, the query will return NULL if no row satisfies its criteria, so perhaps your application level can detect that
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 05-18-07, 05:13
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
What about the following:
Code:
WITH t AS (SELECT pk, DateLastUpdated
           FROM table WHERE type = @type
           UNION ALL
           VALUES (-1, CAST('0001-01-01' AS date)))
SELECT t1.pk
FROM   t AS t1 LEFT OUTER JOIN t AS t2
       ON t1.DateLastUpdated < t2.DateLastUpdated
WHERE  t2.pk IS NULL
The CTE t contains only the rows with the selected type.
The outer join is a self-join of t, which will link none of the rows of t2 to the "max" row of t1.
This explains the "WHERE t2.pk IS NULL".
When there is no row in table with type = @type, the row added by the UNION will be returned.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/

Last edited by Peter.Vanroose; 05-18-07 at 05:18.
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