我想通过传递总和限制来汇总列(价格)。
例如,我有下表,我想将记录限制为10k或15k。
#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>
#include <ArduinoJson.h>
#define FIREBASE_HOST "http://temphu*****.firebaseio.com/"
#define FIREBASE_AUTH "VblTNS************OmWTW6n"
#define WIFI_SSID "A****"
#define WIFI_PASSWORD "9*****"
#define LED 2
void setup() {
pinMode(LED,OUTPUT);
digitalWrite(LED,0);
Serial.begin(9600);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("connected: ");
Serial.println(WiFi.localIP());
Firebase.begin(FIREBASE_HOST,FIREBASE_AUTH);
Firebase.setInt("LEDStatus",0);
}
void loop() {
if(Firebase.getInt("LEDStatus")) {
digitalWrite(LED,HIGH);
}
else {
digitalWrite(LED,LOW);
}
if (Firebase.failed()) { // Check for errors
Serial.print("setting /number failed:");
Serial.println(Firebase.error());
return;
}
delay(1000);
}
我希望结果应该是
对于<= 10000
ID PRICE
x1 10,000
x2 20,000
x3 5,000
x4 7,500.00
<= 15000
ID PRICE
x1 10,000
<= 14000
ID PRICE
x1 10,000
x3 5,000
我做了一些搜索,在postgresql中找到了一些窗口函数,即OVER函数。所以我写了下面的查询
ID PRICE
x3 5,000
x4 7,500.00
但是条件<= 15,000和<= 14000无法带来正确的结果。
我想总结价格列,并获取与给定金额或条件的总和结果相匹配的记录,更具体地说,它应该通过验证并返回可以容纳我们通过的金额内的任何其他记录来返回记录< / p>
请帮帮我。
谢谢
答案 0 :(得分:2)
您的Q不清楚,但是如果您愿意,可以对价格列进行排序,以便获得小于等于限制的最小总和。 尝试按价格列订购:
WITH cte AS (
SELECT *, sum(price) OVER (order BY price) AS total
FROM test1
)
SELECT *
FROM cte
WHERE total <= amount
答案 1 :(得分:0)
在这里https://www.db-fiddle.com/f/mckDPhtrY4vRrkF2NjhQcC/0
播放WITH b AS (
SELECT *, sum(price) OVER (order BY price) AS total
FROM a
)
SELECT *
FROM b
WHERE (total <= your_max OR price <= your_max) AND total >= your_min