Jump to content

I need help with an SQL Query


Athul

Recommended Posts

I have never done any calculations with sql query, so I have no idea how to do this so please help.

 

well i have two tables acc_tbl and ledger_tbl ( screenshots ) i need to find the the difference between total debit and total credit of that particular ledger debit (Dr) and credit(Cr) everything in screenshot. 

All i know was to join the table and select all thr columns available. 

Screenshot (4).png

Screenshot (5).png

Screenshot (7).png

Link to comment
Share on other sites

Sorry, I gave it a try but I couldn't come up with a query that gave your expected results from the data provided.

(Perhap it's because pictures of data don't load very well into my test database)

I could only get

+-------------+---------+---------+
| ledger_code | Debit   | Credit  |
+-------------+---------+---------+
|           5 |  300.00 | 2400.00 |
|          18 | 2400.00 |         |
|          30 |         |  300.00 |
|             | 2700.00 | 2700.00 |
+-------------+---------+---------+

What queries have you tried?

Link to comment
Share on other sites

54 minutes ago, Barand said:

Sorry, I gave it a try but I couldn't come up with a query that gave your expected results from the data provided.

(Perhap it's because pictures of data don't load very well into my test database)

I could only get

+-------------+---------+---------+
| ledger_code | Debit   | Credit  |
+-------------+---------+---------+
|           5 |  300.00 | 2400.00 |
|          18 | 2400.00 |         |
|          30 |         |  300.00 |
|             | 2700.00 | 2700.00 |
+-------------+---------+---------+

What queries have you tried?

Iam a beginer so all i did is just joined the two tables and selected all the colums necessary.I was Stuck there. I didnt know what to do next .

Could u please explain how you got that?

I could not upload .sql so i renmed to txt.

This is thr DB. 

transaction_db.txt

Link to comment
Share on other sites

The way to get the Dr and Cr totals is to use a group aggregation query. It seems strange that this wasn't covered in your course.

Example

mysql> SELECT a.ledger_code
    ->      , type
    ->      , SUM(amount) as total
    -> FROM acc_tbl a
    -> GROUP BY ledger_code, type;
+-------------+------+-------+
| ledger_code | type | total |
+-------------+------+-------+
| 18          | Dr   |  2400 |
| 30          | Cr   |   300 |
| 5           | Cr   |  2400 |
| 5           | Dr   |   300 |
+-------------+------+-------+

 

 

Link to comment
Share on other sites

1 hour ago, Barand said:

The way to get the Dr and Cr totals is to use a group aggregation query. It seems strange that this wasn't covered in your course.

Example

mysql> SELECT a.ledger_code
    ->      , type
    ->      , SUM(amount) as total
    -> FROM acc_tbl a
    -> GROUP BY ledger_code, type;
+-------------+------+-------+
| ledger_code | type | total |
+-------------+------+-------+
| 18          | Dr   |  2400 |
| 30          | Cr   |   300 |
| 5           | Cr   |  2400 |
| 5           | Dr   |   300 |
+-------------+------+-------+

 

 

I didn't do any course I am learning through yt and other sites

Link to comment
Share on other sites

Does this make it easier?

SELECT  a.ledger_code
      , l.ledger_name
      , SUM(IF(type='Dr',amount, null)) as Debits
      , SUM(IF(type='Cr',amount, null)) as Credits
FROM acc_tbl a
     JOIN ledger_tbl l USING (ledger_code)
GROUP BY a.ledger_code;

+-------------+---------------------+--------+---------+
| ledger_code | ledger_name         | Debits | Credits |
+-------------+---------------------+--------+---------+
| 18          | Maintenance         |   2400 |         |
| 30          | Annual Subscription |        |     300 |
| 5           | cash                |    300 |    2400 |
+-------------+---------------------+--------+---------+

 

Link to comment
Share on other sites

2 hours ago, Barand said:

Does this make it easier?

SELECT  a.ledger_code
      , l.ledger_name
      , SUM(IF(type='Dr',amount, null)) as Debits
      , SUM(IF(type='Cr',amount, null)) as Credits
FROM acc_tbl a
     JOIN ledger_tbl l USING (ledger_code)
GROUP BY a.ledger_code;

+-------------+---------------------+--------+---------+
| ledger_code | ledger_name         | Debits | Credits |
+-------------+---------------------+--------+---------+
| 18          | Maintenance         |   2400 |         |
| 30          | Annual Subscription |        |     300 |
| 5           | cash                |    300 |    2400 |
+-------------+---------------------+--------+---------+

 

How long have you been programing?? I didn't even know tat we could use conditional statements inside queries. Well thank you very much for your time and this lesson God bless.

 

Link to comment
Share on other sites

6 minutes ago, Athul said:

How long have you been programing?

  • 53 years designing and developing business systems and applications,
  • 37 years databases and SQL,
  • 21 years PHP
7 minutes ago, Athul said:

I didn't even know tat we could use conditional statements inside queries

https://dev.mysql.com/doc/refman/5.7/en/functions.html

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.