Trouble grouping query results

Hi,
I have 3 tables (Cars, Colors, and Cars_Colors). Cars_Colors
is a junction table to list all the possible combinations of cars
and their colors. Using joins, I produce the following results from
my query:
CAR_NUM,COLOR_NUM
Car1, Color1
Car1, Color2
Car2, Color1
Car3, Color1
Car4, Color1
Car4, Color2
Car4, Color3
Car5, Color3
Query works fine, my trouble is getting the results to
display how I want them to. I would like to display the results in
a single <table>, with a separate <tr> for each
distinct car, where that <tr> displays the car in the first
<td> and displays all of the colors associated with that car
in a second <td>. The colors should be separated by commas.
E.g.:
Car1 | Color1, Color2
Car2 | Color2
Car3 | Color1
Car4 | Color1, Color2, Color3
Car5 | Color3
Now, I was able to get it to work by first querying for the
distinct cars in the Cars_Colors table, then loop through each car
result and perform a second query on the colors associated with the
current car. But this surely can't be the most efficient method.
I'd like to use one query to produce the result set I listed
at the top... with all Car/Color combinations, then use Coldfusion
to display the output... without having to run another query every
time through the loop. I've tried using valuelist() but couldn't
get it to work correctly. I've also tried <cfoutput query
group> and <cfloop query>. I've come close with both, just
can't get the commas working right. I don't know how to keep the
comma from appearing at the end of the colors, e.g.:
Car1 | Color1, Color2
Anyone have a better way? I appreciate any help.
Thanks!

<CFQUERY name="myQuery"...>
SELECT .... from ... where...
ORDER BY car_num, color_num
</cfquery>
<cfoutput
query="myQuery" group="car_num">
<!--- concat the colors into a string for display --->
<cfset tempColors = "" /><cfoutput><cfset
tempColors = listAppend(tempColors, color_num, ", ")
/></cfoutput>
#car# - #ListChangeDelims(tempColors, ", ")#<br />
</cfoutput>
HTH,
CR

Similar Messages

  • Trouble understanding query results

    select SMS_R_System.Name from SMS_R_System order by SMS_R_System.Name
    The query produces simple results as expected:
    Name
    LABS-W7-TEST1 
    LABS-WXP-TEST1 
    LSDWD-SCMAPP1 
    LSDWD-UWSWS1 
    LSDWD-UWSWS2 
    But adding an additional item to the query starts producing unexpected duplicates:
    select SMS_R_System.Name, SMS_G_System_PROCESSOR.NumberOfLogicalProcessors from  SMS_R_System inner join SMS_G_System_PROCESSOR on SMS_G_System_PROCESSOR.ResourceId = SMS_R_System.ResourceId order by SMS_R_System.Name
    Why does this query produce a couple of duplicated results?
    Name                   Processor.Number of Logical Processors
    LABS-W7-TEST1   1 
    LABS-WXP-TEST1 1 
    LSDWD-SCMAPP1 2 
    LSDWD-UWSWS1 1 
    LSDWD-UWSWS1 1 
    LSDWD-UWSWS2 1 
    LSDWD-UWSWS2 1 
    The more items I add to the query, the more duplication. Even checking the Omit duplicate rows (select distinct) doesn't solve the problem:
    select distinct SMS_R_System.Name, SMS_G_System_SYSTEM.SMSID, SMS_G_System_PROCESSOR.NumberOfLogicalProcessors, SMS_G_System_X86_PC_MEMORY.TotalPhysicalMemory, SMS_G_System_LOGICAL_DISK.Size from SMS_R_System inner join SMS_G_System_PROCESSOR on SMS_G_System_PROCESSOR.ResourceID = SMS_R_System.ResourceId inner join SMS_G_System_X86_PC_MEMORY on SMS_G_System_X86_PC_MEMORY.ResourceID = SMS_R_System.ResourceId inner join SMS_G_System_LOGICAL_DISK on SMS_G_System_LOGICAL_DISK.ResourceID = SMS_R_System.ResourceId inner join SMS_G_System_SYSTEM on SMS_G_System_SYSTEM.ResourceID = SMS_R_System.ResourceId order by SMS_R_System.Name
    Name                  Configuration Manager GUID                                  
    ProcMemory      Logical Disk
    LABS-W7-TEST1   GUID:772F69EE-7882-4663-B31E-17378D13C159   1   3071544     51097  
    LABS-W7-TEST1   GUID:772F69EE-7882-4663-B31E-17378D13C159   1   3071544    
    LABS-WXP-TEST1  GUID:D50AC145-C365-4BC0-AB20-019015BCB883   1   1023472     51191  
    LABS-WXP-TEST1  GUID:D50AC145-C365-4BC0-AB20-019015BCB883   1   1023472    
    LSDWD-SCMAPP1   GUID:EEA484BD-4A9A-4DC2-B1DA-D61E41B2B5E7   2   12582388    102046 
    LSDWD-SCMAPP1   GUID:EEA484BD-4A9A-4DC2-B1DA-D61E41B2B5E7   2   12582388       
    LSDWD-UWSWS1    GUID:919F425D-1B39-4095-A668-80674D01939D   1   3145272     81816  
    LSDWD-UWSWS1    GUID:919F425D-1B39-4095-A668-80674D01939D   1   3145272    
    LSDWD-UWSWS2    GUID:CEAC8150-7BCC-4C93-8E7B-2CA6DF46B6BF   1   2096616     102398 
    LSDWD-UWSWS2    GUID:CEAC8150-7BCC-4C93-8E7B-2CA6DF46B6BF   1   2096616    
    What is the key to getting the latest record if multiple rows of the same information is stored in the database?

    Multiple rows will show for the same device if it has multiple CPUs (not to confuse with cores)...
    If a device has 2 physical CPUs, then 2 rows will exist in v_GS_PROCESSOR (TSQL equivalent): DeviceID0 = CPU0 & CPU1. Each CPU has x cores as per the field "NumberOfLogicalProcessors0".
    E.g. select top(100) DeviceID0, SocketDesignation0, NumberOfLogicalProcessors0, SystemName0 from v_GS_PROCESSOR order by systemname0 asc
    If you want to know how many logical processors (cores) and you were using TSQL, then you could use a inner join to only show systems that have cores (not null) and use "sum" to add them up in case of multiple rows. A "left" join would
    also show systems that have a null value for NumberOfLogicalProcessors0.
    You cannot use "sum" in WQL...
    select
    SYS.Name0,sum(CPU.NumberOfLogicalProcessors0) As 'NumProcessors'
    from v_R_System As SYS
    inner join v_GS_PROCESSOR As CPU on CPU.ResourceId = SYS.ResourceId
    group by SYS.Name0, CPU.NumberOfLogicalProcessors0
    order by SYS.Name0 Asc
    E.g. results
    LABS-W7-TEST1   1 
    LABS-WXP-TEST1 1 
    LSDWD-SCMAPP1 2 
    LSDWD-UWSWS1 2
    LSDWD-UWSWS2 2
    To get only one row (one CPU), you can use "GROUP BY".
    e.g.
    SELECT
    SMS_R_System.Name, SMS_G_System_PROCESSOR.NumberOfLogicalProcessors
    FROM
    SMS_R_System
    inner join SMS_G_System_PROCESSOR on SMS_G_System_PROCESSOR.ResourceId = SMS_R_System.ResourceId
    GROUP BY SMS_R_System.Name, SMS_G_System_PROCESSOR.NumberOfLogicalProcessors
    ORDER BY SMS_R_System.Name

  • Group by result in query

    how can i see the group by result in a query means show result also in groups i hope u guys understand wat i meann

    Mushy wrote:
    how can i see the group by result in a query means show result also in groups i hope u guys understand wat i meannJust select the GROUP BY columns. Not sure what you are exactly looking for. Lets say you have a table of employee and you want count of employee on each department you can do this
    SELECT deptno, count(*) FROM emp GROUP BY deptnoHere i have selected deptno and grouped by deptno.
    Is this what you are looking for?

  • Is it possible to list out CATALOG ITEMS GROUP in a Query Result of a Request Offering??

    Hi Experts,
    Is it possible to list out  CATALOG ITEMS GROUP as a result of Query Result in Request Offering ?? Because each and every Catalog Items Groups are being created as a SingleTon Child Class of System.CatalogItemGroup. i.e., Each CatalogItemGroup instance
    will have its own singleton class.
    Is it possible to list out all CatalogItemGroup Instances consolidatedly in the QueryResult Window??
    Though the System.CatalogItemGroup class is an Abstract class, I tried to list out the Classinstances via powershell command as below, which lists all catalog group instances, Note: Actually these are instances of SingleTon
    Child Classes
    "Get-SCClassInstance -Class (Get-SCClass -Name System.CatalogItemGroup)"
    But when I configured the QueryResult window with the "System.CatalogItemGroup" class, it doesn't list out any Group instances in the Porta.......
    Am I missing anything, Any suggestions please???
    Thanks and Regards, Narayana Babu

    Thanks Anton, I already tried that too... But it doesn't list out any Group instances in the Portal.
    Since each Catalog Groups are individual Single ton Class instances derived from "System.CatalogItemGroup" class. Therfore If I specify the internal ID of the abstract class "System.CatalogItemGroup" in the tag below, it doesn't list any in the
    Portal.... But if I specify ID of any one of the derived singleton class, it does displays the one instance of that particular class..
    Thanks and Regards, Narayana Babu

  • How can I get Numbers to return the first row in a group of VLOOKUP query results instead of last one?

    I'm trying to query from one table (call it Table1) a batch of rows in another table (Table2) using VLOOKUP on a date specified in the first table (Table1). My problem is it's returning the last incident in Table2 of the requested date instead of the first incident. This really breaks the OFFSET scheme I'd like to use to collect the rest of the items for that date. Is there some way to compel VLOOKUP to return the first row of query results, not the last?
    NOTE: I see I've asked this before, but forgot to go back and look at responses given. It's been a while and I've limped along until now with the way things were. I'm actually trying to delete this questions, so if you see it, ignore it. I suppose if someone can tell me real quick how to delete a stupid question, that might be helpful.

    you cannot delete a post yourself.  You can flag the post an request a moderator remove.

  • "group by" resulting in 'invalid number' error

    Hi,
    I have following query:
    SELECT from_locn from  appwms.PROD_TRKG_TRAN ptt 
    join
    (select locn_id,locn_brcd from appwms.locn_hdr where locn_brcd like '60-001-%') locn on locn.locn_id=ptt.from_locn
    where  ptt.TRAN_TYPE = 500
    This runs fine and displays results as below:
    FROM_LOCN
    0000317
    0000319
    0000319
    0000319
    0000319
    Now, adding a group clause results in error:
    SELECT from_locn from  appwms.PROD_TRKG_TRAN ptt 
    join
    (select locn_id,locn_brcd from appwms.locn_hdr where locn_brcd like '60-001-%') locn on locn.locn_id=ptt.from_locn
    where  ptt.TRAN_TYPE = 500
    group by from_locn
    ORA-01722: invalid number
    01722. 00000 -  "invalid number"
    *Cause:   
    *Action:
    Kindly let me know what I am missing here

    Hi,
    I don't see any obvious mistakes.
    Whenever you have a problem, post enough so that the people who want to help you can re-create the problem and test their ideas.  Include a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.Make sure the error occurs with the sample data you post.
    Explain, using specific examples, how you get those results from that data.
    Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
    See the forum FAQ: https://forums.oracle.com/message/9362002

  • How to use query-result as a new query-fiter

    HI, All
    Is it possible to execute sub-query filtererd by the pre-query-reslted lists ?
    As far as I investigated, only way to do is that I combine two suject-areas by using set operations like "Union" or around.
    But in this way, there is an impractical restriction to define these datatype and column in common. (namely, "Union" conditions)
    I just want to query filtered by ex-query-results at anytime; for example, I extract a customer lists by one query and use various analysis in the group of customer list.
    Please give me some advise.
    Regards

    First create a Request with your FilterQuerey. Secund Create a Request und create a Filter. Under Advanced you will find the path to the first Requst. Than you can select the columns.
    Thats it :-)
    Stefan

  • Query result in percentage

    Hello, I am lookg for the answer for the query result in percentage by doing a select statement.
    Thanks.
    The following works fine ....
    SELECT
    Territory, Sum(SalesDollars)
    FROM
    SALES
    GROUP BY
    Territory
    result:
    Ohio: $4000
    New York: $2000
    Now I need the result to return a percentage ...
    Ohio: 66.6%
    New York: 33.3%

    Hi,
    with Sales as(
      SELECT 'Ohio' AS Territory, 2000 as SalesDollars from dual
      UNION ALL
      SELECT 'Ohio', 2000 from dual
      UNION ALL
      SELECT 'New York', 1000 from dual
      UNION ALL
      SELECT 'New York', 1000 from dual
    SELECT Territory, Sum(SalesDollars),
      TO_CHAR(ratio_to_report(Sum(SalesDollars)) over (partition by 1) * 100, '99.9') || '%'
    FROM SALES
    GROUP BY Territory;Best regards,
    Dima

  • Optimize query results(timing) returned from custom search query

    Casey can you send the following to Oracle Support as soon as you can, we need their input on optimizing returning search results.
    To Oracle:
    We have noticed in the IdocScript reference guide that there is a way to optimize the query results returning to the user.
    Here is the scenario: We have created a custom web query where we send a query to the UCM and return a result using a custom template via url like so
    http://dnrucm.dnr.state.la.us/ucm/idcplg?IdcService=GET_SEARCH_RESULTS&QueryText=dDocTitle <matches> `AGREEMENT` <AND> xStateLeaseNum<matches>`00340`&ResultCount=30&SortOrder=Desc&SortField=dInDate&urlTemplate=/ucm/groups/public/documents/oos/hcst-search.hcst
    This works fine. The problem is that when a query is broader like
    http://dnrucm.dnr.state.la.us/ucm/idcplg?IdcService=GET_SEARCH_RESULTS&QueryText= xStateLeaseNum<matches>`00340`&ResultCount=30&SortOrder=Desc&SortField=dInDate&urlTemplate=/ucm/groups/public/documents/oos/hcst-search.hcst
    The query takes an extremely long time to execute and the results template sometimes never comes back, which seems like a timeout.
    Is there something else that we can do to optimize the query results?

    Hi John,
    What version of xMII are you using?
    Some things I would try:
    1. Clear the Java Web Start Cache - see if that makes any difference.
    2. If not, what happens if you save the query under a different name, and then try using it in a new transaction?
    3. Please feel free to enter you case into xMII Support via the SAP Support Portal if you are unable to make this work.
    Kind Regards,
    Diana Hoppe

  • Entity Attribute Existence Check - Query Result Parameter not possible ?

    I already read the section of the ADF BC Developer Guide (9.6.2 Implementing an Efficient Existence Check).
    That works and maybe is the best solution, however I am working with a group of Forms Developers which are much more comfortable with writing SQL statements.
    Natural thing for them in this case was to define a declarative compare validator with the following Query Result:
    select PROD_ID from Products where PROD_ID=:ProdId
    where :ProdId is ServiceRequest.ProdId
    Is this possible with ADF BC ?
    Other possibility is to define a List Validator with the following Query Result:
    select PROD_ID from Products
    but this is not performant.
    I know the programmatic approach is always possible, but to make migration easier for Forms developers into the ADF BC world, the declarative SQL option should be available.
    Thanks,
    Claudio.

    This isn't possible out of the box in 10.1.3 since we don't support a mechanism to set the values of the bind variables. In 10.1.3, you would need to build a custom validation rule class (explained in section 26.9 "Implementing Custom Validation Rules") to make this declarative for others on your team.
    I wouldn't recommend the list validator in 10.1.3 for anything but the most trivial uses. This is because it's current implementation retrieves all rows from the query, then does the attribute comparison in memory.
    Among many, many other enhancements, we are greatly enhancing declarative business logic features of ADF Business Components for our 11g/Fusion release (the next major release), where this will be super-easy.

  • List of query result in UDF

    Dear all expert,
    I make an UDF and put query to that UDF.  I want to make list of value of that query result is always appear when user click that UDF.
    It's working fine if, the result of query is 2 rows (the list of value is pop up) but if the result only 1 row, the list of value is not show up.
    The purpose to make this UDF and query is to give information to user. So, I need, the list is to show up

    try this
    SELECT T2.DocTotal as 'SPK Value', T0.DocNum 'Payment No', T0.DocDate 'Payment Date', T1.U_Progress, Sum(T1.U_Prog_Val) 'Progress Value', Sum(T1.U_Ret_Val) 'Retensi Value', T0.DpmAmnt 'Down Payment', T0.DocTotal 'Paid', T0.Comments
    FROM OPCH T0 INNER JOIN PCH1 T1 ON T0.DocEntry = T1.DocEntry INNER JOIN OPOR T2 ON T2.DocNum = T1.BaseRef
    WHERE T0.U_SPK = $[$U_SPK.number] and T0.CEECFlag = 'n'
    GROUP BY T2.DocTotal, T0.DocNum, T0.DocDate, T0.Comments, T0.DpmAmnt, T0.DocTotal, T1.U_Progress
    Union All
    SELECT 0,0, 0, '0', 0, 0, 0, 0, '0'
    Edited by: Suraj V on Jul 24, 2009 1:16 PM

  • BAM Data Control - Group query with Active Data Service

    Trying to get a group query from a BAM data control to work with Active Data Service in an ADF application (JDeveloper 11.1.1.4.0).
    With a flat query, as the data changes, I can see DataChangeEvents fired, resulting in a data push to the client -
    <BAMDataChangeEventFilter> <log>
    #### DataChangeEvent #### on [DataControl name=CEP_Person_DOB_Flat, binding=data.view_mainPageDef.FlatDOB1.view_pageDefs_FlatDOBViewPageDef_WEB_INF_FlatDOB_xml_FlatDOB.QueryIterator]
    Filter/Collection Id : 1966
    Collection Level : 0
    Event Id : 5
    ==== DataChangeEntry (#1)
    ChangeType : INSERT_AFTER
    KeyPath : [2157, 0]
    InsertKeyPath : [null, 0]
    AttributeNames : [id, _PersonKey, _County, _Surname, _AGE, _DOB, _Country, _FirstName]
    AttributeValues : [2157, 10008/129, Vagzukarbsm, Gnnfzxxyqfgpsijcr, 110, Thu Dec 26 00:00:00 GMT 1901, Ekcqvrkoksr, Vwhm]
    When I try a group query on the same data, currently just trying to group by _DOB for every 10 years to count the number of people, I get no data change events fired, so don't get any data pushed to the client, though the data has been changed if I refresh the page.
    Any ideas ?

    can you include bam and jdev versions and also include exception from logs?

  • How to set query result to emai content?

    Dears,
    Since user want to get the email with content like a grid format, just like a sql query result with some columns and rows.
    Is there any simply way to set the sql query result to the email content?
    Thanks!

    Create a transaction or modify the existing one which is sending the email.
    Take the output of the SQL Query action block and use an XSLT action block (lookup the ones in the Transformation portion of the query template you are using in the SQL Query action block to find the one with an HTML output) to convert it to HTML. 
    Go the Web group of action blocks and find the HTML Saver.
    You can save it to the Workbench (actually NW) by using either web://<yourprojectname>/WEB/<yourfoldername>/<yourfilename> or db://<yourprojectname>/WEB/<yourfoldername>/<yourfilename> (web:// publishes it, db:// does not)
    Play around with it a bit to see what will present the data the way you want. 
    Good luck,
    Mike

  • Incorrect query results with conformResultsInUnitOfWork

    Hi,
    has anybody experienced this:
    Take two classes User and Group
    Group has a 1:n Mapping to User (attribute users)
    User has a 1:1 Mapping to User (attribute partner).
    Following query returns too many objects
    User user1 = someUserObject;
    ReadAllQuery readAllQuery = new ReadAllQuery(Group.class);
    Expression e = builder.anyOf("users").get("partner").equal(user1);
    readAllQuery.setSelectionCriteria(e);
    readAllQuery.conformResultsInUnitOfWork();
    Vector vector = (Vector) unitOfWork.executeQuery(readAllQuery);
    It returns
    - the correct Group object as determined from the sql query +
    - any other objects of the same class that are fully instantiated (users is instantiated and for each user, partner is instantiated), even if they don't conform to the expression.
    The same query works properly ;
    - without conformResults
    - or if the other objects are not fully instantiated

    Hi,
    we need an workaround badly and the support is moving at exactly the rate I feared it would.
    So I thought, since we only hit this bug with existing objects and we use conformResultsInUnitOfWork because we want to find the newly created objects, is there a way to get
    - the SQL Query results
    + any new objects that conform to the query
    We still might get new objects that don't conform to the query (this is the bug) but I'll worry about that later if it happens.
    My first attempt at a solution looks like this:
    1. turn off conformResults
    2. manually add all new objects that conform to the query
    I had to guess a little for 2, since I don't know what exactly toplink does to select the "conform" objects.
    private static List findConformNewObjects(UnitOfWork unitOfWork, ReadAllQuery query) {
    List result = new ArrayList();
    Class resultClass = query.getReferenceClass();
    Expression selectionCriteria = query.getSelectionCriteria();
    Enumeration enumeration;
    IdentityHashtable newObjectsCloneToOriginal = unitOfWork.getNewObjectsCloneToOriginal();
    if (newObjectsCloneToOriginal != null && newObjectsCloneToOriginal.size() > 0)
    enumeration = newObjectsCloneToOriginal.keys();
    while (enumeration.hasMoreElements())
    Object o = enumeration.nextElement();
    if (resultClass.isInstance(o))
    if (selectionCriteria != null && selectionCriteria.doesConform(o, unitOfWork, null, query.getInMemoryQueryIndirectionPolicy()))
    result.add(o);
    return result;
    Does this look OK to you? Is there a better way to do it?
    Ana

  • Mapping query result

    Hello,
    I can't map string Display Name to the string property, on the User prompts tab I set prompt with type Query Result, on the Configure Prompts I configure this query by selecting AD User of Group -> Display Name, now I have Prompt Output called DisplayName
    (string). On the Map prompts I can't map this prompt to the string property, why? I just want to choose one user from AD and put it on the form as string.

    You will never get "simple values" from a query result. The option "3. Display Columns" are the properties on the class that you display on the portal. You will not be able to map a query result anywhere. You must attach it somehow to the request (in the
    last tab "4. Options").
    So even though the prompt output says "DisplayName (string)" you are still getting an object out of it. This makes more sense if you select more properties in 3.
    http://codebeaver.blogspot.dk/
    Thanks fro info, I have a script which reads custom string property of my custom service request class and translates it to 'AD user class' and makes it as Reviewer in the Review Activity. But instead of textbox field on the portal I need to use query,
    how can I get info from this service request about query to create AD user in my script?
    I placed query form on the portal to test, it shows only 2000 objects by default, is it possisble to show members of some OU or group in this field?

Maybe you are looking for