我在PostgreSQL中有一个查询,产生:
itemorder name qty
1 A -20
2 A2 350
3 A 50
4 A -10
5 A2 10
itemorder
列提供了我希望看到的正确的行顺序。
我需要从下到上传递行,并为100
的每一行计算一个初始值为+ qty
且A
为itemorder name qty modifyed_sum
1 A -20 120 / 140 + (-20)
2 A2 350 140 / not A
3 A 50 140 / 90 + 50
4 A -10 90 / 100 + (-10)
5 A2 10 100 / not A
的新列。
public static boolean isPaladrome(int mNumber) {
String numToString = String.valueOf(mNumber);
int sLength = numToString.length();
int midPoint = sLength / 2;
return (new StringBuilder(numToString.substring(0, midPoint)).reverse()
.toString()).equals(numToString.substring(sLength - midPoint));
}
我该怎么做?
答案 0 :(得分:1)
试试这个
SELECT 100+ Sum(CASE WHEN name = 'a' THEN qty ELSE 0 END)OVER(ORDER BY itemorder DESC) as modifyed_sum,
qty,
name,
itemorder
FROM Yourtable
ORDER BY itemorder ASC
另一种方式
SELECT 100 + (SELECT Sum(CASE WHEN b.name = 'a' THEN b.qty ELSE 0 END)
FROM yourtable b
WHERE a.itemorder <= b.itemorder),
qty,
name,
itemorder
FROM yourtable a
ORDER BY itemorder ASC
答案 1 :(得分:0)
SELECT itemorder
, name
, qty
, 100 + SUM(CASE WHEN name = 'A' THEN qty ELSE 0 END)
OVER (ORDER BY itemorder ASC ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS modifyed_sum
FROM thetable
ORDER BY itemorder;
ROWS BETWEEN …
功能a value expression将在window function中使用。