Show quantity of products sold in the summer months (January-March) of all years (from 2000 to 2006).
You can use the month
(month) and year
(year) functions to establish a filter.
First, its basic use:
select year(fecha) anio
, month(fecha) mes
from (
select cast('20180101' as date) Fecha
) a
Now, how to filter, in your case, would be something like:
select *
from venta v
where year(v.fecha) between 2000 and 2006
and month(v.fecha) between 1 and 3 /* enero a marzo */
The rest of the adaptation in your particular case is left to the reader's exercise.
I tell you that you must do a group by, I have no where to test the query, I made it in oracle and it worked for me, when converting it for SQLServer it would be the following:
ORACLE
SELECT to_char(fecha_documento,'yyyy-mm') FECHA, nombre_producto "NOMBRE PRODUCTO", cantidad "CANTIDAD PRODUCTO"
FROM venta_detalle vd, producto pr, venta ve
WHERE to_char(fecha_documento,'yyyy') BETWEEN '2000' AND '2006'
AND to_char(fecha_documento,'mm') between '01' AND '03'
GROUP BY to_char(fecha_documento,'yyyy-mm')
ORDER BY 2 DESC;
SQLSERVER
SELECT [fech_ven] AS fecha, [nomb_pro] AS tipo_producto, [cant_pro_ven] AS cantidad_producto
FROM [dbo].[Venta_Detalle]ve_de, [dbo].[Producto]pr, [dbo].[Venta]ve
WHERE year([fech_ven]) BETWEEN 2000 AND 2006
AND month([fech_ven]) BETWEEN 1 AND 3
GROUP BY [fech_ven], [nomb_pro], [cant_pro_ven]
ORDER BY [fech_ven] ASC;
As you have very brief example, I show you what worked for me, any additional to the order
Best regards