SQL Advanced technique

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

  1. #1

    Default SQL Advanced technique

    I found this working

    SELECT table1.table1_id, count(table2.table2_id) as count
    FROM table1, table2
    WHERE table1.table1_id = table2.table1_id
    GROUP by table1.table1_id
    But this returns matching rows only.
    I need to select ALL data from table1 with added (or joined) number of
    matching rows in table2.

    When there are no records matching table1_id in table2.table1_id nothing is
    returned. If I add a condition like "OR 1=1" all rows from table1 are returned,
    but the counted number of mathing rows is not right.

    Please help. Thank you for your time
    I'm using Microsoft SQL Server 2000

    cicovec-at-hotmail Guest

  2. Similar Questions and Discussions

    1. Over the page technique
      Hello, Will someone please share the technique and steps involved in creating "Over the Page" flash ads? Creating the ad in flash is no...
    2. help - masking or other technique
      Prush: After drawing out the text and the circle, I would do the following: 1. Convert your text to paths. 2. Ungroup the "text" 3. Select...
    3. Clustering Technique Question
      Is it better to use a column that has a high cardinality as a clustering index or vice versa?
    4. Masking Technique Problem
      Ok, the attached file is part of a tutorial that I am working on. Masking is the only technique that I have not mastered in Fireworks yet. Can...
    5. MASKING TECHnique
      Ok, the attached file is part of a tutorial that I am working on. Masking is the only technique that I have not mastered in Fireworks yet. Can...
  3. #2

    Default Re: SQL Advanced technique

    Solution found:

    SELECT table1.table1_id, count(table2.table2_id) as count
    FROM table1
    LEFT JOIN table2 ON table1.table1_id = table2.table1_id
    GROUP by table1.table1_id

    For anyone in the future.

    cicovec-at-hotmail Guest

  4. #3

    Default Re: SQL Advanced technique

    I've got one more question, I would like to add one more column to the result
    set. It should also contain number of matching rows in the second table, but
    only the ones, where table2.active eqals 1.

    Could someone help me plase, I've already spent a lot of time figuring out
    this query.

    Thank you in advance.

    cicovec-at-hotmail Guest

  5. #4

    Default Re: SQL Advanced technique

    You can use a correlated subquery, like the attached...

    -- MikeR



    SELECT
    T1.table1_id,
    count (T2.table1_id) AS count, -- Avoid using reserved words as
    column names!
    (
    SELECT
    Count (T2b.table1_id)
    FROM
    table2 T2b
    WHERE
    T2b.table1_id = T1.table1_id
    AND
    T2b.active = 1
    )
    AS ActiveCount
    FROM
    table1 T1
    LEFT JOIN
    table2 T2 ON T1.table1_id = T2.table1_id
    GROUP BY
    T1.table1_id

    MikerRoo Guest

  6. #5

    Default Re: SQL Advanced technique

    Thank you MikerRoo, I found a solution, but yours is much much better.

    SELECT table1.table1_id, COUNT(DISTINCT table2.table2_id) AS count_all,
    COUNT(DISTINCT table2b.table2_id) AS count_active
    FROM table1 LEFT JOIN table2 ON (table1.table1_id = table2.table1_id)
    LEFT JOIN table2 AS table2b ON (table1.table1_id = table2b.table1_id AND
    table2b.active = 1)
    GROUP BY table1.table1_id

    cicovec-at-hotmail 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