从仓库库存中提取数据,按文章分组,但如果描述发生变化,则使用最后一个 USED

Posted

技术标签:

【中文标题】从仓库库存中提取数据,按文章分组,但如果描述发生变化,则使用最后一个 USED【英文标题】:Extract data from wharehouse stock, group by article but, if description change, use the last USED 【发布时间】:2020-04-13 07:09:06 【问题描述】:

我有一个文件 xls,在第一张表中我有我的货物库存记录,我每年都提取这个文件,并将结果放入同一张表的最后一行。 我尝试匹配过去 3 年,文章代码是唯一的,但描述已更改。 有一种按文章代码分组的方法,如果描述发生了变化,使用最后找到的? 这是文件

这是我的查询,我使用 ADODB.Connection:

SELECT tab1[Codice Articolo], tab1Descrizione, Sum(IIf([anno]=2017,[qta],0)) AS Qta2017, Sum(IIf([anno]=2017,[Tot],0)) AS Val2017, Sum(IIf([anno]=2017,[€Pz],0)) AS Cad2017, Sum(IIf([anno]=2018,[Qta],0)) AS Qta2018, Sum(IIf([anno]=2018,[Tot],0)) AS Val2018, Sum(IIf([anno]=2018,[€pz],0)) AS Cad2018, Sum(IIf([anno]=2019,[Qta],0)) AS Qta2019, Sum(IIf([anno]=2019,[Tot],0)) AS Val2019, Sum(IIf([anno]=2019,[€pz],0)) AS Cad2019
FROM tab1
GROUP BY tab1.[Codice Articolo], tab1.Descrizione;

这是qry结果

这是我希望能够拥有的:

我认为我需要对同一张表使用一个连接,我尝试了这段代码的一些变体,没有权利:

SELECT t1[Codice Articolo], t2.Descrizione,(IIf(t1.[anno]=2017,t1.[qta],0)) AS Qta2017, Sum(IIf(t1.[anno]=2017,t1.[Tot],0)) AS Val2017, Sum(IIf(t1.[anno]=2017,t1.[€Pz],0)) AS Cad2017, Sum(IIf(t1.[anno]=2018,t1.[Qta],0)) AS Qta2018, Sum(IIf(t1.[anno]=2018,t1.[Tot],0)) AS Val2018, Sum(IIf(t1.[anno]=2018,t1.[€pz],0)) AS Cad2018, Sum(IIf(t1.[anno]=2019,t1.[Qta],0)) AS Qta2019, Sum(IIf(t1.[anno]=2019,t1.[Tot],0)) AS Val2019, Sum(IIf(t1.[anno]=2019,t1.[€pz],0)) AS Cad2019
FROM tab1 t1
LEFT JOIN (Select t2.Descrizione from Tab1 t2 on t2.anno = max(t1.anno)
LEFT JOIN 
GROUP BY tab1[Codice Articolo], t2.Descrizione;

是正确的方法,但错误是在代码上还是错误的方法? 感谢您的任何建议

【问题讨论】:

【参考方案1】:

问题在于描述,其值有时等于文章代码,有时不等于。这会创建新的组,这不是您想要的。

这是避免这种情况的一种方法:

SELECT 
    tab1[Codice Articolo], 
    Max(IIf(tab1[Codice Articolo] <> tab1Descrizione, tab1Descrizione, null)) AS tab1Descrizione
    Sum(IIf([anno]=2017,[qta],0)) AS Qta2017, 
    Sum(IIf([anno]=2017,[Tot],0)) AS Val2017, 
    ..., 
    Sum(IIf([anno]=2019,[€pz],0)) AS Cad2019
FROM tab1
GROUP BY tab1.[Codice Articolo], tab1.Descrizione;

【讨论】:

对于这篇文章,我只摘录了几篇文章;蝙蝠我有一个例子,在 2017 年,代码在描述中是相同的,但在 2018 年或 2019 年,描述发生了变化。如果代码产品上的描述相同与否,则相等,目标是使用上一年的描述。无论如何,你的代码给我这个错误:运行时错误'-2147467259(80004005)'我使用excel,ADODB和Provider = Microsoft.ACE.OLEDB.12.0【参考方案2】:

我确信我的问题的解决方案是使用 JOIN QUERY,但我看不到光,因为我需要两个 JOIN。

这是我找到的解决方案:

strsql = "SELECT t1.[Codice Articolo], t2.Descrizione," & _
"Sum(IIf(t1.[Esercizio]=2017,t1.[NrPz],0)) AS Qta2017," & _
"Sum(IIf(t1.[Esercizio]=2017,t1.[€Tot],0)) AS Val2017," & _
"Sum(IIf(t1.[Esercizio]=2017,t1.[€Pz],0)) AS Cad2017," & _
"Sum(IIf(t1.[Esercizio]=2018,t1.[NrPz],0)) AS Qta2018," & _
"Sum(IIf(t1.[Esercizio]=2018,t1.[€Tot],0)) AS Val2018," & _
"Sum(IIf(t1.[Esercizio]=2018,t1.[€pz],0)) AS Cad2018," & _
"Sum(IIf(t1.[Esercizio]=2019,t1.[NrPz],0)) AS Qta2019," & _
"Sum(IIf(t1.[Esercizio]=2019,t1.[€Tot],0)) AS Val2019," & _
"Sum(IIf(t1.[Esercizio] = 2019, t1.[€pz], 0)) As Cad2019 " & _
"FROM ([temp$] t1 INNER JOIN [temp$] AS t2 ON t1.[Codice Articolo] = t2.[Codice Articolo]) INNER JOIN (SELECT Max([Esercizio]) AS maxdiEsercizio, [Codice Articolo] FROM [temp$] GROUP BY [Codice Articolo]) t3 ON (t2.[Codice Articolo] = t3.[Codice Articolo]) AND (t2.Esercizio = t3.maxdiEsercizio) " & _
"GROUP BY t1.[Codice Articolo], t2.Descrizione;"

谢谢。 法布里奇奥

【讨论】:

以上是关于从仓库库存中提取数据,按文章分组,但如果描述发生变化,则使用最后一个 USED的主要内容,如果未能解决你的问题,请参考以下文章

从 teradata 提取/输出数据

如何按小时对 HKStatistics 进行分组?

如何对数据进行分组以从选择查询中按周显示平均价格

Laravel 选择列,然后按日期对列值进行分组

geom_text - 使按颜色分组的绘图的文本变黑

ANSI SQL 按(印度)财政年度 YTD 分组 - (过去 5 年 YTD)