计算PostgreSQL 9.5中两列的总和

时间:2016-06-15 16:04:44

标签: sql postgresql

我有一个帖子表,每个帖子都有一个洞察力jsonb列,其中包含类似下面的数据示例。

数据样本(来自Facebook,因此无法更改格式)

[
  {
    "name": "post_story_adds_unique",
    "values": [
      { "value": 93 }
    ],
  },
  {
    "name": "post_story_adds",
    "values": [
      {  "value": 100 }
    ]
  },
  {
    "name": "post_impressions_organic_unique",
    "values": [
      {  "value": 123 }
    ]
  },
  ...
]

我希望得到达到病毒的计算总和,然后按总数排序。

期望的结果

id  message                       post_created          reach   viral   total
69  This World Family dablah...   2016-05-11 18:44:16   6683    646     7329
...

到目前为止,我设法得到了结果,但我无法弄清楚如何获得两列的总和。我不知道在哪里添加另一个联接或选择将两列相加。

到目前为止的结果

id  message                       post_created          reach   viral
69  This World Family dablah...   2016-05-11 18:44:16   6683    646
58  blah blah flip flop blah...   2016-05-22 11:00:01   4880    403
55  This is another message ...   2016-05-24 10:00:00   4417    109

我尝试了各种方法,例如在第一个SUM (reach + viral) as total中加入SELECT,但大多数情况下我都会收到错误,说列不存在。

到目前为止我的SQL:

SELECT
    id,
    message,
    post_created,
    obj.value->'values'->0->'value' AS reach,
    obj2.value->'values'->0->'value' AS viral
  FROM (
    SELECT
      id,
      message,
      post_created,
      insights
    FROM posts
    WHERE (
      page_id = 4 AND
      post_created >= '2016-05-01 00:00:00' AND
      post_created <= '2016-05-31 23:59:59' AND
      insights @> '[{"name":"post_impressions_organic_unique"}, {"name":"post_impressions_viral_unique"}]'
    )
  ) e1
  JOIN LATERAL jsonb_array_elements(insights) obj(value) ON obj.value->>'name' = 'post_impressions_organic_unique'
  JOIN LATERAL jsonb_array_elements(insights) obj2(value) ON obj2.value->>'name' = 'post_impressions_viral_unique'
  ORDER BY reach DESC;

2 个答案:

答案 0 :(得分:0)

要做到这一点,你需要更新你的表:

UPDATE yourTable SET resultsRow = firtRow + secondRow;

答案 1 :(得分:0)

不确定jsonb是否会产生任何影响,但是,添加两列的值就像使用// "form-dirty" directive angular.module('webapp.shared').directive('formDirty', ['$state', 'DlgPromptService', function($state, DlgPromptService) { return { scope: {theForm: '=name'}, restrict: 'A', link: function (scope, element, attrs) { scope.$watch('theForm', function () { // if there's a UI router change scope.$on('$stateChangeStart', function (event, toState, toParams){ // WHEN FORM1 DIRTY: PROMPT AND CONSOLE-LOG BOTH FORMS // WHEN FORM2 DIRTY: PROMPT AND CONSOLE-LOG BOTH FORMS console.log(scope.theForm); // if form was not modified if ( !scope.theForm.$dirty ) { return; } else { // if form was modified then intervene // halt the route change event.preventDefault(); // prompt user DlgPromptService.confirm('Unsaved Changes', 'This form has unsaved changes.<br/>' +'Are you sure you want to leave?') .then(function () { // we will proceed with the route change scope.theForm.$setPristine(); $state.go(toState.name, toParams); }) } }) // if there's a window change (reload, etc) window.onbeforeunload = function (event) { // WHEN FORM1 DIRTY: NO PROMPT, NO CONSOLE LOG // WHEN FORM2 DIRTY: PROMPT AND CONSOLE-LOG FORM2 console.log(scope.theForm); // if form was not modified if ( !scope.theForm.$dirty ) { return; } else { // prompt return "prompt"; } } scope.$on('$destroy', function() { window.onbeforeunload = undefined; }) }) } } }]) // "thisContainsForm2" directive angular.module('webapp.shared').directive('thisContainsForm2', function() { var controller = function($scope, $http, $state, DlgPromptService) { // CONTROLLER STUFF } return { scope : {data: '='}, templateUrl : 'shared/thisContainsForm2.html', controller : controller } }) 一样简单

+