Results 1 to 14 of 14

Thread: So hard that I don't know what it is called...

  1. #1

    Thread Starter
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    So hard that I don't know what it is called...

    Hi guys,

    I am such a noob when it comes to Accounting.

    Guys, what do you call this kind of computation:

    Loaned Amount Amount Left

    100 900
    200 700
    300 400

    How do you do that in a query? Is it possible to do it in SQL or I would have to revise my ****** program.

    Rate Me! Rate Me! Rate Me!

    Time to fly.

    Copyright GraysonSoft Inc. 2007

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: So hard that I don't know what it is called...

    I call it subtraction.
    You can do subtraction in SQL, but you're missing a starting amount - presumably 1000 in your example.

    Balance Loaned
    1000 0
    900 100
    700 200
    Tengo mas preguntas que contestas

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: So hard that I don't know what it is called...

    So we don't waste all our time - tell us what the name of your table is and what fields are in it.

    There must be come kind of loan amount field.

    And I'm guessing a field for the date of the loan.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4

    Thread Starter
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: So hard that I don't know what it is called...

    Quote Originally Posted by szlamany
    So we don't waste all our time - tell us what the name of your table is and what fields are in it.

    There must be come kind of loan amount field.

    And I'm guessing a field for the date of the loan.
    Ok. You guys are correct. After going through some research, it is a subtraction but in a running total form.


    Here is the thing.

    Table - Loans

    Fields:

    Loan

    There is a loan amount field but the balance is a result for the running total. There are other fields like dates, control numbers and stuff but I don't want to clutter it all here. I just want a running total.

    Thanks for your replies.

    Rate Me! Rate Me! Rate Me!

    Time to fly.

    Copyright GraysonSoft Inc. 2007

  5. #5

    Thread Starter
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: So hard that I don't know what it is called...

    Quote Originally Posted by salvelinus
    I call it subtraction.
    You can do subtraction in SQL, but you're missing a starting amount - presumably 1000 in your example.

    Balance Loaned
    1000 0
    900 100
    700 200
    Yes that is correct but the beginning balance is only a constant declared by the program. It is also not saved in the database.

    Could you post the SQL query for that?
    Rate Me! Rate Me! Rate Me!

    Time to fly.

    Copyright GraysonSoft Inc. 2007

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: So hard that I don't know what it is called...

    But you have to clutter us here - as those dates would be exactly what would be used by me to build you a query - with a "running total sub-query" that ran your figures...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: So hard that I don't know what it is called...

    You've got one field in your table? You need a better table. This doesn't sound like a very viable project (sorry if I'm misunderstanding something).
    Anyway, what data does the lonely field hold? The amount loaned? The running balance?
    The general form of the SQL is:
    Code:
    SELECT (field1 - field2) AS SomeValue FROM Loans
    Tengo mas preguntas que contestas

  8. #8

    Thread Starter
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: So hard that I don't know what it is called...

    Quote Originally Posted by szlamany
    But you have to clutter us here - as those dates would be exactly what would be used by me to build you a query - with a "running total sub-query" that ran your figures...
    Sorry for the late reply. I am from a very far and different time zone. Hope you understand.

    Ok. point taken.

    Here it is.


    Table: - Loans

    Fields:

    TransNo - Primary Key
    EmpNo
    MedName
    MedCode
    GenName
    GenCode
    Dosage
    PerscriptPeriod
    RMQty
    UnitCost
    ORNo
    ORDate
    Loan

    The date for the transaction is ORDate. I also use this for the report and the Loan field. I group the report by using MONTH(ORDate) = @ORMonth and YEAR(ORDate) = @ORYear. The problem is that there is no Balance in the table and I will have to revise the program if I can't generate a running total to the query.

    The format of the report is:

    Code:
         Medicine Loan for the Month of ___________                Loanable Amount 3,000.00
    
              OR Number              OR Date          Amount Loaned         Balance Left
              [ORNo]                   [ORDate]         [Loan]                     No field
    Rate Me! Rate Me! Rate Me!

    Time to fly.

    Copyright GraysonSoft Inc. 2007

  9. #9
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: So hard that I don't know what it is called...

    You need to rethink your table design. Balance Left should probably not be a field in a table, but a calculated amount. So far, so good.
    But Loanable Amount should be a field. And as szlamany says, if you want a running total you'll need some kind of date field, even if only a month.
    All this data shouldn't be in just one table, but it's late & I'm too tired to break it out right now. Basically, loan data should be separate from medical data. Use foreign keys to join info for queries where needed.
    Tengo mas preguntas que contestas

  10. #10

    Thread Starter
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: So hard that I don't know what it is called...

    Quote Originally Posted by salvelinus
    You need to rethink your table design. Balance Left should probably not be a field in a table, but a calculated amount. So far, so good.
    But Loanable Amount should be a field. And as szlamany says, if you want a running total you'll need some kind of date field, even if only a month.
    All this data shouldn't be in just one table, but it's late & I'm too tired to break it out right now. Basically, loan data should be separate from medical data. Use foreign keys to join info for queries where needed.
    Don't worry man, got it covered. Already thought of a table design for medical data with foreign keys and relationships.

    Loanable Amount should be a field. That is true and it is already been planned to be included in the Misc Table where all constants are declared there.

    There is a date field. The OR Date column in the report is my date field.

    Just needed the query for the running total.

    Thanks in advance for all your inputs on this. Too bad, the table I showed is not normalized enough so there are a lot of concered issues you've posted.

    You guys are great.
    Rate Me! Rate Me! Rate Me!

    Time to fly.

    Copyright GraysonSoft Inc. 2007

  11. #11
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: So hard that I don't know what it is called...

    This query should work:

    Code:
    Select L1.ORNo, L1.ORDate, L1.Loan
           ,3000-(Select Sum(L2.Loan) From Loans L2
                       Where L2.ORDate<=L1.ORDate
                           and L2.xyz=L1.xyz)
           From Loans L1
    The "xyz" part is from my confusion over whether there is several loans going on at one time here - is it loans for an EmpNo?

    At any rate - the basic concept is to have a sub-query that "totals" all the loans for that date going backwards - and subtract that from 3000.

    Obviously the 3000 should not be a constant but a field from a misc table that's joined in somehow.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12

    Thread Starter
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: So hard that I don't know what it is called...

    Quote Originally Posted by szlamany
    This query should work:

    Code:
    Select L1.ORNo, L1.ORDate, L1.Loan
           ,3000-(Select Sum(L2.Loan) From Loans L2
                       Where L2.ORDate<=L1.ORDate
                           and L2.xyz=L1.xyz)
           From Loans L1
    The "xyz" part is from my confusion over whether there is several loans going on at one time here - is it loans for an EmpNo?

    At any rate - the basic concept is to have a sub-query that "totals" all the loans for that date going backwards - and subtract that from 3000.

    Obviously the 3000 should not be a constant but a field from a misc table that's joined in somehow.
    OMG!!!!!

    That's it. That's so cool.

    I thought you guys are going to post a friggin' Fetch Cursor yada yada yada that my brain could not understand.

    I never thought that a subquery could do that.

    That my friend is the most simple yet effective and a memory saver.

    Anyway, if you have any questions about ASP, ASP.NET, IIS just PM me. I usually do those all the time.
    Rate Me! Rate Me! Rate Me!

    Time to fly.

    Copyright GraysonSoft Inc. 2007

  13. #13
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: So hard that I don't know what it is called...

    Quote Originally Posted by tommygrayson
    OMG!!!!!

    That's it. That's so cool.

    ...I never thought that a subquery could do that.

    That my friend is the most simple yet effective and a memory saver.

    ...
    I spend 120% of my day writing queries - it's good and it's bad

    I'm interested in how to make my app/db usable without the VB client - so maybe we can talk about ASP and such...

    Where are you located?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  14. #14

    Thread Starter
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: So hard that I don't know what it is called...

    Quote Originally Posted by szlamany
    I spend 120% of my day writing queries - it's good and it's bad

    I'm interested in how to make my app/db usable without the VB client - so maybe we can talk about ASP and such...

    Where are you located?
    Ah. The trend of the web apps.

    My permanent residence is now in San Francisco.

    But I travel a lot today doing errands for my uncle.
    Rate Me! Rate Me! Rate Me!

    Time to fly.

    Copyright GraysonSoft Inc. 2007

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