我在显示数据时遇到问题,这些数据在表中重复出现次数最多。 例如,我有一个名为JOB,TASK和TASK_TYPE的表。
JOB包含job_id和job_price,并列出作业。每个作业都包含几个任务
TASK包含job_id和task_id,例如
****job_id** **task_id**
J01 T01
J01 T03
J02 T01
J02 T02
J02 T03
J03 T01
J04 T01
TASK_TYPE包含task_id和task_description。它列出了可供客户使用的不同任务。例如。
**task_id** **task_description**
T01 shopping
T02 dog walking
T03 car wash
我想显示最常用的任务类型,以及它用于的作业,在本例中为T01。到目前为止,我已经获得了此代码,但我不知道如何仅显示最重复的任务。
SELECT j.job_id, t.task_id, tt.type
FROM job j
JOIN task t ON j.job_id = t.job_id
JOIN task_type tt ON t.task_id = tt.task_id GROUP BY tt.type, t.task_id, j.job_id ORDER BY type;
答案 0 :(得分:1)
使用TOP 1
逻辑作为@LONG提到:
SELECT A.TASK_ID, A.JOB_ID, B.TASK_DESCRIPTION
FROM
(SELECT TASK_ID, JOB_ID
FROM TASK
WHERE TASK_ID IN (SELECT TOP 1 TASK_ID FROM TASK GROUP BY TASK_ID ORDER BY COUNT(*) DESC)) A
INNER JOIN
TASK_TYPE B
ON A.TASK_ID = B.TASK_ID;
如果有多个TASK_ID
s具有相同的最高出现频率,请尝试以下操作:
SELECT B.JOB_ID, Z.TASK_ID, C.TASK_DESCRIPTION
(SELECT A.TASK_ID, DENSE_RANK() OVER(ORDER BY A.FREQ DESC) AS MOST_FREQ
FROM
(SELECT TASK_ID, COUNT(*) AS FREQ FROM TASK GROUP BY TASK_ID) A
) Z
INNER JOIN
TASK B
ON Z.TASK_ID = B.TASK_ID AND Z.MOST_FREQ = 1
INNER JOIN
TASK_TYPE C
ON B.TASK_ID = C.TASK_ID;
这会在不同的行中为您提供结果。根据您在帖子中尝试过的查询,这似乎是您所需要的:)
答案 1 :(得分:0)
尝试以下查询,您可以忽略前面的CTE,仅用于样本数据。
WITH CTE
AS
(
select 'J01' as job_id, 'T01' as task_id
union all select 'J01', 'T03'
union all select 'J02' , 'T01'
union all select 'J02' , 'T02'
union all select 'J02' , 'T03'
union all select 'J03' , 'T01'
union all select 'J04' , 'T01'
),
TASK_TYPE
AS
(
SELECT 'T01' as task_id, 'shopping' as task_description
UNION ALL SELECT 'T02' , 'dog walking'
UNION ALL SELECT 'T03' , 'car wash'
)
SELECT TOP 1 B.TASK_ID,C.task_description,STUFF((SELECT ',' + A.job_id FROM CTE as A where A.task_id = B.task_id FOR XML PATH('')),1,1,'') as Related_Job
FROM CTE as B
INNER JOIN TASK_TYPE as C
ON C.task_id = B.task_id
GROUP BY B.TASK_ID,C.task_description
ORDER by COUNT(*) DESC
输出:
TASK_ID task_description Related_Job
T01 shopping J01,J02,J03,J04
答案 2 :(得分:0)
我自己对SQL很陌生,因此我很抱歉我的查询混乱,但它在MySQL中适用于我。
SELECT j.job_id, t.task_id, tt.task_description
FROM job j
JOIN task t ON j.job_id = t.job_id
JOIN task_type tt ON t.task_id = tt.task_id
where t.task_id = (
select tx.task_id
from (SELECT t.task_id, count(t.task_id) as accurance FROM job j JOIN task t ON j.job_id = t.job_id JOIN task_type tt ON t.task_id = tt.task_id group by t.task_id limit 1) as tx)
GROUP BY tt.task_description, t.task_id, j.job_id order by task_description;
我希望这对你来说很容易理解,因为我们都是数据库新手。 如果您有任何问题,请随时提出。
这是我的输出:
+--------+---------+------------------+
| job_id | task_id | task_description |
+--------+---------+------------------+
| J01 | T01 | shopping |
| J02 | T01 | shopping |
| J03 | T01 | shopping |
| J04 | T01 | shopping |
+--------+---------+------------------+
答案 3 :(得分:0)
此解决方案使用WITH语法(使用Oracle 11g R2测试)来组织子查询以使查询易于阅读。它还将处理task_id计数绑定的情况(在提供的示例中,T03和T01绑定)。希望这对你有用!
-- set up example tables
WITH TASK AS (
SELECT 'J01' AS job_id, 'T01' AS task_id FROM dual
UNION ALL
SELECT 'J01' AS job_id, 'T03' AS task_id FROM dual
UNION ALL
SELECT 'J02' AS job_id, 'T03' AS task_id FROM dual
UNION ALL
SELECT 'J02' AS job_id, 'T02' AS task_id FROM dual
UNION ALL
SELECT 'J02' AS job_id, 'T03' AS task_id FROM dual
UNION ALL
SELECT 'J03' AS job_id, 'T01' AS task_id FROM dual
UNION ALL
SELECT 'J04' AS job_id, 'T01' AS task_id FROM dual),
TASK_TYPE AS (
SELECT 'T01' AS task_id, 'shopping' AS task_description FROM dual
UNION ALL
SELECT 'T02' AS task_id, 'dog walking' AS task_description FROM dual
UNION ALL
SELECT 'T03' AS task_id, 'car wash' AS task_description FROM dual),
-- rank order by task_id count
TASK_RANK AS (
SELECT task_id, COUNT(task_id) AS task_counts
FROM TASK
GROUP BY task_id),
-- get name of most frequent tasks (will handle tie)
TOP_TASK AS (
SELECT task_id
FROM TASK_RANK
WHERE task_counts IN (SELECT MAX(task_counts) FROM TASK_RANK))
-- simple query to get tasks
SELECT T.task_id, T.job_id, TT.task_description
FROM TASK T
INNER JOIN TASK_TYPE TT
ON T.task_id = TT.task_id
WHERE T.task_id IN (SELECT task_id FROM TOP_TASK)
ORDER BY task_id
| TASK_ID | JOB_ID | TASK_DESCRIPTION |
|---------|--------|------------------|
| T01 | J04 | shopping |
| T01 | J03 | shopping |
| T01 | J01 | shopping |
| T03 | J02 | car wash |
| T03 | J02 | car wash |
| T03 | J01 | car wash |