SQL总和然后插入结果 [英] SQL Sum totals then inserting results

查看:124
本文介绍了SQL总和然后插入结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何在SQL中的一个表中添加多个字段吗?

例如-表1-

 ------------------------------------------ ---
| FactoryID | AreaID | ReasonCode |时间(分钟)|
| ---------- || -------- | ------------ | ----------- |
| 116 | A1 | 14 | 12 |
| 116 | A1 | 62 | 9 | 



我想在表1中添加时间

所以

SQL =

 选择 SUM(时间(Mins))其中 FactoryID = '  116' '  ------------------------------------
| FactoryID | AreaID | TotalTime(Mins)|
------------------------------------
| 116 | A1 | 18 | 



我尝试使用

 插入 插入表2(FactoryID,AreaID,TotalTime(Mins))
选择 SUM(时间(分钟))位置 FactoryID = '  116'  AreaID = '  A1' 



但无济于事

非常遗憾,如果这看起来很基础,但是欢迎任何帮助或替代方法.

解决方案

Try

 插入 插入表2(FactoryID,AreaID,TotalTime(Mins))
选择 FactoryID,AreaID,SUM(时间(分钟))来自表1
其中 FactoryID = '  116'  AreaID = '  A1'
 通过 FactoryID,AreaID 


您必须为插入中的所有字段(在这种情况下为三个字段)提供值

Does anyone know how to sum a table in sql with multiple fields in.

For Example - Table 1 -

---------------------------------------------
|FactoryID | AreaID | ReasonCode | Time(Mins)|
|----------|--------|------------|-----------|
| 116      |  A1    |  14        | 12        |
| 116      |  A1    |  62        | 9         |



i want to add the time in Table 1

so

SQL =

Select SUM(Time(Mins)) where FactoryID ='116' and AreaID = 'A1'



and then pass this to Table 2 and get the results below

------------------------------------
|FactoryID |AreaID |TotalTime(Mins)|
------------------------------------
| 116      | A1    | 18            |



i have tried using

Insert Into Table2 (FactoryID,AreaID,TotalTime(Mins))
Select SUM(Time(Mins)) where FactoryID ='116' and AreaID = 'A1'



but to no avail

really sorry if this appears as highly basic, but any help or alternative approach would be welcome.

解决方案

Try

Insert into Table2 (FactoryID, AreaID, TotalTime(Mins)
Select FactoryID, AreaID, SUM(Time(Mins)) From Table1
Where FactoryID = '116' And AreaID = 'A1'
Group by FactoryID, AreaID


You have to give values for all fields in the insert (three fields in this case)


这篇关于SQL总和然后插入结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆