sql - 在 Firebird 中使用 select in group by 语句

标签 sql firebird firebird2.5

我使用的是 Firebird 数据库,其中包含以下表格:

文章

<表类=“s-表”> <标题> 产品ID 长SKU <正文> 1 A22121000125 2 A22121000138 3 A22123001508 4 A22124002001

塔勒斯波尔特库洛斯

<表类=“s-表”> <标题> 产品ID 位置 尺寸 <正文> 1 1 小 1 2 中 1 3 大 1 4 超大号 1 5 超大号 2 1 小 2 2 中 2 3 大 2 4 超大号 2 5 超大号 3 1 02 3 2 04 3 3 06 3 4 08

和 兰戈斯塔勒

<表类=“s-表”> <标题> 产品ID 来自位置 到达位置 价格 <正文> 1 1 3 500 1 4 5 600 2 1 3 500 2 4 5 600 3 1 4 200

我希望能够按 longSKU 的子字符串 (shortSKU) 进行分组,并能够为每个 ShortSKU 获取相应的范围和价格。

喜欢这个例子:

<表类=“s-表”> <标题> 短SKU 尺寸来自 大小到 价格 <正文> A221210001 小 大 500 A221210001 超大 超大号 600 A221230015 02 08 200

我正在使用以下 cobe,但收到错误:

Dynamic SQL Error.

SQL error code = -104. Invalid expression in the select list (not contained in either an aggregate function or the >GROUP BY clause).

CREATE OR ALTER VIEW RANGOSPARACOSTOSYPRECIOS(
SHORTSKU,
SIZEFROM,
SIZETO,
PRICE ) AS select substring(ar.codigoparticular from 1 for 10) AS SHORTSKU,
( Select TAL.SIZE 
  From tallesporarticulos TAL 
  Where TAL.productid=Ar.productid 
  and TAL.position= RT.FromPosition) as SIZEFROM,
( Select TAL.SIZE 
  From tallesporarticulos TAL 
  Where TAL.productid=Ar.productid 
  and TAL.position= RT.ToPosition) as SIZETO,
  max(RT.PRICE)
from Articulos Ar
Inner Join tallesporarticulos TA On Ar.productId = TA.productId
Inner Join rangostalle RT On AR.productId = RT.productId
GROUP BY SHORTSKU, SIZEFROM, SIZETO ;

以下代码有效,但我需要将“fromposition”和“ToPosition”值替换为上面代码中的大小值,这就是我收到错误消息的时候。

CREATE OR ALTER VIEW RANGOSPARACOSTOSYPRECIOS(
SHORTSKU,
SIZEFROM,
SIZETO,
PRICE ) AS select substring(ar.codigoparticular from 1 for 10) AS SHORTSKU,
RT.FromPosition as SIZEFROM,
RT.ToPosition as SIZETO,
  max(RT.PRICE)
from Articulos Ar
Inner Join tallesporarticulos TA On Ar.productId = TA.productId
Inner Join rangostalle RT On AR.productId = RT.productId
GROUP BY SHORTSKU, SIZEFROM, SIZETO ;

对于任何有兴趣提供帮助的人,这里有上表中的插入数据。

CREATE TABLE articulos (
  ProductId INTEGER PRIMARY KEY,
  LongSKU varchar(12) NOT NULL
  );

INSERT INTO articulos VALUES (1, 'A22121000125');
INSERT INTO articulos VALUES (2, 'A22121000138');
INSERT INTO articulos VALUES (3, 'A22123001508');
INSERT INTO articulos VALUES (4, 'A22124002001');

CREATE TABLE TALLESPORARTICULOS (
  ProductId INTEGER NOT NULL,
  Position INTEGER NOT NULL,
  Sizes varchar(12) NOT NULL
  );
INSERT INTO TALLESPORARTICULOS (ProductId, position, Sizes) VALUES
(1, 1, 'SMALL'),
(1, 2, 'MEDIUM'),
(1, 3, 'LARGE'),
(1, 4, 'XTRALARGE'),
(1, 1, 'XXTRALARGE'),
(2, 2, 'SMALL'),
(2, 3, 'MEDIUM'),
(2, 4, 'LARGE'),
(2, 5, 'XTRALARGE'),
(2, 5, 'XXTRALARGE'),
(3, 1, '02'),
(3, 2, '03'),
(3, 3, '04'),
(3, 4, '05');

CREATE TABLE RANGOSTALLE (
  ProductId INTEGER NOT NULL,
  FromPosition INTEGER NOT NULL,
  ToPosition INTEGER NOT NULL,
  Price double not null
  );

INSERT INTO RANGOSTALLE (ProductId,FromPosition,ToPosition,Price) VALUES 
(1, 1,3,500),
(1, 4,5,600),
(2, 1,3,500),
(2, 4,5,600),
(3, 1,4,200);

最佳答案

您的脚本包含不少错误。修复它们后,查询就变得相当简单:

select substring(LongSKU from 1 for 10), low.sizes, high.sizes, avg(price)
from articulos join RANGOSTALLE on articulos.ProductId = RANGOSTALLE.ProductId
join TALLESPORARTICULOS low on RANGOSTALLE.ProductId = low.ProductId and RANGOSTALLE.FromPosition = low.Prodposition
join TALLESPORARTICULOS high on RANGOSTALLE.ProductId = high.ProductId and RANGOSTALLE.ToPosition = high.Prodposition
group by 1,2,3

https://dbfiddle.uk/?rdbms=firebird_3.0&fiddle=ae54a7d897da4604396775e3ddc4b764

可以通过将分组移动到派生表中来优化此查询,但这种优化很大程度上取决于实际的表结构和查询要求。

关于sql - 在 Firebird 中使用 select in group by 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70124946/

相关文章:

mysql - 如何从表中获得前三名

带有 bigint 命名输入参数的 Firebird 的执行语句

sql - 一直使用存储过程的缺点是什么?

delphi - 有没有Delphi+Firebird从头开始的文档(服务器端)

firebird - Nbackup从远程别名进行备份

sql - 更改 Firebird 数据库和表的默认排序规则

Firebird 与其他字符集的排序顺序

sql - Firebird 存储过程中的动态 SQL(where)

sql - 数据资源管理器中的语法错误

sql - 获取 MS SQL 中特定 varchar 的行数