[英]How to compare date with a date range
I want to calculate Total of Quantity and GROUP BY CarID but am currently unable to do this. 我想计算“数量总计”和“ GROUP BY CarID”,但目前无法执行此操作。
In my database have some tables: TBL_CAR_TX, TBL_CAR, TBL_PERSON 在我的数据库中有一些表:TBL_CAR_TX,TBL_CAR,TBL_PERSON
table: TBL_CAR_TX
------------------
select * from TBL_CAR_TX
where TranDate >= '2011-06-09' and Trandate <= '2011-06-20'
TranID, AccID, CarID, TranDate, Type, Quantity, .....
--------------------------------------------------------
0 2563 BMW 2011-06-09 H -15 (1)
1 2563 BMW 2011-06-20 R 15
2 2563 BMW 2011-06-20 H 20
3 0055 TOY 2011-06-12 H -10 (2)
4 0055 TOY 2011-06-20 R 10
...
(H): Hold, (R): Release (H):保持,(R):释放
if we change the condition of the WHERE stmt: 如果我们更改WHERE stmt的条件:
select * from TBL_CAR_TX where TranDate >= '2011-06-10' and Trandate <= '2011-06-19'
(these date, I get them from outside) ==> all of the records disappear ==> I can't calculate because the data doesn't display. (这些日期,我从外面得到)==>所有记录都消失了==>由于数据不显示,我无法计算。
Look at the table TBL_CAR_TX, we can see that a person borrow car 'BMW' from '2011-06-09' and until '2011-06-20' he returned it. 看一下表TBL_CAR_TX,我们可以看到一个人从“ 2011-06-09”借车“ BMW”,直到“ 2011-06-20”他都归还了它。 So If I do in SQL Server 2008, how can I keep the record (1) & (2) in the range date from '2011-06-10' to '2011-06-19' to calculate. 因此,如果我在SQL Server 2008中这样做,如何将记录(1)和(2)保留在从'2011-06-10'到'2011-06-19'的日期范围内以进行计算。 If can not, how can I do it in java programming ? 如果不能,如何在Java编程中做到这一点? Any suggest. 任何建议。
table TBL_CAR:
MarketID, CarID, Name, Size, ...
---------------------------
GER BMW ....
JPA TOY ....
Table TBL_PERSON:
AccID, Name, Age, DOB, ...
-------------------------
2563 Robert
0055 Mike
Thanks you very much for your help 非常感谢您的帮助
I want to calculate Total of Quantity and GROUP BY CarID but am currently unable to do this. 我想计算“数量总计”和“ GROUP BY CarID”,但目前无法执行此操作。 So If I do in SQL Server 2008, how can I keep the record (1) & (2) in the range date from '2011-06-10' to '2011-06-19' to calculate. 因此,如果我在SQL Server 2008中这样做,如何将记录(1)和(2)保留在从'2011-06-10'到'2011-06-19'的日期范围内以进行计算。
Just get rid of =
from your SQL. 只需从您的SQL中删除=
。
select CarID, SUM(QUANTITY) from TBL_CAR_TX where TranDate > '2011-06-09' and Trandate < '2011-06-20' GROUP BY CarID
or use BETWEEN SQL CLAUSE. 或使用BETWEEN SQL子句。
select CarID, SUM(QUANTITY) from TBL_CAR_TX where TranDate BETWEEN '2011-06-10' AND '2011-06-19' GROUP BY CarID
If can not, how can I do it in java programming ? 如果不能,如何在Java编程中做到这一点?
You can definitely filter out the data in java but there is no point in doing so when SQL can do the work for you. 您绝对可以在Java中过滤掉数据,但是当SQL可以为您完成工作时,这样做是没有意义的。 Unless it is a massive performance penalty, it is almost always worth doing the processing like this in SQL. 除非这会造成很大的性能损失,否则在SQL中几乎总是值得进行这样的处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.