运行此代码时出现以下错误。请帮帮我。
SELECT store, COUNT(DISTINCT saledate), CountNov, CountDec, SumNov, SumDec, (SumNov/CountNov) AS NovAvgRvn, (SumDec/CountDec) AS DecAvgRvn FROM
(
SELECT store, saledate,
CASE WHEN SUM(CASE EXTRACT(MONTH FROM saledate) WHEN '11' THEN amt END) IS NULL THEN 0
ELSE SUM(CASE EXTRACT(MONTH FROM saledate) WHEN '11' THEN amt END)
END AS SumNov,
CASE WHEN SUM(CASE EXTRACT(MONTH FROM saledate) WHEN '12' THEN amt END) IS NULL THEN 0
ELSE SUM(CASE EXTRACT(MONTH FROM saledate) WHEN '12' THEN amt END)
END AS SumDec,
CASE WHEN COUNT(DISTINCT CASE EXTRACT(MONTH FROM saledate) WHEN '11' THEN saledate END) IS NULL THEN 0
ELSE COUNT(DISTINCT CASE EXTRACT(MONTH FROM saledate) WHEN '11' THEN saledate END)
END AS CountNov,
CASE WHEN COUNT(DISTINCT CASE EXTRACT(MONTH FROM saledate) WHEN '12' THEN saledate END) IS NULL THEN 0
ELSE COUNT(DISTINCT CASE EXTRACT(MONTH FROM saledate) WHEN '12' THEN saledate END)
END AS CountDec
FROM trnsact
WHERE stype = 'p'
GROUP BY store, saledate
) AS T1
WHERE CountDec > 0 AND CountNov > 0
GROUP BY store
ORDER BY store;
错误: 错误代码 - 3504 错误消息 - [Teradata数据库] [TeraJDBC 15.10.00.05] [错误3504] [SQLState HY000]选定的非聚合值必须是关联组的一部分。
答案 0 :(得分:1)
如果你有group by子句,那么你的select语句中不是SUM,AVG等聚合的任何内容都需要包含在你的group by子句中
试试希望它有所帮助:
SELECT store, COUNT(DISTINCT saledate), CountNov, CountDec, SumNov, SumDec, (SumNov/CountNov) AS NovAvgRvn, (SumDec/CountDec) AS DecAvgRvn FROM
(
SELECT store, saledate,
CASE WHEN
SUM(CASE EXTRACT(MONTH FROM saledate)
WHEN '11' THEN amt END) IS NULL THEN 0
ELSE SUM(CASE EXTRACT(MONTH FROM saledate) WHEN '11' THEN amt END)
END AS SumNov,
CASE WHEN SUM(CASE EXTRACT(MONTH FROM saledate) WHEN '12' THEN amt END) IS NULL THEN 0
ELSE SUM(CASE EXTRACT(MONTH FROM saledate) WHEN '12' THEN amt END)
END AS SumDec,
CASE WHEN COUNT(DISTINCT CASE EXTRACT(MONTH FROM saledate) WHEN '11' THEN saledate END) IS NULL THEN 0
ELSE COUNT(DISTINCT CASE EXTRACT(MONTH FROM saledate) WHEN '11' THEN saledate END)
END AS CountNov,
CASE WHEN COUNT(DISTINCT CASE EXTRACT(MONTH FROM saledate) WHEN '12' THEN saledate END) IS NULL THEN 0
ELSE COUNT(DISTINCT CASE EXTRACT(MONTH FROM saledate) WHEN '12' THEN saledate END)
END AS CountDec
FROM trnsact
WHERE stype = 'p'
GROUP BY store, saledate, SumNov, SumDec, CountNov, CountDec
) AS T1
WHERE CountDec > 0 AND CountNov > 0
GROUP BY store, saledate, CountNov, CountDec, SumNov, SumDec, NovAvgRvn, DecAvgRvn
ORDER BY store;
答案 1 :(得分:1)
为什么要使用嵌套查询?
SELECT store, COUNT(DISTINCT saledate),
SUM(CASE EXTRACT(MONTH FROM saledate) WHEN 11 THEN amt ELSE 0 END) as SumNov,
SUM(CASE EXTRACT(MONTH FROM saledate) WHEN 11 THEN amt ELSE 0 END) as SumDec,
COUNT(DISTINCT CASE EXTRACT(MONTH FROM saledate) WHEN 11 THEN saledate END) as CountNov,
COUNT(DISTINCT CASE EXTRACT(MONTH FROM saledate) WHEN 12 THEN saledate END) as CountDec
FROM trnsact
WHERE stype = 'p'
GROUP BY store;
注意:
COUNT()
永远不会返回NULL
,因此根本不需要CASE
。ELSE
子句,SUM()
也不会返回NULL
(必须没有匹配SUM()
的行才能返回NULL
1}},并且组中没有匹配的行,该组将不存在)。EXTRACT()
会返回一个数字,因此请与数字进行比较。答案 2 :(得分:0)
派生表中的COUNT(DISTINCT saledate)
未在月份级别进行聚合,因此1
将为EXTRACT
。您不应该将字符串用于数字数据(CASE(SUM)
的结果)。您不需要COUNT
,因为COALESCE
永远不会返回NULL(您可以使用SELECT store,
-- if you only need the dates from Nov & Dec you can simply do
-- CountNov + CountDec instead
COUNT(DISTINCT saledate),
SUM(CASE EXTRACT(MONTH FROM saledate) WHEN 11 THEN amt ELSE 0 END) AS SumNov,
SUM(CASE EXTRACT(MONTH FROM saledate) WHEN 12 THEN amt ELSE 0 END) AS SumDec,
COUNT(DISTINCT CASE EXTRACT(MONTH FROM saledate) WHEN 11 THEN saledate END) AS CountNov,
COUNT(DISTINCT CASE EXTRACT(MONTH FROM saledate) WHEN 12 THEN saledate END) AS CountDec,
(SumNov/CountNov) AS NovAvgRvn,
(SumDec/CountDec) AS DecAvgRvn
FROM trnsact
WHERE stype = 'p'
-- don't you need a condition to filter for a specific year/month?
AND EXTRACT(MONTH FROM saledate) IN (11,12)
GROUP BY store
ORDER BY store;
代替):
我假设您想要这样的查询:
public class RandomWord extends JFrame{
private JTextField wordField;
private JTextField defField;
private JButton nextButton;
private String words[] = {"Petrichor:",//0
"Iterate:",//1
"Absquatulate:",//2
"Anhuiliform:",//3
"Argle-bargle:","Argus-eyed:",//4
"Automy:",//5
"Benthos:",//6
"Bibliopole:",//7
"Bilboes:",//8
"Bruxism:",//9
"Borborygmus:",//10
"Calipygian:",//11
"Callithumpian:",//12
"Cereology:",//13
"Chad:",//14
"Chiliad:"};//15
private String def[] = {"The smell of earth after rain.",//0
"To utter or perform repeatedly.",//1
"To leave somewhere abruptly.",//2
"Resembling an eel.",//3
"Copious but meaningless talk or writing.",//4
"Vigilant, refering to Argos a Greek mythological watchman with a hundred eyes.",//5
"The casting off of a limb or other part of the body by an animal under threat, such as a lizard.",//6
"The flora and faunda on the bottom of a sea or lake.",//7
"A person who buys and sells books, especially rare ones",//8
"An iron bar with sliding shackles, used to fasten prisoners' ankles.",//9
"Involantary and habitual grinding of the teeth.",//10
"A rumbling or gurgling noise in the intestines.",//11
"Having shapely buttocks.","Like a discordant band or a noisy parade.",//12
"The study or investigation of crop circles.",//13
"A piece of waste paper produced by punching a hole.",//14
"A thousand things or a thousand years."};//15
public RandomWord(){
super("Cool Words -1.5");
setLayout(new FlowLayout());
int idx = new Random().nextInt(words.length);
final String randomWord = words[idx];
final String randomDef = def[idx];
wordField = new JTextField("Petrichor",20);
add(wordField);
defField = new JTextField("The smell of earth after rain",20);
add(defField);
nextButton = new JButton("Next");
add(nextButton);
nextButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
wordField.setText(randomWord);
defField.setText(randomDef);
}
});
}