Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 需要帮助将数据分隔为列吗_Sql_Count_Pivot_Aggregate Functions - Fatal编程技术网

Sql 需要帮助将数据分隔为列吗

Sql 需要帮助将数据分隔为列吗,sql,count,pivot,aggregate-functions,Sql,Count,Pivot,Aggregate Functions,我目前正在使用SQL查询数据库以显示某些信息。我把每一个被拉入的项目都作为单独的实体。每当我运行查询时,我的结果只显示在一列下。有没有办法根据别名将此数据分隔为单独的列 SELECT count(o.orderid) AS Current_Daily FROM orders o WHERE o.ship_dt BETWEEN '2020-11-09 00:00:00' AND '2020-11-15 23:59:59' AND o.orderstatus = 2

我目前正在使用SQL查询数据库以显示某些信息。我把每一个被拉入的项目都作为单独的实体。每当我运行查询时,我的结果只显示在一列下。有没有办法根据别名将此数据分隔为单独的列

SELECT
    count(o.orderid) AS Current_Daily
FROM
    orders o
WHERE
    o.ship_dt BETWEEN '2020-11-09 00:00:00' AND '2020-11-15 23:59:59'
    AND o.orderstatus = 2
UNION
#UNION ALL

SELECT
    count(o.orderid)  AS Previous_Daily
FROM
    orders o
WHERE
    o.ship_dt BETWEEN '2019-11-09 00:00:00' AND '2019-11-15 23:59:59'
    AND o.orderstatus = 2
UNION
#UNION ALL

SELECT
    count(o.orderid) AS Current_Monthly
FROM
    orders o
WHERE
    o.ship_dt BETWEEN '2020-11-01 00:00:00' AND '2020-11-15 23:59:59'
    AND o.orderstatus = 2
UNION
#UNION ALL

SELECT
    count(o.orderid) AS Previous_Monthly
FROM
    orders o
WHERE
    o.ship_dt BETWEEN '2019-11-01 00:00:00' AND '2019-11-15 23:59:59'
    AND o.orderstatus = 2

;

非常感谢您的帮助。

使用条件聚合,如:

select
    sum(case when ship_dt >= '2020-11-09' and ship_dt < '2020-11-15' then 1 else 0 end) as current_daily,
    sum(case when ship_dt >= '2020-11-01' and ship_dt < '2020-11-15' then 1 else 0 end) as current_monthly,
    ... -- more conditional "sum()"s if needed
from orders o
where orderstatus = 2

使用条件聚合,如中所示:

select
    sum(case when ship_dt >= '2020-11-09' and ship_dt < '2020-11-15' then 1 else 0 end) as current_daily,
    sum(case when ship_dt >= '2020-11-01' and ship_dt < '2020-11-15' then 1 else 0 end) as current_monthly,
    ... -- more conditional "sum()"s if needed
from orders o
where orderstatus = 2

您可能只需要条件聚合。大概是这样的:

SELECT SUM(CASE WHEN o.ship_dt >= '2020-11-09' AND o.ship_dt < '2020-11-16' THEN 1 ELSE 0 END) AS Current_Daily,
       SUM(CASE WHEN o.ship_dt >= '2020-11-01' AND o.ship_dt < '2020-11-16' THEN 1 ELSE 0 END) AS Current_Monthly,
FROM orders o
WHERE o.orderstatus = 2;

您可以使用类似的逻辑添加其他列。

您可能只需要条件聚合。大概是这样的:

SELECT SUM(CASE WHEN o.ship_dt >= '2020-11-09' AND o.ship_dt < '2020-11-16' THEN 1 ELSE 0 END) AS Current_Daily,
       SUM(CASE WHEN o.ship_dt >= '2020-11-01' AND o.ship_dt < '2020-11-16' THEN 1 ELSE 0 END) AS Current_Monthly,
FROM orders o
WHERE o.orderstatus = 2;

您可以使用类似的逻辑添加其他列。

用您正在使用的数据库标记您的问题。样本数据和预期结果将有所帮助。不清楚为什么重复两次相同的查询(列别名除外)。请使用正在使用的数据库标记您的问题。样本数据和预期结果将有所帮助。不清楚为什么重复两次相同的查询,除了列别名。这似乎不起作用,我需要将这些数据分开,我不确定我是否澄清了这一点。每个输出必须独立存在。我只是想创建一个更清晰的读数,准确地说明所显示的内容。orderid 462 573 841 1321这是我从这个查询中得到的数据输出。我只是想把这些结果分成单独标记的列。这似乎不起作用,我需要将这些数据分开,我不确定我是否澄清了这一点。每个输出必须独立存在。我只是想创建一个更清晰的读数,准确地说明所显示的内容。orderid 462 573 841 1321这是我从这个查询中得到的数据输出。我只是想把这些结果分成单独标记的列。