Results 1 to 11 of 11

Thread: [RESOLVED] cannot DISTINCT 1 field And 2 Fields at the same time why?

  1. #1

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Resolved [RESOLVED] cannot DISTINCT 1 field And 2 Fields at the same time why?

    hey
    i am trying to Distinct and load 1 field to the listview and another one as regular but it is not working why?
    what i am missing ?
    this is my code
    Code:
    Select DISTINCT PriceName
    Code:
    Dim RsQ As New ADODB.Recordset
            
    RsQ.Open "Select DISTINCT PriceName,PriceCost From PriceList order by PriceName", CN
            Do While Not RsQ.EOF
            Set itm = FrmTipulimToGeneralSale.LTreat.ListItems.Add(, , RsQ!PriceName, 1)
                itm.SubItems(1) = FormatCurrency(RsQ!PriceCost)
            RsQ.MoveNext
            Loop
     RsQ.Close
    tnx for any help
    salsa31

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,743

    Re: cannot DISTINCT 1 field And 2 Fields at the same time why?

    Do you really have items in the table with the same PriceName?
    Can multiple records with the same PriceName have a different PriceCost?
    If so, how would you know which PriceCost you want to display?

  3. #3

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: cannot DISTINCT 1 field And 2 Fields at the same time why?

    Quote Originally Posted by Arnoutdv View Post
    Do you really have items in the table with the same PriceName?
    Can multiple records with the same PriceName have a different PriceCost?
    If so, how would you know which PriceCost you want to display?
    same price name yes
    how would you know which PriceCost you want to display?
    that is why i want to DISTINCT

  4. #4
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,743

    Re: cannot DISTINCT 1 field And 2 Fields at the same time why?

    Let's fabricate an example of your table:
    Code:
    PriceName   PriceCost
    Red         10
    Red         10
    Red         12
    Red         14
    Red         14
    Blue        20
    Blue        20
    Blue        23
    Blue        23
    Which output do you want?

  5. #5

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: cannot DISTINCT 1 field And 2 Fields at the same time why?

    each pricename has as Diffrent PriceCost

    in your E.X there are 5 reds and 4 blue
    i need my list to show 1 red and 1 blue
    thats why i need to use the distinct

  6. #6
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,743

    Re: cannot DISTINCT 1 field And 2 Fields at the same time why?

    Code:
    PriceName   PriceCost
    Red         10
    Red         12
    Red         14
    Blue        20
    Blue        23
    Which output do you want?

    Distinct PriceName will give you:
    - Red
    - Blue

    Which PriceCost should be associated with the unique values?

  7. #7

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: cannot DISTINCT 1 field And 2 Fields at the same time why?

    which output do you want?

    Distinct pricename will give you:
    - red
    - blue

    which pricecost should be associated with the unique values?
    yes sir

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: cannot DISTINCT 1 field And 2 Fields at the same time why?

    If you select distinct pricename ... you will get Red & Blue ... but you need more than that, right? OK. Problem is you also have PriceCost in there... how do you know which ONE of those you want? There's nothing to make them unique.

    Ideally you should have had two tables... one with PriceName and an ID ... the other with ID, PriceNameID, PriceCost, and possibly some kind of date. that will then let you get back to a specific date and identify the specific price in effect.

    LEss than ideal, you have it all in one table... PriceName, PriceCost and a date...

    In both cases you would group by price name and get the max date... then join that back to the table on both fields and that would give you all of the PriceCosts for each of the PriceNames.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,454

    Re: cannot DISTINCT 1 field And 2 Fields at the same time why?

    Quote Originally Posted by salsa31 View Post
    yes sir
    I take this "yes" as:
    Yes the PriceCosts shall be "associated" with the (distinct) PriceName.

    And since there are different (multiple) PriceCosts you will have to somehow
    "gather" or "aggregate" behind the unique PriceNames - why not use the
    SQL-construct which was made for these kind of "aggregations".

    The Group By Clause is quite similar to Distinct, in that you can define
    "the distinct bucket" for which to gather (or aggregate) certain other FieldValues.

    "Select PriceName, Sum(PriceCost) From PriceList Group By PriceName"

    The above has the distinct "Bucket" (PriceName) colored in magenta, the Aggregate-Function (Sum) in blue.

    There's other Aggreate-Functions as e.g.:
    - Avg(PriceCost) ... in case you want the Average of the PriceCosts
    - Count(PriceCost) ... in case you want only the Count of the PriceCosts in the Bucket.

    Olaf

  10. #10

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: cannot DISTINCT 1 field And 2 Fields at the same time why?

    Got it Tnx amigos

  11. #11
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,743

    Re: [RESOLVED] cannot DISTINCT 1 field And 2 Fields at the same time why?

    Which solution did you choose?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width