我在一栏中有情绪分析的结果。我想显示每个数字代表的数字和计数,如下所示:
Neg | Neutral | Pos
---------+-----+-----
30 | 55 | 100
我尝试了很多我在互联网上找到的不同方法,但无济于事。似乎db2语法经常更改,我发现的所有文档都不适用于我当前的bluemix帐户。
我能得到的最接近的是UNION:
SELECT COUNT(*) as NEGATIVE FROM C7 WHERE SENTIMENT = 1
union
SELECT COUNT(*) as NEUTRAL FROM C7 WHERE SENTIMENT = 2
union
SELECT COUNT(*) as Pos FROM C7 WHERE SENTIMENT = 3
但是,这给了我:
NEGATIVE
--------
30
55
100
如何将每个查询的结果并排组合?
我尝试了在DB2中透视Pivoting的答案,但是我遇到了错误:
SELECT
C7.SENTIMENT,
COUNT(CASE WHEN C7.SENTIMENT = 1 THEN Value END) AS NEGATIVE,
COUNT(CASE WHEN C7.SENTIMENT = 2 THEN Value END) AS NEUTRAL,
COUNT(CASE WHEN C7.SENTIMENT = 3 THEN Value END) AS POSITIVE,
FROM C7
GROUP BY C7.SENTIMENT
SQL0206N:SQL0206N" VALUE"在使用它的上下文中无效。 SQLSTATE = 42703
SELECT DISTINCT
C7.SENTIMENT,
(SELECT value FROM C7 WHERE C7.SENTIMENT = 1) AS VERY_NEGATIVE,
(SELECT value FROM C7 WHERE C7.SENTIMENT = 2) AS NEGATIVE,
(SELECT value FROM C7 WHERE C7.SENTIMENT = 3) AS NEUTRAL
FROM C7
SQL0206N:SQL0206N" VALUE"在使用它的上下文中无效。 SQLSTATE = 42703
答案 0 :(得分:1)
好的,所以在重复几个答案之后,我终于得到了我想要的东西:
SELECT DISTINCT
(SELECT COUNT(SENTIMENT) FROM C7 WHERE C7.SENTIMENT = 1) AS NEGATIVE,
(SELECT COUNT(SENTIMENT) FROM C7 WHERE C7.SENTIMENT = 2) AS NEUTRAL,
(SELECT COUNT(SENTIMENT) FROM C7 WHERE C7.SENTIMENT = 3) AS POSITIVE
FROM C7
这似乎是我长时间徘徊的东西,但无法找到,因为有些帖子用奇怪的表名和值来回答问题。
基本上,为了让查询彼此相邻,您必须使用多个SELECT
。
另外,请注意我使用DISTINCT
,否则它会为我的数据库中的每一行提供相同数据的副本。