如何在 Google Sheets 中使用 SQL 进行表查找

问题描述 投票:0回答:1

这是我的数据(

Lot No
。&
Date
的组合始终是唯一的):

所需结果:

我用的是这个:

=QUERY(Production!A:G, "SELECT A,B, C, D, G WHERE A is NOT NULL label C 'NEW LOT NO.', D 'NEW LOT NO. DATE' ", 1)

如何在查询公式中添加列

OLD ASSIGNED

这是表格

编辑

我正在寻找一个解决方案仅使用查询(或嵌套查询)并且不使用其他公式,例如vlookup或过滤器或任何东西。

google-sheets google-sheets-formula
1个回答
1
投票

试试这个:

=arrayformula(QUERY({Production!A:G,iferror(vlookup(Production!A1:A&"-"&Production!B1:B,{Production!C:C&"-"&Production!D:D,Production!G:G},2,false),)}, "SELECT Col1,Col2,Col8,Col3,Col4,Col7 WHERE Col1 is NOT NULL label Col3 'NEW LOT NO.', Col4 'NEW LOT NO. DATE', Col8 'OLD ASSIGNED'  ", 1))

或仅适用于

query

=
query(
{
query({Production!A:G},"select Col7 where Col6 is not null order by Col4,Col3 label Col7 'OLD ASSIGNED'",1),
query({Production!A:G},"select Col1,Col2,Col3,Col4,Col7 where Col1 is not null order by Col2,Col1 label Col3 'NEW LOT NO.', Col4 'NEW LOT NO. DATE' ",1)
},
"select Col2,Col3,Col1,Col4,Col5,Col6 order by Col5,Col3",1)

仅使用

query
,因此您需要为结果选择排序顺序,因为批号 277 出现在 A 列和 C 列中,但日期顺序不同。

如果您想要原始排序顺序,那么如果您准备使用

sequence

,它可以像这样工作
=
query(
{
query({Production!A:G},"select Col7 where Col6 is not null order by Col4,Col3 label Col7 'OLD ASSIGNED'",1),
query({Production!A:G,sequence(counta(Production!A:A)+countblank(Production!A:A),1)},"select Col1,Col2,Col3,Col4,Col7,Col8 where Col1 is not null order by Col2,Col1 label Col3 'NEW LOT NO.', Col4 'NEW LOT NO. DATE' ",1)
},
"select Col2,Col3,Col1,Col4,Col5,Col6 order by Col7",1)

sequence
用于生成行号序列(因为
row()
需要
arrayformula
- 这是你不想要的)。序列中的数字范围是
counta(Production!A:A)+countblank(Production!A:A)
,即生产表中的行数。

© www.soinside.com 2019 - 2024. All rights reserved.