Union or Join or Nested Select - Can't Remember

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Union or Join or Nested Select - Can't Remember

    Its been along time since I have had to write tsql from the hip so any help
    would be greatly valued. I have a table that contains Country, State, City and
    I need to COUNT the total records for each. Example: United States (10)
    ---Texas (6) -------Dallas (4) -------Huston (2) ---New York (4) -------New
    York (1) -------Buffalo (3) United Kindom(8) ---England (6) -------London(4)
    -------Cork(2) ---Scotland (2) -------Fife (2) Or another way of looking at the
    output: United States (10) ------------ Texas (6) ------------ Dallas (4)
    United States (10) ------------ Texas (6) ------------ Huston (2) United States
    (10) ------------ New York (4) ------- New York (1) United States (10)
    ------------ New York (4) ------- Buffalo (3) United Kindom(8) ------------
    England (6) --------- London(4) United Kindom(8) ------------ England (6)
    --------- Cork(2) United Kindom(8) ------------ Scotland(2) --------- Fife(2)
    I know I am missing something really simple and I have done it before but I
    just can't remember (two plus years the brain has been on the back burner) As
    you can see the group by clause blows it up, I can't use the cfoutput group by
    ether do to the fact that I am dumping the output into CF_MultiRelatedSelects
    SELECT country&' ('&count(country)&')' as cln, state&'
    ('&count(state)&')' as cls, city&' ('&count(city)&')' as
    clc FROM peole GROUP BY country, state,city ORDER BY country, state,city

    Knum Guest

  2. Similar Questions and Discussions

    1. Nested join puzzler
      I seem to have dug my way into a hole that's a bit over my head. I have a db laid out as shown here and I can query it with some very basic joins...
    2. Sub-Select vs. Join
      Hi All, I have two tables. One table holds a list of states names. The other table holds the statesID the user slected along with there...
    3. MS Access Union Join not working w/ Coldfusion MX
      Hope this makes sense. I'm new to this and trying to figure it out. I'm having problems getting a union join to work w/ Coldfusion MX 6.1. I have...
    4. sub select vs. join
      Can anyone help convert the following sub-select into a join of some kind that will return the results described below? SELECT A.RST FROM A...
    5. JOIN/UNION question
      OK basically I want to combine columns from two queries into a single table. I have one method that works but it feels like there is a simpler way to...
  3. #2

    Default Re: Union or Join or Nested Select - Can't Remember

    Hi Knum

    I got these 2 working.

    HTH

    Zoe

    QUERY 1
    <CFQUERY NAME="queryName" ...>
    SELECT country & '(' & count(country) & ')' as Country_Count,
    '' as State_Count,
    '' as City_Count
    FROM peole
    GROUP BY Country

    UNION ALL

    SELECT '' as Country_Count,
    state & '(' & count(state) & ')' as State_Count,
    '' as City_Count
    FROM peole
    GROUP BY State

    UNION ALL

    SELECT '' as Country_Count,
    '' as State_Count,
    city & '(' & count(city) & ')' as City_Count
    FROM peole
    GROUP BY City

    ORDER BY Country_Count, State_Count, City_Count
    </cfquery>

    <CFDUMP VAR="#queryName#" LABEL="query 1">

    QUERY 2
    <CFQUERY NAME="queryName" ...>
    SELECT 'Country' as FieldType, Country as FieldValue, count(country) as
    FieldCount
    FROM peole
    GROUP BY country
    UNION ALL
    SELECT 'State' as FieldType, State as FieldValue, count(State) as FieldCount
    FROM peole
    GROUP BY State
    UNION ALL
    SELECT 'City' as FieldType, City as FieldValue, count(City) as FieldCount
    FROM peole
    GROUP BY City
    ORDER BY FieldType, FieldValue, FieldCount
    </cfquery>

    <CFDUMP VAR="#queryName#" LABEL="query 2">

    zoeski80 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