0

How to Calculate ruining Balance in Sql Server 2008. i am using following query working perfectly in sql server 2012 but not in sql server 2008.

SELECT empid, tentryno, glcode, descript, linenarrat, ttype, amount, tapplid, ftype, mentryno, mthttype, entrydate, ref, refdate, remarks, narrat, postflag, mapplid, mtype, post, acctheadcode, accthead, subacctheadcode, subaccthead
       , CASE 
        WHEN ttype = 'D'
            THEN amount
        ELSE 0.00
        END AS Debit, CASE 
        WHEN ttype = 'C'
            THEN amount
        ELSE 0.00
        END AS Credit, SUM(CASE 
            WHEN ttype = 'D'
                THEN amount
            ELSE amount * - 1
            END) OVER (
        PARTITION BY glcode ORDER BY tseqid
        ) AS balance, STATUS, receid, system.title, dbo.system.address, dbo.system.phone1, dbo.system.phone2, dbo.system.fax, dbo.system.signimage, dbo.system.logo
FROM CreditDebitDetails
CROSS JOIN system
WHERE STATUS != 0
Mudassir Hasan
  • 28,083
  • 20
  • 99
  • 133
  • 1
    You can try to format your query if you want someone to help you. Also this question is not related directly to C#. – mybirthname Dec 09 '16 at 09:53
  • _ruining Balance_ really ? Or 'running' balance? Put a little more effort into a question here, please! Do you really expect us to scroll over all that distance? – TaW Dec 09 '16 at 09:56
  • `;WITH cte AS ( <> ) SELECT * FROM cte a CROSS apply (SELECT Sum(CASE WHEN ttype = 'D' THEN amount ELSE amount * -1 END) AS balance FROM cte b WHERE a.glcode = b.glcode AND a.tseqid >= b.tseqid) cs` – Pரதீப் Dec 09 '16 at 10:04
  • You are right Prdp... my bad :) – Mark Kremers Dec 09 '16 at 10:11
  • thanx sir. i have use your query successfully run on sql server 2008. – zain ularifeen Dec 09 '16 at 17:02

0 Answers0