我有一张桌子:device
----------------------------------------------------------------
| device__some_id | device__os_name | device__os_version |
---------------------------------------------------------------
| 0 android 3.0 |
| 0 android 3.0 |
| 0 android 4.0 |
| 1 ios 8.0 |
|________________________________________________________________|
我有一个查询,它为每个租户返回count(*)
,os_name,os_version组合。
输出:
--------------------------------------------------------------------------
| device__some_id | device__os_name | device__os_version | Count |
-------------------------------------------------------------------------
0 android 3.0 2
0 android 4.0 1
1 ios 8.0 1
使用的查询:
SELECT f,p,q,count (*)
FROM (SELECT device__some_id as f, device__os_name as p, device__os_version as q FROM device) as foo
GROUP BY f,p,q
问题:
我有另一张表dim_os,其中包含以下字段:
| dim_os__some_id | dim_os__os_type | dim_os__os_version |
------------------------------------------------------------
1 android 3.0
2 android 4.0
3 ios 8.0
我想要做的只是用device_os_name
表中的device_os_version
替换第一个查询输出中some_id
和dim_os
的每个出现。
我的尝试:
SELECT f, p, count (*)
FROM (SELECT dev.device__some_id as f, j2.dim_os__some_id as p FROM device AS dev
INNER JOIN dim_os as j1 ON dev.device__os_name = j1.dim_os__os_type
INNER JOIN dim_os as j2 ON dev.device__os_version = j2.dim_os__os_version) as foo
GROUP BY f,p ;
没有给我正确的计数。我哪里错了?
答案 0 :(得分:1)
嗯,不知道你为什么要做两个内连接,我会尝试一个(有两个条件)。
但我并不完全确定我理解你想要的输出!
无论如何:
SELECT f, p, count (*)
FROM
(SELECT
dev.device__some_id as f,
j1.dim_os__some_id as p
FROM device AS dev
INNER JOIN dim_os as j1
ON dev.device__os_name = j1.dim_os__os_type and
dev.device__os_version = j1.dim_os__os_version) as foo
GROUP BY f,p ;
请参阅SqlFiddle