2012-10-25 45 views
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):

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

最后一列是movementagedays它告诉我们有多少天没有与产品操纵。

我只是想添加一列“islager”,如果值在移动拦截列的相应行中小于-90,它将以“超过90天”结束。

所以我写了类似

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

,但我不知道这是否是正确的,并在哪里把它放在那里。 此外,IDK的如果大码以及writen,性能等

我会很高兴,如果有人可以帮助我进行了优化。 对不起我的SQL技能不好以及我的英文

回答

2

你懂了差不多没错。你正确的说法应该是:

SELECT *, 
    CASE WHEN movementagedays < -90 
     THEN 'more than 90 days' 
     ELSE NULL END AS isLager 
FROM rv_transaction 
+0

哎,现在还活着!谢谢1000x –