You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SQL连接TEILNEHMERKURS与KURS表并统计数据的语句求助

Hey Stefan, let's work through this SQL problem together! Since you’ve got the TEILNEHMERKURS (participant-course junction table, I assume) and KURS (course table) to work with, here’s a structured approach to build the right query for your statistical needs:

Step 1: Confirm Table Relationships First

First up, let’s make sure we’re joining the tables correctly. Typically, KURS will have a primary key like kurs_id, and TEILNEHMERKURS will have a matching foreign key (also likely kurs_id) to link participants to their courses. If your schema uses different column names, adjust the join condition accordingly.

Step 2: Common Dimension-Based Statistical Queries

Below are examples of common stats you might need—adapt these to your specific output requirements:

Example 1: Count Participants per Course

If you want to see how many participants are enrolled in each course (including courses with zero participants):

SELECT 
    k.kurs_name, -- Replace with your actual course name column
    COUNT(t.teilnehmer_id) AS total_participants
FROM KURS k
LEFT JOIN TEILNEHMERKURS t ON k.kurs_id = t.kurs_id
GROUP BY k.kurs_id, k.kurs_name
ORDER BY total_participants DESC;

Using LEFT JOIN ensures courses with no enrolled participants still appear in the results with a count of 0.

Example 2: Stats by Course Category

If KURS has a category column (like kurs_kategorie), here’s how to calculate average participants per category:

SELECT 
    course_stats.kurs_kategorie,
    AVG(course_stats.participant_count) AS avg_participants_per_category
FROM (
    SELECT 
        k.kurs_id,
        k.kurs_kategorie,
        COUNT(t.teilnehmer_id) AS participant_count
    FROM KURS k
    LEFT JOIN TEILNEHMERKURS t ON k.kurs_id = t.kurs_id
    GROUP BY k.kurs_id, k.kurs_kategorie
) AS course_stats
GROUP BY course_stats.kurs_kategorie;
Step 3: Fix Your Existing Query

Since your current statement isn’t giving the expected output, could you share a few more details?

  • The full schema of both tables (column names and data types)
  • Your existing SQL query
  • A sample of what you expected vs. what you’re actually getting

With that info, I can pinpoint exactly where the issue is and tweak the query to match your needs perfectly.

内容的提问来源于stack exchange,提问作者Stefan Leith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:12:31