1

I have a column name 'AmountLC' in the db table. Another column name 'DebitCredit' determines whether the 'AmountLC' is Positive or Negative. If DebitCredit="H", the values in AmountLC is Negative. If DebitCredit="S", the values in AmountLC is Positive. I need to make query to get the sum of AmountLC. I have tried to get the logic but didn't get any. I would be grateful if anyone can help me.

I tried the below code as per other stackoverflow discussions.

$F7 = mysql_query("SELECT 
                SUM(CASE WHEN DebitCredit='H' THEN DebitCredit ELSE 0 END) as NegativeTotal,
                SUM(CASE WHEN DebitCredit='S' THEN DebitCredit ELSE 0 END) as PostiveTotal 
                FROM T1_CSV_Table 
                WHERE Month='$getMonth' AND TaxCode='A0'");

THANKS! Update Code

$F7 = mysql_query("SELECT 
                    SUM(CASE WHEN DebitCredit='H' THEN AmountLC*-1 ELSE AmountLC END) as Total 
                    FROM T1_CSV_Table 
                    WHERE Month='$getMonth' AND TaxCode='A0'");
    $rowf7 = mysql_fetch_array($F7);
    echo "Total-".$rowf7['Total'];

Update code for results: SELECT AmountLC,DebitCredit FROM T1_CSV_Table WHERE TaxCode="A0" AND Month="12"

enter image description here

Zendie
  • 1,176
  • 1
  • 13
  • 30
  • 3
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Feb 08 '17 at 23:57
  • 1
    Shouldn't `THEN DebitCredit` be `THEN AmountLC`? – Barmar Feb 08 '17 at 23:59
  • 1
    How about `SUM(CASE WHEN DebitCredit='H' THEN AmountLC*-1 ELSE AmountLC END) as Total,` – RiggsFolly Feb 09 '17 at 00:01
  • yeah, i know it's not a logic. just tried a code. There are no separate positive and negative values. I need to decide depends on the DebitCredi value. – Zendie Feb 09 '17 at 00:01
  • @RiggsFolly I will try that – Zendie Feb 09 '17 at 00:01
  • @RiggsFolly I have updated the code as you said. But nothing returns – Zendie Feb 09 '17 at 00:13
  • Strange, it works for me! What data type is `AmountLC` – RiggsFolly Feb 09 '17 at 00:19
  • AmountLC is decimal(10,2) – Zendie Feb 09 '17 at 00:19
  • When you say nothing is returned what do you mean, if the query runs you should get something back even if its not the right number – RiggsFolly Feb 09 '17 at 00:21
  • Total-array(2) { [0]=> NULL ["Total"]=> NULL } This is the array I get when I run the query – Zendie Feb 09 '17 at 00:23
  • When I run the query in the db, I get Total=0.00 – Zendie Feb 09 '17 at 00:25
  • Are you sure you have any values in the DB for this month and TaxCode – RiggsFolly Feb 09 '17 at 00:26
  • yes, sure. Almost 2lks columns in the table – Zendie Feb 09 '17 at 00:28
  • Do a test for me, run `SELECT AmountLC, DebitCredit WHERE Month='Hard_code_a_valid_month' AND TaxCode='A0'` and show us a few results – RiggsFolly Feb 09 '17 at 00:30
  • ok I will try that – Zendie Feb 09 '17 at 00:32
  • I updated the result in the question. thanks – Zendie Feb 09 '17 at 00:36

2 Answers2

1

You can do that calculation all in one go if all you want is a total of the debits and credits

SELECT 
    SUM(CASE WHEN DebitCredit='H' THEN AmountLC*-1 ELSE AmountLC END) as Total
FROM T1_CSV_Table 
WHERE Month='$getMonth' AND TaxCode='A0'

NOTE: Your script is at risk of SQL Injection Attack Have a look at what happened to Little Bobby Tables Even if you are escaping inputs, its not safe! Use prepared parameterized statements

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • I am accepting this an answer. I will try to figure it out whats wrong with my code. Thanks for the logic :) – Zendie Feb 09 '17 at 00:31
-1

try this please:

SELECT
    SUM(IF(DebitCredit='H', DebitCredit, 0) AS NegativeTotal,
    SUM(IF(DebitCredit='S', DebitCredit, 0) AS PostiveTotal
FROM T1_CSV_Table 
WHERE
    MONTH='$getMonth'
AND
    TaxCode='A0';
Bernd Buffen
  • 14,525
  • 2
  • 24
  • 39