我有3张桌子
交易记录表具有列
var printContents = document.getElementById('print_data').innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
setTimeout(function() {
location.reload();
}, 1000);
}
。产品表具有
date_id, prod_id,spend_amt, qty, transaction_fid
日期表具有
prod_id, desc, category
。如果产品date_id, week_id
最近4周售出,我需要标记产品active
该怎么做。
答案 0 :(得分:1)
使用EXISTS
子句检查产品是否存在日期范围内的交易。
select
p.*
case when exists
(
select *
from transaction t
join date d on d.date_id = t.date_id
where t.prod_id = p.prod_id
and d.week_id between 201815 and 201818
) then 1 else 0 end as is_active
from product p;
很奇怪,没有在表格中看到任何日期。在您的请求中,您要询问最近四个星期,如果不知道交易的日期,这是不可能的。但是,在您的请求注释中,您说您想查看范围week_id between 201815 and 201818
,因此我使用了它。
答案 1 :(得分:0)
如果您退出了最近4周内有交易的product_id,则可以使用CASE标记。
示例:
SELECT p.*
, (CASE WHEN wk4.prod_id IS NOT NULL THEN 'Y' ELSE 'N' END) AS isActive
FROM Product p
LEFT JOIN (
SELECT t.prod_id
FROM Transaction t
JOIN "date" d ON d.date_id = t.date_id
WHERE d.week_id >= to_number(to_char(current_date-(7*4), 'YYYYWW'))
GROUP BY t.prod_id
) wk4 ON wk4.prod_id = p.prod_id
答案 2 :(得分:0)
最后我有办法做到这一点。
SELECT pr.prod_id, (case when count(distinct tr.transaction_fid)>=1 THEN 'Y' ELSE 'N' end) as active_flag
FROM transaction tr
INNER JOIN product pr ON pr.prod_id = tr.prod_id
INNER JOIN
(
SELECT date_id, week_id
FROM date
WHERE week_id BETWEEN 201815 AND 201818
) d ON d.date_id = tr.date_id