sql中的sumProduct
我正在服务器上的表中尝试从excel实现sumproductsql中的sumProduct,sql,sql-server,sql-server-2008,sql-server-2012,window-functions,Sql,Sql Server,Sql Server 2008,Sql Server 2012,Window Functions,我正在服务器上的表中尝试从excel实现sumproduct select * into #myTable2 from #myTable1 select a, b, c, d, e, ( select (c * e)/100*3423) from #myTable1 t1 inner join #myTable t2 on t1.b = t2.b where b like 'axr%' ) as sumProduct from #myTable1 但这不太管用。找不到错误,可能是我太累了
select *
into #myTable2
from #myTable1
select
a,
b,
c,
d,
e,
(
select (c * e)/100*3423) from #myTable1 t1
inner join #myTable t2
on t1.b = t2.b
where b like 'axr%'
) as sumProduct
from #myTable1
但这不太管用。找不到错误,可能是我太累了或者错过了
编辑:
样本数据和期望结果
只会提到重要的栏目
c e b a sumProduct
2 4 axr1 2012.03.01 2*4 + 3*8
3 8 axr3 2012.03.01 2*4 + 3*8
7 5 axr23 2011.01.01 7*5 + 3*2
3 2 axr34 2011.01.01 7*5 + 3*2
编辑2:
我需要一些语法方面的帮助。我正试图重写这一部分:
select (c * e)/100*3423) from #myTable1 t1
inner join #myTable t2
on t1.b = t2.b
where b like 'axr%'
) as sumProduct
from #myTable1
作为
无法获得正确的语法,但应该这样工作
编辑3:
让它像这样工作:
case
when b like 'axr%' then
(sum(c*e)/100*3423)end as sumProduct
在代码的末尾
group by --had an error without this
a,b,c,d,e
我怎么能对每个日期都这样做呢?假设日期是“a”列或任何名称。我如何在上面的代码中通过a合并分区
想要像这样的东西吗
case
when b like 'axr%' then
(sum(c*e)/100*3423 over (partition by a))end as sumProduct
在SQL中,求和乘积的语法非常简单:
select sum(c * e)
from #mytable1;
我不太清楚这是如何应用于您的查询的,您的查询似乎还有其他逻辑
编辑:
您需要一个窗口函数:
select t.*,
sum(c*e) over (partition by a)
from #mytable1;
在SQL中,求和乘积的语法非常简单:
select sum(c * e)
from #mytable1;
我不太清楚这是如何应用于您的查询的,您的查询似乎还有其他逻辑
编辑:
您需要一个窗口函数:
select t.*,
sum(c*e) over (partition by a)
from #mytable1;
老实说,我还不明白你的代码。但是假设您有两个表,其中包含值和唯一的连接Id,那么我的实现可能会帮助您获得灵感:
-- create new DB or point to an existing one
use [test];
CREATE TABLE [dbo].[table1](
[id] [int] NOT NULL,
[value] [int] NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[table2](
[id] [int] NOT NULL,
[value] [int] NOT NULL
) ON [PRIMARY]
GO
insert into table1 values (1, 5), (2, 10);
insert into table2 values (1, 2), (2, 4);
select sum(P.products) as sumproduct from
(select (t1.value * t2.value) as products from table1 as t1 inner join table2 as t2 on t1.id = t2.id) as P
老实说,我还不明白你的代码。但是假设您有两个表,其中包含值和唯一的连接Id,那么我的实现可能会帮助您获得灵感:
-- create new DB or point to an existing one
use [test];
CREATE TABLE [dbo].[table1](
[id] [int] NOT NULL,
[value] [int] NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[table2](
[id] [int] NOT NULL,
[value] [int] NOT NULL
) ON [PRIMARY]
GO
insert into table1 values (1, 5), (2, 10);
insert into table2 values (1, 2), (2, 4);
select sum(P.products) as sumproduct from
(select (t1.value * t2.value) as products from table1 as t1 inner join table2 as t2 on t1.id = t2.id) as P
样本数据和期望结果将真正澄清您的表格。从选择c*e/100*3423中删除最后一个参数样本数据和期望结果将真正澄清您的表格。从选择c*e/100*3423中删除最后一个参数请查看样本数据,也许这有助于你理解我需要写的东西,比如:当t.b像'axr%'时,sumt.c*t.e/100*3234作为我表格中的sumProduct结束,我看不到我的编辑@Gordon谢谢您请查看示例数据,也许这有助于您理解我需要编写的内容,如:当t.b如“axr%”时,sumt.c*t.e/100*3234结束为myTable中的sumProduct时,请参见我的编辑@Gordon Thank You我想对所有日期的“axr%”形式的两列值进行求和。我使用的是a、b、c……等等,因为它们所包含的内容并不相关,我可能有一个模棱两可的列名错误。在我的代码中,您可以看到我有一个表,我正试图将其自身连接起来,因为我写的第一件事是:从myTable1中选择*进入myTable2。谢谢你的代码,这很有帮助。我想对所有日期的两列值进行求和,其形式为“axr%”。我使用的是a、b、c……等等,因为它们所包含的内容并不相关,我可能有一个模棱两可的列名错误。在我的代码中,您可以看到我有一个表,我正试图将其自身连接起来,因为我写的第一件事是:从myTable1中选择*进入myTable2。谢谢你的代码,它很有帮助