sql数据总和的问题 [英] Problem in sum of sql data

查看:69
本文介绍了sql数据总和的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





如果我选择之前添加到当前日期之前的日期,我希望将sql数据库中的一列与其今天选择的日期工作正常相加数据(如今天是2013年10月10日,数据值是2,3,4,前一个日期是4/10/2013,数据值是5,6,7,当我选择上一个日期时,它显示总价值的总和1 + 2 + 3 + 4 + 5 + 6 + 7.

这将是5 + 6 + 7.



我的查询是



 query =从EntryTable中选择sum(txtPurity),其中txtCustomerName = @ Name AND txtDate> = @ Date AND cmbItemType ='G1' ; 

解决方案

嘿那里,如果您想要特定日期的数据,那么为什么要使用> ; = 运营商?

肯定会包含@Date的数据以及此日期之后的日期。



只需使用 txtDate = @Date



如果能解决问题请告诉我。



Azee ......


 txtDate< =  @ Date  





for 4/10/2013它还包括第4个,第5个和第6个!!!


几乎所有与日期相关的问题都是因为数据库中的日期列table未声明为DATE或DATETIME数据类型和/或传递给SQL Select的参数未声明为DATE或DATETIME数据类型。我怀疑包含日期和/或@date参数的数据库列不是DATE数据类型。这是你的问题的原因。如果使用DATETIME数据类型并且您只使用日期而不是时间,那么DATETIME值的时间部分将为零。



如果您始终遵循使用DATE或DATETIME数据类型来存储日期并始终使用DATE或DATETIME数据类型参数来传递日期的最佳做法,那么您将大大减少使用日期的问题。



请参阅 DateTime数据类型 [ ^ ]



请参阅配置参数和参数数据类型 [ ^ ]日期时间类型

hi,

I want to sum of a column in sql database its working fine when today date selected if I am selecting previous date it add previous to current date data(like today is 6/10/2013 and data value is 2,3,4 and previous date is 4/10/2013 and data value is 5,6,7 when I am selecting previous date it show the sum of total value 1+2+3+4+5+6+7.
It would be 5+6+7.

My query is

query = "select sum(txtPurity) from EntryTable where txtCustomerName=@Name AND txtDate >=@Date AND cmbItemType='Gl'";

解决方案

Hey there, If you want data for a specific date, then why are you using >= operator?
It will definitely include data for @Date as well as for dates after this date.

just use txtDate = @Date .

Let me know if solves the problem.

Azee...


txtDate <= @Date



for 4/10/2013 it will include 4th, 5th and 6th also!!!


Almost always problems related to dates are because the date column in the database table is not declared as DATE or DATETIME data type and/or the parameter passed to the SQL Select is not declared as DATE or DATETIME data type. I suspect that either the database column containing the date and/or the @date parameter are not of DATE data type. That is the cause of your problem. If a DATETIME data type is used and you only use dates and not times, then time part of the DATETIME value will be zero.

If you always follow the best practice of using DATE or DATETIME data type to store dates and always using DATE or DATETIME data type parameters to pass dates, you will greatly reduce problems using dates.

See DateTime data type[^]

See Configuring Parameters and Parameter Data Types[^] DateTime type


这篇关于sql数据总和的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆