我正在尝试构建一个T-SQL查询,以使用不同的名称列出某些列和具有特定条件的值。
这就是我在桌子上的内容:
Category | Verdict | Requests
-----------|--------------|----------
Category1 | Allowed | 1000
Category2 | denied_now | 100
Category2 | denied_later | 101
Category3 | Allowed | 203
我想最终得到这样的结果。
Category | Verdict | Requests
-----------|--------------|----------
Category1 | Allowed | 1000
Category2 | denied_all | 201
Category3 | Allowed | 203
我尝试了SELECT
与GROUP BY
和HAVING
相结合,但我总是遇到错误。如何将这些值组合成Verdict = denied%的新名称下的新行?
答案 0 :(得分:1)
我认为这应该可以解决问题:
import java.awt.*;
import java.awt.font.TextAttribute;
import java.math.*;
import java.text.*;
import java.util.Map;
import javax.swing.*;
import javax.swing.JFormattedTextField.*;
import javax.swing.event.*;
import javax.swing.text.InternationalFormatter;
public class DocumentListenerAdapter {
public DocumentListenerAdapter() {
JFrame frame = new JFrame("AbstractTextField Test");
final JFormattedTextField textField1 = new JFormattedTextField(new Double(10.01));
textField1.setFormatterFactory(new AbstractFormatterFactory() {
@Override
public AbstractFormatter getFormatter(JFormattedTextField tf) {
NumberFormat format = DecimalFormat.getCurrencyInstance();
format.setMinimumFractionDigits(2);
format.setMaximumFractionDigits(2);
format.setRoundingMode(RoundingMode.HALF_UP);
InternationalFormatter formatter = new InternationalFormatter(format);
formatter.setAllowsInvalid(false);
//formatter.setMinimum(0.0);
//formatter.setMaximum(1000.00);
return formatter;
}
});
final Map attributes = (new Font("Serif", Font.BOLD, 16)).getAttributes();
attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
final JFormattedTextField textField2 = new JFormattedTextField(new Double(10.01));
textField2.setFormatterFactory(new AbstractFormatterFactory() {
@Override
public AbstractFormatter getFormatter(JFormattedTextField tf) {
NumberFormat format = DecimalFormat.getInstance();
format.setMinimumFractionDigits(3);
format.setMaximumFractionDigits(3);
format.setRoundingMode(RoundingMode.HALF_UP);
InternationalFormatter formatter = new InternationalFormatter(format);
formatter.setAllowsInvalid(false);
//formatter.setMinimum(0.0);
//formatter.setMaximum(1000.00);
return formatter;
}
});
textField2.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void changedUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
@Override
public void insertUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
@Override
public void removeUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
private void printIt(DocumentEvent documentEvent) {
DocumentEvent.EventType type = documentEvent.getType();
double t1a1 = (((Number) textField2.getValue()).doubleValue());
if (t1a1 > 1000) {
Runnable doRun = new Runnable() {
@Override
public void run() {
textField2.setFont(new Font(attributes));
textField2.setForeground(Color.red);
}
};
SwingUtilities.invokeLater(doRun);
} else {
Runnable doRun = new Runnable() {
@Override
public void run() {
textField2.setFont(new Font("Serif", Font.BOLD, 16));
textField2.setForeground(Color.black);
}
};
SwingUtilities.invokeLater(doRun);
}
}
});
//https://stackoverflow.com/a/20008786/714968
JFormattedTextField jftf2 = new JFormattedTextField();
final InternationalFormatter fmt = new InternationalFormatter(
new MessageFormat("{0,number,000}-{1,number,0000}"));
jftf2.setFormatterFactory(new JFormattedTextField.AbstractFormatterFactory() {
@Override
public JFormattedTextField.AbstractFormatter getFormatter(JFormattedTextField tf) {
return fmt;
}
});
jftf2.setValue(new Object[]{111, 1234});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(textField1, BorderLayout.NORTH);
frame.add(textField2, BorderLayout.CENTER);
frame.add(jftf2, BorderLayout.SOUTH);
frame.setVisible(true);
frame.pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
DocumentListenerAdapter main = new DocumentListenerAdapter();
}
});
}
}
我发现SELECT Category, CASE WHEN Verdict LIKE 'denied%' THEN 'denied_all' ELSE Verdict END AS Verdict, SUM(Requests) Requests
FROM TableName
GROUP BY Category, CASE WHEN Verdict LIKE 'denied%' THEN 'denied_all' ELSE Verdict END
ORDER BY Category
中没有任何条件。
所以我在这里做的只是取代所有被拒绝的%'与'否则_all'判决所以它们可以分组,然后我按类别和修改判决进行分组,最后在Request字段中对所有值求和。没什么大秘密。
如果您不想重复HAVING
,我认为您必须使用其中任何一种:
CASE WHEN
或者:
SELECT Category, Verdict, SUM(Requests) Requests
FROM (
SELECT Category, CASE WHEN Verdict LIKE 'denied%' THEN 'denied_all' ELSE Verdict END AS Verdict, Requests
FROM TableName
) SubQ
GROUP BY Category, Verdict
ORDER BY Category