Solved

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

Posted on 2008-10-28
7
174 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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