Quering rows in a table and outputing on single line

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

  1. #1

    Default Quering rows in a table and outputing on single line

    Hi,

    I would really appreciate any help with the following:

    I want to use a single query to extract the data below (this is a small
    sampling of the actual column list).
    Initially the results will be ordered by EmployeeName.
    The last line will contain the average days missed for these results - I can
    do this in a separate
    query if necessary.

    ISSUES:
    I need to be able to sort the data (ASC/DESC) by any/every column.
    The db table cannot be changed because of other programs using the same data.
    This data will be queried 100's of times per day by various users, so
    efficiency is a must.

    DB TABLE:

    EmployeeName EmpDaysMissed EmpWorkYear
    Fred Mertz 004 2004
    Fred Mertz 012 2005
    Lucy Ricardo 112 2004
    Lucy Ricardo 087 2005
    Ricky Ricardo 001 2004
    Lucy Ricardo 099 2003

    OUTPUT RESULTS: - results will be formatted inside an HTML table

    EmployeeName YR05 DM05 YR04 DM04 YR03 DM03
    Fred Mertz 2005 012 2004 004 2003 (blank)
    Lucy Ricardo 2005 087 2004 112 2003 099
    Ricky Ricardo 2005 012 2004 004 2003 (blank)
    AVERAGES: 037 040 099

    If there isn't any data for a particular year I need to leave that cell blank
    in the html table.

    The actual query will contain at least 10,000 records, therefore I can't run
    another query for each
    employee during the cfoutput query statement.

    There is a mainframe that sends a new file to the SQL server each night.
    SQL then deletes the current year data (in this case 2005) and uploads the
    flat file containing the most current data on the employees.

    mike bonjourno Guest

  2. Similar Questions and Discussions

    1. #25473 [Opn->Bgs]: Updating single row in table causing all rows in table to be updated.
      ID: 25473 Updated by: sniper@php.net Reported By: jim at bluedojo dot com -Status: Open +Status: ...
    2. #25473 [Fbk->Opn]: Updating single row in table causing all rows in table to be updated.
      ID: 25473 User updated by: jim at bluedojo dot com Reported By: jim at bluedojo dot com -Status: Feedback +Status: ...
    3. #25473 [Opn->Fbk]: Updating single row in table causing all rows in table to be updated.
      ID: 25473 Updated by: sniper@php.net Reported By: jim at bluedojo dot com -Status: Open +Status: ...
    4. #25473 [Opn]: Updating single row in table causing all rows in table to be updated.
      ID: 25473 User updated by: jim at bluedojo dot com Reported By: jim at bluedojo dot com Status: Open Bug Type: ...
    5. #25473 [NEW]: Updating single row in table causing all rows in table to be updated.
      From: jim at bluedojo dot com Operating system: WinXP PHP version: 4.3.3 PHP Bug Type: MySQL related Bug description: ...
  3. #2

    Default Re: Quering rows in a table and outputing on single line

    I think the best way to approach this is to scrunch your query into a 2D array
    and then output the array. That way it will continue to work after the
    available years change.

    It will take some trial and error (not mine of course) but the general idea
    will be:

    First, change the order by clause of your query so that it sorts by EmployeeID
    then by year, or whatever you call those fields. Next, do a Q of Q where you
    select the distinct year, ordered by year. You can also use Q of Q to get your
    averages.

    The first row of your array will be your display headers. Element[1]1[1] will
    be "Employee Name". You can loop through the first Q of Q to generate the rest
    of them.

    Then do something like this (I am going to use cfscript syntax)
    ThisGuy = MainQuery.EmployeeName[1];
    ThisRow = 2; // row 1 has the headers
    Years = ValueList(QofQ.year); // saves typing
    for (ii = 1, ii lte MainQuery.Recordcount; ii = ii +1) {
    if (MainQuery.EmployeeName[ii] is not ThisGuy) {
    ThisRow = ThisRow + 1;
    myArray[ThisRow][1] = ThisName;
    ThisGuy = MainQuery.EmployeeName[ii];
    }
    // 1st year has columns 2 and 3, 2nd has 4 and 5, etc
    Col1 = ListFind(Years, MainQuery.year[ii]) + 2;
    Col2 = Co1 + 1;
    MyArray[ThisRow][Col1] = MainQuery.Year[ii];
    MyArray[ThisRow][Col2] = MainQuery.DaysAway[ii];
    }

    But without any errors

    Dan Bracuk Guest

  4. #3

    Default Re: Quering rows in a table and outputing on single line

    Never do this kind of thing by looping and multiple queries.

    This is a very common problem and you will find much help on the SQL forums.
    For example [url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=6216[/url]

    If you are on SQL server 2005, it now has a PIVOT built into T-Sql.

    For SQL server 2000 or earlier, you can also easily link into Access and use
    transform to generate a cross tab query.


    MikerRoo Guest

  5. #4

    Default Re: Quering rows in a table and outputing on single line

    There's also a pivot workaround for SQL2K. Reply if you want the code, or check [url]www.sqlteam.com[/url] in the Forums there.
    philh 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