0

I am using MySql database. I have made following query and run at prompt, its running correctly.

SELECT 
    tr_date,
    tr_invno,
    particulars,
    received,
    issued, 
       @running_total:=@running_total + t.balance AS running_balance
FROM
(SELECT 
    srno,
    tr_date,
    tr_invno,
    tr_type,
    tr_accounthead as particulars,
    case when tr_type=1 then tr_qty else 0 end received,
    case when tr_type=2 then tr_qty else 0 end issued, 
    case when tr_type=1 then tr_qty else -(tr_qty) end balance 
    FROM tblinvtransaction where tr_date between '2021-09-01' and '2021-09-09' and tr_itemcode = '01') t
JOIN (SELECT @running_total:=(select sum(case when tr_type='1' then tr_qty else -(tr_qty) end) from tblinvtransaction where tr_date<'2021-09-01' and tr_itemcode = '01')
) r
ORDER BY t.tr_date, t.tr_type

but when i use it in C# its giving error "Fatal error encountered during command execution"

        string query = @"SELECT tr_date, tr_invno, particulars, received, issued, @running_total:= @running_total + t.balance AS running_balance 
            FROM(SELECT srno, tr_date, tr_invno, tr_type, tr_accounthead as particulars,
                case when tr_type = 1 then tr_qty else 0 end received,
                case when tr_type = 2 then tr_qty else 0 end issued,
                case when tr_type = 1 then tr_qty else -(tr_qty)end balance
                FROM tblinvtransaction where tr_date between '2021-09-01' and '2021-09-09' and tr_itemcode = '01') t
            JOIN(SELECT @running_total:= (select sum(case when tr_type = '1' then tr_qty else -(tr_qty)end) from tblinvtransaction where tr_date < '2021-09-01' and tr_itemcode = '01')
            ) r
            ORDER BY t.tr_date, t.tr_type)";
        string MySql = string.Concat(query);
        MySqlDataAdapter da = new MySqlDataAdapter(MySql, connection);
        DS_ItemLedger ds = new DS_ItemLedger();
        da.Fill(ds, "DT_ItemLedger");

Please guide.

3
  • It might work but it's not officially supported to use variables like this. If your MySQL is modern, like v8+ there are proper ways of doing a running total. Or you could just do it in c#.. Commented Sep 23, 2021 at 10:56
  • I am using v8+, please guide proper ways of doing a running total. Commented Sep 23, 2021 at 12:17
  • What is the full exception message and call stack? idownvotedbecau.se/noexceptiondetails Commented Sep 23, 2021 at 12:54

4 Answers 4

1

MySqlCommand typically doesn't allow user variables in SQL statements by default. To enable this, you have to add Allow User Variables = true; to your connection string. See https://mysqlconnector.net/connection-options/#AllowUserVariables for more details.

It should work with MySql.Data, but you may have to switch from MySql.Data to MySqlConnector to fully get this working properly.

Sign up to request clarification or add additional context in comments.

Comments

0

you can't declare variable in raw query so, you should create a stored procedure, and call it in your code

Comments

0

You can do this?

SELECT 
    tr_date,
    tr_invno,
    particulars,
    received,
    issued, 
    running_total + t.balance AS running_balance
FROM
(SELECT 
    srno,
    tr_date,
    tr_invno,
    tr_type,
    tr_accounthead as particulars,
    case when tr_type=1 then tr_qty else 0 end received,
    case when tr_type=2 then tr_qty else 0 end issued, 
    case when tr_type=1 then tr_qty else -(tr_qty) end balance,
    (select sum(case when tr_type='1' then tr_qty else -(tr_qty) end) from tblinvtransaction where tr_date<'2021-09-01' and tr_itemcode = '01') as running_total
    FROM tblinvtransaction where tr_date between '2021-09-01' and '2021-09-09' and tr_itemcode = '01') t
ORDER BY t.tr_date, t.tr_type

3 Comments

unknown column running_total error occur.
dont do a join, call the (select sum(case when tr_type='1' then tr_qty else -(tr_qty) end) from tblinvtransaction where tr_date<'2021-09-01' and tr_itemcode = '01') as running_total on your second query directly, i edited the answear
Now error has been removed but running total calculation goes wrong. Its not adding previous line total. Just add second query result in all rows.
0

I have found an alternate solutions as;

Solution 1.

SELECT 
    srno,
    tr_date,
    tr_invno,
    tr_type,
    tr_accounthead as particulars,
    case when tr_type=1 then tr_qty else 0 end received,
    case when tr_type=2 then tr_qty else 0 end issued, 
    **sum(case when tr_type=1 then tr_qty else -(tr_qty) end) over (order by tr_date, srno rows unbounded preceding)+4 as running_total**
    FROM tblinvtransaction where tr_date between '2021-09-01' and '2021-09-09' and tr_itemcode = '02'
ORDER BY tr_date, tr_type

Solution 2.

SELECT 
    srno,
    tr_date,
    tr_invno,
    tr_type,
    tr_accounthead as particulars,
    case when tr_type=1 then tr_qty else 0 end received,
    case when tr_type=2 then tr_qty else 0 end issued
    ,**(
    SELECT SUM(case when tr_type=1 then tr_qty else -(tr_qty) end)
        FROM tblinvtransaction AS A
        WHERE A.srno <= B.srno and  A.tr_itemcode ='01'
    ) AS CumulativeSum**
FROM tblinvtransaction AS B
WHERE B.tr_itemcode ='01'
ORDER BY B.srno

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.