Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it L'avatar di Lino80
    Registrato dal
    Oct 2005
    Messaggi
    1,560

    sommare 2 record se l ID è uguale

    devo sommare i 2 record della tabella "Scarico" se il campo idprodotto è uguale, come faccio?
    sommare il campo "scarico", il fatturato e le spese

    in pratica se "idprodotto" è uguale non mi elenca tutti e 2 i record, ma solo il primo..
    codice:
    <%
    
    'ricavo i dati del form
    dim dataDal, dataAl, g1,g2,m1,m2,a1,a2
    
    g1=request.Form("g1")
    g2=request.Form("g2")
    m1=request.Form("m1")
    m2=request.Form("m2")
    a1=request.Form("a1")
    a2=request.Form("a2")
    
    dataDal=a1 & "-" & m1 & "-" & g1
    dataAl=a2 & "-" & m2 & "-" & g2
    
    if not isDate(dataDal) or not isDate(dataAl) then
    	dataDal=Year(now()) & "-" & Month(now()) & "-" & Day(now())
    	dataAl=Year(now()) & "-" & Month(now()) & "-" & Day(now())
    end if
    
    if cdate(dataDal)>cdate(dataAl) then dataDal=dataAl
    
    
    Dim QSOrdinaPer, SQL, RS, SqlWh, SQLsc
    
    dim totIDs
    totIDs=0
    'prendo tutti gli idprodotto e i totali delle vendite nel periodo scelto
    
    SQLsc="select idprodotto, scarico, sum(pre_vendita*scarico) as fatturato, sum(pre_acquisto*scarico) as spese"
    SQLsc=SQLsc & " from "
    SQLsc=SQLsc & "(select * from Scarico where data_scarico>=#" & dataDal & "# and data_scarico<=#" & dataAl & "#)"
    SQLsc=SQLsc & " group by idprodotto, scarico order by idprodotto asc"
    
    set RS=Conn.execute(SQLsc)
    
    if not RS.eof then
    
    	while not RS.eof
    	
    	totIDs=totIDs+1
    	
    	Redim preserve Fatturato(totIDs)
    	Redim preserve Spese(totIDs)
    	Redim preserve IDs(totIDs)
    	Redim preserve Scarico(totIDs)
    
    	Fatturato(totIDs-1)=RS("fatturato")
    	Spese(totIDs-1)=RS("spese")
    	IDs(totIDs-1)=RS("idprodotto")
    	Scarico(totIDs-1)=RS("scarico")
    	
    	RS.movenext
    	wend
    end if
    
    set RS=nothing
    
    %>
    
    
    <p align="right">
    	">Indietro
    </p>
    
    
    
    Ricerca dal <%= dataDal %> al <%= dataAl %></p>
    
    <%
    if totIDs=0 then
    %>
    
    
    Nessun risultato trovato nel periodo selezionato</p>
    <%
    else
    %>
    <table align="center" border="0" width="650">
    	<tr>
    		<td width="50" bgcolor="#DBDBDB">Modello</td>
    		<td width="50" bgcolor="#DBDBDB">Misura</td>
    		<td width="50" bgcolor="#DBDBDB">Venduti</td>
    		<td width="25" bgcolor="#DBDBDB">Fatturato</td>
    		<td width="50" bgcolor="#DBDBDB">Costo</td>
    		<td width="50" bgcolor="#DBDBDB">Guadagno</td>
    	</tr>
    <%
    	
    	
    	SQL = " SELECT PDescrizione,GDescrizione FROM Prodotti where id in ("
    	for i=0 to totIDs-1
    		if i>0 then SQL=SQL & ","
    		SQL=SQL & IDs(i)
    	next
    	SQL=SQL & ") order by id asc"
    	
    	Set RS = Conn.Execute(SQL)
    	
    	dim counter
    	counter=0
    	
    	If Not RS.EOF Then 
    	While Not RS.EoF
    %>
    		<tr>
    			<td valign="top"><%= RS("PDescrizione")%></td>
    			<td valign="top"><%= RS("GDescrizione")%></td>
    			<td valign="top"><%= Scarico(counter) %></td>
                <td valign="top"><%= Fatturato(counter) %> &euro;</td>
    			<td valign="top"><%= Spese(counter) %> &euro;</td>
    			<td valign="top"><%= formatnumber(Fatturato(counter)-Spese(counter),2) %> &euro;</td>
    		</tr>
    <%
    	counter=counter+1
    	RS.MoveNext
    	wend
    	
    	set RS=nothing
    %>
    
    </table>
    
    <%
    end if
    end if
    %>
    
    
    <%
    	Conn.Close
    	Set Conn = Nothing
    %>

  2. #2
    SELECT SUM(campo) AS totale FROM tabella1 WHERE campoID = 5

    usi l'alias totale per ottenere il totale
    rs("totale")

  3. #3
    Utente di HTML.it L'avatar di Lino80
    Registrato dal
    Oct 2005
    Messaggi
    1,560
    così?:

    codice:
    SQLsc="select idprodotto, scarico, sum(pre_vendita*scarico) as fatturato, sum(pre_acquisto*scarico) as spese, SUM(idprodotto*scarico) AS totale FROM Scarico WHERE ID = 5"
    non ho capito il campoID = 5, come lo modifico?


  4. #4
    Utente di HTML.it L'avatar di Lino80
    Registrato dal
    Oct 2005
    Messaggi
    1,560
    se scarico una quantità alla volta mi va a sommare il totale del fatturato:

    codice:
    <%= formatnumber(Fatturato(counter)-Spese(counter),2) %>
    se scarico da 2 a più quantità alla volta non mi va a sommare il fatturato, posto il codice:

    codice:
    <%
    	Dim QSID, Add, SQL, PVendita, PAcquisto
    	Dim quantita_old, quantita_att,data
    	
    	quantita_old=0: quantita_att=0: PVendita=0.00: PAcquisto=0.00
    	
    	QSID = Request.Form("ID")
    	Add = Request.Form("Add")
    	
    	If Not IsNumeric(Add) Then Response.Redirect "errore.asp?cod=" & server.URLEncode("il campo quantità non è numerico")
    	
    	Add=cint(Add)
    	
    	'//--------------------------------------------------------------------------------------
    	'aggiorno la tabella Scarico
    	'prelevo la vecchia quantità del prodotto
    	'calcolo la nuova quantità
    	'salvo
    	
    	sql="Select Prodotto, CPrezzo, VPrezzo from Prodotti where ID=" & replace(QSID,"'","''")
    	set rs=conn.execute(sql)
    	
    	if not rs.eof then 
    		quantita_old=rs(0)
    		PVendita=rs(2)
    		PAcquisto=rs(1)
    	end if
    	
    	set rs=nothing
    	
    	if quantita_old>0 then quantita_att=quantita_old-Add
    	
    	
    	'Check sul prezzo di vendita e prezzo di acquisto
    	PVendita=replace(PVendita,".","")
    	PVendita=replace(PVendita,",",".")
    	if not isNumeric(PVendita) then PVendita=0.00
    	
    	PAcquisto=replace(PAcquisto,".","")
    	PAcquisto=replace(PAcquisto,",",".")
    	if not isNumeric(PAcquisto) then PAcquisto=0.00
    	
    	
    	data=Year(now()) & "-" & Month(Now()) & "-" & Day(Now())
    	
    	'inserisco il record nella tabella Scarico
    	SQL="Insert into Scarico(idprodotto,scarico,disp_pre,disp_att,data_scarico,pre_vendita,pre_acquisto) values("
    	SQL=SQL & replace(QSID,"'","''") & "," & Add & "," & quantita_old & "," & quantita_att & ",#" & data & "#," & PVendita & "," & PAcquisto & ")"
    	
    	on error resume next
    	conn.execute(sql)
    	if err.number>0 then
    		Response.Write("Errore nell'aggiornare la tabella Scarico")
    		err.clear()
    	end if
    	
    	'//--------------------------------------------------------------------------------------
    	
    	SQL = "UPDATE Prodotti Set Prodotto = Prodotto - " & Add & " WHERE ID = " & replace(QSID,"'","''")
    	
    	Conn.Execute(SQL)
    	if err.number>0 then
    		Response.Write("Errore nell'update di Prodotti")
    		err.clear()
    	end if
    	
    	
    	Conn.close
    	Set Conn = Nothing
    	
    	Response.Redirect "scarica.asp"
    %>
    cosa è sbagliato?

    grazie

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.