Microsoft SQL Server查询改进

马卢达塞克

有没有机会提高下面的查询速度:

select distinct
    a.InvoiceAcc,
    (select top 1 b.CustomerName 
     from dbo.tblsales as b 
     where b.InvoiceAcc = a.InvoiceAcc),
    (select sum(b.SalesValue) 
     from dbo.tblsales as b 
     where b.InvoiceAcc = a.InvoiceAcc and b.Month = '2016-1') as [01],
    (select sum(b.SalesValue) 
     from dbo.tblsales as b 
     where b.InvoiceAcc = a.InvoiceAcc and b.Month = '2016-2') as [02],
    (select sum(b.SalesValue) 
     from dbo.tblsales as b 
     where b.InvoiceAcc = a.InvoiceAcc and b.Month = '2016-3') as [03],
    (select sum(b.SalesValue) 
     from dbo.tblsales as b 
     where b.InvoiceAcc = a.InvoiceAcc and b.Month = '2016-4') as [04],
    (select sum(b.SalesValue) 
     from dbo.tblsales as b 
     where b.InvoiceAcc = a.InvoiceAcc and b.Month = '2016-5') as [05],
    (select sum(b.SalesValue) from dbo.tblsales as b where b.InvoiceAcc = a.InvoiceAcc and b.Month = '2016-6') as [06],
    (select sum(b.SalesValue) from dbo.tblsales as b where b.InvoiceAcc = a.InvoiceAcc and b.Month = '2016-7') as [07],
    (select sum(b.SalesValue) from dbo.tblsales as b where b.InvoiceAcc = a.InvoiceAcc and b.Month = '2016-8') as [08],
    (select sum(b.SalesValue) from dbo.tblsales as b where b.InvoiceAcc = a.InvoiceAcc and b.Month = '2016-9') as [09],
    (select sum(b.SalesValue) from dbo.tblsales as b where b.InvoiceAcc = a.InvoiceAcc and b.Month = '2016-10') as [10],
    (select sum(b.SalesValue) from dbo.tblsales as b where b.InvoiceAcc = a.InvoiceAcc and b.Month = '2016-11') as [11],
    (select sum(b.SalesValue) from dbo.tblsales as b where b.InvoiceAcc = a.InvoiceAcc and b.Month = '2016-12') as [12]
from
    dbo.tblsales as a 
group by 
    a.InvoiceAcc, CustomerName

在SQL Server中将参数月发送为2016年InvoiceAcc(非必需)的视图是什么

非常感谢。

霍根

根据您的示例,您需要

select 
  InvoiceAcc,
  CustomerName,
  sum(CASE WHEN [Month] = '2016-1' then salesvalue else 0 end) as [01],
  sum(CASE WHEN [Month] = '2016-2' then salesvalue else 0 end) as [02],
  sum(CASE WHEN [Month] = '2016-3' then salesvalue else 0 end) as [03],
  sum(CASE WHEN [Month] = '2016-4' then salesvalue else 0 end) as [04],
  sum(CASE WHEN [Month] = '2016-5' then salesvalue else 0 end) as [05],
  sum(CASE WHEN [Month] = '2016-6' then salesvalue else 0 end) as [06],
  sum(CASE WHEN [Month] = '2016-7' then salesvalue else 0 end) as [07],
  sum(CASE WHEN [Month] = '2016-8' then salesvalue else 0 end) as [08],
  sum(CASE WHEN [Month] = '2016-9' then salesvalue else 0 end) as [09],
  sum(CASE WHEN [Month] = '2016-10' then salesvalue else 0 end) as [10],
  sum(CASE WHEN [Month] = '2016-11' then salesvalue else 0 end) as [11],
  sum(CASE WHEN [Month] = '2016-12' then salesvalue else 0 end) as [12]
from dbo.tblsales 
group by InvoiceAcc, CustomerName

我确实发现测试(例如Month = '2016-1')很奇怪-我期望像这样的日期字段和测试MONTH(invoiceDate) = 1

我还认为,在sql中使用“ distinct”通常表示查询效果不佳-从设计良好的模型中正确设计的查询很少需要使用“ distinct”。


如果要删除其他名称,最好的方法是:

select 
  InvoiceAcc,
  CustomerName,
  sum(CASE WHEN [Month] = '2016-1' then salesvalue else 0 end) as [01],
  sum(CASE WHEN [Month] = '2016-2' then salesvalue else 0 end) as [02],
  sum(CASE WHEN [Month] = '2016-3' then salesvalue else 0 end) as [03],
  sum(CASE WHEN [Month] = '2016-4' then salesvalue else 0 end) as [04],
  sum(CASE WHEN [Month] = '2016-5' then salesvalue else 0 end) as [05],
  sum(CASE WHEN [Month] = '2016-6' then salesvalue else 0 end) as [06],
  sum(CASE WHEN [Month] = '2016-7' then salesvalue else 0 end) as [07],
  sum(CASE WHEN [Month] = '2016-8' then salesvalue else 0 end) as [08],
  sum(CASE WHEN [Month] = '2016-9' then salesvalue else 0 end) as [09],
  sum(CASE WHEN [Month] = '2016-10' then salesvalue else 0 end) as [10],
  sum(CASE WHEN [Month] = '2016-11' then salesvalue else 0 end) as [11],
  sum(CASE WHEN [Month] = '2016-12' then salesvalue else 0 end) as [12]
from (
  SELECT InvoiceAcc, CustomerName, Month, salesvalue,
         ROW_NUMBER() OVER (PARTITION BY InvoiceAcc ORDER BY CustomerName) AS RN
         FROM tblsales
) x
WHERE RN=1
group by InvoiceAcc, CustomerName

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Microsoft SQL Server查询改进

来自分类Dev

无法在Microsoft SQL Server中运行查询

来自分类Dev

Microsoft SQL Server - 打开查询其中 > 日期

来自分类Dev

如何使用多个“ CASE”改进此SQL Server查询?

来自分类Dev

SQL查询:如何改进?

来自分类Dev

SQL:改进选择查询

来自分类Dev

SQL如何从Microsoft SQL Server查询文件中获取表?

来自分类Dev

Microsoft SQL Server Management Studio-查询结果为文本

来自分类Dev

如何将参数传递给Microsoft SQL Server查询?

来自分类Dev

查询Microsoft SQL Server 2012 70-461书

来自分类Dev

使用Datetime Convert在Microsoft SQL Server中进行查询

来自分类Dev

熊猫阅读SQL查询改进

来自分类Dev

SQL Select查询性能改进

来自分类Dev

SQL查询性能改进的建议

来自分类Dev

SQL Self子查询改进

来自分类Dev

SQL Select查询性能改进

来自分类Dev

如何改进此 SQL 查询?

来自分类Dev

Microsoft SQL Server,错误:87

来自分类Dev

Microsoft SQL Server列大小

来自分类Dev

Microsoft SQL Server做什么?

来自分类Dev

Microsoft SQL Server,视图转换

来自分类Dev

Microsoft SQL Server列大小

来自分类Dev

TSQL 帮助 Microsoft SQL Server

来自分类Dev

如何以良好的性能改进查询以使用SQL Server拆分字符串

来自分类Dev

使用CTE查询优化SQL Server 2008 R2进行改进

来自分类Dev

如何以良好的性能改进我的查询以使用SQL Server拆分字符串

来自分类Dev

php查询,隐藏内容Microsoft SQL

来自分类Dev

改进用于 xpath 评估的 SQL 查询

来自分类Dev

如何修复Microsoft SQL Server,错误:262?