Solved

How to add the preview value of a cell to the actual cell value?

Posted on 2008-10-28
7
183 Views
Last Modified: 2012-05-05
I want to make a subtotal colum that would consist of the last value from the subtotal plus the actual row value from 'qtd_venda'.

Example:

qtd_venda , subtotal

20, 20
20, 40
20, 60
35, 95
10, 105

Thanks.
SELECT NF.nro_nota, VI.qtd_venda
FROM SCGE_venda_item_tb VI 
    LEFT JOIN SCGE_venda_tb V ON V.id_venda = VI.id_venda
    LEFT JOIN SCGE_nota_fiscal_tb NF ON NF.id_venda = VI.id_venda

Open in new window

0
Comment
Question by:Emoraes
  • 3
  • 2
7 Comments
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22828278
So a running total?

The problem I can see with your query is that your subtotal comes from VI but your first column comes from NF which is in the right part of a left outer join.  You will end up with ALL items without nf.nro_nota being grouped into one.

I need to know the order in which your records appear.  You can't give a running total without knowing the order in which the records should appear.  Is there a date column, or an ID column somewhere?  

It's an easy query once I have that.

0
 

Author Comment

by:Emoraes
ID: 22851454
Actually I solved this problem creating a function to calc on-the-fly the running total. Don´t know if it is the best solution or the best one, but solved my problem.

Oh and the order is by nro_nota
GO
CREATE FUNCTION dbo.Calc_Subtotal (@nNota int)
RETURNS int
 BEGIN
   RETURN (
   SELECT SUM(VI.qtd_venda)
     FROM SCGE_venda_item_tb VI 
       LEFT JOIN SCGE_venda_tb V ON V.id_venda = VI.id_venda
       LEFT JOIN SCGE_nota_fiscal_tb NF ON NF.id_venda = VI.id_venda
   WHERE NF.nro_nota <= @nNota)
 END;
GO
SELECT NF.nro_nota, VI.qtd_venda, dbo.Calc_Subtotal(NF.nro_nota) as Subtotal
FROM SCGE_venda_item_tb VI 
    LEFT JOIN SCGE_venda_tb V ON V.id_venda = VI.id_venda
    LEFT JOIN SCGE_nota_fiscal_tb NF ON NF.id_venda = VI.id_venda
GO
DROP FUNCTION Calc_Subtotal;

Open in new window

0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22851562
Will that really do what you want?  You have no filtering to say what records in vi or v should be summed.  This will sum all records with a lower scge_nota_fiscal_tb.nro_nota value.

This is along the lines of what I was going to do, without a function though, but I needed to know what field was the "sorting field" which appears to be scge_nota_fiscal_tb.nro_nota.
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:Emoraes
ID: 22869687
Hmmm interesting position.

I´ll re-analyze the query and post here the infos.
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 23072919
I think that http:#22851562 points out that the query has a problem in that it's not limiting to the original join criteria.  The author never posted back about this, but I'm sure of it.
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 23112343
PAQed with points refunded (125)

Computer101
EE Admin
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Review MS SQL cluster diagram 9 101
2 comma seperated list - SQL Server 12 44
How to place a condition in a filter criteria in t-sql? 12 77
Need help in debugging a UDF results 7 29
There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question