我坐在一点点泡菜。我有一个包含多个表的数据库,可以使用正在创建的产品实时更新,每个产品都会转到数据库中的特定表。现在我必须每小时查询以监控生产,但必须每小时手动运行查询(如下所示)是否可以进行此查询,但它显示每小时和该小时的每个总计?基本上从05:00开始到16:00+结束,每个都显示在一条单独的线上?
SET
@start = '2014-06-23 05:00',
@end = '2014-06-23 06:00';
SELECT
(SELECT COUNT(id) FROM HandsetKit_T WHERE createdDate BETWEEN @start AND @end) AS 'Handset Kit',
(SELECT CONCAT (
(SELECT COUNT(p.id) FROM Pallet_T p, PalletNotify_T pn WHERE p.id = pn.palletID AND p.createdDate BETWEEN @start AND @end AND pn.esbStatus = '3' AND p.mtnAssetID NOT LIKE '11' AND p.mtnAssetID NOT LIKE '12') , ' / ' ,
(SELECT COUNT(p.id) FROM Pallet_T p, PalletNotify_T pn WHERE p.id = pn.palletID AND p.createdDate BETWEEN @start AND @end AND pn.esbStatus = '4' AND p.mtnAssetID NOT LIKE '11' AND p.mtnAssetID NOT LIKE '12'))) AS 'HK EOL',
(SELECT COUNT(id) FROM Kit_T WHERE createdDate BETWEEN @start AND @end AND mtnAssetID = '3') AS 'Wallet One',
(SELECT COUNT(id) FROM Kit_T WHERE createdDate BETWEEN @start AND @end AND mtnAssetID = '10') AS 'Wallet Two',
(SELECT COUNT(b.id) FROM Brick_T b, BrickNotify_T bn WHERE b.id = bn.brickID AND bn.esbStatus = '3' AND b.createdDate BETWEEN @start AND @end) AS 'Wholesale',
(SELECT CONCAT (
(SELECT COUNT(p.id) FROM Pallet_T p, PalletNotify_T pn WHERE p.id = pn.palletID AND p.createdDate BETWEEN @start AND @end AND pn.esbStatus = '3' AND p.mtnAssetID = '12') , ' / ' ,
(SELECT COUNT(p.id) FROM Pallet_T p, PalletNotify_T pn WHERE p.id = pn.palletID AND p.createdDate BETWEEN @start AND @end AND pn.esbStatus = '4' AND p.mtnAssetID = '12'))) AS 'Wholesale EOL',
(SELECT COUNT(bb.id) FROM BlisterBrick_T bb, BlisterBrickNotify_T bbn WHERE bb.id = bbn.blisterBrickID AND bb.createdDate BETWEEN @start AND @end AND bbn.esbStatus = '3') AS 'Blister',
(SELECT CONCAT (
(SELECT COUNT(p.id) FROM Pallet_T p, PalletNotify_T pn WHERE p.id = pn.palletID AND p.createdDate BETWEEN @start AND @end AND pn.esbStatus = '3' AND p.mtnAssetID = '11') , ' / ' ,
(SELECT COUNT(p.id) FROM Pallet_T p, PalletNotify_T pn WHERE p.id = pn.palletID AND p.createdDate BETWEEN @start AND @end AND pn.esbStatus = '4' AND p.mtnAssetID = '11'))) AS 'Blister EOL'
答案 0 :(得分:1)
如果现有模式运行良好,那么我将添加另一个表,该表合并所有其他表上由触发器填充的所有产品(products
)。也许它唯一的列是product_type
和createdDate
:
然后查询很简单:
SELECT COUNT(*)
FROM products
WHERE createdDate BETWEEN @start AND @end
GROUP BY product_type
如果当前架构已经很繁琐,那么是时候重构数据库了。通常,具有相同结构的并行表是糟糕的数据库设计。 (并行表是一件好事的例外情况包括数据存档,数据复制和类似用途。)