MySQL合并多个查询报告

时间:2018-12-19 17:03:12

标签: mysql sql database join aggregate-functions

在此处查看SQLFiddle:http://sqlfiddle.com/#!9/9bb273

我需要从3个查询中创建一个报告。它必须是没有子查询的单个查询(由于ORM的限制)。

主要查询是:

Traceback (most recent call last):
File "/mnt/hdd/segmentation/segmentation/main.py", line 73, in <module>
model = ResNet50().setup()
File "/mnt/hdd/segmentation/segmentation/model2.py", line 57, in setup
self.model.compile(optimizer=SGD(momentum=0.9, decay=5e-4), loss=myloss)
File "/usr/local/lib/python3.5/dist-packages/keras/engine/training.py", line 342, in compile
sample_weight, mask)
File "/usr/local/lib/python3.5/dist-packages/keras/engine/training_utils.py", line 404, in weighted
score_array = fn(y_true, y_pred)
File "/mnt/hdd/segmentation/segmentation/loses.py", line 11, in myloss
mask_res = tf.reshape(mask_res, [mask_res.shape[0] * mask_res.shape[1]])
File "/home/oradomskyi/.local/lib/python3.5/site-packages/tensorflow/python/ops/gen_array_ops.py", line 3903, in reshape
"Reshape", tensor=tensor, shape=shape, name=name)
File "/home/oradomskyi/.local/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 513, in _apply_op_helper
raise err
File "/home/oradomskyi/.local/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 510, in _apply_op_helper
preferred_dtype=default_dtype)
File "/home/oradomskyi/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1036, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/home/oradomskyi/.local/lib/python3.5/site-packages/tensorflow/python/framework/constant_op.py", line 235, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "/home/oradomskyi/.local/lib/python3.5/site-packages/tensorflow/python/framework/constant_op.py", line 214, in constant
value, dtype=dtype, shape=shape, verify_shape=verify_shape))
File "/home/oradomskyi/.local/lib/python3.5/site-packages/tensorflow/python/framework/tensor_util.py", line 522, in make_tensor_proto
"supported type." % (type(values), values))

如您所见,此报告的日期范围为10/1至12/18。构成报告的结果集是...

SELECT SQL_CALC_FOUND_ROWS 
Organization.name as organization_name, 
Program.unique_id as program_uuid, 
Program.name as program_name,
Program.start_date,
Program.end_date,
Program.grace_period,
'Placeholder A' as 'Participant Count',
'Placeholder B' as 'Total Participant Points',
count(distinct Transaction.id) as 'Transaction Count',
sum(TransactionItem.quantity) as 'Total Redemptions',
sum(((TransactionProduct.retail + IFNULL(TransactionProduct.shipping,0) + IFNULL(TransactionProduct.handling,0)) * TransactionItem.quantity)) as 'Total'
FROM `TransactionItem` 
JOIN `Transaction` ON `Transaction`.id = `TransactionItem`.transaction_id 
JOIN `TransactionProduct` ON `TransactionItem`.reference_id = `TransactionProduct`.reference_id 
JOIN `Participant` ON `Transaction`.participant_id = `Participant`.id 
JOIN `Program` ON `Program`.id = `Participant`.program_id 
JOIN `Organization` ON `Organization`.id = `Participant`.organization_id 
WHERE 1=1  
AND `Organization`.`unique_id` = 'demo2' 
AND `Program`.`unique_id` = 'demo2' 
AND `Transaction`.`created_at` >= '2018-10-01 00:00:00' 
AND `Transaction`.`created_at` <= '2018-12-18 00:00:00';

如您所见,有2个数据点无法从此查询中获取。

(1)属于“ demo2”计划的参与者总数。该查询获取该数据点。

organization_name | program_uuid | program_name | start_date | end_date | grace_period | Participant Count | Total Participant Points | Transaction Count | Total Redemptions | Total
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Demo2 Org           demo2          Demo2          2018-10-01   2018-12-27  5             Placeholder A       Placeholder B              11                   92                 2853.13

返回:

/* Placeholder A */
select program_id, count(*) as 'Participant Count' from participant 
where active = 1
group by program_id;

(2)日期10/1和12/18之间的所有行的Adjustments.amount的总和。该查询满足了这一要求。

program_id   |  Participant Count
----------------------------------
2               102

返回:

/* Placeholder B */
select sum(amount) as 'Total Particpant Points' from adjustment 
where participant_id in (select id from participant where program_id =2)
and type = 1
and created_at >= '2018-10-01 00:00:00' and created_at <= '2018-12-18 00:00:00';

是否有一种方法可以在单个查询中收集所有这些数据而没有子查询?

0 个答案:

没有答案
相关问题