如何在SQL Server 2005中使用单个查询为不同条件计算列数据? [英] How can I calculate a column data for different conditions using single query in sql server 2005?

查看:84
本文介绍了如何在SQL Server 2005中使用单个查询为不同条件计算列数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用以下查询根据单个条件对特定的列数据进行计数.

Hi All,

I am using below query to count a particular column data based on single condition.

SELECT COUNT(DISTINCT(S.INVOICENO)) AS LESSTWO , YEAR(S.invoicedate) YER,Month(S.invoicedate) MNTH
  FROM SALESDATA S where  S.BILLINGTYPE='INVOICE' AND (S.invoicedate >='4-1-2009' and S.invoicedate <='4-30-2010')
                          AND S.TAXABLEAMT <=2000
                   GROUP BY YEAR(S.invoicedate) ,Month(S.invoicedate) ORDER BY YEAR(S.invoicedate) ,Month(S.invoicedate)




但是我必须计算另外3个条件.是否可以基于3个条件(即2001-3000,3001-4000,4001-5000,> 5000)进行计数,并且我想要所有条件的结果集?


问候,

N.SRIRAM




But I have to calculate for 3 more conditions. Is it possible to count based on 3 conditions(i.e. 2001 - 3000,3001 - 4000, 4001 - 5000,>5000) and I want result set for all the conditions?


Regards,

N.SRIRAM

推荐答案

我无法为您提供所需的确切解决方案,但在
I can not provide you a exact solution you want but answer given in THIS[^] of getting group in range with SQL will help you towards.


亲爱的朋友
我认为
Dear Friend
I think
SELECT distinct COUNT(S.INVOICENO) AS LESSTWO , YEAR(S.invoicedate)As YER,Month(S.invoicedate)as MNTH
  FROM SALESDATA S where  S.BILLINGTYPE=''INVOICE'' AND (S.invoicedate between ''4-1-2009'' and  ''4-30-2010'')
                          AND S.TAXABLEAMT <=2000
                   GROUP BY YEAR(S.invoicedate) ,Month(S.invoicedate) ORDER BY YEAR(S.invoicedate) ,Month(S.invoicedate)



这将解决问题


如果仍然引起错误,请随时与表的确切结构以及您所需要的输出伴随的错误联系

问候
Vipin Kumar Mallaya G



this will solve the issue


If there is still causing the error then feel free to contact along with the table exact structure and the error you got along with the output needed

regards
Vipin Kumar Mallaya G


如果是我,我会在多个查询中执行,并将每个结果存储在自己的变量中.这样做还可以使sql更具可维护性,甚至比尝试将所有内容打包到单个查询中还要快.
If it were me, I''d do it in multiple queries, and store each result in its own variable. That would also make the sql more maintainable, and maybe even faster than trying to wrangle all that into a single query.


这篇关于如何在SQL Server 2005中使用单个查询为不同条件计算列数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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