1

我在 Adempiere 工作,现在在 pgAdmin III 中使用来自 adempiere 的数据库。我得到了这段代码,我刚刚学习 SQL 3 天。

所以我有这个代码:

SELECT t.m_transaction_id, t.ad_client_id, t.ad_org_id, t.movementtype, 
t.movementdate, t.movementqty, t.m_attributesetinstance_id, 
asi.m_attributeset_id, asi.serno, asi.lot, asi.m_lot_id, asi.guaranteedate,
t.m_product_id, p.value, p.name, p.description, p.upc, p.sku, p.c_uom_id,
p.m_product_category_id, p.classification, p.weight, p.volume, p.versionno,
t.m_locator_id, l.m_warehouse_id, l.x, l.y, l.z, t.m_inventoryline_id,
il.m_inventory_id, t.m_movementline_id, ml.m_movement_id, t.m_inoutline_id,
iol.m_inout_id, t.m_productionline_id, prdl.m_productionplan_id, prdp.m_production_id,
t.c_projectissue_id, pjl.c_project_id, 
COALESCE(il.line, ml.line, iol.line, prdl.line, pjl.line) 
AS line,
daysbetween(
(T .movementdate):: TIMESTAMP WITH TIME ZONE,
getdate()
)AS movementagedays

FROM adempiere.m_transaction t
JOIN adempiere.m_locator l ON t.m_locator_id = l.m_locator_id
JOIN adempiere.m_product p ON t.m_product_id = p.m_product_id
LEFT JOIN adempiere.m_attributesetinstance asi ON t.m_attributesetinstance_id =
asi.m_attributesetinstance_id
LEFT JOIN adempiere.m_inventoryline il ON t.m_inventoryline_id = il.m_inventoryline_id
LEFT JOIN adempiere.m_movementline ml ON t.m_movementline_id = ml.m_movementline_id
LEFT JOIN adempiere.m_inoutline iol ON t.m_inoutline_id = iol.m_inoutline_id
LEFT JOIN adempiere.m_productionline prdl ON t.m_productionline_id =
prdl.m_productionline_id
LEFT JOIN adempiere.m_productionplan prdp ON prdl.m_productionplan_id =
prdp.m_productionplan_id
LEFT JOIN adempiere.c_projectissue pjl ON t.c_projectissue_id = pjl.c_projectissue_id;

以一张漂亮的桌子结尾(查看 rv_transaction):

表 http://imageshack.us/a/img829/3558/tablerl.png

最后一列是motionagedays,它告诉我们有多少天没有对产品进行操作。

我只想添加一列“islager”,如果在 movementagedays 列的相应行中该值小于 -90,则该列将以“超过 90 天”结尾。

所以我写了类似的东西

CASE WHEN movementagedays < -90 THEN 'more than 90 days' 
END AS isLager
FROM rv_transaction 

但我不知道它是否正确以及放在哪里。 此外,如果大代码写得好,性能优化等,idk。

如果有人可以帮助我,我会很高兴。对不起,我的 SQL 技能和我的英语一样糟糕

4

1 回答 1

2

几乎是对的。对你来说正确的陈述应该是:

SELECT *,
    CASE WHEN movementagedays < -90
         THEN 'more than 90 days'
         ELSE NULL END AS isLager
FROM rv_transaction
于 2012-10-25T07:38:12.067 回答