Select Issues - نوشته شده در (1140 Views)
Advanced Member
Carefree
مطلب: 4224
4224
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?
 پیش‌فرض مرتب‌سازی برای تاریخ DESC به معنی جدیدترین است  
 تعداد در صفحه 
نوشته شده در
Forum Admin
HuwR
مطلب: 20611
20611
are those exactly as you have executed them ?
نوشته شده در
Advanced Member
Carefree
مطلب: 4224
4224
Yes
نوشته شده در
Forum Admin
HuwR
مطلب: 20611
20611
when you execute them, do they error or just not return what you were expecting?
نوشته شده در
Advanced Member
Carefree
مطلب: 4224
4224
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.
نوشته شده در
Forum Admin
HuwR
مطلب: 20611
20611
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



نوشته شده در
Advanced Member
Carefree
مطلب: 4224
4224
No, didn't work. Same result. Thanks for trying.
نوشته شده در
Snitz Forums Admin
ruirib
مطلب: 26364
26364
Does your table have a primary key? If it does, 1. and 3. can be solved using IN and a subquery.
نوشته شده در
Advanced Member
Carefree
مطلب: 4224
4224
Yes, it does. Have an example of how to handle 3?
نوشته شده در
Snitz Forums Admin
ruirib
مطلب: 26364
26364
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.
نوشته شده در
Advanced Member
Carefree
مطلب: 4224
4224
Sorry for my delay in replying, I've been trying to make it work (without success).
شما باید یک متن وارد کنید