Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Problem with SELECT COUNT DISTINCT...
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Problem with SELECT COUNT DISTINCT...

 Post Reply Post Reply
Author
theSCIENTIST View Drop Down
Senior Member
Senior Member


Joined: 31 July 2003
Location: United Kingdom
Status: Offline
Points: 440
Post Options Post Options   Thanks (0) Thanks(0)   Quote theSCIENTIST Quote  Post ReplyReply Direct Link To This Post Topic: Problem with SELECT COUNT DISTINCT...
    Posted: 22 August 2005 at 2:15pm
Right, I've been turning my head with this one, and can't figure how to do it.

Scenario:

I'm SELECTing DISTINCT from a table, it returns records fine, but I need to COUNT how many it returns, I can count in the recorset loop doing a c = c + 1 at the end I have my count, however, in this loop I have an array that needs to be populated at the same time, this array needs to be dynamically sized before being filled, so I can't just pre array(20), as it may return more that that, giving it a high value it's not good programming, so I need to know the COUNT from the DISTINCT records returned before I go into the loop, so I can [ Redim array(count) ] and get it sized perfectly.

Question:

Does anyone know how to do a COUNT in the same statement as a normal SELECT DISTINCT ... ?

I'm using ASP and MySQL, not sure if MSSQL can use COUNT, but I'm also willing to hear from all of you that may have an idea on how to do this with MSSQL, as I can get ideas from it.

Thanks.

Edited by theSCIENTIST - 23 August 2005 at 3:49am
Back to Top
theSCIENTIST View Drop Down
Senior Member
Senior Member


Joined: 31 July 2003
Location: United Kingdom
Status: Offline
Points: 440
Post Options Post Options   Thanks (0) Thanks(0)   Quote theSCIENTIST Quote  Post ReplyReply Direct Link To This Post Posted: 23 August 2005 at 3:53am
Ok, I found the solution, it turns out one can use [ SELECT COUNT(DISTINCT fld_Whatever) AS count FROM tbl_Whatever; ], funny thing thou, I tried this before in many different combinations and it was always erroring out.

I then asked here because I thought and was really convinced, one couln't use the COUNT with the DISTINCT, anyway my fault.
Back to Top
Phat View Drop Down
Senior Member
Senior Member


Joined: 23 February 2003
Status: Offline
Points: 386
Post Options Post Options   Thanks (0) Thanks(0)   Quote Phat Quote  Post ReplyReply Direct Link To This Post Posted: 23 August 2005 at 4:27am
Can't you use Redim Preserve?

Not sure how good it is on memory though.
Back to Top
theSCIENTIST View Drop Down
Senior Member
Senior Member


Joined: 31 July 2003
Location: United Kingdom
Status: Offline
Points: 440
Post Options Post Options   Thanks (0) Thanks(0)   Quote theSCIENTIST Quote  Post ReplyReply Direct Link To This Post Posted: 23 August 2005 at 6:28am
I don't want to preserve anything, I just want to set my array from the start with the correct size of what it will hold.

Redim Preserve is used if you want to resize the array but retain any data already in it, which is not the case here.

Yeah, Redim Preserve has a somewhat slow performance, due to the nature of the operation, but nothing to worry about on a fast or moderaly used server, unless the server is very slow/old or the data to be preserved is extenssive and the array multi-dimensional, but overall can be used without any noticable impact.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.