estoy tratando de crear un query que me arroje los artículos que se le han vendido al cliente el mes anterior y en el mismo se obtengan los pedidos pendientes del articulo y del mismo cliente por separado estas serian las consultas
venta mes anterior.
SELECT T0.[CardName],T1.[ItemCode], sum(T1.[OpenCreQty]), T1.[unitMsr] FROM ORDR T0 INNER JOIN RDR1 T1 ON T0.[DocEntry] = T1.[DocEntry] WHERE MONTH(T0.DocDate) = MONTH(GETDATE())-1 AND YEAR(T0.DocDate) = YEAR(GETDATE()) and
T0.[CANCELED] = 'N' GROUP BY T0.[CardName],T1.[ItemCode], T1.[unitMsr]
pedidos atrasados
SELECT T0.[CardName],T1.[ItemCode],sum(T1.[OpenCreQty]), T1.[unitMsr] FROM ORDR T0 INNER JOIN RDR1 T1 ON T0.[DocEntry] = T1.[DocEntry] WHERE MONTH(T0.DocDate) = MONTH(GETDATE())-1 AND YEAR(T0.DocDate) = YEAR(GETDATE()) and
T0.[CANCELED] = 'N' GROUP BY T0.[CardName],T1.[ItemCode], T1.[unitMsr]
espero pueda alguien ayudarme ya que el union all solo me arroja en la msima columna y lo quiero en columnas distintas.
SELECT T0.[CardName],T1.[ItemCode], sum(T1.[OpenCreQty]), T1.[unitMsr]
FROM ORDR T0 INNER JOIN RDR1 T1 ON T0.[DocEntry] = T1.[DocEntry] WHERE MONTH(T0.DocDate) = MONTH(GETDATE())-1 AND YEAR(T0.DocDate) = YEAR(GETDATE()) and
T0.[CANCELED] = 'N' GROUP BY T0.[CardName],T1.[ItemCode], T1.[unitMsr]
union all
SELECT T0.[CardName],T1.[ItemCode],sum(T1.[OpenCreQty]), T1.[unitMsr]
FROM ORDR T0 INNER JOIN RDR1 T1 ON T0.[DocEntry] = T1.[DocEntry] WHERE MONTH(T0.DocDate) = MONTH(GETDATE())-1 AND YEAR(T0.DocDate) = YEAR(GETDATE()) and
T0.[CANCELED] = 'N' GROUP BY T0.[CardName],T1.[ItemCode], T1.[unitMsr]
que tal Jose lo intente pero al hacer eso solo me agrega en la misma columna ambos datos, lo que necesito es que estén en columnas separadas. sobre la misma fila.
ejemplo
cliente---------item---------cantidad vendida--------cantidad colocada en ordenes de compra-----umi
juan aaaa 30 80 par
así es como están por separado las consultas las columnas que ocupo cantidad vendida y cantidad atrasada siempre y cuando sea mismo cliente y articulo.
SELECT T0.[CardName],T1.[ItemCode], sum(T1.[OpenCreQty]), sum(T1.[Quantity]),T1.[unitMsr] ,
FROM ORDR T0 INNER JOIN RDR1 T1 ON T0.[DocEntry] = T1.[DocEntry] WHERE MONTH(T0.DocDate) = MONTH(GETDATE())-1 AND YEAR(T0.DocDate) = YEAR(GETDATE()) and
T0.[CANCELED] = 'N' GROUP BY T0.[CardName],T1.[ItemCode], T1.[unitMsr]
la consulta que me propones no da los resultados que necesito, el resultado que me da la consulta propuesta pero en la columna cantidad vendida me hacen falta artículos y hay diferencia en cantidades. es decir tu consulta me da 10 art. y mi consulta 40.
al principio yo tambien crei que seria algo facil pero no tengo el conocimiento para unir las tablas oinv, inv1, ordr, rdr1 y me de los resultados que necesito.
FROM OCRD T0
LEFT JOIN (
SELECT T0.[CardName],T1.[ItemCode], sum(T1.[OpenCreQty]) AS ‘VTA’, T1.[unitMsr] FROM ORDR T0 INNER JOIN RDR1 T1 ON T0.[DocEntry] = T1.[DocEntry] WHERE MONTH(T0.DocDate) = MONTH(GETDATE())-1 AND YEAR(T0.DocDate) = YEAR(GETDATE()) and
T0.[CANCELED] = ‘N’ GROUP BY T0.[CardName],T1.[ItemCode], T1.[unitMsr]
)T1 ON T1.[CardCode]=T0.[CardCode]
LEFT JOIN (
SELECT T0.[CardName],T1.[ItemCode],sum(T1.[OpenCreQty]) AS ‘ATRASO’, T1.[unitMsr] FROM ORDR T0 INNER JOIN RDR1 T1 ON T0.[DocEntry] = T1.[DocEntry] WHERE MONTH(T0.DocDate) = MONTH(GETDATE())-1 AND YEAR(T0.DocDate) = YEAR(GETDATE()) and
T0.[CANCELED] = ‘N’ GROUP BY T0.[CardName],T1.[ItemCode], T1.[unitMsr]
)T2 ON T2.[CardCode]=T0.[CardCode]
SELECT
T0.[CardName],
T1.[ItemCode],
T2.ATRASO ,
T1.VTA ,
T1.[unitMsr]
FROM OCRD T0
LEFT JOIN
(SELECT T0.[CardName],
T1.[ItemCode],
sum(T1.[OpenCreQty]) AS 'VTA',
T1.[unitMsr]
FROM
ORDR T0 INNER JOIN RDR1 T1 ON T0.[DocEntry] = T1.[DocEntry]
WHERE
MONTH(T0.DocDate) = MONTH(GETDATE())-1 AND YEAR(T0.DocDate) = YEAR(GETDATE()) and T0.[CANCELED] = 'N'
GROUP BY
T0.[CardName],
T1.[ItemCode],
T1.[unitMsr]
)T1 ON T1.[CardCode]=T0.[CardCode]
LEFT JOIN
(SELECT T0.[CardName],
T1.[ItemCode],
sum(T1.[OpenCreQty]) AS 'ATRASO',
T1.[unitMsr]
FROM
ORDR T0 INNER JOIN RDR1 T1 ON T0.[DocEntry] = T1.[DocEntry]
WHERE
MONTH(T0.DocDate) = MONTH(GETDATE())-1 AND YEAR(T0.DocDate) = YEAR(GETDATE()) and T0.[CANCELED] = 'N'
GROUP BY
T0.[CardName],
T1.[ItemCode],
T1.[unitMsr]
)T2 ON T2.[CardCode]=T0.[CardCode]
queda asi pero marca lo siguiente no se si cambie algo en que sea SAP B1
1). [Microsoft][SQL Server Native Client 10.0][SQL Server]Invalid column name ‘CardCode’.
2). [Microsoft][SQL Server Native Client 10.0][SQL Server]Invalid column name ‘CardCode’.
3). [Microsoft][SQL Server Native Client 10.0][SQL Server]Statement 'Job 2020-10-14 12:26:28 -1
Pienso que les falta el CardCode a tus consultas, agregalo al select y al group by
SELECT T0.[CardName], T0.CardCode,
T1.[ItemCode],
sum(T1.[OpenCreQty]) AS ‘ATRASO’,
T1.[unitMsr]
FROM
ORDR T0 INNER JOIN RDR1 T1 ON T0.[DocEntry] = T1.[DocEntry]
WHERE
MONTH(T0.DocDate) = MONTH(GETDATE())-1 AND YEAR(T0.DocDate) = YEAR(GETDATE()) and T0.[CANCELED] = ‘N’
GROUP BY
T0.[CardName],
T1.[ItemCode],
T1.[unitMsr], T0.CardCode
SELECT
T0.[CardName],
T0.[CardCode],
T1.[ItemCode],
T1.VTA,
T2.ATRASO,
T1.[unitMsr]
FROM OCRD T0
LEFT JOIN
(SELECT
T0.[CardName],
T0.[CardCode],
T1.[ItemCode],
sum(T1.[quantity]) AS 'VTA',
T1.[unitMsr]
FROM
OINV T0 INNER JOIN INV1 T1 ON T0.[DocEntry] = T1.[DocEntry]
WHERE
MONTH(T0.DocDate) = MONTH(GETDATE())-1 AND YEAR(T0.DocDate) = YEAR(GETDATE()) and T0.[CANCELED] = 'N'
GROUP BY
T0.[CardName],
T1.[ItemCode],
T1.[unitMsr],
T0.[CardCode]
)T1 ON T1.[CardCode]=T0.[CardCode]
LEFT JOIN
(SELECT
T0.[CardName],
T0.[CardCode],
T1.[ItemCode],
sum(T1.[OpenCreQty]) AS 'ATRASO',
T1.[unitMsr]
FROM
ORDR T0 INNER JOIN RDR1 T1 ON T0.[DocEntry] = T1.[DocEntry]
WHERE
T0.[CANCELED] = 'N' and T1.[OpenCreQty]<>0
GROUP BY
T0.[CardName],
T1.[ItemCode],
T1.[unitMsr],
T0.[CardCode]
)T2 ON T2.[CardCode]=T0.[CardCode]
quedo justo asi, sin embargo el resultado solo deberia ser una linea ya que en la imagen que te adjunto aparecen muchas lineas, solo deberia aparecer la linea dos esto comprobandolo con las copnsultas por separado
que tal @Jhonattan quise analizar tu consulta porque creo tener la solución, pero veo en tus dos consultas que son la misma consulta. Tanto de VENTAS mes ANTERIOR como PEDIDOS atrasados.
Que tal @Jhonattan por ahi vi unas imagenes que adjuntaste, este seria el Query en cuestion, me contas si te funciona:
SELECT TX.[CardName],TX.[ItemCode], SUM(TX.[PED]), SUM(TX.[FAC]),T1.[unitMsr]
FROM (
SELECT T0.[CardName],T1.[ItemCode], SUM(0) “PED”, SUM(T1.[Quantity]) “FAC”,T1.[unitMsr]
FROM OINV T0
INNER JOIN INV1 T1 ON T0.[DocEntry] = T1.[DocEntry]
WHERE MONTH(T0.DocDate) = MONTH(GETDATE())-1 AND YEAR(T0.DocDate) = YEAR(GETDATE())
AND T0.[CANCELED] = ‘N’
GROUP BY T0.[CardName],T1.[ItemCode], T1.[unitMsr]
UNION ALL
SELECT T0.[CardName],T1.[ItemCode],SUM(T1.[OpenCreQty]), SUM(0), T1.[unitMsr]
FROM ORDR T0
INNER JOIN RDR1 T1 ON T0.[DocEntry] = T1.[DocEntry]
WHERE MONTH(T0.DocDate) = MONTH(GETDATE())-1 AND YEAR(T0.DocDate) = YEAR(GETDATE())
AND T0.[CANCELED] = ‘N’
GROUP BY T0.[CardName],T1.[ItemCode], T1.[unitMsr]
) TX
GROUP BY TX.[CardName],TX.[ItemCode],TX.[unitMsr]