仅为特定列mysql

时间:2015-12-17 11:00:49

标签: mysql

我们在mysql数据库中有两个表。下面附有截图。

Given table ads_testTable

Given table ads_testTable

here is the screenshot of my dimesnionvalue_flattable

here is the screenshot of my dimesnionvalue_flattable

我们必须运行如下所示的查询。

SELECT Quiz_Attempt.L1_Key dimID, 
       Quiz_Attempt.L1_Label CatVars,
       COALESCE(**xyz**,0) AS series0 
FROM DSQ_ADSSCHEMA.ADS_TestTable dataTable   
RIGHT OUTER JOIN LS_CONFIG.DSQ_DIMENSIONVALUES_FLAT Quiz_Attempt on dataTable.Quiz_Attempt = Quiz_Attempt.L1_Key  
WHERE Quiz_Attempt.L0_Key = 'All Levels' AND
      Quiz_Attempt.DimensionID = 'Packet'  
GROUP BY Quiz_Attempt.L1_Key, Quiz_Attempt.L1_Label;

我的动机是编写一个查询来代替 xyz ,这样我就可以根据 dimID 的值得到testtable中的avM of obtainMarks列。每个不同的Quiz_Attempt是一个不同的测试,所以如果一个数据包在testTable中重复一个特定的Quiz_Attempt,那么该AttemptID只应该只有一个值。

1 个答案:

答案 0 :(得分:0)

我认为您的查询可以采取以下形式:

SELECT 
  L1_Key dimID, 
  L1_Label CatVars,
  COALESCE('**xyz**',0) AS series0 
FROM (
  SELECT *  
  FROM (SELECT * FROM ADS_TestTable GROUP BY ADS_TestTable.Quiz_Attempt) dataTable
  RIGHT OUTER JOIN DSQ_DIMENSIONVALUES_FLAT Quiz_Attempt on dataTable.Quiz_Attempt = Quiz_Attempt.L1_Key  
  WHERE Quiz_Attempt.L0_Key = 'All Levels' AND
        Quiz_Attempt.DimensionID = 'Packet' 
  GROUP BY dataTable.Quiz_Attempt
) A GROUP BY dimID, CatVars;

JOIN在内部查询中完成,并按Quiz_Attempt分组,以便每次尝试获得一行。然后将此结果用于计算您的需求。