在PostgreSQL中创建摘要表

时间:2019-01-13 09:34:15

标签: postgresql summary

系统要求我使用PostgreSQL从表中创建聚合数据。

我不知道是否可以直接从PostgreSQL完成此操作,因为我总是使用SQL Server编写查询。为此,我通常将查询结果复制到excel中,然后对其进行透视。

SELECT 
  date(order_date)
, count(customer_id) as total_customer
, product_category
, sum(quantity) as total_qty
, sum(total_price) as total_price
FROM public."CURRENT_WP_SALES"
WHERE order_status = 'sale'
GROUP BY date(order_date), product_category
ORDER BY date(order_date), product_category asc

我得到的结果是这样的:

+============+================+================+===========+=============+
|    date    | total_customer |    product     | total_qty | total_price |
+============+================+================+===========+=============+
| 2018-12-20 |              2 | frozen food    |         2 |         500 |
+------------+----------------+----------------+-----------+-------------+
| 2018-12-20 |              4 | instant noodle |         5 |         300 |
+------------+----------------+----------------+-----------+-------------+
| 2018-12-20 |              4 | meds           |         1 |          50 |
+------------+----------------+----------------+-----------+-------------+
| 2018-12-20 |              6 | candy          |        10 |         200 |
+------------+----------------+----------------+-----------+-------------+

预期结果如下:

+============+================+================+===========+=============+
|    date    | total_customer |    product     | total_qty | total_price |
+============+================+================+===========+=============+
|            |                | frozen food    |         2 |             |
+            +                +----------------+-----------+             +
|            |                | instant noodle |         5 |             |
+ 2018-12-20 +       16       +----------------+-----------+     1050    +
|            |                | meds           |         1 |             |
+            +                +----------------+-----------+             +
|            |                | candy          |        10 |             |
+------------+----------------+----------------+-----------+-------------+

如果有任何直接从PostgreSQL执行此操作的方法,请告诉我。

1 个答案:

答案 0 :(得分:0)

没有DBMS会完全允许您的要求,但是有一些紧密的解决方案:

    if ((isset($_GET['submit']) && $_GET['submit'] == 'error') || (isset($_POST['submit']) && $_POST['submit'] == 'error')) {

    $success_message = mysqli_real_escape_string($conn, $_POST['msg']);

    switch ($success_message) {
        case '2':
            echo "<h2>Page under Maintenance</h2>";
            echo "<p>This page is currently undergoing some critical maintenance. You can try again in a couple hours.</p> For any help or enquiries send us a message on ".$support_email."";
            break;
        case '3':
            echo "<h2>404 Page Not Found</h2>";
            echo "<p>It seems the page you're looking for doesn't exist...</p>";
            mail($errors_email, 'Alert: 404 Not Found', 'A user (ref '.$user_ref.' with IP address '.$ip_address.') landed on this page ('.$current_url.') from '.$previous_url.' with a 404 error. Care to see what went wrong?');
            break;  
        default:
            mail($errors_email, 'User Landed on an Unknown Error Page', 'A user ('.$user_ref.') landed on the success_template.php page and something went wrong. This is just an alert to test all of the forms pointing to this page to see if they are valid.');
            break;
        }
    }