0

我有 8 个查询来执行计算并将结果保存在其中一列中。

每个查询的列标题遵循相同的结构;

CompanyYearAssetID, CompanyName, AssetName, Year, DatapointID, NameOfWhateverWasCalculated

在每次计算中,都会为结果分配相同的 DatapointID。例如;

CompanyYearAssetID, CompanyName, AssetName, Year, DatapointID, ReservoirOIL
10001             , CompanyA   , Asset1   , 2011, 105014     , 5
10002             , CompanyB   , Asset1   , 2011, 105014     , 3
10003             , CompanyC   , Asset1   , 2011, 105014     , 0

计算列的名称及其 DatapointID 是;

  • 未分配油 105014
  • WellworkOIL 105015
  • 油箱油 105016
  • 年度停机OIL 105017
  • 故障计划外工厂OIL 105018
  • 工厂计划维护OIL 105019
  • 出口油 105020
  • 市场油 105039

我想创建一个查询以理想地使用列标题将所有结果放入一个表中

CompanyYearAssetID, DatapointID, DatapointValue

有谁知道我将如何构建这个?

以防万一您需要它们,这里有 8 个查询名称;

  1. PE_Field_UnallocatedOIL
  2. PE_Field_WellworkOIL
  3. PE_Field_ReservoirOIL
  4. PE_Field_AnnualShutdownOIL
  5. PE_Field_FailureUnplannedPlantOIL
  6. PE_Field_PlantPlannedMaintenanceOIL
  7. PE_Field_ExportOIL
  8. PE_Field_MarketOIL
4

1 回答 1

0

好吧,如果查询 1 和查询 2 都返回相同的数据像这样

CompanyYearAssetID, CompanyName, AssetName, Year, DatapointID, ReservoirOIL
10001             , CompanyA   , Asset1   , 2011, 105014     , 5
10002             , CompanyB   , Asset1   , 2011, 105014     , 3
10003             , CompanyC   , Asset1   , 2011, 105014     , 0


SELECT * FROM (
   Select * from Query1
   UNION ALL
   Select * from Query2)

所以这个查询的结果就是

CompanyYearAssetID, CompanyName, AssetName, Year, DatapointID, ReservoirOIL
10001             , CompanyA   , Asset1   , 2011, 105014     , 5
10002             , CompanyB   , Asset1   , 2011, 105014     , 3
10003             , CompanyC   , Asset1   , 2011, 105014     , 0
10001             , CompanyA   , Asset1   , 2011, 105014     , 5
10002             , CompanyB   , Asset1   , 2011, 105014     , 3
10003             , CompanyC   , Asset1   , 2011, 105014     , 0

您像普通表一样查询

 SELECT * FROM (
       Select * from Query1
       UNION ALL
       Select * from Query2)
where Year = 2011 AND ReservoirOIL > 0
于 2012-08-30T14:10:27.030 回答