Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello,
i wanna bring the total till current date through my this query
please tell me some amendments.

What I have tried:

SQL
create procedure sp_broughtForward
(
  @gl_acid int,
  @userid int,
  @dateFrom datetime,
  @dateTo datetime
  )
  as 
  begin

SELECT 	ROW_NUMBER() OVER (Order by gLedeger.PK_id) as sno,gLedeger.gl_date as date,gLedeger.gl_narration  as Narration ,
	case when gLedeger.gl_drcr > 0 then abs(gLedeger.gl_drcr) else 0.00 end as debit,
	case when gLedeger.GL_DRCR	 < 0 then abs(gLedeger.gl_drcr) else 0.00 end as credit,
	SUM(gLedeger.gl_drcr) OVER(ORDER BY gledeger.pk_ID ROWS UNBOUNDED PRECEDING) AS    Balance  
	FROM gLedeger   LEFT JOIN master ON 
    gLedeger.gl_acid = master.PK_ID left join registration r on master.userId= @userid
    WHERE gLedeger.gl_acid = @gl_acid
	and gl_date between Convert(Date, @dateFrom ,103) and Convert(Date,@dateTo,103)
	ORDER BY gLedeger.gl_date
end
Posted
Updated 1-Nov-16 0:52am
v3

1 solution

Your questions very brief and difficult to fully understand the situation, however it appears you want to retrieve the data from your query plus have a total or subtotal in the result.

The easiest way to do this is to create two queries that return the result of your query and union all a second query with the subtotal you want. Please note the fields returned in a union all queries must be the same order and data types.

eg

Create procedure sp_broadForward
(
  @gl_acid int,
  @userid int,
  @dateFrom datetime,
  @dateTo datetime
  )
  as 
  begin
 
SELECT 	ROW_NUMBER() OVER (Order by gLedeger.PK_id) as sno,gLedeger.gl_date as date,gLedeger.gl_narration  as Narration ,
	case when gLedeger.gl_drcr > 0 then abs(gLedeger.gl_drcr) else 0.00 end as debit,
	case when gLedeger.GL_DRCR	 < 0 then abs(gLedeger.gl_drcr) else 0.00 end as credit,
	SUM(gLedeger.gl_drcr) OVER(ORDER BY gledeger.pk_ID ROWS UNBOUNDED PRECEDING) AS    Balance  
	FROM gLedeger   LEFT JOIN master ON 
    gLedeger.gl_acid = master.PK_ID left join registration r on master.userId= @userid
    WHERE gLedeger.gl_acid = @gl_acid
	and gl_date between Convert(Date, @dateFrom ,103) and Convert(Date,@dateTo,103)   -- etc (data)

Union all

select Field1s1, Fields2 --etc (Query to return the total or subtotal you want)

End
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900