这是我想要转换的当前查询,它需要一个项目ID,然后接收具有最新日期和不等于TRAN的类型的相应价格/ qty / vendor_id。
SELECT cs_po_products.price, cs_po_products.qty, cs_po.vendor_id
FROM cs_po_products, cs_po
WHERE cs_po_products.stock_number = :items_id
AND cs_po_products.po_number = cs_po.id
AND type !='TRAN'
ORDER BY cs_po.date_ordered DESC
LIMIT 1
这个查询工作正常,但是必须要花很多时间才能使页面加载有点慢。我正在尝试创建一个查询,它将为每个stock_number获取最新date_ordered的ROW。在搜索答案时,我遇到了很多技巧,但最常见的是在连接中使用MAX()of date_ordered的两个表的JOIN。但唉,我似乎无法提出正确的查询。
这是许多尝试之一,但我认为是最接近的尝试之一。
SELECT a.price, a.qty, b.vendor_id, a.stock_number, b.max, b.type
FROM cs_po_products a
LEFT JOIN (SELECT MAX(date_ordered)as max,vendor_id,type, date_ordered,id FROM cs_po) b
ON b.id = a.po_number
WHERE stock_number != '' AND date_ordered IS NOT NULL
AND type !='TRAN'
ORDER BY stock_number, date_ordered DESC
如果有人能指出我正确的方向,请提前谢谢
编辑:
所提供的所有答案仍然为库存编号提供多行,例如:
price qty vendor_id stock_number date_ordered
0.05446 123750 51 00010005.01S 2014-02-24 15:29:00
0.05446 123750 51 00010005.01S 2014-01-02 14:25:00
0.05446 123750 51 00010005.01S 2013-11-04 13:48:00
0.05402 123750 51 00010005.01S 2013-08-20 10:02:00
0.0532 123750 51 00010005.01S 2013-07-12 09:21:00
0.0538 123750 51 00010005.01S 2013-04-02 12:15:00
0.0532 123750 51 00010005.01S 2012-12-27 00:00:00
0.0555 101750 51 00010005.01S 2012-11-07 10:55:00
0.555 137500 51 00010005.01S 2012-11-02 09:39:00
0.0532 137500 51 00010005.01S 2012-11-02 09:37:00
0.0532 123750 51 00010005.01S 2012-10-03 00:00:00
我希望在这个结果中唯一的行是最顶层的,因为它有最新的日期,但在实际结果中还有更多的stock_numbers,我想要那些相应的最大日期以及
答案 0 :(得分:0)
在第一个查询中,只需删除限制和where条件,然后按cs_po_products.stock_number
添加具有相同order by
的组,以便它只选择第一次出现。我希望这应该有效。
这些链接可以帮助你
SQL Select only rows with Max Value on a Column或
How to select single row based on the max value in multiple rows
答案 1 :(得分:0)
尝试这种方式:
SELECT a.price, a.qty, b.vendor_id, a.stock_number, b.max, b.type
FROM cs_po_products a
LEFT JOIN (SELECT vendor_id, type, date_ordered, id,
MAX(date_ordered)as max
FROM cs_po
GROUP BY date_ordered) b
ON a.po_number = b.id
WHERE a.stock_number != ''
AND b.date_ordered IS NOT NULL
AND b.type !='TRAN'
ORDER BY a.stock_number, b.date_ordered DESC
答案 2 :(得分:0)
从cs_po获取每个id的最新date_ordered的子查询,然后将其与cs_po连接以获取表中的其他值: -
SELECT cs_po_products.price, cs_po_products.qty, cs_po.vendor_id
FROM cs_po_products
INNER JOIN
(
SELECT id, MAX(date_ordered) AS date_ordered
FROM cs_po
GROUP BY id
) sub0
ON cs_po_products.po_number = sub0.id
INNER JOIN cs_po
ON sub0.id = cs_po.id
AND sub0.date_ordered = cs_po.date_ordered
WHERE type !='TRAN'
EDIT。
最好猜测你想要得到什么。但是如果没有表格布局和输入测试数据,这是一个很大的猜测。
SELECT a.*, b.*
FROM
(
SELECT a.stock_number, MAX(date_ordered) AS date_ordered
FROM cs_po_products a
INNER JOIN cs_po b
ON b.id = a.po_number
GROUP BY a.stock_number
) sub0
INNER JOIN cs_po_products a
ON a.stock_number = sub0.stock_number
INNER JOIN cs_po b
ON b.id = a.po_number
AND b.date_ordered = sub0.date_ordered
WHERE type !='TRAN'
答案 3 :(得分:0)
避免使用反连接进行子查询:
SELECT cpp.price, cpp.qty, cp1.vendor_id, cpp.stock_number, cp1.date_ordered
FROM cs_po_products cpp
JOIN cs_po cp1
ON cp1.id = cpp.po_number
AND cp1.date_ordered IS NOT NULL
AND cp1.type != 'TRAN'
LEFT JOIN cs_po cp2
ON cp2.id = cpp.po_number
AND cp2.type != 'TRAN'
AND cp2.date_ordered > cp1.date_ordered
WHERE cpp.stock_number != ''
AND cp2.id IS NULL
ORDER BY cpp.stock_number ASC
这将为cs_po_products记录返回多行,该记录具有多个具有相同最近日期的co_po记录,因为您未指定在该情况下使用哪条记录。
你的一些专栏对他们属于哪个表格不明确,所以如果我错了,请告诉我。