(Solved) Problem in formulating sql query for generating a balance sheet
-
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.4800000If 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 3and 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.4800000But 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.4800000Please help me and thanks a lot in advance.
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
-
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
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
-
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
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
-
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.4800000If 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 3and 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.4800000But 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.4800000Please help me and thanks a lot in advance.
you would need to look at an
INNER JOIN
BUT also formulate yourSELECT
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
-
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
because what i did with the joins doesnt make sense. i just messed up the query 2 and 3 using JOIN. please help me
-
you would need to look at an
INNER JOIN
BUT also formulate yourSELECT
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
thanks for the reply. but i really dont know what to do in this. please assist me
-
thanks for the reply. but i really dont know what to do in this. please assist me
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
-
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
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) -
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
-
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.4800000If 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 3and 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.4800000But 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.4800000Please help me and thanks a lot in advance.
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.Custthanks to you all