我的数据看起来像这样:
的节点
Name Attribute Date
14 A1 11-OCT-2015
14 A2 7-Nov-2015
12 B1 11-Nov-2015
载体
Node V_NAME color Date
14 V1 blue 11-OCT-2015
14 V1 red 10-Nov-2015
14 V2 blue 7-Nov-2015
12 V3 black 11-Nov-2015
12 V4 black 11-Nov-2015
我希望得到如下结果
Node Attribute V_NAME color
14 A2 V1 red
14 A2 V2 blue
12 B1 V3 black
12 B1 V4 black
两个表中的日期列不相同
答案 0 :(得分:0)
if (typeof Minicart != "undefined") {
Minicart.prototype.updateItem = function(el) {
var cart = this;
var input = $j(this.selectors.quantityInputPrefix + $j(el).data('item-id'));
var quantity = parseInt(input.val(), 10);
cart.hideMessage();
cart.showOverlay();
$j.ajax({
type: 'POST',
dataType: 'json',
url: input.data('link'),
data: {qty: quantity, form_key: cart.formKey}
}).done(function(result) {
cart.hideOverlay();
if (result.success) {
cart.updateCartQty(result.qty);
if (quantity !== 0) {
cart.updateContentOnUpdate(result);
} else {
cart.updateContentOnRemove(result, input.closest('li'));
}
} else {
cart.showMessage(result);
}
}).error(function() {
cart.hideOverlay();
cart.showError(cart.defaultErrorMessage);
});
return false;
};
}
答案 1 :(得分:0)
使用NOT EXISTS
仅返回与其他日期不同的向量的向量。
select v.Node, n.Attribute, v.V_NAME, v.color
from nodes as n
join vectors as v on n.name = v.node
where not exists (select 1 from vectors as v2
where v2.v_name = v.v_name
and v2.date > v.date)
替代答案,使用子查询来选择每个v_name的最新日期:
select v.Node, n.Attribute, v.V_NAME, v.color
from nodes as n
join vectors as v on n.name = v.node
where (v.v_name, v.date) = (select v_name, max(date) from vectors group by v_name)
答案 2 :(得分:0)
我使用两个cte来计算每个类别中最近的行。然后加入两个。
<强> SqlFiddleDemo 强>
WITH n_node as (
SELECT "Name", "Attribute",
row_number() over (partition by "Name" order by "Date" DESC) rn
FROM Nodes
),
n_vector as (
SELECT "Node", "V_NAME", "color",
row_number() over (partition by "Node", "V_NAME" order by "Date" DESC) rn
FROM Vectors
)
SELECT "Name", "Attribute", "V_NAME", "color"
FROM n_node
JOIN n_vector
ON n_node.rn = n_vector.rn
AND n_node.rn = 1
AND n_node."Name" = n_vector."Node"
ORDER BY "Name" DESC
<强>输出强>
| Name | Attribute | V_NAME | color |
|------|-----------|--------|-------|
| 14 | A2 | V1 | red |
| 14 | A2 | V2 | blue |
| 12 | B1 | V3 | black |
| 12 | B1 | V4 | black |