首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

insert into查询有关问题--!--谢谢

2012-01-06 
insert into查询问题--在线等!--多谢!需要插入的表:INSERTINTOsysdba.account( ordertotal05 )Values()数

insert into查询问题--在线等!--多谢!
需要插入的表:
INSERT   INTO   sysdba.account
      ( 'ordertotal05 ')
Values()

数据来源:
select   sum(sa.ordertotal)   as   o   from   account   a
inner   join   salesorder   sa   on   a.accountid=sa.accountid
where   orderdate> = '2005-1-1 '   and   orderdate <= '2005-12-31 '
group   by   a.accountID
having   sum(sa.ordertotal) <=15024

需要插入数据的表和数据来源表都是account表,关键字是accountID
请问如何写sql???

多谢帮忙!!

[解决办法]
楼主这么做是什么意思?
能不能举些例子.感觉好像不对.
INSERT INTO sysdba.account(accountID,ordertotal05)
select a.accountID,sum(sa.ordertotal) as o from account a
inner join salesorder sa on a.accountid=sa.accountid
where orderdate> = '2005-1-1 ' and orderdate <= '2005-12-31 '
group by a.accountID
having sum(sa.ordertotal) <=15024

[解决办法]
感觉应该是update,不是insert.
[解决办法]
那你应该不是insert,而且update吧.

update account
set ordertotal05 = t.o
from account,(select a.accountID,sum(sa.ordertotal) as o from account a
inner join salesorder sa on a.accountid=sa.accountid
where orderdate> = '2005-1-1 ' and orderdate <= '2005-12-31 '
group by a.accountID
having sum(sa.ordertotal) <=15024)t
where account.accountID=t.accountID
[解决办法]
1.插入表(此种情况为:当sysdba.account与acount非同一表时)

INSERT INTO sysdba.account(accountID,ordertotal05)
select a.accountID,sum(sa.ordertotal) as o from account a
inner join salesorder sa on a.accountid=sa.accountid
where orderdate> = '2005-1-1 ' and orderdate <= '2005-12-31 '
group by a.accountID
having sum(sa.ordertotal) <=15024

2.更新表(此种情况为:当sysdba.account与acount同一表时)
update account
set ordertotal05 = t.o
from account,(select a.accountID,sum(sa.ordertotal) as o from account a
inner join salesorder sa on a.accountid=sa.accountid
where orderdate> = '2005-1-1 ' and orderdate <= '2005-12-31 '
group by a.accountID
having sum(sa.ordertotal) <=15024)t
where account.accountID=t.accountID


热点排行