Home All Groups Group Topic Archive Search About

More Efficient SQL Statement - Select Count(1)

Author
30 Jun 2006 6:55 PM
Joe K.
I know that you can write select statement (Select Count(1)) within Oracle
if finds one record it will stop the search critieria. 

Please help me modify the SQL statement listed below to use the equivalent
if it finds one records it stops and output is 1 if not the output is 0.

Thank You,


SET @COUNT_CALLS_REC_1 =
(Select count(Icent_Num)
from TKCalls.dbo.tblCalls
where DATEDIFF(mi, StartedTime, GETDATE()) <=30
AND left(cast(Icent_Num as varchar(20)),6) = ('962472'))

Author
30 Jun 2006 7:10 PM
Arnie Rowland
Preface the query with a return limitation -ROWCOUNT.

USE Northwind
GO

SET ROWCOUNT 0

SELECT CustomerID
FROM   Customers
WHERE  Country = 'Mexico'

SET ROWCOUNT 0

--
Arnie Rowland, YACE*
"To be successful, your heart must accompany your knowledge."

*Yet Another Certification Exam


Show quote
"Joe K." <Joe K*@discussions.microsoft.com> wrote in message news:FDDCBA4B-3553-4459-B7DA-25436FBEC8C9@microsoft.com...
>
> I know that you can write select statement (Select Count(1)) within Oracle
> if finds one record it will stop the search critieria. 
>
> Please help me modify the SQL statement listed below to use the equivalent
> if it finds one records it stops and output is 1 if not the output is 0.
>
> Thank You,
>
>
> SET @COUNT_CALLS_REC_1 =
> (Select count(Icent_Num)
> from TKCalls.dbo.tblCalls
> where DATEDIFF(mi, StartedTime, GETDATE()) <=30
> AND left(cast(Icent_Num as varchar(20)),6) = ('962472'))
Author
30 Jun 2006 7:19 PM
Arnie Rowland
Sorry, the first SET ROWCOUNT should be 1 -NOT 0.

--
Arnie Rowland, YACE*
"To be successful, your heart must accompany your knowledge."

*Yet Another Certification Exam


  "Arnie Rowland" <ar***@1568.com> wrote in message news:%23ojjUjHnGHA.2264@TK2MSFTNGP04.phx.gbl...
  Preface the query with a return limitation -ROWCOUNT.

  USE Northwind
  GO

  SET ROWCOUNT 0

  SELECT CustomerID
  FROM   Customers
  WHERE  Country = 'Mexico'

  SET ROWCOUNT 0

  --
  Arnie Rowland, YACE*
  "To be successful, your heart must accompany your knowledge."

  *Yet Another Certification Exam


Show quote
  "Joe K." <Joe K*@discussions.microsoft.com> wrote in message news:FDDCBA4B-3553-4459-B7DA-25436FBEC8C9@microsoft.com...
  >
  > I know that you can write select statement (Select Count(1)) within Oracle
  > if finds one record it will stop the search critieria. 
  >
  > Please help me modify the SQL statement listed below to use the equivalent
  > if it finds one records it stops and output is 1 if not the output is 0.
  >
  > Thank You,
  >
  >
  > SET @COUNT_CALLS_REC_1 =
  > (Select count(Icent_Num)
  > from TKCalls.dbo.tblCalls
  > where DATEDIFF(mi, StartedTime, GETDATE()) <=30
  > AND left(cast(Icent_Num as varchar(20)),6) = ('962472'))
Author
30 Jun 2006 7:17 PM
Omnibuzz
something like this?

if exists(
Select 1 from TKCalls.dbo.tblCalls
where DATEDIFF(mi, StartedTime, GETDATE()) <=30
AND left(cast(Icent_Num as varchar(20)),6) = ('962472')
)
SET @COUNT_CALLS_REC_1 = 1
else
SET @COUNT_CALLS_REC_1 = 0
--
-Omnibuzz (The SQL GC)

http://omnibuzz-sql.blogspot.com/
Author
30 Jun 2006 7:34 PM
Alejandro Mesa
Joe k.,

Use the operator "exists" and do not manipulate the columns in the "where"
clause as possible, to let SQL Server to use the indexes optimally in case
they exists.

if exists (
select *
from TKCalls.dbo.tblCalls
where
   StartedTime between dateadd(minute, -30, GETDATE()) and GETDATE()
   and cast(Icent_Num as varchar(20)) like '962472%'
)
   set ...
else
   set ...
go


AMB

Show quote
"Joe K." wrote:

>
> I know that you can write select statement (Select Count(1)) within Oracle
> if finds one record it will stop the search critieria. 
>
> Please help me modify the SQL statement listed below to use the equivalent
> if it finds one records it stops and output is 1 if not the output is 0.
>
> Thank You,
>
>
> SET @COUNT_CALLS_REC_1 =
> (Select count(Icent_Num)
> from TKCalls.dbo.tblCalls
> where DATEDIFF(mi, StartedTime, GETDATE()) <=30
> AND left(cast(Icent_Num as varchar(20)),6) = ('962472'))
Author
30 Jun 2006 8:10 PM
Gert-Jan Strik
Are you sure that on Oracle, the statement SELECT COUNT(1) will never
return a value > 1? I would be very surprised if this is the Oracle
behavior, because that is not what the SQL statement is specifying...

Gert-Jan


Joe K. wrote:
Show quote
>
> I know that you can write select statement (Select Count(1)) within Oracle
> if finds one record it will stop the search critieria.
>
> Please help me modify the SQL statement listed below to use the equivalent
> if it finds one records it stops and output is 1 if not the output is 0.
>
> Thank You,
>
> SET @COUNT_CALLS_REC_1 =
> (Select count(Icent_Num)
> from TKCalls.dbo.tblCalls
> where DATEDIFF(mi, StartedTime, GETDATE()) <=30
> AND left(cast(Icent_Num as varchar(20)),6) = ('962472'))
Author
30 Jun 2006 8:45 PM
josearaujof
doesn't "select top 1 * from ..." work for you?

José.

Gert-Jan Strik wrote:
Show quote
> Are you sure that on Oracle, the statement SELECT COUNT(1) will never
> return a value > 1? I would be very surprised if this is the Oracle
> behavior, because that is not what the SQL statement is specifying...
>
> Gert-Jan
>
>
> Joe K. wrote:
> >
> > I know that you can write select statement (Select Count(1)) within Oracle
> > if finds one record it will stop the search critieria.
> >
> > Please help me modify the SQL statement listed below to use the equivalent
> > if it finds one records it stops and output is 1 if not the output is 0.
> >
> > Thank You,
> >
> > SET @COUNT_CALLS_REC_1 =
> > (Select count(Icent_Num)
> > from TKCalls.dbo.tblCalls
> > where DATEDIFF(mi, StartedTime, GETDATE()) <=30
> > AND left(cast(Icent_Num as varchar(20)),6) = ('962472'))
Author
30 Jun 2006 9:11 PM
Arnie Rowland
As I recall, one of the issues with TOP 1 is that it has to find all records
(or at least indexes) first in order to determine which one is the TOP 1. So
that may not be 'efficient'.

;-)
--
Arnie Rowland, YACE*
"To be successful, your heart must accompany your knowledge."

*Yet Another Certification Exam


<josearau***@gmail.com> wrote in message
news:1151700301.773682.267740@75g2000cwc.googlegroups.com...
doesn't "select top 1 * from ..." work for you?

José.

Gert-Jan Strik wrote:
Show quote
> Are you sure that on Oracle, the statement SELECT COUNT(1) will never
> return a value > 1? I would be very surprised if this is the Oracle
> behavior, because that is not what the SQL statement is specifying...
>
> Gert-Jan
>
>
> Joe K. wrote:
> >
> > I know that you can write select statement (Select Count(1)) within
> > Oracle
> > if finds one record it will stop the search critieria.
> >
> > Please help me modify the SQL statement listed below to use the
> > equivalent
> > if it finds one records it stops and output is 1 if not the output is 0.
> >
> > Thank You,
> >
> > SET @COUNT_CALLS_REC_1 =
> > (Select count(Icent_Num)
> > from TKCalls.dbo.tblCalls
> > where DATEDIFF(mi, StartedTime, GETDATE()) <=30
> > AND left(cast(Icent_Num as varchar(20)),6) = ('962472'))
Author
1 Jul 2006 8:02 AM
Tibor Karaszi
> As I recall, one of the issues with TOP 1 is that it has to find all records (or at least indexes)
> first in order to determine which one is the TOP 1. So that may not be 'efficient'.

Only with ORDER BY. Without ORDER BY, you ask for any one row, so the optimizer and engine know it
can stop after the first row it encounters.

Show quote
"Arnie Rowland" <ar***@1568.com> wrote in message news:%23G0c2mInGHA.3388@TK2MSFTNGP05.phx.gbl...
> As I recall, one of the issues with TOP 1 is that it has to find all records (or at least indexes)
> first in order to determine which one is the TOP 1. So that may not be 'efficient'.
>
> ;-)
> --
> Arnie Rowland, YACE*
> "To be successful, your heart must accompany your knowledge."
>
> *Yet Another Certification Exam
>
>
> <josearau***@gmail.com> wrote in message
> news:1151700301.773682.267740@75g2000cwc.googlegroups.com...
> doesn't "select top 1 * from ..." work for you?
>
> José.
>
> Gert-Jan Strik wrote:
>> Are you sure that on Oracle, the statement SELECT COUNT(1) will never
>> return a value > 1? I would be very surprised if this is the Oracle
>> behavior, because that is not what the SQL statement is specifying...
>>
>> Gert-Jan
>>
>>
>> Joe K. wrote:
>> >
>> > I know that you can write select statement (Select Count(1)) within Oracle
>> > if finds one record it will stop the search critieria.
>> >
>> > Please help me modify the SQL statement listed below to use the equivalent
>> > if it finds one records it stops and output is 1 if not the output is 0.
>> >
>> > Thank You,
>> >
>> > SET @COUNT_CALLS_REC_1 =
>> > (Select count(Icent_Num)
>> > from TKCalls.dbo.tblCalls
>> > where DATEDIFF(mi, StartedTime, GETDATE()) <=30
>> > AND left(cast(Icent_Num as varchar(20)),6) = ('962472'))
>
>
Author
1 Jul 2006 10:24 AM
Gert-Jan Strik
In case of an ORDER BY, it depends on the columns in the ORDER BY. If
they match an index, then the optimizer might still be able to stop
searching after finding the first entry.

Gert-Jan


Tibor Karaszi wrote:
Show quote
>
> > As I recall, one of the issues with TOP 1 is that it has to find all records (or at least indexes)
> > first in order to determine which one is the TOP 1. So that may not be 'efficient'.
>
> Only with ORDER BY. Without ORDER BY, you ask for any one row, so the optimizer and engine know it
> can stop after the first row it encounters.
>
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "Arnie Rowland" <ar***@1568.com> wrote in message news:%23G0c2mInGHA.3388@TK2MSFTNGP05.phx.gbl...
> > As I recall, one of the issues with TOP 1 is that it has to find all records (or at least indexes)
> > first in order to determine which one is the TOP 1. So that may not be 'efficient'.
> >
> > ;-)
> > --
> > Arnie Rowland, YACE*
> > "To be successful, your heart must accompany your knowledge."
> >
> > *Yet Another Certification Exam
> >
> >
> > <josearau***@gmail.com> wrote in message
> > news:1151700301.773682.267740@75g2000cwc.googlegroups.com...
> > doesn't "select top 1 * from ..." work for you?
> >
> > José.
> >
> > Gert-Jan Strik wrote:
> >> Are you sure that on Oracle, the statement SELECT COUNT(1) will never
> >> return a value > 1? I would be very surprised if this is the Oracle
> >> behavior, because that is not what the SQL statement is specifying...
> >>
> >> Gert-Jan
> >>
> >>
> >> Joe K. wrote:
> >> >
> >> > I know that you can write select statement (Select Count(1)) within Oracle
> >> > if finds one record it will stop the search critieria.
> >> >
> >> > Please help me modify the SQL statement listed below to use the equivalent
> >> > if it finds one records it stops and output is 1 if not the output is 0.
> >> >
> >> > Thank You,
> >> >
> >> > SET @COUNT_CALLS_REC_1 =
> >> > (Select count(Icent_Num)
> >> > from TKCalls.dbo.tblCalls
> >> > where DATEDIFF(mi, StartedTime, GETDATE()) <=30
> >> > AND left(cast(Icent_Num as varchar(20)),6) = ('962472'))
> >
> >
Author
1 Jul 2006 10:36 AM
Tibor Karaszi
True.
Thanks or catching that, Gert-Jan.

Show quote
"Gert-Jan Strik" <sorry@toomuchspamalready.nl> wrote in message
news:44A64D5A.B3629004@toomuchspamalready.nl...
> In case of an ORDER BY, it depends on the columns in the ORDER BY. If
> they match an index, then the optimizer might still be able to stop
> searching after finding the first entry.
>
> Gert-Jan
>
>
> Tibor Karaszi wrote:
>>
>> > As I recall, one of the issues with TOP 1 is that it has to find all records (or at least
>> > indexes)
>> > first in order to determine which one is the TOP 1. So that may not be 'efficient'.
>>
>> Only with ORDER BY. Without ORDER BY, you ask for any one row, so the optimizer and engine know
>> it
>> can stop after the first row it encounters.
>>
>> --
>> Tibor Karaszi, SQL Server MVP
>> http://www.karaszi.com/sqlserver/default.asp
>> http://www.solidqualitylearning.com/
>>
>> "Arnie Rowland" <ar***@1568.com> wrote in message news:%23G0c2mInGHA.3388@TK2MSFTNGP05.phx.gbl...
>> > As I recall, one of the issues with TOP 1 is that it has to find all records (or at least
>> > indexes)
>> > first in order to determine which one is the TOP 1. So that may not be 'efficient'.
>> >
>> > ;-)
>> > --
>> > Arnie Rowland, YACE*
>> > "To be successful, your heart must accompany your knowledge."
>> >
>> > *Yet Another Certification Exam
>> >
>> >
>> > <josearau***@gmail.com> wrote in message
>> > news:1151700301.773682.267740@75g2000cwc.googlegroups.com...
>> > doesn't "select top 1 * from ..." work for you?
>> >
>> > José.
>> >
>> > Gert-Jan Strik wrote:
>> >> Are you sure that on Oracle, the statement SELECT COUNT(1) will never
>> >> return a value > 1? I would be very surprised if this is the Oracle
>> >> behavior, because that is not what the SQL statement is specifying...
>> >>
>> >> Gert-Jan
>> >>
>> >>
>> >> Joe K. wrote:
>> >> >
>> >> > I know that you can write select statement (Select Count(1)) within Oracle
>> >> > if finds one record it will stop the search critieria.
>> >> >
>> >> > Please help me modify the SQL statement listed below to use the equivalent
>> >> > if it finds one records it stops and output is 1 if not the output is 0.
>> >> >
>> >> > Thank You,
>> >> >
>> >> > SET @COUNT_CALLS_REC_1 =
>> >> > (Select count(Icent_Num)
>> >> > from TKCalls.dbo.tblCalls
>> >> > where DATEDIFF(mi, StartedTime, GETDATE()) <=30
>> >> > AND left(cast(Icent_Num as varchar(20)),6) = ('962472'))
>> >
>> >
Author
1 Jul 2006 3:07 PM
Arnie Rowland
Thanks Guys, I learn alot hanging out with you.

--
Arnie Rowland, YACE*
"To be successful, your heart must accompany your knowledge."

*Yet Another Certification Exam


Show quote
"Tibor Karaszi" <tibor_please.no.email_kara***@hotmail.nomail.com> wrote in
message news:O2a6zoPnGHA.3436@TK2MSFTNGP02.phx.gbl...
> True.
> Thanks or catching that, Gert-Jan.
>
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
>
> "Gert-Jan Strik" <sorry@toomuchspamalready.nl> wrote in message
> news:44A64D5A.B3629004@toomuchspamalready.nl...
>> In case of an ORDER BY, it depends on the columns in the ORDER BY. If
>> they match an index, then the optimizer might still be able to stop
>> searching after finding the first entry.
>>
>> Gert-Jan
>>
>>
>> Tibor Karaszi wrote:
>>>
>>> > As I recall, one of the issues with TOP 1 is that it has to find all
>>> > records (or at least indexes)
>>> > first in order to determine which one is the TOP 1. So that may not be
>>> > 'efficient'.
>>>
>>> Only with ORDER BY. Without ORDER BY, you ask for any one row, so the
>>> optimizer and engine know it
>>> can stop after the first row it encounters.
>>>
>>> --
>>> Tibor Karaszi, SQL Server MVP
>>> http://www.karaszi.com/sqlserver/default.asp
>>> http://www.solidqualitylearning.com/
>>>
>>> "Arnie Rowland" <ar***@1568.com> wrote in message
>>> news:%23G0c2mInGHA.3388@TK2MSFTNGP05.phx.gbl...
>>> > As I recall, one of the issues with TOP 1 is that it has to find all
>>> > records (or at least indexes)
>>> > first in order to determine which one is the TOP 1. So that may not be
>>> > 'efficient'.
>>> >
>>> > ;-)
>>> > --
>>> > Arnie Rowland, YACE*
>>> > "To be successful, your heart must accompany your knowledge."
>>> >
>>> > *Yet Another Certification Exam
>>> >
>>> >
>>> > <josearau***@gmail.com> wrote in message
>>> > news:1151700301.773682.267740@75g2000cwc.googlegroups.com...
>>> > doesn't "select top 1 * from ..." work for you?
>>> >
>>> > José.
>>> >
>>> > Gert-Jan Strik wrote:
>>> >> Are you sure that on Oracle, the statement SELECT COUNT(1) will never
>>> >> return a value > 1? I would be very surprised if this is the Oracle
>>> >> behavior, because that is not what the SQL statement is specifying...
>>> >>
>>> >> Gert-Jan
>>> >>
>>> >>
>>> >> Joe K. wrote:
>>> >> >
>>> >> > I know that you can write select statement (Select Count(1)) within
>>> >> > Oracle
>>> >> > if finds one record it will stop the search critieria.
>>> >> >
>>> >> > Please help me modify the SQL statement listed below to use the
>>> >> > equivalent
>>> >> > if it finds one records it stops and output is 1 if not the output
>>> >> > is 0.
>>> >> >
>>> >> > Thank You,
>>> >> >
>>> >> > SET @COUNT_CALLS_REC_1 =
>>> >> > (Select count(Icent_Num)
>>> >> > from TKCalls.dbo.tblCalls
>>> >> > where DATEDIFF(mi, StartedTime, GETDATE()) <=30
>>> >> > AND left(cast(Icent_Num as varchar(20)),6) = ('962472'))
>>> >
>>> >
>
Author
30 Jun 2006 8:24 PM
scott0100
This should work as well...


--
scott0100
------------------------------------------------------------------------
scott0100's Profile: http://www.dbtalk.net/m491
View this thread: http://www.dbtalk.net/t316762

AddThis Social Bookmark Button