如何整理列表?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 BYto 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
WHEREto 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 BYwith aggregate functions likeCOUNTorMAXto 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




