Mysql 问题发生时的SQL案例
我正在尝试将交易日期排序为账龄策略。当Mysql 问题发生时的SQL案例,mysql,sql,Mysql,Sql,我正在尝试将交易日期排序为账龄策略。当LastDate在该位置停留的时间超过账龄天数时限额政策应显示为超龄,如果不在范围内,则参照当前日期。 以下是当前表格: +---------+------+----------+-------------+ |LastDate | Part | Location | Aging Days | +---------+------+----------+-------------+ 12/1/2016 123 VVV 90 8/1
LastDate
在该位置停留的时间超过账龄天数时
限额政策应显示为超龄
,如果不在范围内,则参照当前日期。
以下是当前表格:
+---------+------+----------+-------------+
|LastDate | Part | Location | Aging Days |
+---------+------+----------+-------------+
12/1/2016 123 VVV 90
8/10/2017 444 RRR 10
8/01/2017 144 PR 21
7/15/2017 12 RRR 10
以下是查询:
select
q.lastdate,
r.part, r.location,
a.agingpolicy as 'Aging Days'
from opsintranexcel r (nolock)
left InventoryAging a (nolock) on r.location=a.location
left join (select part,MAX(trandate) as lastdate from opsintran group by
part) q on r.part=q.part
下面是我想添加的额外列:
+---------+------+----------+------------+---------+
|LastDate | Part | Location | Aging Days | Age |
+---------+------+----------+------------+---------+
12/1/2016 123 VVV 90 Overage
8/10/2017 444 RRR 10 Within
8/01/2017 144 PR 21 Within
7/15/2017 12 RRR 10 Overage
非常感谢您的帮助。我认为下面的代码适合您
SELECT
q.lastdate,
r.part,
r.location,
a.agingpolicy as 'Aging Days'
'Age' =
CASE
WHEN DATEDIFF( day, q.LastDate, GETDATE() ) > a.agingpolicy THEN 'Overage'
ELSE THEN 'Within'
END
FROM opsintranexcel r (nolock)
LEFT JOIN InventoryAging a (nolock) on r.location=a.location
LEFT JOIN (
SELECT part,MAX(trandate) as lastdate
FROM opsintran
WHERE trantype='II' and PerPost>='201601'
GROUP BY part) q ON r.part=q.part
如果超过或在账龄天数内,您可以检查当前日期和lastdate值之间的差异
CASE WHEN DATEDIFF(NOW(), q.lastdate) > a.agingpolicy
THEN 'Overage'
ELSE 'Within'
END AS age
您应该将查询修改为:
select
q.lastdate,
r.part, r.location,
a.agingpolicy as 'Aging Days',
if(DATEDIFF(NOW(), q.lastdate)) > a.agingpolicy, 'Overage','Within') as 'Age'
from opsintranexcel r (nolock)
left InventoryAging a (nolock) on r.location=a.location
left join (select part,MAX(trandate) as lastdate from opsintran where
trantype='II' and PerPost>='201601' group by part) q on r.part=q.part
你们在邮局把我弄丢了。若你们喜欢,请按下按钮作为真实的回答:(!)()。