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