You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何整理列表?SQL能否实现同人员Bewertung与Leistung合并拆分?

Hey there! Let's break down your two questions one by one:

1. How to Organize Lists (SQL-Focused)

Assuming you're asking about organizing data lists/result sets in SQL, here are go-to methods:

  • Sort your results: Use ORDER BY to arrange rows by specific columns. For example, to sort by person ID and then the type of entry:
    SELECT * FROM your_table ORDER BY person_id, entry_type;
    
  • Filter irrelevant data: Narrow down your list with WHERE to only include records you care about:
    SELECT * FROM your_table WHERE person_id IS NOT NULL AND entry_type IN ('Bewertung', 'Leistung');
    
  • Group related data: Use GROUP BY with aggregate functions like COUNT or MAX to summarize data for groups (e.g., all entries per person):
    SELECT person_id, COUNT(*) AS total_entries FROM your_table GROUP BY person_id;
    
  • Pivot rows to columns: Transform categorical data (like your 'Bewertung' and 'Leistung' entries) into columns—this ties directly into your second question!

If you meant organizing lists outside of SQL (like in an app), just clarify, but given your second question, I'm guessing SQL is the context here.

2. Merging 'Bewertung' and 'Leistung' for the Same Person into One Row

Absolutely, this is totally doable with SQL! The exact approach depends on how your data is structured, but here are three reliable methods that work across most databases:

Method 1: Conditional Aggregation (Portable Across All Databases)

This is my go-to because it works in MySQL, PostgreSQL, SQL Server, etc. If your data has one row per person-entry type (e.g., one row for 'Bewertung' and another for 'Leistung' for the same person), use CASE statements to pull each value into its own column:

SELECT 
    person_id,
    MAX(CASE WHEN entry_type = 'Bewertung' THEN value END) AS Bewertung,
    MAX(CASE WHEN entry_type = 'Leistung' THEN value END) AS Leistung
FROM 
    your_table
GROUP BY 
    person_id;

The MAX function (you could also use MIN if appropriate) aggregates the values into a single row per person. If a person only has one of the two entries, the missing column will show NULL.

Method 2: Using JOIN (For Separate Tables or Filtered Subqueries)

If your 'Bewertung' and 'Leistung' data is in separate tables, or you want to filter first, use a FULL OUTER JOIN to keep all people even if they only have one entry:

SELECT 
    COALESCE(b.person_id, l.person_id) AS person_id,
    b.value AS Bewertung,
    l.value AS Leistung
FROM 
    (SELECT person_id, value FROM your_table WHERE entry_type = 'Bewertung') b
FULL OUTER JOIN 
    (SELECT person_id, value FROM your_table WHERE entry_type = 'Leistung') l
ON 
    b.person_id = l.person_id;

COALESCE ensures we get a person ID even if one side of the join is missing.

Method 3: PIVOT (For Databases That Support It)

If you're using SQL Server, Oracle, or another database with built-in pivot functionality, you can use the PIVOT clause to transform rows into columns directly:

SELECT person_id, [Bewertung], [Leistung]
FROM 
    your_table
PIVOT 
    (MAX(value) FOR entry_type IN ([Bewertung], [Leistung])) AS PivotResult;

This is clean and concise, but note that not all databases support PIVOT (looking at you, MySQL and PostgreSQL).

All these methods will group entries by person, showing both values in the same row if they exist. If a person has multiple entries for the same type (e.g., two 'Bewertung' entries), you might want to adjust the aggregation—like using GROUP_CONCAT in MySQL to combine them into a single string:

SELECT 
    person_id,
    GROUP_CONCAT(CASE WHEN entry_type = 'Bewertung' THEN value END SEPARATOR ', ') AS Bewertung,
    GROUP_CONCAT(CASE WHEN entry_type = 'Leistung' THEN value END SEPARATOR ', ') AS Leistung
FROM 
    your_table
GROUP BY 
    person_id;

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

火山引擎 最新活动