0

I need to find out the final profit based on the fact that some items have discount, but the discount is set between a date interval and only then is applied on a set of items. What's wrong here?

use Companie

select sum((Facturi_Emise.Cantitate)*C.Pret+(C.Pret*B.Adaos)/100)
when (Facturi_Emise.Data_Emitere BETWEEN A.Data_Start and A.Data_stop)

select sum(((Facturi_Emise.Cantitate)*C.Pret+(C.Pret*B.Adaos)/100)-
(C.Pret*A.Discount)/100)

from Facturi_Emise

LEFT JOIN

(Select Data_Start,Data_Stop,Id_Discount,Discount from Discount)as A

on Facturi_Emise.Id_Discount=A.Id_Discount

LEFT JOIN

(select Id_Produs,Adaos from Produse)as B

 on Facturi_Emise.Id_Produs=B.Id_Produs

LEFT JOIN

(Select Id_produs,Pret from Livrari)as C

on Produse.Id_Produs=C.Id_Produs 
Prix
  • 19,417
  • 15
  • 73
  • 132

1 Answers1

0

You can't use when by itself, it's a part of a case expression, which you incidentally can use to get the sum of a selecton of the records:

select
  sum(case
    when Facturi_Emise.Data_Emitere BETWEEN A.Data_Start and A.Data_stop then
      C.Pret * (Facturi_Emise.Cantitate + B.Adaos / 100)
    else
      0
  end) as RegularPrice,
  sum(case
    when Facturi_Emise.Data_Emitere BETWEEN A.Data_Start and A.Data_stop then
      0
    else
      C.Pret * (Facturi_Emise.Cantitate + (B.Adaos - A.Discount) / 100)
  end) as DiscountPrice
from ...
Guffa
  • 687,336
  • 108
  • 737
  • 1,005