Results 1 to 8 of 8
  1. #1
    reentry is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2015
    Posts
    9

    Subqueries with more results

    Hi,

    I Have some troubles with a SQL Statment. The result should be a list of the same Articles with current price and calculated price per kg.

    Each article has more prices.

    The querie works fine with 1 article:

    Code:
    SELECT Preis.ArtikelID, Preis.BruttoPreis, Artikel.Bezeichnung, Artikel.Einheit, Artikel.Liefereinheit, Artikel.Gewicht, Artikel.ArtikelNr, round((Preis.BruttoPreis / Artikel.Gewicht *1 ),2) as KiloPreis
    FROM Preis INNER JOIN Artikel ON Preis.ArtikelID = Artikel.ArtikelID
    WHERE (((Preis.Preisid)=
                                     (SELECT Max(Preis.PreisID)
                                      FROM Preis WHERE Artikelid = 10113)));
    My problem is if i try to search for all Artikel which contains for example orange...

    I thought this would work:

    Code:
    SELECT Preis.ArtikelID, Preis.BruttoPreis, Artikel.Bezeichnung, Artikel.Einheit, Artikel.Liefereinheit, Artikel.Gewicht, Artikel.ArtikelNr, Round((Preis.BruttoPreis/Artikel.Gewicht*1),2) AS KiloPreis, Artikel.Bezeichnung
    FROM Preis INNER JOIN Artikel ON Preis.ArtikelID = Artikel.ArtikelID
    WHERE (((Preis.Preisid) = 
                                       (SELECT Max(Preis.PreisID) 
                                        FROM Preis WHERE Artikelid IN 
                                                                                 (SELECT ArtikelID 
                                                                                   FROM Artikel WHERE Bezeichnung like '*Orange*'))));
    I get 1 Article instead of all articles which contain Orange... I think the problem is the MAX function but i have no idea how i can solve it.



    Does anyone have an idea ? or solution ?

    Best Regards Matthias

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    If you want the most recent price record for each article, consider TOP N query: http://allenbrowne.com/subquery-01.html#TopN
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    reentry is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2015
    Posts
    9
    I considered your hint ( i created the below statement) and i get a better result, but now i get lots of duplicates Seen in Table below. There are exactly 31 Articles which have orange in their name, thats the reason why i chose TOP 31. Dont be irretated by the empty cells, i am working with some test data therefore there are lots of data missing.

    How can i reduce these Duplicates because he chose not only the current price id? Group by doesnt work and if i use DISTINCT the sql staments seems to be in an endless Loob ..


    Code:
    SELECT Preis.ArtikelID, Preis.BruttoPreis, Artikel.Bezeichnung, Artikel.Einheit, Artikel.Liefereinheit, Artikel.Gewicht, Artikel.ArtikelNr, Round((Preis.BruttoPreis/Artikel.Gewicht*1),2) AS KiloPreis, Artikel.Bezeichnung
    FROM Preis INNER JOIN Artikel ON Preis.ArtikelID = Artikel.ArtikelID
    WHERE (((Preis.Preisid) in (Select TOP 31 Preis.PreisID From Preis Where Artikelid IN (Select ArtikelID from Artikel Where Bezeichnung like '*Orange*'))));
    PreisID Artikelid ArtikelNr Bezeichnung Einheit Liefereinheit Gewicht BruttoPreis KiloPreis
    28452 10316 kof0541 Orangen 15 kg Übersee
    0 0 19,5
    28302 10316 kof0541 Orangen 15 kg Übersee
    0 0 19,5
    30969 10316 kof0541 Orangen 15 kg Übersee
    0 0 19,5
    28453 10416 kof0546 Orangen XXL 8,50 kg
    0 0 26
    30970 10416 kof0546 Orangen XXL 8,50 kg
    0 0 26
    28454 10417 kof0547 Kleine Orangen Kiste I 6 kg
    0 0 9,8
    30971 10417 kof0547 Kleine Orangen Kiste I 6 kg
    0 0 9,8
    28659 10580 kof1328 Struwelpeter Orangen 8 kg
    0 0 18
    31163 10580 kof1328 Struwelpeter Orangen 8 kg
    0 0 18
    28737 10653 kof1431 VB Kbl-Orangen FILET 5 Liter
    0 0 38
    31240 10653 kof1431 VB Kbl-Orangen FILET 5 Liter
    0 0 38
    28974 10184 kof1800 6 x 2 Liter Orangenjuice Fris
    0 0 6,7
    31474 10184 kof1800 6 x 2 Liter Orangenjuice Fris
    0 0 6,7
    28170 10184 kof1800 6 x 2 Liter Orangenjuice Fris
    0 0 6,1
    31475 10875 kof1801 FR Blutorangensaft Frisch 6 x 2 Lt
    0 0 6,85
    28975 10875 kof1801 FR Blutorangensaft Frisch 6 x 2 Lt
    0 0 6,85
    29036 10936 kof1875 Orangenkonfitüre 3 kg
    0 0 13,5
    31535 10936 kof1875 Orangenkonfitüre 3 kg
    0 0 13,5
    29227 11107 kof2324 Orange Roughy 10kg 120/170gr
    0 0 20,5
    29242 11119 kof2339 Orange Roughy 5 kg 170/230gr
    0 0 22,9
    28306 10320 kof2830 Blutorangeneis 1 x 4 Liter
    0 0 14
    29522 10320 kof2830 Blutorangeneis 1 x 4 Liter
    0 0 19,8
    29581 11430 kof2947 Coulis-Blutorangenpüree 12x480gr
    0 0 3,8
    29622 11470 kof3006 TK-Orangesanguinepüree 6x1kg (Blutorang)
    0 0 6,1
    30161 11982 kof4212 Orange Schokolade 4 x 2,50 kg
    0 0 29,75
    30209 12029 kof4272 Orange Turitella 117 Stk
    0 0 17,5
    30307 12127 kof4522 Turitella Orange 1 Pkg
    0 0 15,5
    30385 12205 kof4703 Fruchtw Orangenessig 12x250ml
    0 0 5,3
    30444 12264 kof4924 Orangenöl 6x500ml
    0 0 7,9
    30586 12406 kof5673 Unbehandelte Orangen 5 kg
    0 0 12
    30778 12598 kof6540 Orangen per Stk
    0 0 0,3
    Last edited by reentry; 02-25-2015 at 06:43 AM. Reason: Fail post

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    The PreisID values are unique, therefore the records are not duplicates.

    Apparently these tables have a 1-to-many relationship. One of the tables has multiple records with the same ArtikelID. Therefore, joining these tables appears to 'duplicate' data from the other table.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    reentry is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2015
    Posts
    9
    U are right they arent dublicates wrong term, but i need only the data for the highest Price ID for each article ( article to price is a 1 to n relationship).

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Please show us more of your table designs.
    As June says, these are not duplicates.

    And after seeing your latest post ( I was writing) which of these do you want since the Price is same in all 3 records???

    28452 10316 kof0541 Orangen 15 kg Übersee
    0 0 19,5

    28302 10316 kof0541 Orangen 15 kg Übersee
    0 0 19,5
    30969 10316 kof0541 Orangen 15 kg Übersee
    0 0 19,5
    Last edited by June7; 02-25-2015 at 12:49 PM. Reason: Please ignore the formatting remnants, I can't delete them!!

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    The link I posted shows how to get the TOP n for each group - in your case the groups are ArtikelID.

    If you eliminate the PreisID field, could do a GROUP BY or DISTINCT query.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    reentry is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2015
    Posts
    9
    @
    Always the one with the highest PreisID in this case this one
    30969 10316 kof0541 Orangen 15 kg Übersee
    0 0 19,5
    Because all other arnt calculated with the current price.
    Attached Thumbnails Attached Thumbnails dbww.PNG  

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 12-07-2012, 02:03 PM
  2. Summing 2 subqueries
    By bd528 in forum Access
    Replies: 8
    Last Post: 09-27-2012, 02:22 PM
  3. Structuring Subqueries
    By dandoescode in forum Queries
    Replies: 1
    Last Post: 03-13-2012, 06:42 PM
  4. Creating subqueries in SQL view
    By AmyM in forum Queries
    Replies: 2
    Last Post: 11-20-2011, 05:21 PM
  5. LEFT/RIGHT/OUTER JOINS in a set of subqueries
    By luckycharms in forum Queries
    Replies: 1
    Last Post: 08-01-2011, 05:06 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums