Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / Programming / SQL / July 2008

Tip: Looking for answers? Try searching our database.

Need Ideas for SQL Query

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
todtown - 21 Jul 2008 20:56 GMT
I have a query that needs to return only if there are records that
match the criteria. My current query is something like this:

SELECT MAX(RECORD_DATE),RECORD_NAME
FROM TABLE_NAME
WHERE RECORD_DATE >= '7/21/2008' /*Today's Date*/
GROUP BY RECORD_NAME

The problem is that there can be thousands of records. The query can
often time out. And all I need to know is if there are any records. If
there is even one that's all I need to know.

Could somebody help me with this one?

tod
Hugo Kornelis - 21 Jul 2008 21:15 GMT
>I have a query that needs to return only if there are records that
>match the criteria. My current query is something like this:
[quoted text clipped - 9 lines]
>
>Could somebody help me with this one?

Hi tod,

If you only need to know if rows match the filter, then you don't need
to determine the maximum record_date to achieve that. Instead, use an
EXISTS query so that SQL Server knows it can stop after finding the
first match.

IF EXISTS
  (SELECT *
   FROM   YourTable
   WHERE  YourDate >= '20080721'   -- Use unambiguous date format
   )
PRINT 'It exists!';
ELSE
PRINT 'None found..';

Signature

Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis

Roy Harvey (SQL Server MVP) - 21 Jul 2008 23:04 GMT
>Hi tod,
>
[quoted text clipped - 11 lines]
>ELSE
> PRINT 'None found..';

Or if you would prefer the answer in a SELECT result instead of PRINT
commands:

SELECT CASE WHEN EXISTS
                (SELECT * FROM YourTable
                  WHERE YourDate >= '20080721')
           THEN 'Yes'
           ELSE 'No'
      END as TestResult

Of course instead of 'Yes' and 'No' and pair of values of compatible
type can be used, including 1 and 0.

Roy Harvey
Beacon Falls, CT
--CELKO-- - 21 Jul 2008 21:18 GMT
>> I have a query that needs to return only if there are records [sic: rows are not records] that match the criteria. And all I need to know is if there are any records [sic]. If here is even one that's all I need to know. <<

SELECT DISTINCT CURRENT_TIMESTAMP, 'Yes, we will have work!'
  FROM Dummy -- any table
WHERE EXISTS
      (SELECT *
         FROM Foobar
        WHERE record_date >= CURRENT_TIMESTAMP);

Your approach was to count the rows as if they were records in a
sequence.  They are elements in a set; look at the set as a whole, not
as separate records. Use the right words and the proper mindset
follows.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.