Using DISTINCT for some but not others in 1 query

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

  1. #1

    Default Using DISTINCT for some but not others in 1 query

    I feel really stupid.

    That doesn't happen too often..

    but I feel really stupid.

    SQL is completely kicking my face in right now.
    So to get on with my question... (I'm kinda new to all this)

    I have a gigundormous databse. I understand that if I do something like
    "SELECT DISTINCT CATAGORY_NAME PRODUCT_NAME" that it will not work, because
    although there are duplicats in CATAGORY_NAME, obviously the PRODUCT_NAME's are
    all going to be unique, therefore it will retrive DISTINCT for neither. This
    being true, I need someway to select CATAGORY_NAME distinctly, and also SELECT
    PRODUCT_NAME. Why not use two different queries? Good question, the reason is
    because I need to <cfoutput query=""> to get that loop effect going.. and if I
    can only output one query at a time (CF doesn't support nested queries, or
    atleast that's what it told me when I tried to do it) then I need to find
    another way to do it.

    If it'll help, instead of telling you I need to do this selecting, I'll simply
    tell you what I'm trying to do and see if you can help me there (if not, the
    answer to the first question would be just STUPENDIOUS!).

    I have a ginormous table. (important) Fields include "CatName" "Brand" and
    "ProductName". I need to be able to display it something like the following:

    --CatName1---- --CatName2---- --CatName3----
    ---product 1--- ---product 1--- ---product 1---
    ---product 2--- ---product 2--- ---product 2---
    ---product 3--- ---product 3--- ---product 3---
    ---product 4--- ---product 4--- ---product 4---
    ---product 5--- ---product 5--- ---product 5---
    -click for more- -click for more- -click for more-

    I have the formatting for the table worked out. Basically it puts a <tr> when
    currentrow mod 4 eq 1 and it puts a </tr> when currentrow mod 4 eq 0. In other
    words, it gives me 4 CatNames per row. I'm sure you can see where I'm running
    into a problem. Since there are only about 40 catagories and 3500 products, if
    the CatName is not unique, I'm going to get 3500 listings. But if I don't ALSO
    select the ProductName in this same query, it's not going to loop, and only the
    first set of 40 catagories is going to have products listed under it.

    The 2nd problem I'm having is how to get the first 5 products (alphebetically)
    under each CatName to display, although I figure I'll try this problem when I
    come to it. I was thinking a GROUP BY CatName ORDER BY ProductName and then do
    a little ditty to just pull the first 5.

    Anyway, any light on any of these subjects would be wonderful. I think I'm way
    over my head. I love you guys!

    fidibidabah Guest

  2. Similar Questions and Discussions

    1. SQL Distinct
      I want to select distinct value from one field of the table and select rest of the corresponding fields accordingly. I have a table that has fields:...
    2. Query distinct month and date
      ASP 3.0 VB - access We are trying to take a date field and pull the month and year into a drop down menu. I am using access and can format the...
    3. need help on DISTINCT
      Hi ALl, I need some help on DISTINCT in a query I'm trying to run. I have two tables which are linked. This is the query I use: SQL = "SELECT...
    4. SELECT DISTINCT + ORDER BY gives ERROR 145: ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
      Following is my stored procedure. If I take the DISTINCT out then everything works fine. BUT I need the distinct because it returns duplicate...
    5. SELECT DISTINCT + ORDER BY gives ERROR 145: ORDER BY items mustappear in the select list if SELECT DISTINCT is specified.
      Dan, You should be able to do this: SELECT Id, FaxID, ReceivedTime, Pages FROM ( SELECT DISTINCT .Id AS Id,
  3. #2

    Default Re: Using DISTINCT for some but not others in 1 query

    I don't think you need to consider "distinct" at all

    If you do
    SELECT CATAGORY_NAME, PRODUCT_NAME

    Using

    ORDER BY SELECT CATAGORY_NAME, PRODUCT_NAME

    Then in the cfoutput query tag use the group attribute

    <cfoutput query="queryName" group="CATAGORY_NAME">

    The tricky part would be outputting as you want.

    But you could also use 2 queries

    <cfoutput query="query1Name">
    #CATAGORY_NAME#
    <cfloop query="query2Name">
    <!--- need to check that the category name or id is the same --->
    <cfif query1Name.CatID IS query2Name.CatID>
    #PRODUCT_NAME#
    </cfif>
    </cfif>
    </cfoutput>

    Ken

    The ScareCrow Guest

  4. #3

    Default Re: Using DISTINCT for some but not others in 1 query

    I see. I don't understand it even a little, but I'll play around with it.

    I assume when you said "ORDER BY SELECT CATAGORY_NAME, PRODUCT_NAME" you ment
    "ORDER BY CATAGORY_NAME, PRODUCT_NAME"? Cos I'm pretty sure you can't order by
    select :) I also didn't know you could order by two different things (I guess
    in that priority?), that's very interesting. Plus that cfloop thing I've never
    heard of either. I'll start trying stuff and see what I come up with. Thanks :)

    fidibidabah Guest

  5. #4

    Default Re: Using DISTINCT for some but not others in 1 query

    Yes, I forgot to delete the "SELECT" from the copy & paste.

    You can have any valid column in the select list, you can also set the order
    for each column (Asc or Desc)
    In ms access and sql server you can also apply functions to them. Don't know
    about other db's as I don't use them.

    If you forget about the output for the moment, you can do this

    Select catagory_name, product_name
    From Categories Inner Join Products On categories.CatID = Products.CatID
    Order By catagory_name, product_name

    Then in cf

    <cfoutput query="QueryName" group="catagory_name">
    #category_name#
    <br>
    <cfoutput>
    #product_name#<br>
    </cfoutput>
    </cfoutput>

    Ken

    The ScareCrow Guest

  6. #5

    Default Re: Using DISTINCT for some but not others in 1 query

    Hm, well this is interesting. Your two query thing worked out almost the way I
    wanted.

    The problem is that the first catagory is coming up fine with the list of
    products under it, but the rest of the catagories are all coming up right, but
    with the first catagories products under it! So i have a perfect catagory
    listing, and then the same products under it for each one. To help you, let me
    paste my exact code:

    <cfquery name="cat" datasource="source">
    SELECT DISTINCT CATNAME
    FROM TABLE
    ORDER BY CATNAME
    </cfquery>

    <cfquery name="product" datasource="source">
    SELECT CATNAME, NAME
    FROM TABLE
    ORDER BY NAME
    </cfquery>

    those are the queries, and then the following is the ouput:

    <table>
    <cfoutput query="cat" group="catname">
    <cfif cat.currentrow mod 4 eq 1>
    <tr>
    </cfif>
    <td>
    <div>
    #catName#<br>
    <cfloop query="product"><cfif cat.catName IS
    product.catName> #name#<br></cfif>
    </cfloop>
    </div>
    </td>
    <cfif cat.currentrow mod 4 eq 0>
    </tr>
    </cfif>
    </cfoutput>
    </table>

    I removed almost all the formatting and tag attributes (to poke at the code
    easier). Any more help would be wonderful :( I feel I'm half way there tho!

    fidibidabah Guest

  7. #6

    Default Re: Using DISTINCT for some but not others in 1 query

    That QUERY look fancy... I'm just trying to understand it.. I thought you could
    only join if you had multiple tables? Is that what "Inner Join" means? To join
    things with comment attributes within the same table? That would be really
    awesome. The part I'm confused about... your query there says From Categories
    (which I assume is suppose to be the table name), and then 'cetagories.catID =
    products.catID' but that doesn't make sense to me. I don't have a table called
    "products". The categoryname and productname are both in the same table :(
    Sorry, maybe I'm being dense here..

    I really do appreciate your time Ken :)

    fidibidabah Guest

  8. #7

    Default Re: Using DISTINCT for some but not others in 1 query

    I think you should do som research on db development, but the following should
    get you going

    <cfquery name="cat" datasource="source">
    SELECT CATNAME, NAME
    FROM TABLE
    ORDER BY CATNAME, NAME
    </cfquery>
    <table>
    <cfoutput query="cat" group="catname">
    <cfif cat.currentrow mod 4 eq 1>
    <tr>
    </cfif>
    <td>
    <div>
    #catName#<br>
    <cfoutput>
    #name#<br>
    </cfoutput>
    </div>
    </td>
    <cfif cat.currentrow mod 4 eq 0>
    </tr>
    </cfif>
    </cfoutput>
    </table>

    Ken

    The ScareCrow Guest

  9. #8

    Default Re: Using DISTINCT for some but not others in 1 query

    If I get your question right, I just had the same problem.

    I tried, in your case:

    SELECT DISTINCT CATAGORY_NAME, PRODUCT_NAME

    And it seemed to choose only the first one as distinct. If you need two as
    distinct and one as not distinct, I'm not sure... But unless I've got it
    figured wrong in my case or it's not the way it's supposed to work, the line
    above should choose the CATAGORY_NAME as Distinct and not PRODUCT_NAME.

    I hope this helps.


    lther Guest

  10. #9

    Default Re: Using DISTINCT for some but not others in 1 query

    Thank you so much. I decided to deal with it a slightly different way which is
    much easier on the brain. I really do appreciate all your help tho', I've
    learned a lot from it. As I said, I'm a begining with all this stuff, but I'm
    learning quick (knew nothing about MySQL, SQL language, PHP, or CFMX7.. nothing
    at all, a week ago).

    I do appreciate your help, and I think this app is going to turn out just fine.

    Thanks!

    fidibidabah Guest

  11. #10

    Default Re: Using DISTINCT for some but not others in 1 query

    Just thought I would clarify the "Distinct" keyword

    This key word effects every column in the select list, so in the example given
    here it would return rows with the distinct column pair. If there was 3
    columns it would return the distinct tuples.

    Ken

    The ScareCrow 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