Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. (Solved) Problem in formulating sql query for generating a balance sheet

(Solved) Problem in formulating sql query for generating a balance sheet

Scheduled Pinned Locked Moved Database
databasehelpsalesquestion
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P paul4everyone

    Greetings to you, Please assist me in combining these sql queries I have some record in my database (sql ce 4.0), using; 1.

    Select Customer_ID as Customer, Credit, Debit, Account_Balance as Balance, Transaction_Date as Date
    from ThriftsTrans Where Transaction_Date Between '15/Dec/2013' And '18/Dec/2013'

    Produces this result;

    Customer Credit Debit Balance Date

    000001 12000 12000 2013-12-16T14:32:00.9770000
    000001 5000 17000 2013-12-16T14:32:26.0070000
    000001 2000 15000 2013-12-16T14:32:43.3870000
    000001 2000 17000 2013-12-17T10:23:08.6470000
    000002 50000 50000 2013-12-17T10:25:35.4730000
    000002 1000 51000 2013-12-17T10:25:49.4230000
    000002 3000 48000 2013-12-17T10:26:07.4800000

    If i run this; 2.

    Select Customer_ID as Customer, sum(Credit) as Credit, sum(Debit) as Debit, (count(Credit) + count(Debit)) as Count from ThriftsTrans Where Transaction_Date Between '15/Dec/2013' And '18/Dec/2013' Group By Customer_ID

    I will get;

    Customer Credit Debit Count

    000001 19000 2000 4
    000002 51000 3000 3

    and lastly if i run; 3.

    Select Customer_ID as Customer, Account_Balance as Balance, Transaction_Date as Date From ThriftsTrans Where Transaction_Date In (Select Max(Transaction_Date) From ThriftsTrans Where Transaction_Date
    Between '15/Dec/2013' And '18/Dec/2013' Group By Customer_ID)

    I will get;

    Customer Balance Date

    000001 17000 2013-12-17T10:23:08.6470000
    000002 48000 2013-12-17T10:26:07.4800000

    But my problem now, is how do i combine query 2 & query 3 to get the following output from my database;

    Customer Credit Debit Count Balance Date

    000001 19000 2000 4 17000 2013-12-17T10:23:08.6470000
    000002 51000 3000 3 48000 2013-12-17T10:26:07.4800000

    Please help me and thanks a lot in advance.

    M Offline
    M Offline
    Mycroft Holmes
    wrote on last edited by
    #2

    I suggest you start exploring the wonderful world of JOINs. Identify the common field (foreign key) and create an inner join then select the filed you need from the 2 tables.

    Never underestimate the power of human stupidity RAH

    P 1 Reply Last reply
    0
    • M Mycroft Holmes

      I suggest you start exploring the wonderful world of JOINs. Identify the common field (foreign key) and create an inner join then select the filed you need from the 2 tables.

      Never underestimate the power of human stupidity RAH

      P Offline
      P Offline
      paul4everyone
      wrote on last edited by
      #3

      Thanks Holmes, I know the solution to my problem can be solved by using one of the JOINS. But my challenge is that my little brain is not just connecting me to the appropriate JOIN to use. I tried using a CROSS JOIN but the errors thrown shows that i am not formulating the query properly at all. Please help me. thanks a lot

      T M 2 Replies Last reply
      0
      • P paul4everyone

        Thanks Holmes, I know the solution to my problem can be solved by using one of the JOINS. But my challenge is that my little brain is not just connecting me to the appropriate JOIN to use. I tried using a CROSS JOIN but the errors thrown shows that i am not formulating the query properly at all. Please help me. thanks a lot

        T Offline
        T Offline
        thatraja
        wrote on last edited by
        #4

        paul4everyone wrote:

        I tried using a CROSS JOIN but the errors thrown shows that i am not formulating the query properly at all.

        Why didn't you share those details in your question? :|

        thatraja

        Code converters | Education Needed No thanks, I am all stocked up. - Luc Pattyn When you're wrestling a gorilla, you don't stop when you're tired, you stop when the gorilla is - Henry Minute

        P 1 Reply Last reply
        0
        • P paul4everyone

          Greetings to you, Please assist me in combining these sql queries I have some record in my database (sql ce 4.0), using; 1.

          Select Customer_ID as Customer, Credit, Debit, Account_Balance as Balance, Transaction_Date as Date
          from ThriftsTrans Where Transaction_Date Between '15/Dec/2013' And '18/Dec/2013'

          Produces this result;

          Customer Credit Debit Balance Date

          000001 12000 12000 2013-12-16T14:32:00.9770000
          000001 5000 17000 2013-12-16T14:32:26.0070000
          000001 2000 15000 2013-12-16T14:32:43.3870000
          000001 2000 17000 2013-12-17T10:23:08.6470000
          000002 50000 50000 2013-12-17T10:25:35.4730000
          000002 1000 51000 2013-12-17T10:25:49.4230000
          000002 3000 48000 2013-12-17T10:26:07.4800000

          If i run this; 2.

          Select Customer_ID as Customer, sum(Credit) as Credit, sum(Debit) as Debit, (count(Credit) + count(Debit)) as Count from ThriftsTrans Where Transaction_Date Between '15/Dec/2013' And '18/Dec/2013' Group By Customer_ID

          I will get;

          Customer Credit Debit Count

          000001 19000 2000 4
          000002 51000 3000 3

          and lastly if i run; 3.

          Select Customer_ID as Customer, Account_Balance as Balance, Transaction_Date as Date From ThriftsTrans Where Transaction_Date In (Select Max(Transaction_Date) From ThriftsTrans Where Transaction_Date
          Between '15/Dec/2013' And '18/Dec/2013' Group By Customer_ID)

          I will get;

          Customer Balance Date

          000001 17000 2013-12-17T10:23:08.6470000
          000002 48000 2013-12-17T10:26:07.4800000

          But my problem now, is how do i combine query 2 & query 3 to get the following output from my database;

          Customer Credit Debit Count Balance Date

          000001 19000 2000 4 17000 2013-12-17T10:23:08.6470000
          000002 51000 3000 3 48000 2013-12-17T10:26:07.4800000

          Please help me and thanks a lot in advance.

          S Offline
          S Offline
          Simon_Whale
          wrote on last edited by
          #5

          you would need to look at an INNER JOIN BUT also formulate your SELECT accordingly to show only the columns that you require.

          Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

          P 1 Reply Last reply
          0
          • T thatraja

            paul4everyone wrote:

            I tried using a CROSS JOIN but the errors thrown shows that i am not formulating the query properly at all.

            Why didn't you share those details in your question? :|

            thatraja

            Code converters | Education Needed No thanks, I am all stocked up. - Luc Pattyn When you're wrestling a gorilla, you don't stop when you're tired, you stop when the gorilla is - Henry Minute

            P Offline
            P Offline
            paul4everyone
            wrote on last edited by
            #6

            because what i did with the joins doesnt make sense. i just messed up the query 2 and 3 using JOIN. please help me

            1 Reply Last reply
            0
            • S Simon_Whale

              you would need to look at an INNER JOIN BUT also formulate your SELECT accordingly to show only the columns that you require.

              Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

              P Offline
              P Offline
              paul4everyone
              wrote on last edited by
              #7

              thanks for the reply. but i really dont know what to do in this. please assist me

              S 1 Reply Last reply
              0
              • P paul4everyone

                thanks for the reply. but i really dont know what to do in this. please assist me

                S Offline
                S Offline
                Simon_Whale
                wrote on last edited by
                #8

                http://www.w3schools.com/sql/sql_join_inner.asp[^] have a read through that

                Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

                P 1 Reply Last reply
                0
                • S Simon_Whale

                  http://www.w3schools.com/sql/sql_join_inner.asp[^] have a read through that

                  Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

                  P Offline
                  P Offline
                  paul4everyone
                  wrote on last edited by
                  #9

                  Thanks for the link but all the examples i am seeing is slightly different from my case. i dont know how to capture this fraction of code in the query. i mean

                  (Select Max(Transaction_Date) From ThriftsTrans Where Transaction_Date
                  Between '15/Dec/2013' And '18/Dec/2013' Group By Customer_ID)

                  1 Reply Last reply
                  0
                  • P paul4everyone

                    Thanks Holmes, I know the solution to my problem can be solved by using one of the JOINS. But my challenge is that my little brain is not just connecting me to the appropriate JOIN to use. I tried using a CROSS JOIN but the errors thrown shows that i am not formulating the query properly at all. Please help me. thanks a lot

                    M Offline
                    M Offline
                    Mycroft Holmes
                    wrote on last edited by
                    #10

                    I suggest you start studying, this article [^]may be useful to you.

                    Never underestimate the power of human stupidity RAH

                    1 Reply Last reply
                    0
                    • P paul4everyone

                      Greetings to you, Please assist me in combining these sql queries I have some record in my database (sql ce 4.0), using; 1.

                      Select Customer_ID as Customer, Credit, Debit, Account_Balance as Balance, Transaction_Date as Date
                      from ThriftsTrans Where Transaction_Date Between '15/Dec/2013' And '18/Dec/2013'

                      Produces this result;

                      Customer Credit Debit Balance Date

                      000001 12000 12000 2013-12-16T14:32:00.9770000
                      000001 5000 17000 2013-12-16T14:32:26.0070000
                      000001 2000 15000 2013-12-16T14:32:43.3870000
                      000001 2000 17000 2013-12-17T10:23:08.6470000
                      000002 50000 50000 2013-12-17T10:25:35.4730000
                      000002 1000 51000 2013-12-17T10:25:49.4230000
                      000002 3000 48000 2013-12-17T10:26:07.4800000

                      If i run this; 2.

                      Select Customer_ID as Customer, sum(Credit) as Credit, sum(Debit) as Debit, (count(Credit) + count(Debit)) as Count from ThriftsTrans Where Transaction_Date Between '15/Dec/2013' And '18/Dec/2013' Group By Customer_ID

                      I will get;

                      Customer Credit Debit Count

                      000001 19000 2000 4
                      000002 51000 3000 3

                      and lastly if i run; 3.

                      Select Customer_ID as Customer, Account_Balance as Balance, Transaction_Date as Date From ThriftsTrans Where Transaction_Date In (Select Max(Transaction_Date) From ThriftsTrans Where Transaction_Date
                      Between '15/Dec/2013' And '18/Dec/2013' Group By Customer_ID)

                      I will get;

                      Customer Balance Date

                      000001 17000 2013-12-17T10:23:08.6470000
                      000002 48000 2013-12-17T10:26:07.4800000

                      But my problem now, is how do i combine query 2 & query 3 to get the following output from my database;

                      Customer Credit Debit Count Balance Date

                      000001 19000 2000 4 17000 2013-12-17T10:23:08.6470000
                      000002 51000 3000 3 48000 2013-12-17T10:26:07.4800000

                      Please help me and thanks a lot in advance.

                      P Offline
                      P Offline
                      paul4everyone
                      wrote on last edited by
                      #11

                      After asking so many questions, i finally succeeded in tweaking out a query that is correct. it is as follows;

                      Select Customer, Count, Credit, Debit, Balance, Date from
                      (
                      Select Customer_ID as Customer, (count(Credit) + count(Debit)) as Count, sum(Credit) as Credit, sum(Debit) as Debit
                      from ThriftsTrans Where Transaction_Date Between '15/Dec/2013' And '18/Dec/2013' Group By Customer_ID
                      ) as a left join (
                      Select Customer_ID as Cust, Account_Balance as Balance,
                      Transaction_Date as Date From ThriftsTrans Where Transaction_Date In
                      (Select Max(Transaction_Date) From ThriftsTrans Where Transaction_Date
                      Between '15/Dec/2013' And '18/Dec/2013' Group By Customer_ID)
                      ) as b on a.Customer = b.Cust

                      thanks to you all

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups