我正在尝试在服务器上的表中实现the产品(从excel)。
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*2EDIT2:我需要一些语法方面的帮助。我想重写这部分:
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作为
case
when t.b like 'axr%' then
(sum(t.c * t.e) /100*3234) end as sumProduct from #myTable t不能正确的语法,但应该像那样工作
编辑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‘或任何名称)。如何将 over (partition by a)合并到上面的代码中?
想要这样的东西
case
when b like 'axr%' then
(sum(c*e)/100*3423 over (partition by a))end as sumProduct发布于 2015-09-11 11:19:29
和积的语法在SQL中非常简单:
select sum(c * e)
from #mytable1;我不太清楚这如何应用于您的查询,因为查询中似乎有其他逻辑。
编辑:
你想要一个窗口函数:
select t.*,
sum(c*e) over (partition by a)
from #mytable1;发布于 2015-09-11 11:36:03
我真的不明白你的代码。但是,假设您有两个具有值的表和一个惟一的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 Phttps://stackoverflow.com/questions/32522130
复制相似问题