我在MySQL中有一个表:
Col1 | Col2
a A
a B
c C
a B
我想创建一个这样的表:
col1 | col2 | freq
a A 0.33
a B 0.67
col1是Col1中的指定项目。 col2是与指定项目(即a)发生的不同项目。 freq列是col2中项目的出现频率。
有人能给我一些如何创建这样一个查询的提示吗?非常感谢。
答案 0 :(得分:6)
试试这个:
Select A.Col1, A.Col2, A.Count1 * 1.0 / B.Count2 As Freq
From (
Select Col1, Col2, Count(*) As Count1
From YourTableName
Group By Col1, Col2
) As A
Inner Join (
Select Col1, Count(*) As Count2
From YourTableName
Group By Col1
) As B
On A.Col1 = B.Col1
答案 1 :(得分:0)
您也可以使用在SQL Server中编码的
DECLARE @Count INT;
SELECT @Count = COUNT(1) FROM YourTableName WHERE Col1 = 'a'
SELECT Col1, Col2, CAST(COUNT(1)* 1.00 /@Count AS DECIMAL(4,2) ) AS Frequency
FROM YourTableName
WHERE Col1 = 'a'
GROUP BY Col1, Col2
这样你就有了更好的表现
答案 2 :(得分:0)
带窗口函数
SELECT Col1, Col2, Count1*1.0 / Count2 AS freq
FROM (
SELECT
Col1,
Col2,
COUNT() OVER(PARTITION BY Col1, Col2) AS Count1,
COUNT() OVER(PARTITION BY Col1) AS Count2
FROM YourTableName
)
GROUP BY Col1, Col2