Firebird SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesFirebird SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 2nd, 2006, 03:19 AM
FrankBKK FrankBKK is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Posts: 5 FrankBKK User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 29 m 38 sec
Reputation Power: 0
How to count the rows of a query result ?

Maybe I worked to long but I have no clue how to solve this !


How can I count the number of rows returned by this query ?
(I need to use this value in a stored procedure)

SELECT
AUTO_ID_CUSTOMER
, SUM(UNIT_QTY*UNIT_PRICE)
FROM V_TRANSACTIONS
WHERE INV_DATE > '01/01/2006'
GROUP BY AUTO_ID_CUSTOMER
HAVING SUM(UNIT_QTY*UNIT_PRICE) < 100000

I also tried something like

select count(distinct v1.auto_id_customer)
FROM V_TRANSACTIONS v1
WHERE v1.INV_DATE > '01/01/2006'
and (select sum(v2.UNIT_QTY*v2.UNIT_PRICE)
FROM V_TRANSACTIONS v2
where v2.auto_id_customer = v1.auto_id_customer
and v2.INV_DATE > '01/01/2006') < 100000

which works, but unfortunately the execution time on even a small table is not acceptable (> 1 min.)

Reply With Quote
  #2  
Old September 5th, 2006, 03:25 PM
clivew clivew is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 2,045 clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 37 m
Reputation Power: 382
Quote:
Originally Posted by FrankBKK
Maybe I worked to long but I have no clue how to solve this !


How can I count the number of rows returned by this query ?
(I need to use this value in a stored procedure)

SELECT
AUTO_ID_CUSTOMER
, SUM(UNIT_QTY*UNIT_PRICE)
FROM V_TRANSACTIONS
WHERE INV_DATE > '01/01/2006'
GROUP BY AUTO_ID_CUSTOMER
HAVING SUM(UNIT_QTY*UNIT_PRICE) < 100000

I also tried something like

select count(distinct v1.auto_id_customer)
FROM V_TRANSACTIONS v1
WHERE v1.INV_DATE > '01/01/2006'
and (select sum(v2.UNIT_QTY*v2.UNIT_PRICE)
FROM V_TRANSACTIONS v2
where v2.auto_id_customer = v1.auto_id_customer
and v2.INV_DATE > '01/01/2006') < 100000

which works, but unfortunately the execution time on even a small table is not acceptable (> 1 min.)


Does this run any faster?
Code:
SELECT COUNT(*) FROM
(
SELECT
  AUTO_ID_CUSTOMER
, SUM(UNIT_QTY*UNIT_PRICE)
FROM V_TRANSACTIONS
WHERE INV_DATE > '01/01/2006'
GROUP BY AUTO_ID_CUSTOMER
HAVING SUM(UNIT_QTY*UNIT_PRICE) < 100000
)


Clive

Reply With Quote
  #3  
Old September 6th, 2006, 11:59 AM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 8th Plane (8500 - 8999 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 8,527 pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 1 Day 3 h 19 m 49 sec
Reputation Power: 537
On Firebird 2.0
Quote:
Context variable ROW_COUNT returns select countD.
ROW_COUNT can now return the number of rows returned by a SELECT statement.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesFirebird SQL Development > How to count the rows of a query result ?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap