Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.
-
duces_wild #41
Re: Query question
ahh yes but i will not let there be duplicate names
hehehe.
ill give it a whirl and see what happens
duces_wild Guest
-
Query Question Please HELP
I have a Table with huge volume of records . table structuer is some thing like this country : region : destrict : destination: Activity date :... -
SOS! Query Question
Hi thanks for reading my message. I am totally a novice in coldfusion, just started learning couple of weeks back. I wanted to do a query but I... -
6.1 Query of Query Question. Ref to own Col
Hey I'm running a QoQ using session variables - every thing works fine until I try to refer to a QoQ column. Let me write the example: 1.... -
SQL query question
Hi all, I have 2 tables, with a 1-n relation: parent( oid, parent_name) child(oid, child_name, iod_parent) How do I get the parent_names of... -
ASP SQL query question
Greetings, I have a question about a database query from an asp page. My query is functional and produces the data that I need; however, one... -
cybose #42
Query Question
I have a table "testTable" with columns A and B with the following
values...
A B
------ ------
P1 Q1
P1 Q2
P1 Q3
P2 Q1
P2 Q3
P3 Q1
P3 Q2
P3 Q3
I need to select only those values from column A that have the values
(Q1,Q2 AND Q3) in column B.
The result I'm expecting is something like...
P1 Q1
P1 Q2
P1 Q3
P3 Q1
P3 Q2
P3 Q3
What would be the form of the query ?
cybose Guest
-
Bill Karwin #43
Re: Query Question
cybose wrote:
Here are a couple of solutions:> I need to select only those values from column A that have the values
> (Q1,Q2 AND Q3) in column B.
SELECT DISTINCT T.A, T.B
FROM T
JOIN T1 ON T.A = T1.A AND T1.B = 'Q1'
JOIN T2 ON T.A = T2.A AND T2.B = 'Q2'
JOIN T3 ON T.A = T3.A AND T3.B = 'Q3'
SELECT A, B
FROM T
WHERE A IN (
SELECT A
FROM T AS T2
WHERE T2.B IN ('Q1', 'Q2', 'Q3')
GROUP BY T2.A
HAVING COUNT(DISTINCT T2.B) = 3)
The latter solution uses a subquery, therefore it requires MySQL 4.1 or
later.
Regards,
Bill K.
Bill Karwin Guest
-
cybose #44
Re: Query Question
Thanks !
However, the aim wass to get all rows that "for a given value in A has
all the specified values in B"
For instance if only Q1 and Q3 are specified, the result should be -
A B
---- ----
P2 Q1
P2 Q3
and if only Q1 is specified-
A B
---- ----
P1 Q1
P2 Q1
P3 Q1
and if Q1,Q2 and Q3 are specified-
A B
---- ----
P1 Q1
P1 Q2
P1 Q3
P3 Q1
P3 Q2
P3 Q3
That seems to be hard to model with a nested query.
Would I have to do something like
ResultSet1 = SELECT * FROM T WHERE B = "Q1"
ResultSet2 = SELECT * FROM T WHERE B = "Q2"
ResultSet3 = SELECT * FROM T WHERE B = "Q3"
and then find the intersection of these resultsets.
But it looks like there'll be as many resultsets to intersect
as there are Qs. Can this be avoided?
And in your solution using joins how are T1, T2, T3 defined?
Regards,
VB
Bill Karwin wrote:> cybose wrote:>> > I need to select only those values from column A that have the values
> > (Q1,Q2 AND Q3) in column B.
> Here are a couple of solutions:
>
> SELECT DISTINCT T.A, T.B
> FROM T
> JOIN T1 ON T.A = T1.A AND T1.B = 'Q1'
> JOIN T2 ON T.A = T2.A AND T2.B = 'Q2'
> JOIN T3 ON T.A = T3.A AND T3.B = 'Q3'
>
> SELECT A, B
> FROM T
> WHERE A IN (
> SELECT A
> FROM T AS T2
> WHERE T2.B IN ('Q1', 'Q2', 'Q3')
> GROUP BY T2.A
> HAVING COUNT(DISTINCT T2.B) = 3)
>
> The latter solution uses a subquery, therefore it requires MySQL 4.1 or
> later.
>
> Regards,
> Bill K.cybose Guest
-
Bill Karwin #45
Re: Query Question
cybose wrote:
Yes, by using the subquery solution; list the values in the IN clause of> But it looks like there'll be as many resultsets to intersect
> as there are Qs. Can this be avoided?
the subquery, and change the number in the HAVING clause of the subquery
to the quantity of values.
Sorry, I did it wrong. T1, T2, and T3 are correlation names (aka table> And in your solution using joins how are T1, T2, T3 defined?
aliases) for T. For instance:
....JOIN T AS T1 ON T.A = T1.A AND T1.B = 'Q1'
Regards,
Bill K.
Bill Karwin Guest
-
cybose #46
Re: Query Question
If I wanted to select only those rows that had both "Q1 AND Q3 "...> Yes, by using the subquery solution; list the values in the IN clause of
> the subquery, and change the number in the HAVING clause of the subquery
> to the quantity of values.
SELECT A, B
FROM T
WHERE A IN (
SELECT A
FROM T AS T2
WHERE T2.B IN ('Q1', 'Q3')
GROUP BY T2.A
HAVING COUNT(DISTINCT T2.B) = 2)
The Result is
+------+------+
| A | B |
+------+------+
| P1 | Q1 |
| P1 | Q2 |
| P1 | Q3 |
| P2 | Q1 |
| P2 | Q3 |
| P3 | Q1 |
| P3 | Q2 |
| P3 | Q3 |
+------+------+
And not
A B
---- -----
P2 Q1
P2 Q3
I think, the WHERE clause above, is more like an OR expression when
whats needed is an AND type expression...
Regards,
Vibeesh
cybose Guest
-
Bill Karwin #47
Re: Query Question
Aha -- it wasn't clear from your original question that you wanted
groups from A that map _only_ to Q1 and Q2, not those that include Q1
and Q2 but might have others.
How about this solution:
SELECT A, B
FROM T
WHERE A IN (
SELECT T2.A
FROM T AS T2
GROUP BY T2.A
HAVING GROUP_CONCAT(DISTINCT T2.B ORDER BY T2.B) = 'Q1,Q2');
See [url]http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html[/url] for
details on the GROUP_CONCAT() function.
This solution also works only in MySQL 4.1 and later, since the
GROUP_CONCAT() function is not implemented in MySQL 4.0.
Regards,
Bill K.
Bill Karwin Guest



Reply With Quote

