我正在尝试按日期和市场(美国/英国)合并两个数据集-一个销售目标和另一个实际销售。
为此,我使用的是第三张表,该表使用GENERATE_DATE_ARRAY
创建要报告的日期的主列表-这样,我就不会在没有设置目标且没有目标的空白处结束销售报告。
我发现我要对销售进行两次计数,因此将数据和查询减少到可重复的状态:
#standardSQL
WITH dates AS (
SELECT day FROM UNNEST(GENERATE_DATE_ARRAY(DATE '2018-07-05', '2018-07-09', INTERVAL 1 DAY)) AS day
),
targets AS (
SELECT DATE '2018-07-06' AS day, 'UK' AS Market, NUMERIC '2.4' AS quantity
UNION ALL SELECT '2018-07-06', "US", 8.4
UNION ALL SELECT '2018-07-06', "US", 1.2
UNION ALL SELECT '2018-07-08', "UK", 3.0
UNION ALL SELECT '2018-07-08', "US", 10.9
),
sales AS (
SELECT DATE '2018-07-08' AS day, 'UK' AS Market, 4 AS quantity
UNION ALL SELECT '2018-07-06', 'US', 15
)
SELECT
dates.day AS day,
targets.market AS market,
SUM(targets.quantity) AS targetQuantity,
SUM(sales.quantity) AS quantity
FROM dates
LEFT JOIN targets
ON dates.day = CAST(targets.day AS DATE)
LEFT JOIN sales
ON dates.day = CAST(sales.day AS DATE) AND targets.market = sales.market
GROUP BY day, market
ORDER BY day, market
这将产生以下结果:
结果显示,尽管数据为15,但7月6日(第3行)的报告销售数量为30。
targets
数据中该日期和市场有两行时,这种情况正在发生,但是我不知道如何为此编写代码。
感谢您的帮助!
答案 0 :(得分:1)
下面应该可以工作。这个想法是预先汇总销售表和目标表,以避免重复
console.log(this.props.playlist.selectedTrack)