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

  1. #21

    Default Re: Query question

    What do the values in these columns represent ?
    REFER_TO_OTHER_PERSON_ID
    REFER_TO_OTHER_PERSON_ID_2


    mxstu 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. #22

    Default Re: Query question

    I use REFER_TO_OTHER_PERSON_ID and REFER_TO_OTHER_PERSON_ID_2 to refer to the ID. So in my example Martin refers to ID 1 which is Jack.

    Samall Guest

  4. #23

    Default Re: Query question

    In this type of situation you usually use a self-referencing table join, but
    why you have two different columns to refer back to the ID column? How are
    these records related to each other? (ex.. Is Jack a supervisor and Martin is
    his employee and James is an employee of Martin?)

    mxstu Guest

  5. #24

    Default Re: Query question

    No this is just an example. I will adjust my example to this:

    ID
    NAME
    REFER_TO_OTHER_PERSON_ID


    1, jack, NULL
    2, bryan, NULL
    3, martin, 1

    What I want in the output is:
    JACK
    --martin (REFER_TO_OTHER_PERSON_ID -> JACK which is ID 1)
    BRYAN



    Samall Guest

  6. #25

    Default Re: Query question

    ....

    (SQL is not tested)

    If you want to return Jack, even if no other records refer to his ID ....
    SELECT p.ID AS SupervisorID, p.Name AS SupervisorName,
    s.ID AS EmployeeID, s.Name AS EmployeeName
    FROM YourTable p LEFT JOIN YourTable s ON p.ID =
    s.REFER_TO_OTHER_PERSON_ID
    WHERE p.ID = 1

    Otherwise,
    SELECT p.ID AS SupervisorID, p.Name AS SupervisorName,
    s.ID AS EmployeeID, s.Name AS EmployeeName
    FROM YourTable p INNER JOIN YourTable s ON p.ID =
    s.REFER_TO_OTHER_PERSON_ID
    WHERE p.ID = 1

    mxstu Guest

  7. #26

    Default Re: Query question

    Argh! I hate the formatting in the attach code option
    mxstu Guest

  8. #27

    Default Re: Query question

    No that is not what I'm looking for. I will adjust my example to this:

    ID
    NAME
    REFER_TO_OTHER_PERSON_ID


    1, jack, NULL
    2, bryan, NULL
    3, martin, 1
    4, bla, NULL
    5, bla2, NULL
    6 bla3, 2
    etc..

    What I want in the output is:
    JACK
    --martin (REFER_TO_OTHER_PERSON_ID -> JACK which is ID 1)
    BRYAN
    --bla3 (REFER_TO_OTHER_PERSON_ID -> BRYAN which is ID 2)
    BLA
    BLA2
    etc...

    In your example I'm stuck to: WHERE p.ID = 1. I want to show the complete DB
    table.


    Samall Guest

  9. #28

    Default Re: Query question

    Okay. That was only an example using the sample data provided. Try the
    attached code. As in the last example, only records that do not refer to
    another record (i.e. REFER_TO_OTHER_PERSON_ID IS NULL) are returned.

    It's important to note that the CFOUTPUT groups the results by the same column
    used in the ORDER BY clause of the SQL statement. So if you change the ORDER BY
    clause, you need to to change the CFOUTPUT GROUP attribute to match.



    <cfquery name="getData" datasource="YourDSN">
    SELECT p.ID AS SupervisorID, p.Name AS SupervisorName,
    s.ID AS EmployeeID, s.Name AS EmployeeName
    FROM YourTable p LEFT JOIN YourTable s ON p.ID = s.REFER_TO_OTHER_PERSON_ID
    WHERE p.REFER_TO_OTHER_PERSON_ID IS NULL
    ORDER BY p.ID, s.ID
    </cfquery>

    <cfoutput query="getData" group="SupervisorID">
    <b>#SupervisorName#</b><br>
    <cfif EmployeeName is not "">
    <cfoutput>--------#EmployeeName#<br></cfoutput>
    </cfif>
    </cfoutput>

    mxstu Guest

  10. #29

    Default Re: Query question

    Thanks mxstu! That is working fine.
    Can I ask one more question. Let's say I want to output this:

    What I want in the output is:
    JACK
    --martin
    BRYAN
    --some_name_2
    ------some_name_3
    JAMES
    etc...

    Is that possible on the DB table I now have? or do I need to create an extra
    field in my DB table?

    Samall Guest

  11. #30

    Default Re: Query question

    Is two levels the limit or do you forsee a need to display additional levels
    ..... ?

    JACK
    --martin
    BRYAN
    --some_name_2
    ------some_name_3
    ------------some_name_4
    -----------------some_name_5
    JAMES
    etc...

    mxstu Guest

  12. #31

    Default Re: Query question

    Hi,
    3 levels is the limit.

    JACK level1
    --martin level2
    BRYAN level 1
    --some_name_2 level2
    ------some_name_3 level3
    etc..

    Thanks!
    Samall Guest

  13. #32

    Default Query question

    Hello, Can someone help me on this point:
    I have a simple DB table (ID, NAME, REFER_TO_OTHER_PERSON_ID).
    ---------------------
    The content of the DB table is:
    1, jack, NULL
    2, bryan, NULL
    3, martin, 1
    4, bla, NULL
    5, bla2, NULL
    6 bla3, 2
    etc..
    ---------------------
    What I want to output is:
    JACK
    --martin (REFER_TO_OTHER_PERSON_ID -> JACK which is ID 1)
    BRYAN
    --bla3 (REFER_TO_OTHER_PERSON_ID -> BRYAN which is ID 2)
    etc...
    ---------------------
    The query to do that is:
    <cfquery name="getData" datasource="YourDSN">
    SELECT p.ID AS SupervisorID, p.Name AS SupervisorName,
    s.ID AS EmployeeID, s.Name AS EmployeeName
    FROM YourTable p LEFT JOIN YourTable s ON p.ID = s.REFER_TO_OTHER_PERSON_ID
    WHERE p.REFER_TO_OTHER_PERSON_ID IS NULL
    ORDER BY p.ID, s.ID
    </cfquery>

    <cfoutput query="getData" group="SupervisorID">
    <b>#SupervisorName#</b><br>
    <cfif EmployeeName is not "">
    <cfoutput>--------#EmployeeName#<br></cfoutput>
    </cfif>
    </cfoutput>

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

    No here is the problem. Let's say I want to output: 3 levels:

    JACK
    --martin
    BRYAN
    --some_name_2
    ------some_name_3
    JAMES
    etc...

    Is that possible on the DB table I now have? or do I need to create an extra
    field in my DB table?


    Samall Guest

  14. #33

    Default Query question

    Please can somebody help me with this though query

    I have 2 fields in my table: owner and machine
    none of them are unique

    I want to do a query - as input I have a list of owners
    as output I need the machines, but only one

    jack radio
    jack computer
    jack cdplayer
    bill radio
    bill shoes
    larry shoes
    larry trouser
    larry gps
    kate cdplayer
    kate car

    as input I give 'jack AND bill AND larry'
    as output I need 'radio computer cdplayer shoes trouser gps'

    Can anybody help me?


    Jaak Guest

  15. #34

    Default Re: Query question

    Jaak,
    you can use the keyword distinct in your select. Then you don't get
    duplicate data rows.

    select distinct owner, maschine from table where owner in
    ('jack','bill','larry')

    or if you only need the maschines

    select distinct maschine from table where owner in ('jack','bill','larry')

    brgd,
    Birthe


    Jaak wrote:
    > Please can somebody help me with this though query
    >
    > I have 2 fields in my table: owner and machine
    > none of them are unique
    >
    > I want to do a query - as input I have a list of owners
    > as output I need the machines, but only one
    >
    > jack radio
    > jack computer
    > jack cdplayer
    > bill radio
    > bill shoes
    > larry shoes
    > larry trouser
    > larry gps
    > kate cdplayer
    > kate car
    >
    > as input I give 'jack AND bill AND larry'
    > as output I need 'radio computer cdplayer shoes trouser gps'
    >
    > Can anybody help me?
    Birthe Gebhardt Guest

  16. #35

    Default Query Question

    I have a query that pulls counts on how many people sign up for my mailing list
    each day. If no one signs up on a particular day, I don't get a row for it. How
    do I test for the missing days and add them with 0 for the day?





    SELECT datevalue(addDate) AS addDate, COUNT(dateValue(addDate)) as dailyTotal
    FROM email
    WHERE status = 1 AND addDate BETWEEN #startDate# AND #today# GROUP BY
    dateValue(addDate)

    hertelt Guest

  17. #36

    Default Re: Query Question

    Join your results to a query that selects a series of all dates, such as (for
    Oracle):

    select trunc (sysdate - rownum) date_series
    from all_objects
    where trunc (sysdate - rownum) between startDate and today

    FYI all_objects is just a table with lots of rows. You can use anything that
    has more rows than you expect to require.

    JR

    jonwrob Guest

  18. #37

    Default Re: Query Question

    jonwrob's approach will only work if a table exsists that actually has every
    single date you need. I don't know that much about Oracle, so I'll take his
    word for it that all_objects has every date you need.

    But you might not be on oracle. If that's the case, I suggest a daily job
    that looks for records the previous day, and adds the record as necessary.
    Once you have that running, write a template for a one off job that fills in
    your blanks.

    For this one off job, get a list of all the dates you have, compare it all the
    dates in your date range of interest, and produce a list of missing dates.
    Loop through this list and add your missing records.

    Dan Bracuk Guest

  19. #38

    Default Re: Query Question

    There are better ways in PostgreSQL, Oracle and MS SQL 2005.

    But, Here's a universal method (in MS Access syntax since that looks to be
    what you're using):
    You will need to create a new, static table in your DB. Once it's created,
    it doesn't ever need to be touched again.
    Create the table "tDigits".
    It will have one column, "iDigit". Make this column an integer (NOT long
    integer) and set it as the primary key.
    Fill tDigits with ten rows, containing the digits 0 thru 9. This table must
    have EXACTLY those rows and no others.
    When that is done, the attached query will do what you seek as long as the
    start and end dates are within 99 days of each other.
    It should be obvious how to extend the coverage to 999 days but performance
    suffers exponentially as the range magnitude is increased.



    SELECT
    tDaysInQuestion.dtSpanDay AS addDate
    , COUNT (EE.addDate) AS dailyTotal
    FROM
    (
    SELECT
    DateValue (DateAdd ('d', ONES.iDigit + 10 * TENS.iDigit,
    #startDate#)) AS dtSpanDay
    FROM
    tDigits AS ONES,
    tDigits AS TENS
    WHERE
    DateAdd ('d', ONES.iDigit + 10 * TENS.iDigit, #startDate#) BETWEEN
    #startDate# AND #today#
    )
    AS tDaysInQuestion
    LEFT JOIN
    email AS EE ON (tDaysInQuestion.dtSpanDay = DateValue (EE.addDate)
    AND status = 1)
    GROUP BY
    tDaysInQuestion.dtSpanDay

    MikerRoo Guest

  20. #39

    Default Query question

    is it pssable to retrieve a row from my database table querry?
    i check to see if the name exists.....
    if it does i get his ID, name, pass ,email from his row.......

    for example u use
    <cfif #QryNmae.recordset# is '1'>
    <--- return the existing name with his information --->
    </cfif>

    duces_wild Guest

  21. #40

    Default Re: Query question

    select yourfields,
    from yourtables
    where lower(name) = 'john smith'

    However, name searches have this pesky little problem of two people having the same name.
    Dan Bracuk 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