Results 1 to 26 of 26

Thread: SQL statement : How to Creat Last in this Sql

Hybrid View

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    Question SQL statement : How to Creat Last in this Sql

    I'm using DB MS Access. with Vb2005.
    I have this select:
    " SELECT LAST(BNotice),LAST(BAgent),LAST(BPriceS), SUM(IIF(BType='Buy',BAmount,-BAmount,)),BSource,BClass,BKind,BMaterial FROM TBbills Where BType IN ('Damage','Sell','Buy') and Bmdeleted ='Yes' GROUP BY BSource,BClass,BKind,BMaterial"

    I Have another Filde in the Table "TBbills Called History and it's Typed Date/Time.
    I want to define the LAST(BAgent),LAST(BPriceS) according with the Last HIstory.

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: SQL statement : How to Creat Last in this Sql

    You could use ORDER BY.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    Re: SQL statement : How to Creat Last in this Sql

    I tried like this but didn't succcess
    " SELECT BNotice,BAgent,BPriceS, SUM(IIF(BType='Buy',BAmount,-BAmount,)),BSource,BClass,BKind,BMaterial FROM TBbills Where BType IN ('Damage','Sell','Buy') and Bmdeleted ='Yes' GROUP BY BSource,BClass,BKind,BMaterial order by Last(BHistory)"

    although the previous Sql is working well.
    but this sql it showed me this error:
    You tried to execute a query that does not include the specified expression 'BNotice' as part of an aggregate function.
    Last edited by nader; Dec 25th, 2008 at 10:04 AM.

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

    Re: SQL statement : How to Creat Last in this Sql

    The message say it all.... you are missing BNotice and BAgent fin the Group statement.

    I would be very careful about using Last also. That may work in Access but probably will not in another database system.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    Re: SQL statement : How to Creat Last in this Sql

    So How can I select the BNotice,BAgent,BPriceS order by Max (History). in that Sql

  6. #6
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: SQL statement : How to Creat Last in this Sql

    Something like this?
    Code:
    SELECT BNotice,BAgent,BPriceS FROM TBbills ORDER BY History DESC
    To retrieve only the topmost record then add the TOP clause.
    Code:
    SELECT TOP 1 BNotice,BAgent,BPriceS FROM TBbills ORDER BY History DESC
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    Re: SQL statement : How to Creat Last in this Sql

    Oky, I got this, but how to use it in that Sql:
    it's mean like this:
    SELECT BNotice,BAgent,BPriceS, SUM(IIF(BType='Buy',BAmount,-BAmount,)),BSource,BClass,BKind,BMaterial FROM TBbills Where BType IN ('Damage','Sell','Buy') and Bmdeleted ='Yes' GROUP BY BSource,BClass,BKind,BMaterial ORDER BY History DESC"

    this sql it didn't work well

  8. #8
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: SQL statement : How to Creat Last in this Sql

    What did you mean by 'it didn't work well'?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    Re: SQL statement : How to Creat Last in this Sql

    I'm sorry I didn't inser the right Sql
    this is the sql I tried and it made this error:
    " SELECT BSource,BClass,BKind,BMaterial, SUM(IIF(BType='Buy',BAmount,-BAmount,)),(BNotice,BAgent,BPriceS order by BHistory DESC ) FROM TBbills Where BType IN ('Damage','Sell','Buy') and Bmdeleted ='Yes' GROUP BY BSource,BClass,BKind,BMaterial "

    the error:
    Syntax error (missing operator) in query expression '(BNotice,BAgent,BPriceS order by BHistory DESC )'.

  10. #10
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: SQL statement : How to Creat Last in this Sql

    As odd as it is I cannot find '(BNotice,BAgent,BPriceS order by BHistory DESC )' in your sql so I cannot suppose that it has got something to do with the error, you should post the sql which is causing the error.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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

    Re: SQL statement : How to Creat Last in this Sql

    This:

    ,(BNotice,BAgent,BPriceS order by BHistory DESC )

    Is not valid in an SQL Statement At leaset not where you have placed it
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  12. #12
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: SQL statement : How to Creat Last in this Sql

    Oooppsss... So they are in the middle that's why I cannot find them. And they are not supposed to be there.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    Re: SQL statement : How to Creat Last in this Sql

    I tried this Sql and it succeed with me.
    vb Code:
    1. " SELECT Last(BNotice),Last(BAgent),Last(BPriceS), SUM(IIF(BType='Buy',BAmount,-BAmount,)),BSource,BClass,BKind,BMaterial FROM TBbills Where BType IN  ('Damage','Sell','Buy')  and Bmdeleted ='Yes' GROUP BY BSource,BClass,BKind,BMaterial "

    I used this Last(BNotice),Last(BAgent),Last(BPriceS) in that sql
    I wan to select these three columns in that sql order by BHistory DESC. Not by Last.. that all I want.
    Last edited by nader; Dec 26th, 2008 at 08:08 PM.

  14. #14
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: SQL statement : How to Creat Last in this Sql

    If you don't want to use LAST then why are you still using it?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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

    Re: SQL statement : How to Creat Last in this Sql

    Its best to post relevant table definitions and sample correct output, rather than hoping people can guess what you want based on incorrect SQL. Help us help you.

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    Re: SQL statement : How to Creat Last in this Sql

    I used the "Last" with three columns (BNotice,BAgent,BPriceS). Now I want to make modification by define these columns by "order by BHistory DESC".

    For more clear, I'll offe the issue more easy and make it short:
    If I have these Columns(A,B,C,D,E.F,G,H) in the Table1. The typed of A,B,C,E,F,G is text and D is number and H is Date/time..
    and here is the data in that table

    A------B------C------D------F------G------H
    dd2---cc5-----ff8----11-----tt5----uu4---4 dec 08

    kk8---cc7----ss6----33-----mm2---zz5----2 dec 08

    dd2--cc5----ff8-----44----gg3-----yy5-----1 dec 08

    If I use this sql:
    Select Last( F),Last( G), SUM(D), A,B,C FROM Table1 Group by A, B, C
    the result will be like this
    A------B------C------D------F------G
    dd2--cc5----ff8-----55----gg3-----yy5 ' according by Gruop and Last
    kk8---cc7----ss6----33-----mm2---zz5

    but I want to make modification on that sql by change the condition of select the Last( F),Last( G), by " order by H DESC ".
    to be the result like this:

    A------B------C------D------F------G
    dd2---cc5-----ff8----55-----tt5----uu4 ' according by Gruop and order by H DESC
    kk8---cc7----ss6----33-----mm2---zz5


    That's all I want. I hope I'm clear now and I'm sorry for confusing them. Thank you a lot.
    Last edited by nader; Dec 27th, 2008 at 08:02 AM.

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

    Re: SQL statement : How to Creat Last in this Sql

    There's no primary/unique key per record?

    Since use of Last() implies necessity that the records are sorted before aggregation then simply apply relevant processing/transformation per dataset (use intermediate "tables").

    SELECT A, B, C, SUM(D)
    FROM (SELECT A, B, C, D FROM yourTable ORDER BY H)
    GROUP BY A, B, C

    Note that aggregation and sorting are database CPU intensive (performance issue). What other columns and indexes are available so other query variants can be tested instead of being SQL output oriented.
    Last edited by leinad31; Dec 28th, 2008 at 09:07 AM.

  18. #18

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    Re: SQL statement : How to Creat Last in this Sql

    I tried this way:
    vb Code:
    1. "Select A,B,C SUM(D) FROM(Select F,G From Table1 ORDER BY H DESC) GROUP BY A, B, C "
    It showed me this error:
    No value given for one or more required parameters.
    Last edited by nader; Dec 29th, 2008 at 11:41 AM.

  19. #19
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: SQL statement : How to Creat Last in this Sql

    Did you run it as quoted? You need to change the table name and the fields so it would match with what you have in your database.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  20. #20

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    Re: SQL statement : How to Creat Last in this Sql

    I created a simple one.
    The table is a Table1 and the Columns are A,B,C,D,F,G,H

  21. #21
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: SQL statement : How to Creat Last in this Sql

    To perform an ORDER you should try it like this

    Code:
    SELECT * FROM Table1 ORDER BY H
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  22. #22

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    Re: SQL statement : How to Creat Last in this Sql

    Do you mean I have to try like this
    "Select A,B,C, SUM(D) FROM(Select * F,G From Table1 ORDER BY H DESC) GROUP BY A, B, C "
    this will show syntax error on F

  23. #23
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: SQL statement : How to Creat Last in this Sql

    Quote Originally Posted by nader
    Do you mean I have to try like this

    this will show syntax error on F
    Before trying out a more complex sql you should try it out like the one I posted just to get you the feeling on how it is done.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  24. #24

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    Re: SQL statement : How to Creat Last in this Sql

    Quote Originally Posted by dee-u
    Before trying out a more complex sql you should try it out like the one I posted just to get you the feeling on how it is done.
    I tried before and it succeed with me.

    The problem in using it in this sql
    Code:
    "Select A,B,C, SUM(D) FROM(Select * F,G From Table1 ORDER BY H DESC) GROUP BY A, B, C "
    Last edited by nader; Dec 30th, 2008 at 03:36 AM.

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

    Re: SQL statement : How to Creat Last in this Sql

    You obviously can't select/aggregate on columns A, B, C, D if the source view/table doesn't contain those columns; only columns F and G were returned to succeeding/outer SELECT statement.

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

    Re: SQL statement : How to Creat Last in this Sql

    Because
    Code:
    SELECT * F, G
    isn't valid SQL syntax. You making this seem harder than it really is. It's a simple two step process, return recordset (imagine your returning a table view) ordered by H with all columns needed for next step... you can even check first that SQL is valid before using it as the source in next step... Next step is to then perform aggregation on first query's result (or treat it as a table view), you do group by, sum(), last() on relevant columns.

    Emphasis on relevant columns, declare columns correctly and completely. Get your columns right!! Its been several days and the column list is still wrong.
    Last edited by leinad31; Jan 2nd, 2009 at 09:54 AM.

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