Trouble with LIKE statement

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

  1. #1

    Default Trouble with LIKE statement

    Hey, all -

    I'm trying to create a CF report where a QoQ provides a total of times an
    entry appears in a column from Access. To make it simple, there are different
    users (Nate, Sarah, and Matt) that could be in the fields for the category in
    question (User_Comp). Sometimes it's just one user's name, sometimes two or
    even all three appear. I want to make a table that will list how many times
    each user's name appears. I can get it to list out the total amount that the
    field only holds *one* of the users' names (such as Nate), by using an equals
    sign in the WHERE statement, but I can't get it to include the ones where
    multiple names appear in the field (such as Matt, Sarah, Nate). I've been
    trying to use LIKE (and I've tried CONTAINS), but both attempts give me an
    error:

    null null
    The error occurred on line -1.

    LIKE seems to be the operator to use, but I can't see what I'm doing wrong.
    Here's a simplified look at the code - any help would be GREATLY appreciated.
    I'm concerned that I'm just overlooking something small...

    ---------------

    <CFOUTPUT QUERY="GetType" GROUP="Public_priv">

    <CFQUERY DATASOURCE="lister" NAME="getall">
    SELECT *
    FROM questionaire
    WHERE
    Class='#class#'
    AND public='#public#'
    AND month = '#month#'
    </CFQUERY>

    <!---------------------------------------------------------------------------
    | Get counts for each user.
    ---------------------------------------------------------------------------->


    <CFQUERY DBTYPE="query" NAME="user_Nate">
    SELECT User_Comp
    FROM getall
    WHERE User_Comp LIKE '%Nate%'
    </CFQUERY>


    <!---------------------------------------------------------------------------
    | Print Table cell data.
    ---------------------------------------------------------------------------->

    <TR>
    <TD> #Class_Full# &nbsp;
    <TD> #public_priv# &nbsp;

    <CFIF user_Nate.recordcount EQ "0">
    <TD ALIGN="right"> N/A
    <CFELSE>
    <TD ALIGN="right"> #(user_Nate.recordcount)# &nbsp;
    </CFIF>


    </CFOUTPUT>

    Dionubis Guest

  2. Similar Questions and Discussions

    1. If Statement???
      guys i was wondering if anybody here could help me. I have a page done up with all dynamic text and attributes on it that come from a management...
    2. Trouble with INSERT statement
      I'm trying to create my very first INSERT statement, but I keep getting an error and I can't figure out why. The error: Syntax error in INSERT...
    3. VisualAge 6 installation trouble on aix 5.2, gcc trouble too :)
      I've installed gcc 3.x from UCLA site. and I got this error - In file included from test.c:23: /usr/include/pthread.h:554: error: parse error...
    4. Can i say OR in an if/then statement
      I am trying to say if (condition) is true OR (condition) is true THEN Can I use OR or do ihave to break it down into an elseif statement? I...
    5. IIF statement
      Assuming the data is being entered via a form, then use the AfterUpdate event of the control that is bound to the signature field. The code you need...
  3. #2

    Default Re: Trouble with LIKE statement

    How about

    names = (Nate,Sara,Matt);

    SELECT User_Comp
    FROM getall
    WHERE User_Comp in (#listqualify(names)#)

    Dan Bracuk Guest

  4. #3

    Default Re: Trouble with LIKE statement

    Won't that just adjust the way the string is listed? I would like to total up
    the number of times, say, Nate's name appears. So, for example, these are the
    fields in User_Comp:

    Nate
    Nate,Sarah
    Sarah,Matt
    Sarah,Matt,Nate

    I would want a table listing: Nate - 3, Sarah - 3, Matt - 2.

    Will ListQualify do that for me?


    Dionubis Guest

  5. #4

    Default Re: Trouble with LIKE statement

    Dionubis,

    Are you storing delimited lists of information in a single column? I have to
    tell you that is a bad table design. Aside from violating database modeling
    recommendations, it is difficult to work (as you've noticed) and inefficient to
    query If you were using a more normalized structure, your current problem
    would be solved quite easily and the query would most likely be faster. The
    data should be stored in rows, like:

    QuestionaireID | UserID
    -------------------------------------
    1 | 23 <-- Nate
    2 | 23 <-- Nate
    2 | 16 <-- Sarah
    3 | 16 <-- Sarah
    3 | 8 <-- Matt

    Then you could retrieve the totals with a simple join and a COUNT(*) .




    mxstu Guest

  6. #5

    Default Re: Trouble with LIKE statement

    just use "Cfloop" and run thru the query as many time as you request
    jorgepino Guest

  7. #6

    Default Re: Trouble with LIKE statement

    Yes, sadly it is being stored as such - it was created before I took on the
    job, and I'm trying to see if I can take care of it without disrupting the
    entire system they've set up...

    I'll try out the cfloop suggestion, but I have a feeling I might have to go
    the UserID route...

    Thanks for your help!

    - D

    Dionubis Guest

  8. #7

    Default Re: Trouble with LIKE statement

    Ugh. Sorry to hear that. Don't know what the original designers were
    thinking.

    I'll play around with it too and see if I can come up with anything. I think
    jorgepino is correct though. You will probably need to loop through the
    results, using ListFindNoCase() or ReFindNoCase(), total up the number of
    occurrences for each name. Once you get the counts, you want to display them
    in an HTML table right?

    mxstu Guest

  9. #8

    Default Re: Trouble with LIKE statement

    Might be worth the time and effort.

    Originally posted by: Dionubis
    Yes, sadly it is being stored as such - it was created before I took on the
    job, and I'm trying to see if I can take care of it without disrupting the
    entire system they've set up...

    I'll try out the cfloop suggestion, but I have a feeling I might have to go
    the UserID route...

    Thanks for your help!

    - D



    Dan Bracuk Guest

  10. #9

    Default Re: Trouble with LIKE statement

    Originally posted by: mxstu
    Once you get the counts, you want to display them in an HTML table right?

    Yeah, exactly.
    Dionubis Guest

  11. #10

    Default Re: Trouble with LIKE statement

    And the list of names is delimited by a comma, and each name only appears once
    in the column?

    For example, you could have this in the same column: Sarah,Matt,Nate
    but not this: Nate,Sarah,Matt,Nate

    mxstu Guest

  12. #11

    Default Re: Trouble with LIKE statement

    ..

    <cfset nameStruct = structNew()>
    <cfloop query="getAll">
    <cfloop list="#User_Comp#" index="currName">
    <cfif StructKeyExists(nameStruct, currName)>
    <cfset nameStruct[currName] = val(nameStruct[currName]) + 1>
    <cfelse>
    <cfset nameStruct[currName] = 1>
    </cfif>
    </cfloop>
    </cfloop>

    <cfdump var="#nameStruct#">

    mxstu Guest

  13. #12

    Default Re: Trouble with LIKE statement

    Originally posted by: mxstu
    And the list of names is delimited by a comma, and each name only appears once
    in the column?

    For example, you could have this in the same column: Sarah,Matt,Nate
    but not this: Nate,Sarah,Matt,Nate

    Right - it would be in the same order, as in Sarah's name would always appear
    before Matt's name, etc. - but they won't always all be there. It might be
    Sarah,Matt,Nate; it could be Matt,Nate; or Sarah,Nate.

    I'll try your suggested code. I won't be able until Monday - thanks for your
    help.

    Dionubis 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