user2668701
user2668701

Reputation: 3

T-SQL select statement...SUM and HAVING issue

I have a table similar to below...

I need to add the values of each row with the exact same date. (For example the first two rows will equal '5', next two, '5', next two, '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  

The result should also have a new LocName...so it should look like...

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

I have tried...

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

but the results were as expected...one row with a total of all rows...

Also tried.....

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

but result was...

Msg 4145, Level 15, State 1, Line 3
An expression of non-boolean type specified in a context where a condition is expected, near ','.

tried several others but result was similar to non-boolean msg above...

Upvotes: 0

Views: 123

Answers (2)

Hellion
Hellion

Reputation: 1740

Perhaps something along these lines?

Select 
  max(StoretCode), 
  'AERBAS' as Locname, 
  max(ParName), 
  DateTime, 
  Sum(Value)
from dbo.DATA
group by DateTime
order by DateTime

Upvotes: 1

paparazzo
paparazzo

Reputation: 45096

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  

If you want a hard coded value for LocName then 'LocName'

Upvotes: 1

Related Questions