Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

sqlite - Sql loop over unique elements of a column

I am trying to get data of rows into columns. This is my data before query.

enter image description here

This is my query.

SELECT
    Date,
    SUM(CASE WHEN (Terms="Art") THEN 1 ELSE 0 END) AS Art,
    SUM(CASE WHEN (Terms="Action") THEN 1 ELSE 0 END) AS Action,
    SUM(CASE WHEN (Terms="Board") THEN 1 ELSE 0 END) AS Board,
    SUM(CASE WHEN (Terms="Puzzle") THEN 1 ELSE 0 END) AS Puzzle,
    SUM(CASE WHEN (Terms="Adventure") THEN 1 ELSE 0 END) AS Adventure,
    SUM(CASE WHEN (Terms="Others") THEN 1 ELSE 0 END) AS Others
FROM 
    __table__
GROUP BY 
    Date

After Query this is my data.

enter image description here

Which is fine and intended data. But problem is in query as you can see I have to explicitly write each term. I want to automate that using loop. Theoratical solution is to write subquery to get all unique terms and then loop over them. But I don't know how. Help will be appreciated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...