Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #41

    Default 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

  2. Similar Questions and Discussions

    1. 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 :...
    2. 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...
    3. 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....
    4. 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...
    5. 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...
  3. #42

    Default 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

  4. #43

    Default Re: Query Question

    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.
    Bill Karwin Guest

  5. #44

    Default 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

  6. #45

    Default Re: Query Question

    cybose wrote:
    > But it looks like there'll be as many resultsets to intersect
    > as there are Qs. Can this be avoided?
    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.
    > And in your solution using joins how are T1, T2, T3 defined?
    Sorry, I did it wrong. T1, T2, and T3 are correlation names (aka table
    aliases) for T. For instance:

    ....JOIN T AS T1 ON T.A = T1.A AND T1.B = 'Q1'

    Regards,
    Bill K.
    Bill Karwin Guest

  7. #46

    Default Re: Query Question


    > 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.
    If I wanted to select only those rows that had both "Q1 AND Q3 "...

    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

  8. #47

    Default 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139