Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Community Forums
 Code Support: ASP (Non-Forum Related)
 Select Issues
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Carefree
Advanced Member

Philippines
4207 Posts

Posted - 29 April 2012 :  15:50:17  Show Profile  Reply with Quote
I have three issues.
  1. Select distinct values for one field if ALL values for another field are identical - like this:

    SELECT DISTINCT A, B FROM TABLE WHERE (ALL B=1)

  2. Identify whether values are repeated in a column, if certain other conditions are also met - like this:

    SELECT DISTINCT A, B FROM TABLE WHERE (C>'' AND B='') GROUP BY B HAVING COUNT(*) > 1

  3. Identify distinct values when values in another column are repeated - like this:

    SELECT DISTINCT A, B FROM TABLE GROUP BY (B HAVING COUNT(*) > 1) ORDER BY A ASC



Whenever I use "GROUP BY", w3wp.exe freezes until it times out. Anyone have a clue why?

Edited by - Carefree on 01 May 2012 22:25:09

HuwR
Forum Admin

United Kingdom
20584 Posts

Posted - 02 May 2012 :  02:00:57  Show Profile  Visit HuwR's Homepage  Reply with Quote
are those exactly as you have executed them ?

MVC .net dev/test site | MVC .net running on Raspberry Pi
Go to Top of Page

Carefree
Advanced Member

Philippines
4207 Posts

Posted - 02 May 2012 :  02:29:44  Show Profile  Reply with Quote
Yes
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20584 Posts

Posted - 02 May 2012 :  06:27:30  Show Profile  Visit HuwR's Homepage  Reply with Quote
when you execute them, do they error or just not return what you were expecting?

MVC .net dev/test site | MVC .net running on Raspberry Pi
Go to Top of Page

Carefree
Advanced Member

Philippines
4207 Posts

Posted - 02 May 2012 :  09:05:25  Show Profile  Reply with Quote
They just freeze until they timeout. I think using ALL in #1 tells it that I'm retarded but #2 & #3 are due to using "distinct AND group by" in the same query, but I haven't found anything saying that I shouldn't.
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20584 Posts

Posted - 02 May 2012 :  09:29:56  Show Profile  Visit HuwR's Homepage  Reply with Quote
You wouldn't normally use a DISTINCT clause that way, if you are using groupby since grouping by stuff is very similar to requesting distinct anyway

instead try doing something like

SELECT A, B FROM TABLE WHERE (C>'' AND B='') GROUP BY B HAVING COUNT(DISTINCT A) > 1




MVC .net dev/test site | MVC .net running on Raspberry Pi
Go to Top of Page

Carefree
Advanced Member

Philippines
4207 Posts

Posted - 02 May 2012 :  10:05:55  Show Profile  Reply with Quote
No, didn't work. Same result. Thanks for trying.
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 02 May 2012 :  10:32:01  Show Profile  Send ruirib a Yahoo! Message  Reply with Quote
Does your table have a primary key? If it does, 1. and 3. can be solved using IN and a subquery.


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Carefree
Advanced Member

Philippines
4207 Posts

Posted - 02 May 2012 :  16:10:10  Show Profile  Reply with Quote
Yes, it does. Have an example of how to handle 3?
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 02 May 2012 :  18:19:49  Show Profile  Send ruirib a Yahoo! Message  Reply with Quote
The subquery would be something like (SELECT B FROM TABLE GROUP BY B HAVING COUNT(*)>1) and the overall query could be:

SELECT DISTINCT A FROM TABLE WHERE B IN (SELECT B FROM TABLE GROUP BY B HAVING COUNT(*)>1 )

I did this without testing, so it may actually need a bit of testing to make sure it would work as you'd want.


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Carefree
Advanced Member

Philippines
4207 Posts

Posted - 06 May 2012 :  16:59:51  Show Profile  Reply with Quote
Sorry for my delay in replying, I've been trying to make it work (without success).
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 06 May 2012 :  17:23:05  Show Profile  Send ruirib a Yahoo! Message  Reply with Quote
Maybe if you give an example table?


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Carefree
Advanced Member

Philippines
4207 Posts

Posted - 11 May 2012 :  02:37:29  Show Profile  Reply with Quote
Sorry for long pauses, they're keeping me very busy. The cognitive people say the rate of deterioration has slowed, so maybe this helps.

Anyway, sure. Sample DB

Example of 3:
SELECT DISTINCT INCIDENT_SU FROM INCIDENTS WHERE INCIDENT_GT IN (SELECT INCIDENT_GT FROM INCIDENTS HAVING COUNT(*)>1 GROUP BY INCIDENT_GT)

Edited by - Carefree on 11 May 2012 02:49:09
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20584 Posts

Posted - 12 May 2012 :  02:13:40  Show Profile  Visit HuwR's Homepage  Reply with Quote
Your HAVING clause should come after the GROUP BY not BEFORE

so the sub query would be (SELECT INCIDENT_GT FROM INCIDENTS GROUP BY INCIDENT_GT HAVING COUNT(*)>1)

MVC .net dev/test site | MVC .net running on Raspberry Pi
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 12 May 2012 :  06:07:25  Show Profile  Send ruirib a Yahoo! Message  Reply with Quote
quote:
Originally posted by HuwR

Your HAVING clause should come after the GROUP BY not BEFORE

so the sub query would be (SELECT INCIDENT_GT FROM INCIDENTS GROUP BY INCIDENT_GT HAVING COUNT(*)>1)


That was my mistake, I typed the SQL without even testing it.

It does work when the syntax error is corrected (I have also corrected it in my original reply).


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Carefree
Advanced Member

Philippines
4207 Posts

Posted - 13 May 2012 :  11:47:09  Show Profile  Reply with Quote
That solved it! Thanks you two.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.16 seconds. Powered By: Snitz Forums 2000 Version 3.4.07