使用postgreSQL帮助我按日期选择得分范围内的多个计数
|date|score|
|----|-----|
|12/1|2 |
|12/1|4 |
|12/1|5 |
|12/2|3 |
|date|low0-2|high3-4|perfect|
|----|------|-------|-------|
|12/1|1 |1 |1 |
|12/2|0 |1 |0 |
答案 0 :(得分:0)
如果我正确地解释了您的示例,那么第二部分就是您希望从第一部分得到的输出,对吗?
您在这里:
styled.div.attrs(({ x, y, radius }) => ({
style: {
transform: `translate(${x - radius}px, ${y - radius}px)`
}
}))`
生产(用于测试的实际日期):
import styled from "styled-components";
const Container = styled.div.attrs(({ x, y, radius }) => ({
style: {
transform: `translate(${x - radius}px, ${y - radius}px)`
}
}))`
cursor: grab;
position: absolute;
width: 25px;
height: 25px;
background-color: red;
border-radius: 50%;
${({ isDragging }) =>
isDragging &&
`
opacity: 0.8
cursor: grabbing
`}
`;
export default Container;
互动示例:https://dbfiddle.uk/?rdbms=postgres_9.6&fiddle=2a7f160954020cdffc799e8a02491dc5
答案 1 :(得分:0)
您可以使用FILTER
子句进行组聚合,这是Postgres的一种特殊方式:
SELECT
my_date,
COUNT(score) FILTER (WHERE score BETWEEN 0 AND 2) AS "low0-2",
COUNT(score) FILTER (WHERE score BETWEEN 3 AND 4) AS "high3-4",
COUNT(score) FILTER (WHERE score = 5) AS "perfect"
FROM
scores
GROUP BY my_date
ORDER BY my_date
否则,您可以使用CASE
子句采用更多的SQL Standard方法:
SELECT
my_date,
SUM(
CASE
WHEN score BETWEEN 0 AND 2 THEN 1
ELSE 0
END
) AS "low0-2",
SUM(
CASE
WHEN score BETWEEN 3 AND 4 THEN 1
ELSE 0
END
) AS "high3-4",
SUM(
CASE
WHEN score = 5 THEN 1
ELSE 0
END
) AS "perfect"
FROM
scores
GROUP BY my_date
ORDER BY my_date