我无法将两个表中的某些信息分组以比较信息。
我有两张桌子
- (void)fooMethod {
// ERROR - "Use of undeclared identifier 'foo', did you mean '_foo'?"
foo = @1;
// So instead you must specifically choose to use the property or the ivar:
// Property
self.foo = @1;
// Ivar
_foo = @1;
}
我希望看到以下内容:
Baseline
|ResourceGroup Unit TotalUnits|
|Mechanic 50 600|
|Electric 50 600|
|Mechanic 52 600|
|Electric 52 600|
Actual
|ResourceGroup Unit TotalUnits|
|Mechanic 50 500|
|Electric 50 500|
|Mechanic 52 500|
|Electric 52 500|
我正在尝试使用以下查询,但它似乎不起作用。我想我错过了什么。谁能看到我哪里出错?
|ResourceGroup Baseline.TotalUnits Actual.TotalUnits|
|Mechanic 1200 1000|
|Electric 1200 1000|
答案 0 :(得分:2)
这两个表的交叉连接将为每个ResourceGroup提供2x2 = 4行,这意味着您将拥有4行Mechanic和4行Electric数据。在您加入它们之后进行分组会给您带来不匹配的价值。
解决方案是在连接结果表后单独对这些表进行分组。这将为您提供以下两个需要通过ResourceGroup列加入的表:
Baseline
|ResourceGroup TotalUnits|
|Mechanic 1200|
|Electric 1200|
Actual
|ResourceGroup TotalUnits|
|Mechanic 1000|
|Electric 1000|
计算出以下sql语句:
SELECT
A.ResourceGroup,
A.TotalUnits as BaselineTotalUnits,
B.TotalUnits as ActualTotalUnits
FROM (SELECT ResourceGroup, SUM(TotalUnits) as TotalUnits FROM Baseline GROUP BY ResourceGroup) A
JOIN (SELECT ResourceGroup, SUM(TotalUnits) as TotalUnits FROM Actual GROUP BY ResourceGroup) B
ON A.ResourceGroup = B.ResourceGroup