我正在尝试创建一个查询,该查询在执行时将显示日期,状态(有多个状态选项)和该日期的事件数-按状态区分。
我能够创建一个查询,该查询显示我想要的所有数据,但是我得到的是重复的日期。我认为做到这一点的方法是使用别名,但是我找不到方法。我也尝试了案例陈述,但也没有成功。
SELECT cast(event_date AS date) AS date, count(event_status) AS amount, status
FROM events
GROUP BY date, status
现在这就是我得到的:
date | amount | status
---------------------------------------
2019-05-07 | 5 | YES
2019-05-07 | 1 | NO
2019-05-06 | 4 | YES
2019-05-05 | 3 | YES
2019-05-04 | 6 | YES
2019-05-04 | 2 | MAYBE
这就是我想要得到的:
date | POSITVE | Negative
---------------------------------------
2019-05-07 | 5 | 1
2019-05-06 | 4 | 0
2019-05-05 | 3 | 0
2019-05-04 | 6 | 2
其中任何不等于“是”的状态均为负。 有什么建议么? 只是要澄清一下:我想要的输出只是选项之一,我知道可以用不同的方法来解决我的问题,但这很适合我。谢谢
答案 0 :(得分:2)
使用case
和Sum
,例如:
SELECT cast(event_date AS date) AS date,
SUM(CASE WHEN status='YES' THEN 1 ELSE 0 END) as POSITIVE,
SUM(CASE WHEN status !='YES' THEN 1 ELSE 0 END) as NEGATIVE
FROM events
GROUP BY date
答案 1 :(得分:2)
您可以进行条件聚合:
for i in l:
print(i)
但是,('2', '40', '1', '69', '42')
('10', '67', '3', '101', '43')
('1', '122', '2', '153', '45')
('10', '151', '3', '183', '44')
('2', '186', '4', '216', '47')
('5', '214', '5', '245', '46')
具有速记版本:
select cast(event_date AS date) AS date,
sum(case when status = 'Yes' then 1 else 0 end) as Positive,
sum(case when status <> 'Yes' then 1 else 0 end) as Negative
from events e
group by cast(event_date AS date);
答案 2 :(得分:1)
MariaDB与MySQL一样,在数字上下文中将布尔值视为整数。我将其写为:
const app = document.getElementById('root')
const container = document.createElement('div')
container.setAttribute('class', 'container')
var prev = ''
app.appendChild(container)
var opt = []
var request = new XMLHttpRequest()
request.open('GET', 'https://findfalcone.herokuapp.com/planets', true)
request.onload = function() {
// Begin accessing JSON data here
var data = JSON.parse(this.response)
data.forEach(element=>{
opt.push(element)
});
console.log(opt)
populateDrop1()
}
function populateDrop1(){
const dropdown = document.getElementById('sel1')
var i =0
opt.forEach(element => {
const option = document.createElement('option')
option.setAttribute('value','')
option.textContent = element.name
i++
dropdown.appendChild(option)
//console.log(document.getElementById("id").value)
});
}
function populateDrop2(){
//console.log(opt)
const dropdown = document.getElementById('sel2')
var i =0
opt.forEach(element => {
const option = document.createElement('option')
option.setAttribute('value','')
option.textContent = element.name
i++
dropdown.appendChild(option)
//console.log(document.getElementById("id").value)
});
}
function populateDrop3(){
//console.log(opt)
const dropdown = document.getElementById('sel3')
var i =0
opt.forEach(element => {
const option = document.createElement('option')
option.setAttribute('value','')
option.textContent = element.name
i++
dropdown.appendChild(option)
//console.log(document.getElementById("id").value)
});
}
function populateDrop4(){
//console.log(opt)
const dropdown = document.getElementById('sel4')
var i =0
opt.forEach(element => {
const option = document.createElement('option')
option.setAttribute('value','')
option.textContent = element.name
i++
dropdown.appendChild(option)
//console.log(document.getElementById("id").value)
});
}
function show1(ele){
prev = ele.selectedIndex-1
//prev.push(ele.options[ele.selectedIndex].text)
opt.splice(prev,1)
populateDrop2()
}
function show2(ele){
prev = ele.selectedIndex-1
//prev.push(ele.options[ele.selectedIndex].text)
opt.splice(prev,1)
populateDrop3()
}
function show3(ele){
prev = ele.selectedIndex-1
//prev.push(ele.options[ele.selectedIndex].text)
opt.splice(prev,1)
populateDrop4()
}
request.send()