我正在使用tomcat、jsp和MSSQL2008,希望获得一些汇总数据,如每日、每周、每月和每年。从这里,我搜索了如何对来自MSSQL的这些摘要数据使用datapart()函数。在JSP中,我使用
PreparedStatement pr = conn.prepareStatement(query);
ResultSet rs=pr.executeQuery();
在MSMS中,查询指令进行测试。
我在JSP程序中遇到了两个问题,但在MS SQL Server Management Studio中使用相同的查询可以很好地工作。
我的问题是:
switch (unit)
{
case "Daily" :
query = "SELECT cast(datepart(dy, ProcessDate) as varchar(3)) Day, " +
"cast(year(ProcessDate) as varchar(4)) Year, " +
"SUM(case WHEN Warranty = 'Y' then 1 else 0 end) InWarranty, " +
"SUM(case WHEN Warranty = 'N' then 1 else 0 end) OutOfWarranty, " +
"SUM(case WHEN Warranty = 'X' then 1 else 0 end) NAWarranty " +
"FROM zz_RMA_Summary " +
"WHERE ProcessDate BETWEEN '" + sDate + "' AND '" + eDate + "' " +
"group by year(ProcessDate), datepart(dy, ProcessDate) " +
"order by year(ProcessDate) DESC, datepart(dy, ProcessDate) DESC ";
break;
case "Weekly" :
query = "SELECT cast(datepart(wk, ProcessDate) as varchar(2)) Week, " +
"cast(year(ProcessDate) as varchar(4)) Year, " +
"SUM(case WHEN Warranty = 'Y' then 1 else 0 end) InWarranty, " +
"SUM(case WHEN Warranty = 'N' then 1 else 0 end) OutOfWarranty, " +
"SUM(case WHEN Warranty = 'X' then 1 else 0 end) NAWarranty " +
"FROM zz_RMA_Summary " +
"WHERE ProcessDate BETWEEN '" + sDate + "' AND '" + eDate + "' " +
"group by year(ProcessDate), datepart(wk, ProcessDate) " +
"order by year(ProcessDate) DESC, datepart(wk, ProcessDate) DESC ";
break;
case "Monthly" :
query = "SELECT cast(datepart(mm, ProcessDate) as varchar(2)) Month, " +
"cast(year(ProcessDate) as varchar(4)) Year, " +
"SUM(case WHEN Warranty = 'Y' then 1 else 0 end) InWarranty, " +
"SUM(case WHEN Warranty = 'N' then 1 else 0 end) OutOfWarranty, " +
"SUM(case WHEN Warranty = 'X' then 1 else 0 end) NAWarranty " +
"FROM zz_RMA_Summary " +
"WHERE ProcessDate BETWEEN '" + sDate + "' AND '" + eDate + "' " +
"group by year(ProcessDate), datepart(mm, ProcessDate) " +
"order by year(ProcessDate) DESC, datepart(mm, ProcessDate) DESC ";
break;
case "Yearly" :
query = "SELECT cast(yyyyy, ProcessDate) as varchar(4)) Year, " +
"SUM(case WHEN Warranty = 'Y' then 1 else 0 end) InWarranty, " +
"SUM(case WHEN Warranty = 'N' then 1 else 0 end) OutOfWarranty, " +
"SUM(case WHEN Warranty = 'X' then 1 else 0 end) NAWarranty " +
"FROM zz_RMA_Summary " +
"WHERE ProcessDate BETWEEN '" + sDate + "' AND '" + eDate + "' " +
"group by datepart(yyyy, ProcessDate) " +
"order by datepart(yyyy, ProcessDate) DESC ";
break;
}
您的问题是由日期
作为字符串
传递引起的,这是错误的。
为什么要用动态sql,是强制性的吗?
您能将它与参数
(属于日期
类型)一起使用而不能与字符串
级联使用吗?
declare @t table (dt datetime);
insert into @t values ('20170110'), ('20171001'); -- Jan, 10; Oct, 1st
set language british -- this happens when my login's default language is us_english
select *
from @t
where dt between '01/09/2017' and '01/01/2018' -- I'm in Europe an I want the dates starting from Sept, 1st
---
dt
2017-10-01 00:00:00.000 -- I'm happy with correct result
set language us_english -- this happens when my login's default language is us_english
select *
from @t
where dt between '01/09/2017' and '01/01/2018' -- I'm in Europe an I want the dates starting from Sept, 1st
----
dt
2017-01-10 00:00:00.000 -- quite strange since I filtered starting from September
2017-10-01 00:00:00.000
文档: 操作: 合作伙伴: HQL(“INCASAT”=“兑现”,“VANDUT”=“出售”):
问题内容: SQL2008。 我有一个测试表: 我用10k测试行填充它。 我运行以下两个查询: 我不知道为什么这两个查询有不同的执行计划。 查询1确实针对UQ_Sale_RowVersion索引进行索引搜索。 查询2对PK_Sale进行索引扫描。 我想查询2做索引查找。 我将不胜感激。 谢谢你。 [编辑] 尝试使用datetime2而不是rowversion。同样的问题。 我也尝试强制使用索引(查
如何在Grafana的同一面板中使用来自不同查询的prometheus查询结果。 实例 我在格拉法纳有3个普罗米修斯查询,
问题内容: 我有一个包含多个文档的mongo集合,假设以下内容(假设出于任何原因,汤姆在2012年都有两名历史老师) 我希望能够查询“汤姆”曾经有过的所有不同的班级,即使汤姆有多个“历史”班级且有多个老师,我只想查询得到最少数量的文档,以便汤姆在所有这些都将显示一次,而“历史记录”只显示一次,而不是查询结果包含重复“历史记录”的多个文档。 我看了一下:http : //mongoengine- o
使用JDBC驱动程序,我如何使用来自不同查询的多个结果集,而不不断地打开和关闭连接,因为我正在提取所需的w.e并将其传递给另一个方法。每次打开新的conn、语句和结果集时 我试图在一个方法中使用多个结果集,但它一直抛出异常,称结果集已关闭。我没有太多的SqlServver经验,所以任何指导都会有所帮助:-)
我在我的项目中使用SQL服务器,我试图在两个时间戳之间检索表中的条目。表的架构如下所示: 我需要获取在特定时间戳和当前时间戳之后添加的条目。为此,我尝试了下面提到的查询: 当在SQL客户机上直接执行这个查询时,我得到了所有预期的结果。但是当我从Java JDBC客户端执行相同的查询时,< code>resultset包含的条目的ADDED_TIME等于parameter1。 下面是客户端的Java