Category:Computer-aided design software
Category:AutodeskQ:
Wrap results of a SQL query with a single query
I've been reading a lot about getting a single result, and while I feel as if I understand what is happening, I still don't know exactly how to implement it in my query.
I have the following table of "skills" and I'm wanting to query a list of users (as a string) with their aggregate skill points.
Table "skills"
Id | Name | Level
--------------+--------------+------------
1 | Java | 2
2 | C# | 1
3 | PHP | 1
4 | SQL | 2
There are thousands of records in this table, which is what I'm going to be querying against. So, I'm thinking something like this:
SELECT (SELECT SUM(LEVEL) FROM skills WHERE id = 2)
But this is yielding every level of the skills, instead of just the level of the skill that is equal to the id of the user.
Now, I feel like I'm close but not quite there, but I'm not exactly sure how I can modify my query.
EDIT: I'm aware that there is a similar question to this but I'm still a bit confused. In the answers provided there, I'm not understanding the answer clearly.
A:
Use a sub-query:
SELECT (SELECT SUM(LEVEL) FROM skills WHERE id = 2)
As to your edit:
SELECT (SELECT SUM(LEVEL) FROM skills WHERE id = 2) AS skill_points
A:
Try something like
SELECT
*,
SUM(CASE WHEN id=2 THEN Level ELSE 0 END) AS skill_points
FROM skills
GROUP ac619d1d87
Related links:
Commentaires