2013-07-09 48 views
0

我有两个表格,出货量和条目。按当月的星期选择

我必须选择并插入另一个表中所选item_id = 113的条目和item_id = 204的出货量,我的select是当前月份和年份的条目和出货量的总和。

我有这两个查询,每个项目。

总和(cantidad_ent)=项目数 出生日期=日期

SELECT sum(cantidad_ent) Entradas_Tortilla, DATEPART(wk, fecha) Semana, DATEPART(m, fecha) Mes, DATEPART(yy, fecha) 
FROM entradas 
where id_articulo=113 
and month(fecha)=month(getdate()) and year(fecha)=year(getdate()) 
GROUP BY DATEPART(wk, fecha), DATEPART(m, fecha), DATEPART(yy, fecha) 
order by DATEPART(wk, fecha), datepart(m, fecha) 

总和(cant_sale)=出货数量

SELECT sum(cant_Sale) Salidas_Costales, DATEPART(wk, fecha) Semana, DATEPART(m, fecha) Mes, DATEPART(yy, fecha) 
FROM salidas 
where id_articulo=204 
and month(fecha)=month(getdate()) and year(fecha)=year(getdate()) 
GROUP BY DATEPART(wk, fecha), DATEPART(m, fecha), DATEPART(yy, fecha) 
order by DATEPART(wk, fecha), datepart(m, fecha) 

他们返回

Tortillas_entries/Week/Month/Year 
     4503  27 7 2013 
     3822  28 7 2013 

FlourSack_shipments/Week/Month/Year 
     7    27 7 2013 
     6    27 7 2013 

文发送面粉袋从仓库出来,他们被处理成玉米饼,然后我们做一个入口从做的玉米饼。该查询由,所以我知道有多少饼和面粉袋发送和一个星期,我也必须要注册多少饼总麻袋一周

(tortillas_entries /面粉袋)为作了Rendimiento(生产性能) 七分之四千五百○三6分之3822

总发送同时注册进入玉米饼和麻袋的,所以在同一天,我们有多少麻袋被送往了多少玉米饼作了来自那些麻袋。

结果即时寻找会是这样的:

Tortillas_entries/FlourSack_Shipment/Performance/Week/Month/Year 
     4503    7    643.28 27 7 2013 
     3822    6    637  28 7 2013 

在此先感谢!

+0

是'fecha'类型的日期时间还是日期? –

+0

Smalldatetime @ mr.Reband –

回答

0

试试这个:

WITH My_CTE (Entradas_Tortilla, Semana, Mes, Ano) AS 
(
SELECT sum(cantidad_ent) AS 'Entradas_Tortilla' 
    ,DATEPART(wk, fecha) AS 'Semana' 
    ,DATEPART(m, fecha) AS 'Mes' 
    ,DATEPART(yy, fecha) AS 'Ano' 
FROM entradas 
WHERE id_articulo=113 and month(fecha)=month(getdate()) and year(fecha)=year(getdate()) 
GROUP BY DATEPART(wk, fecha) 
    ,DATEPART(m, fecha) 
    ,DATEPART(yy, fecha) 
) 
SELECT m.Entradas_Tortilla 
    ,sum(s.cant_Sale) AS 'Salidas_Costales' 
    ,CAST(m.Entradas_Tortilla AS REAL)/CAST(sum(s.cant_Sale) AS REAL) AS 'Performance' 
    ,Semana 
    ,Mes 
    ,Ano 
FROM My_CTE m INNER JOIN salidas s ON m.Semana = DATEPART(wk, s.fecha) 
    AND m.Mes = DATEPART(m, s.fecha) 
    AND m.Ano = DATEPART(yy, s.fecha) 
WHERE s.id_articulo=204 and month(s.fecha)=month(getdate()) and year(s.fecha)=year(getdate()) 
GROUP BY Entradas_Tortilla, Semana, Mes, Ano 
ORDER BY Ano, Mes, Semana 

希望我没有做一个错字或留下点什么了。这可能比使用公共表格表达更优雅的方式,但我认为这将适用于你。测试它,但让我知道,如果我搞砸了:-)编辑 - 是的,忘了把整数作为真正的,使他们可以分开;现在修复了

+0

谢谢,我试了一下,它完美的作品,祝你有美好的一天! =) –

0

像这样的INNER JOIN应该工作:

SELECT 
    sum(e.cantidad_ent) Tortillas_entries, 
    sum(s.cant_Sale) FlourSack_Shipment, 
    sum(cast(e.cantidad_ent as decimal(6,2)))/sum(s.cant_Sale) Performance, 
    DATEPART(wk, e.fecha) Semana, 
    DATEPART(m, e.fecha) Mes, 
    DATEPART(yy, e.fecha) 
FROM entradas e 
inner join salidas s on 
    DATEPART(wk, e.fecha) = DATEPART(wk, s.fecha) AND 
    DATEPART(m, e.fecha) = DATEPART(m, s.fecha) AND 
    DATEPART(yy, e.fecha) = DATEPART(yy, s.fecha) 
where e.id_articulo=113 
and s.id_articulo=204 
and month(e.fecha)=month(getdate()) and year(e.fecha)=year(getdate()) 
GROUP BY DATEPART(wk, e.fecha), DATEPART(m, e.fecha), DATEPART(yy, e.fecha) 
order by DATEPART(wk, e.fecha), datepart(m, e.fecha) 

SQLFiddle例如发现here

+0

返回的值不正确,但感谢您的时间,祝您有个美好的一天! –