我正在尝试使用以下查询来收集2015年5月的数据。列import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class AddEquipment extends JPanel {
private JTextField txtu2;
private JTextField txtu3;
private JTextField txtu4;
private JTextField txtu5;
private JTextField txtu6;
private JButton btnu8;
private JButton btnu9; //Back Button
public AddEquipment() {
setLayout(null);
JLabel lblu8 = new JLabel("Add Equipment");
lblu8.setForeground(Color.WHITE);
lblu8.setFont(new Font("Tahoma", Font.BOLD, 16));
lblu8.setBounds(12, 13, 319, 33);
add(lblu8);
JLabel lblu3 = new JLabel("Name");
lblu3.setFont(new Font("Tahoma", Font.BOLD, 13));
lblu3.setBounds(60, 91, 46, 14);
add(lblu3);
txtu2 = new JTextField();
txtu2.setColumns(10);
txtu2.setBounds(221, 88, 377, 22);
add(txtu2);
JLabel lblu4 = new JLabel("Section");
lblu4.setFont(new Font("Tahoma", Font.BOLD, 13));
lblu4.setBounds(60, 156, 69, 14);
add(lblu4);
txtu3 = new JTextField();
txtu3.setColumns(10);
txtu3.setBounds(221, 153, 377, 22);
add(txtu3);
JLabel lblu5 = new JLabel("Type");
lblu5.setFont(new Font("Tahoma", Font.BOLD, 13));
lblu5.setBounds(60, 221, 46, 14);
add(lblu5);
txtu4 = new JTextField();
txtu4.setColumns(10);
txtu4.setBounds(221, 218, 377, 22);
add(txtu4);
JLabel lblu6 = new JLabel("Value");
lblu6.setFont(new Font("Tahoma", Font.BOLD, 13));
lblu6.setBounds(60, 286, 46, 14);
add(lblu6);
txtu5 = new JTextField();
txtu5.setColumns(10);
txtu5.setBounds(221, 283, 377, 22);
add(txtu5);
JLabel lblu7 = new JLabel("Amount");
lblu7.setFont(new Font("Tahoma", Font.BOLD, 13));
lblu7.setBounds(60, 351, 69, 14);
add(lblu7);
txtu6 = new JTextField();
txtu6.setColumns(10);
txtu6.setBounds(221, 348, 377, 22);
add(txtu6);
btnu8 = new JButton("Submit");
btnu8.setFont(new Font("Tahoma", Font.PLAIN, 13));
//btnu8.setBackground(Color.BLACK); // button color
//btnu8.setBackground( new Color(0, 0, 0, 50) ); //opacity
btnu8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnu8.setBounds(422, 534, 110, 30);
add(btnu8);
btnu9 = new JButton("Back");
btnu9.setFont(new Font("Tahoma", Font.PLAIN, 13));
btnu9.setBounds(542, 534, 110, 30);
add(btnu9);
JLabel lblback3 = new JLabel("");
lblback3.setIcon(new ImageIcon("\\\\I-INTRA-03\\IIS-Lehre\\DAPRO-WF4.informatik.hs-ulm.de\\15\\group_together\\GUI_graphics\\background_panel.jpg"));
lblback3.setBounds(0, 0, 664, 577);
add(lblback3);
}
//Getter-Method for the Back Button "btnu9"
public JButton getBackAddEquipment(){
return btnu9;
}
//Getter-Method for the "Submit" Button "btnu8"
public JButton getAddEquipmentBtn(){
return btnu8;
}
public String getName() {
return this.txtu2.getText();
}
// method sets the string value of the textfield
public void setName(String txtu2) {
this.txtu2.setText(txtu2);
}
public String getSection() {
return this.txtu3.getText();
}
public void setSection(String txtu3) {
this.txtu3.setText(txtu3);
}
public String getType() {
return this.txtu4.getText();
}
public void setType(String txtu4) {
this.txtu4.setText(txtu4);
}
public String getValue() {
return this.txtu5.getText();
}
public void setValue(String txtu5) {
this.txtu5.setText(txtu5);
}
public String getAmount() {
return this.txtu6.getText();
}
public void setAmount(String txtu6) {
this.txtu6.setText(txtu6);
}
}
是created
列,位于datetime
,我想将其转换为UTC
。因此,我正在使用PST
函数。
DATEADD()
以下是此查询的输出:
( select count(ordernumber) as Orders ,
sum(revenue) as Revenue,
'Type1' as OrderType,
CAST( CAST( DATEPART(Month, DATEADD(hh,-7,created)) as varchar )
+ '/' + '1/'
+ CAST( DATEPART(Year, DATEADD(hh,-7,created)) as varchar)
as Date ) as Date
from ORDERS
where orange = 1
and custnum = '1234'
and DATEADD(hh,-7,DATEADD(hh,-7,created)) between
CAST('2015-05-01 00:00:00.000' as DateTime)
and CAST('2015-05-31 23:59:59.999' as DateTime)
group by CAST( CAST( DATEPART(Month, DATEADD(hh,-7,created)) as varchar )
+ '/' + '1/'
+ CAST( DATEPART(Year, DATEADD(hh,-7,created)) as varchar )
as Date )
)
正如我们上面所看到的,我也获得了六月部分的数据,这是我不想要的。我只想要2015年5月的正确数据。我该怎么做?我在转换中遗漏了什么吗?
非常感谢任何帮助。
答案 0 :(得分:3)
此:
CAST('2015-05-31 23:59:59.999' as DateTime)
来自2016-06-01
,因为DateTime
类型的精确度为3.33 ms。不要使用BETWEEN
,而是使用一面封闭并在另一面打开的表达式,例如:
x >= '2015-05-01' and x < '2015-06-01'
修改:您还在WHERE
条款中使用了此内容:
DATEADD(hh,-7,DATEADD(hh,-7,created))
因此,您用来过滤结果集的日期减去了14小时而不是7,而您在结果集中显示的日期是存储日期减去7小时,这说明为什么他们有时会有不同的约会。
答案 1 :(得分:1)
您的between子句(如上所述)不正确,但您的代码......比它需要的更详细。您可能会发现以下内容更具表现力且更易于理解。在这里,我们使用Common Table Expression (CTE)创建本质上是虚拟视图的内容,其中包含我们需要的附加列:创建订单的月份的表示。我们将使用date
值的约定,该日为该月的第1天。一旦我们拥有了它,那很容易:
declare @UTC_offset int = -8 -- PST is UTC-08:00, PDT is UTC-07:00
declare @report_month date = '2015-05-01' ,
with
(
select * ,
month_created_pst = dateadd(day,
1-day( convert(date , dateadd(hour , @UTC_offset,created ) ) ) ,
dateadd(hour , @UTF_offset , created )
)
from ORDERS
) as orders_local_time
select Orders = count( t.ordernumber ) ,
Revenue = sum( t.revenue ) ,
OrderType = 'Type1' ,
Date = t.month_created_pst
from ORDERS t
where t.orange = 1
and t.custnum = '1234'
and t.date_created_pst >= @report_month
and t.month_created_pst = @report_month
group by t.month_created_pst