在Postgresql中,我的一个列只包含5个值中的一个。这5个值中的一个是“成熟”。我正在尝试编写一个查询,该查询计算该列中特定剪切整个表中“成熟”的次数。
以下查询产生错误:DatabaseReference mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("semester").child("0").child("subjects").child("0");
mDatabaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
boolean guestType = (boolean) dataSnapshot.child("BCT").getValue();
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
。
查询:
syntax error at or near "Matured"
另外,我有以下结果:
select count(case when stagename is Matured end) from db_table
使用查询:
"Matured";694
"Credit Approved";3
"Delinquent";572
"Current";1356
"Canceled";16
但我需要列中的结果而不是行。像这样:
select distinct stagename,
sum(case when stagename is not null then 1 else 0 end)
from db_table
group by stagename
stage_count | 694 | 3 | 572 | 1356 | 16
有什么想法吗?
答案 0 :(得分:0)
您在查询中错过了Matured的单引号以及语句不正确的情况,这就是它返回语法错误的原因。您可以使用以下查询来获取所需的结果。 您还可以阅读有关postgres的crosstab功能。
select count(case when stagename = 'Matured' then 1 end),
count(case when stagename = 'Credit Approved' then 1 end),
count(case when stagename = 'Delinquent' then 1 end),
count(case when stagename = 'Current' then 1 end),
count(case when stagename = 'Canceled' then 1 end)
from db_table
注意:如果您的列包含前导和尾随空格,请使用trim函数和列名称,然后进行比较。