Results 1 to 24 of 24

Thread: [RESOLVED] SQL and top 20 items in a table

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Hendersonville , NC
    Posts
    260

    Resolved [RESOLVED] SQL and top 20 items in a table

    I have an inventory cycle count file which may contain several thousand records. I am trying to get the TOP 20 items (based on qty_adjusted value) and return only those to a dataset.

    I am trying to use the TOP 20 statement but am getting an Invalid Token error at execution time.


    SELECT TOP 20 di3.Qty_A00001 +
    FROM Dobsojm1.Item_00003 di3 +
    JOIN Dobsojm1.ITEM_00001 di1 +
    ON di3.ITEM_00001 = di1.ITEM_00001 +
    ORDER BY di3.Qty_A00001 DESC"

    Also tried


    SELECT * +
    FROM Dobsojm1.Item_00003 di3 +
    JOIN Dobsojm1.ITEM_00001 di1 +
    ON di3.ITEM_00001 = di1.ITEM_00001 +
    ORDER BY di3.Qty_A00001 DESC LIMIT 20"

    Neither works... Where am I going wrong here.... Oh .... also there may be duplicated "high" adjustments so I need to figure those into the top 20.

    thank you

    gollnick
    William E Gollnick

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL and top 20 items in a table

    What is the database backend? What do the tables look like?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Hendersonville , NC
    Posts
    260

    Re: SQL and top 20 items in a table

    It is a DB2 (iseries) using the IBMDB2400 client using SQL to retreive my data.
    Not sure what you mean by what do my tables look like. Just ordinary tables.
    William E Gollnick

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL and top 20 items in a table

    I think like this in DB2:

    SELECT di3.Qty_A00001 +
    FROM Dobsojm1.Item_00003 di3 +
    JOIN Dobsojm1.ITEM_00001 di1 +
    ON di3.ITEM_00001 = di1.ITEM_00001 +
    ORDER BY di3.Qty_A00001 DESC +
    FETCH FIRST 20 ROWS ONLY
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Hendersonville , NC
    Posts
    260

    Re: SQL and top 20 items in a table

    I will try that but if I have multiples say

    88 82 80 75 75 75 60 55 54 52 50 45 40 42 40 38 38 38 37 35

    as my first high order rows ..... I do not wat to count those dulpicate 75's or 38's as a total aggrigate to my 20.... so in the above data set (based on field qty_A00001) i would have only gotten my top 16 .... understand?? This may not even be possible

    thanks

    gollnick
    William E Gollnick

  6. #6
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL and top 20 items in a table

    What about doing something like this:

    SELECT di3.Qty_A00001,COUNT(di3.Qty_A00001) +
    FROM Dobsojm1.Item_00003 di3 +
    JOIN Dobsojm1.ITEM_00001 di1 +
    ON di3.ITEM_00001 = di1.ITEM_00001 +
    GROUP BY di3.Qty_A00001 +
    ORDER BY di3.Qty_A00001 DESC +
    FETCH FIRST 20 ROWS ONLY

    Or
    SELECT DISTINCT di3.Qty_A00001 +
    FROM Dobsojm1.Item_00003 di3 +
    JOIN Dobsojm1.ITEM_00001 di1 +
    ON di3.ITEM_00001 = di1.ITEM_00001 +
    ORDER BY di3.Qty_A00001 DESC +
    FETCH FIRST 20 ROWS ONLY
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Hendersonville , NC
    Posts
    260

    Re: SQL and top 20 items in a table

    I will try both of those when I get back to the office in the morning. Had to leave for the day already. Will post back on ya or naa... Thanks

    gollnick
    William E Gollnick

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Hendersonville , NC
    Posts
    260

    Re: SQL and top 20 items in a table

    Hello.. this method worked somewhat

    SELECT di3.Qty_A00001,COUNT(di3.Qty_A00001) +
    FROM Dobsojm1.Item_00003 di3 +
    JOIN Dobsojm1.ITEM_00001 di1 +
    ON di3.ITEM_00001 = di1.ITEM_00001 +
    GROUP BY di3.Qty_A00001 +
    ORDER BY di3.Qty_A00001 DESC +
    FETCH FIRST 20 ROWS ONLY



    However ..... the only column brought into the dataset is the qty_A00001.... I need a lot of other columns and if I try and define those I get an invalid token .. from or into expected..

    Anything else I missed?

    thank you

    gollnick
    William E Gollnick

  9. #9
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL and top 20 items in a table

    Every column not in the aggregate ( COUNT,SUM,MAX,MIN,AVG) needs to be in the group by.

    If you need other columns then the question becomes different as does the solution. What do you actually need. What columns from what tables?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Hendersonville , NC
    Posts
    260

    Re: SQL and top 20 items in a table

    Hi .. I need at a minimum these additional columns from the tables

    di3.item_Part di1.item_Desc di3.Item_loct di3.Item_PkDate

    If I see how on these I can figure the rest

    Thank you

    gollnick
    William E Gollnick

  11. #11
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL and top 20 items in a table

    Try something like this:

    Select di3.item_Part di1.item_Desc di3.Item_loct di3.Item_PkDate +
    FROM FROM Dobsojm1.Item_00003 di3 +
    JOIN Dobsojm1.ITEM_00001 di1 +
    ON di3.ITEM_00001 = di1.ITEM_00001 +
    WHERE di3.Qty_A00001 IN ( +
    SELECT DISTINCT di3.Qty_A00001 +
    FROM Dobsojm1.Item_00003 di3 +
    JOIN Dobsojm1.ITEM_00001 di1 +
    ON di3.ITEM_00001 = di1.ITEM_00001 +
    ORDER BY di3.Qty_A00001 DESC +
    FETCH FIRST 20 ROWS ONLY )
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Hendersonville , NC
    Posts
    260

    Re: SQL and top 20 items in a table

    I built the command string this way.......


    " Select di3.item_00001 di1.item_00002 di3.Item_00003 di3.Last_00001 FROM FROM Dobsojm1.Item_00003 di3 JOIN Dobsojm1.ITEM_00001 di1 ON di3.ITEM_00001 = di1.ITEM_00001 WHERE di3.Qty_A00001 IN ( SELECT DISTINCT di3.Qty_A00001 FROM Dobsojm1.Item_00003 di3 JOIN Dobsojm1.ITEM_00001 di1 ON di3.ITEM_00001 = di1.ITEM_00001 ORDER BY di3.Qty_A00001 DESC FETCH FIRST 40 ROWS ONLY )"


    This is the generated error at excute time
    SQL0104 Token . was not valid. Valid tokens: , FROM INTO.

    Very confused

    gollnick
    William E Gollnick

  13. #13
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL and top 20 items in a table

    From is listed twice FROM FROM Dobsojm1.Item_00003 di3 JOIN

    Missing all the commas between the fields in the select statement
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Hendersonville , NC
    Posts
    260

    Re: SQL and top 20 items in a table

    Sorry......

    Did those changes...... now I have the string

    " Select di3.item_00001 , di1.item_00002 , di3.Item_00003 , di3.Last_00001 FROM Dobsojm1.Item_00003 di3 JOIN Dobsojm1.ITEM_00001 di1 ON di3.ITEM_00001 = di1.ITEM_00001 WHERE di3.Qty_A00001 IN ( SELECT DISTINCT di3.Qty_A00001 FROM Dobsojm1.Item_00003 di3 JOIN Dobsojm1.ITEM_00001 di1 ON di3.ITEM_00001 = di1.ITEM_00001 ORDER BY di3.Qty_A00001 DESC FETCH FIRST 40 ROWS ONLY )"

    Now I get the exception thrown

    SQL0199 Keyword ORDER not expected. Valid tokens: ) UNION EXCEPT

    Sorry I'm slow on this. Simple sql statements I can handle.. This is something new to me.

    gollnick
    William E Gollnick

  15. #15
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL and top 20 items in a table

    I don't have DB2. I would try the Inner statement first just see if it runs and gets you the info you want. If that does then We go on from there. There are is another option (at least in SQL Server) I could use also. That would be treating the select that is use for the In statement as a dirived table and performing an INNER JOIN on that also.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Hendersonville , NC
    Posts
    260

    Re: SQL and top 20 items in a table

    I've tried..... this is from the sql manager script create .. so DB2 does not really run into play

    Connected to relational database Uitasc3 on Uitasc3.electrolux-na.com as Gollnwe1 - 767074/Quser/Qzdasoinit

    > Select di3.item_00001 , di1.item_00002 , di3.Item_00003 , di3.Last_00001 FROM Dobsojm1.Item_00003 di3 JOIN Dobsojm1.ITEM_00001 di1 ON di3.ITEM_00001 = di1.ITEM_00001 WHERE di3.Qty_A00001 > 0 IN ( SELECT DISTINCT di3.Qty_A00001 FROM Dobsojm1.Item_00003 di3 JOIN Dobsojm1.ITEM_00001 di1 ON di3.ITEM_00001 = di1.ITEM_00001 ORDER BY di3.Qty_A00001 DESC FETCH FIRST 40 ROWS ONLY )

    SQL State: 42601
    Vendor Code: -199
    Message: [SQL0199] Keyword IN not expected. Valid tokens: FOR WITH FETCH ORDER UNION EXCEPT OPTIMIZE. Cause . . . . . : The keyword IN was not expected here. A syntax error was detected at keyword IN. The partial list of valid tokens is FOR WITH FETCH ORDER UNION EXCEPT OPTIMIZE. This list assumes that the statement is correct up to the unexpected keyword. The error may be earlier in the statement but the syntax of the statement seems to be valid up to this point. Recovery . . . : Examine the SQL statement in the area of the specified keyword. A colon or SQL delimiter may be missing. SQL requires reserved words to be delimited when they are used as a name. Correct the SQL statement and try the request again.

    Processing ended because the highlighted statement did not complete successfully


    gollnick
    William E Gollnick

  17. #17
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL and top 20 items in a table

    This is worng

    Select di3.item_00001 , di1.item_00002 , di3.Item_00003 , di3.Last_00001 FROM Dobsojm1.Item_00003 di3 JOIN Dobsojm1.ITEM_00001 di1 ON di3.ITEM_00001 = di1.ITEM_00001 WHERE di3.Qty_A00001 > 0 IN ( SELECT DISTINCT di3.Qty_A00001 FROM Dobsojm1.Item_00003 di3 JOIN Dobsojm1.ITEM_00001 di1 ON di3.ITEM_00001 = di1.ITEM_00001 ORDER BY di3.Qty_A00001 DESC FETCH FIRST 40 ROWS ONLY )



    Look at the where Clause:
    WHERE di3.Qty_A00001 > 0 IN

    That is not valid Remove the > 0 before the IN or write it like this:
    WHERE di3.Qty_A00001 > 0 AND di3.Qty_A00001 IN
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Hendersonville , NC
    Posts
    260

    Re: SQL and top 20 items in a table

    Did that .. got further..


    > Select di3.item_00001 , di1.item_00002 , di3.Item_00003 , di3.Last_00001 FROM Dobsojm1.Item_00003 di3 JOIN Dobsojm1.ITEM_00001 di1 ON di3.ITEM_00001 = di1.ITEM_00001 AND di3.ITEM_TYPE = di1.packa00001 WHERE di3.Qty_A00001 > 0 AND di3.Qty_A00001 IN ( SELECT DISTINCT di3.Qty_A00001 FROM Dobsojm1.Item_00003 di3 JOIN Dobsojm1.ITEM_00001 di1 ON di3.ITEM_00001 = di1.ITEM_00001 ORDER BY di3.Qty_A00001 DESC FETCH FIRST 40 ROWS ONLY )

    SQL State: 42601
    Vendor Code: -199
    Message: [SQL0199] Keyword ORDER not expected. Valid tokens: ) UNION EXCEPT. Cause . . . . . : The keyword ORDER was not expected here. A syntax error was detected at keyword ORDER. The partial list of valid tokens is ) UNION EXCEPT. This list assumes that the statement is correct up to the unexpected keyword. The error may be earlier in the statement but the syntax of the statement seems to be valid up to this point. Recovery . . . : Examine the SQL statement in the area of the specified keyword. A colon or SQL delimiter may be missing. SQL requires reserved words to be delimited when they are used as a name. Correct the SQL statement and try the request again.


    gollnick
    William E Gollnick

  19. #19
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL and top 20 items in a table

    What version of DB2? the FETCH FIRST FETCH FIRST * ROWS ONLY is version 8 and above only.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Hendersonville , NC
    Posts
    260

    Re: SQL and top 20 items in a table

    I do not think that is the issue.. This works fine


    SELECT *
    FROM Dobsojm1.Item_00003 di3
    JOIN Dobsojm1.ITEM_00001 di1
    ON di3.ITEM_00001 = di1.ITEM_00001 AND di3.ITEM_TYPE = di1.packa00001
    Where di3.Last_00001 = '2011-01-04'
    ORDER BY di3.Qty_A00001 DESC
    FETCH FIRST 40 ROWS ONLY

    gollnick
    William E Gollnick

  21. #21
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL and top 20 items in a table

    So lets try it as a drivived table with an Inner Join then

    sql Code:
    1. Select di3.item_00001 , di1.item_00002 , di3.Item_00003 , di3.Last_00001 \
    2. FROM Dobsojm1.Item_00003 di3
    3. JOIN Dobsojm1.ITEM_00001 di1
    4.     ON di3.ITEM_00001 = di1.ITEM_00001 AND di3.ITEM_TYPE = di1.packa00001
    5. INNER JOIN ( SELECT DISTINCT di3.Qty_A00001
    6.                 FROM Dobsojm1.Item_00003 di3
    7.                 JOIN Dobsojm1.ITEM_00001 di1
    8.                     ON di3.ITEM_00001 =         di1.ITEM_00001
    9.                 ORDER BY di3.Qty_A00001 DESC
    10.                 FETCH FIRST 40 ROWS ONLY ) myInner
    11.     on MyInner.Qty_A00001 = di3.Qty_A00001
    12. WHERE di3.Qty_A00001 > 0
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  22. #22
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: SQL and top 20 items in a table

    The sorting is additional overhead (you need to return entire resultset of join before sorting can be done )... all preceding approaches to problem is not scalable with table data growth. Is the join necessary to get first 20 rows, or are there instances where di3.ITEM_00001 = di1.ITEM_00001 will not be satisfied (key doesn't exist in other table)?

    What are the existing indexes? And is Qty_A00001 update/delete transaction heavy?

    Related reference http://publib.boulder.ibm.com/infoce...2z_indexes.htm
    Last edited by leinad31; Jan 6th, 2011 at 07:49 PM.

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Hendersonville , NC
    Posts
    260

    Re: SQL and top 20 items in a table

    YES..... that worked ... thank you. I can build on this from here...

    Thanks

    gollnick
    William E Gollnick

  24. #24
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL and top 20 items in a table

    OK... If this resolves your issue please do us a favor and mark the thread as resolved.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

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