SQL an interesting question.
A friend recently asked me to help solve a problem, the problem description:
A table, table has three fields: f1, f2, f3, in which each field there are likely to be between 1 to 9 the number nine is to the statistics of the entire table in the 1 to 9 the number.
Such as:
F1
F2
F3
1
1
2
1
2
3
2
1
2
In the case of: "1" appeared four times, "2" in the 4 th, "3" in the 1st.
Of course, this question is asked, as far as possible the use of SQL statements to get. If the procedures used by variables such as an array, as if there is no need to raise this issue. When I spent a few minutes of thinking, OVER, feel that using SQL to consider this issue, as if there will be a point of meaning, how to use the most simple ideas and methods will be to get this issue? In this issue and will share with you, we can hope that the work of busy while mood change Fleeting fame can return that little fun.
In this solution, I will be announced as follows:
SELECT A1, COUNT (A1) AS Expr1
FROM (SELECT A1 FROM TEST
UNION ALL
SELECT A2 FROM TEST
UNION ALL
SELECT A3 FROM TEST
)
GROUP BY A1
I deliberately font color is set to white, in order to avoid interference with your thinking, if you want to see, they can drag the mouse, select a blank area above That will paste in tablet or query analyzer can be. SQLSERVER2000 in the SQL debugging through.
Tags: sql








0 Comments to “SQL an interesting question.”
No Comments. Send your comment.
Leave a Reply
You must be logged in to post a comment.