在postgresql数据库中将列转换为行

时间:2018-05-24 09:53:34

标签: sql postgresql pivot crosstab postgresql-9.4

我目前的查询是:

select rulename,status, count(*)
from triggered_rule_info
group by status, rulename

结果是:

rulename    status  count
eorule1      ack    1
eorule1      open   1
eorule1      close  7
eorule2      open   1
eorule2      ack    1

但我希望结果是:

rulename     ack    open    close
eorule1       1      1       7
eorule2       1      1  

我怎样才能做到这一点?我的postgresql版本是9.4。

2 个答案:

答案 0 :(得分:1)

为此您可以使用filter子句:

select rulename
       count(*) filter (where status = 'ack') as ack,
       count(*) filter (where status = 'open') as open,
       count(*) filter (where status = 'close') as closed
from triggered_rule_info
group by rulename
order by rulename;

答案 1 :(得分:0)

您可以使用 var chartData = { "barCircleMobile":[ {"index":0.3, "value":100, "fill":"#37b54e", "label":"WebMd Health"}, {"index":0.4, "value":50, "fill":"#69dad9", "label":"Livestrong.com"}, {"index":0.5, "value":31.25, "fill":"#00a8dd", "label":"Everyday Health"}, {"index":0.6, "value":18.75, "fill":"#157996", "label":"About.com"}, {"index":0.7, "value":6.5, "fill":"#0068ff", "label":"Drugs.com"}, ], "barCircleWeb":[ {"index":0.3, "value":100, "fill":"#20d974", "label":"WebMD Health"}, {"index":0.4, "value":40, "fill":"#3bc1c4", "label":"Everyday Health"}, {"index":0.5, "value":30, "fill":"#23889d", "label":"Livestrong.com"}, {"index":0.6, "value":20, "fill":"#0071a3", "label":"About.com Health Section"}, {"index":0.7, "value":10, "fill":"#0042a3", "label":"Healthline"}, ] }; function drawBarCircleChart(data,target,values,labels){ var w = 362, h = 362, size = data[0].value * 1, radius = 200, sectorWidth = .1, radScale = 25, sectorScale = 1.75, target = d3.select(target), valueText = d3.select(values), labelText = d3.select(labels); var arc = d3.svg.arc() .innerRadius(function(d,i){return (d.index/sectorScale) * radius + radScale; }) .outerRadius(function(d,i){return ((d.index/sectorScale) + (sectorWidth/sectorScale)) * radius + radScale; }) .startAngle(Math.PI) .endAngle(function(d) { return Math.PI + (d.value / size) * 2 * Math.PI; }); var path = target.selectAll("path") .data(data); //TODO: seperate color and index from data object, make it a pain to update object order path.enter().append("svg:path") .attr("fill",function(d,i){return d.fill}) .transition() .ease("elastic") .duration(1000) .delay(function(d,i){return i*100}) .attrTween("d", arcTween); valueText.selectAll("tspan").data(data).enter() .append("tspan") .attr({ x:50, y:function(d,i){return i*14}, "text-anchor":"end" }) .text(function(d,i){return data[i].value}); labelText.selectAll("tspan").data(data).enter() .append("tspan") .attr({ x:0, y:function(d,i){return i*14} }) .text(function(d,i){return data[i].label}); function arcTween(b) { var i = d3.interpolate({value: 0}, b); return function(t) { return arc(i(t)); }; } } // Animation Queue setTimeout(function(){drawBarCircleChart(chartData.barCircleWeb,"#circleBar- web-chart","#circleBar-web-values","#circleBar-web-labels")},500); setTimeout(function() {drawBarCircleChart(chartData.barCircleMobile,"#circleBar-mobile- chart","#circleBar-mobile-values","#circleBar-mobile-labels")},800); d3.select("#circleBar-web-icon") .transition() .delay(500) .duration(500) .attr("opacity","1"); d3.select("#circleBar-web-text") .transition() .delay(750) .duration(500) .attr("opacity","1"); d3.select("#circleBar-web-clipLabels") .transition() .delay(600) .duration(1250) .attr("height","150"); d3.select("#circleBar-mobile-icon") .transition() .delay(800) .duration(500) .attr("opacity","1"); d3.select("#circleBar-mobile-text") .transition() .delay(1050) .duration(500) .attr("opacity","1"); d3.select("#circleBar-mobile-clipLabels") .transition() .delay(900) .duration(1250) .attr("height","150"); CASE - WHEN子句来获得所需的结果。如果GROUP BY包含更多其他值,则可能需要添加更多CASE语句。

<强> 实施例

status