0

我有一个类似于下面的表...

我需要添加具有完全相同日期的每一行的值。(例如前两行将等于'5',接下来的两行,'5',接下来的两行,'8')

StoretCode   LocName    ParName   DateTime       Value
95010-A-9    AB 901_4   On Line   201305160000   1
NULL         AB 905_8   On Line   201305160000   4
95010-A-9    AB 901_4   On Line   201305170000   2
NULL         AB 905_8   On Line   201305170000   3
95010-A-9    AB 901_4   On Line   201305180000   4
NULL         AB 905_8   On Line   201305180000   4  

结果也应该有一个新的 LocName...所以它应该看起来像...

StoretCode   LocName  ParName  DateTime      Value
95010-A-9    AERBAS   On Line  201305160000  5
95010-A-9    AERBAS   On Line  201305170000  5
95010-A-9    AERBAS   On Line  201305180000  8

我试过了...

Select 'AB 901_4','AB 905_8', Sum(Value)
from dbo.DATA  

但结果与预期的一样......一行总共有所有行......

也试过了......

Select 'AB 901_4','AB 905_8', Sum(Value)
from dbo.DATA
Where LocName, DateTime = LocName, DateTime

但结果是……

消息 4145,级别 15,状态 1,第 3 行
在预期条件的上下文中指定的非布尔类型表达式,靠近“,”。

尝试了其他几个,但结果类似于上面的非布尔味精......

4

2 回答 2

1
Select StoretCode , 'AERBAS' as LocName , ParName , DATA.DateTime, datecount.count as [value]
from dbo.DATA 
join ( select DateTime, count(*) as count from dbo.DATA group by DateTime ) datecount 
  on data.DateTime = datecount.DateTime 
where date.StoretCode is not null  

如果您想要 LocName 的硬编码值,则为 'LocName'

于 2013-08-09T18:19:32.917 回答
1

也许沿着这些思路?

Select 
  max(StoretCode), 
  'AERBAS' as Locname, 
  max(ParName), 
  DateTime, 
  Sum(Value)
from dbo.DATA
group by DateTime
order by DateTime
于 2013-08-09T19:11:20.813 回答