Query Problem: Field doesn't always exist

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

  1. #1

    Default Query Problem: Field doesn't always exist

    I have 3 tables, a Survey table, a Ticket table, and a Notes Table. The notes
    table contains notes for the Ticket, and the Survey is a questionairre about
    the Ticket.

    Not every Ticket has Notes. I'm creating a site that displays the Surveys, and
    related Ticket and Note information. I need it to query the Note table if
    applicable.

    Let's say I have
    <cfquery name = "name" datasource = "db">
    select a.*, b.* from ticket a, survey b
    where a.ticketid = b.ticketid
    </cfquery>

    that will only get the surveys and tickets, which will always have matching
    fields.

    <cfquery name = "name" datasource = "db">
    select a.*, b.*, c.*
    from ticket a, survey b, notes c
    where a.ticketid = b.ticketid and a.ticketid = c.ticketid
    </cfquery>
    This will only pull records with notes. How do I get records with notes, and
    without??

    I seem to have this type of query problem often. Any suggestions?:brokenheart;

    RuBot Guest

  2. Similar Questions and Discussions

    1. Access Denied. The file may not exist, or there could bepermission problem.
      I'm seeing few other posts on this subject and am losing my hair so here goes. Using Contribute 3 for my clients to update files while our CMS...
    2. Calculated Field in Query of Query
      How do you add a calculated field to a Query of Query using a function DollarFormat(datafield) as calcField
    3. Defining field value to exist when not other value
      I have a text field definition that I need to make either one value or another. When the record is created, I want it to be value 1, when the user...
    4. Problem with ADO query against a DBF date field
      Marit, Jet and VBScript use the same underlying numeric representations of date/time values, and your best bet is to pass your value as a numeric...
    5. table does not exist error in AD query?
      this runs perfectly as a .vbs but I'd like to convert it to an ASP... any ideas? Response.Buffer = True Dim objConnection, objCommand,...
  3. #2

    Default Re: Query Problem: Field doesn't always exist

    Use a left join to retrieve all of the records from one table even if there
    are no related records in the other. Something like ...

    --- not tested. adjust syntax for your database type.
    SELECT someColumns
    FROM ticket a INNER JOIN survey b ON a.ticketid = b.ticketid
    LEFT JOIN notes c ON a.ticketid = c.ticketid

    mxstu Guest

  4. #3

    Default Re: Query Problem: Field doesn't always exist

    Tables - Survey, Ticket, Notes

    Every survey record matches a ticket record, but not all ticket records have a
    matching notes record.

    Conceptually, isn't the join you listed still looking to match a ticket to a
    note? Will this still include records that do not have a matching field?

    Sorry I'm a gomer sometimes.

    RuBot Guest

  5. #4

    Default Re: Query Problem: Field doesn't always exist

    What mxstu posted will do exactly what you're asking for.

    LEFT JOIN is also sometimes referred to as LEFT OUTER JOIN, but the OUTER is
    inferred. Basically, it's like adding a row to the Notes table that matches
    anything in the other table that doesn't already have a match in the Notes
    table. Oracle accomplished this with the (+) syntax.

    Here's some more reading on LEFT JOINs:
    [url]http://www.w3schools.com/sql/sql_join.asp[/url]
    [url]http://www.devguru.com/Technologies/jetsql/quickref/leftjoin.html[/url]

    Kronin555 Guest

  6. #5

    Default Re: Query Problem: Field doesn't always exist

    FYI, If you happen to be using a version of Oracle before 9.x, you can not use
    the JOIN syntax, but you can use the old proprietary (+) notation in the WHERE
    clause for the OUTER join. (Oracle 9 and above, you can use the ANSI-SQL 92
    JOIN syntax.)

    Something like this:

    SELECT someColumns
    FROM ticket a, survey b, notes c
    WHERE a.ticketid = b.ticketid
    AND a.ticketid = c.ticketid(+)

    Phil

    paross1 Guest

  7. #6

    Default Re: Query Problem: Field doesn't always exist

    I have one thing to add: I'm using Microsoft Access to accomplish this task, so I'm thinking the (+) syntax isn't going to work.

    Any ideas?
    RuBot Guest

  8. #7

    Default Re: Query Problem: Field doesn't always exist

    Oh, do what mxstu said right? Let me try that and I'll get back to everyone
    RuBot Guest

  9. #8

    Default Re: Query Problem: Field doesn't always exist

    Here's my Query, and here's my error:

    <cfquery name = "getSurveys" datasource="HelpDesk">
    select a.*, b.*, c.*
    from survey a inner join Help_Desk_List b
    on a.ticketID = b.entry_ID
    left join notestbl c
    on a.ticketID = c.ticket_ID
    </cfquery>

    "Syntax error (missing operator) in query expression 'a.ticketID = b.entry_ID
    left join notestbl c on a.ticketID = c.ticket_ID'. "

    An added bonus to this problem is that I have duplicate ticketIDs in the
    survey table '0' which represents random surveys generated unassociated with
    any tickets.

    I plan on doing this:

    <cfquery name = 'getSurvTicketID' datasource = 'HelpDesk'>
    select ticketID from survey
    </cfquery>
    <cfloop query = "getSurvTicketID">
    <cfif ticketID neq 0>
    <cfquery name = "getSurveys" datasource="HelpDesk">
    select a.*, b.*, c.*
    from survey a inner join Help_Desk_List b
    on a.ticketID = b.entry_ID
    left join notestbl c
    on a.ticketID = c.ticket_ID
    </cfquery>
    </cfif>
    </cfloop>



    RuBot Guest

  10. #9

    Default Re: Query Problem: Field doesn't always exist

    >"Syntax error (missing operator)
    If you're using Access, I think it requires parenthesis around multiple join
    statements. Your best bet is to check your Access documentation. It is
    probably something like ....


    Select ....
    from (survey a inner join Help_Desk_List b
    on a.ticketID = b.entry_ID)
    left join notestbl c
    on a.ticketID = c.ticket_ID

    >An added bonus to this problem is that I have duplicate ticketIDs in the
    survey
    >table '0' which represents random surveys generated unassociated with any
    tickets.
    >I plan on doing this:
    Why loop? If you just want to exclude records where the ticketID value = '0',
    then add a where clause to your query...


    SELECT ......
    FROM survey a .....
    WHERE a.ticketID <> '0'



    mxstu Guest

  11. #10

    Default Re: Query Problem: Field doesn't always exist

    It turns out that I can't try this because........the ticketID field in the
    Survey table is actually a string because I use a date/time (mmddyyhhmmss) for
    random survey id generation, and it won't save as a long.

    Have any ideas for a better random id generator that won't generate duplicates?

    RuBot Guest

  12. #11

    Default Re: Query Problem: Field doesn't always exist

    Have any ideas for a better random id generator that won't generate duplicates?

    With Access, you can use autonumber.

    Phil
    paross1 Guest

  13. #12

    Default Re: Query Problem: Field doesn't always exist

    Yeah, I have the autonumber set as the survey's id, and within each record it
    contains the ticketID which is the foreign link to the autonumbered entry_ID in
    the help_desk_list table, which in turn links to the autonumbered ticketID in
    the notes table, if a record exists.

    RuBot Guest

  14. #13

    Default Re: Query Problem: Field doesn't always exist

    Well, here's what I did. I changed the field back to numeric and used mmddhhmmss.
    RuBot 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