0

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...

3
  • Where does the new LocName come from? Is it a constant value that all rows will get or does it depend on the original value? Commented Aug 9, 2013 at 18:31
  • I'm sorry...I will need to create the new LocName and delete the rows that were added together... Commented Aug 9, 2013 at 18:44
  • Why don't you group by datetime. Select 'AB 901_4','AB 905_8', Sum(Value) from dbo.DATA Group by [DateTime] Commented Aug 9, 2013 at 18:59

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

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  

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

5 Comments

Thanks, but that groups everything by StoretCode and removed the 'Value' column name...the actual table I am working on has 74 distinct StoretCodes...I want to add the rows with the LocNames I mentioned.
Don't know how to help you as this answers the question as I understand it.
How is this output different from the desired output?
To be a little more specific...I am actually working on a table with 74 StoretCodes (including the NULL), 95 LocNames (so many have NULL StoretCodes)...I need to combine the two LocNames I mentioned in my example...
Against the posted sample data does this query produce the correct results or not? Not clear to me what combine the two LocNames means.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.