SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query Help


Query Help

Author
Message
akamorse
akamorse
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 34
Thanks for reading BigGrin

Im trying to create a view, and im having trouble doing one function. I have a table with the following

ID timestamp value
1, 04/07/2013 00:00:00, 10
1, 04/07/2013 00:30:00, 11
2, 04/07/2013 00:00:00, 11
2, 04/07/2013 00:30:00, 11

I need the following

ID Count value
1, 2, 21
2, 2, 22

I using the below to get my results when the table only has one month range.

SELECT ID, COUNT(TimeStamp) AS Count, SUM(Vale) AS Value
FROM mytable
GROUP BY ID
Where (ID = '1') OR
(ID = '2') OR

But now i need to add multiple months, and im struggling. Any help would be greatly appreciated.
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27493 Visits: 13268
akamorse (7/3/2013)
Thanks for reading BigGrin

Im trying to create a view, and im having trouble doing one function. I have a table with the following

ID timestamp value
1, 04/07/2013 00:00:00, 10
1, 04/07/2013 00:30:00, 11
2, 04/07/2013 00:00:00, 11
2, 04/07/2013 00:30:00, 11

I need the following

ID Count value
1, 2, 21
2, 2, 22

I using the below to get my results when the table only has one month range.

SELECT ID, COUNT(TimeStamp) AS Count, SUM(Vale) AS Value
FROM mytable
GROUP BY ID
Where (ID = '1') OR
(ID = '2') OR

But now i need to add multiple months, and im struggling. Any help would be greatly appreciated.


This would be sufficient:


SELECT ID, COUNT(TimeStamp) AS Count, SUM(Vale) AS Value
FROM mytable
GROUP BY ID



There's no need for a WHERE clause. By the way, not a very effective ID if it has duplicates.


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
akamorse
akamorse
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 34
Oh im a idiot Doze

Amend my previous.

SELECT MeterID, COUNT(TimeStamp) AS Count, SUM(WattHours) AS WattHours
FROM dbo.NewvMData
GROUP BY MeterID
HAVING (MeterID = '203201582') OR
(MeterID = '203201588') OR

As i have Meters in the table i dont wish results returned. Previously i only have one months data in the table, now i have three.

so the previous results were

203201582 1440 137497219.28343752
203201588 1440 18056670.550566405
204001454 1440 919623.39525939862

Where as now im looking to return the Months totals seperate,

203201582 1440 137497219.28343752 01/06/2013
203201588 1440 18056670.550566405 01/06/2013
204001454 1440 919623.39525939862 01/06/2013
203201582 1440 137497219.28343752 01/05/2013
203201588 1440 18056670.550566405 01/05/2013
204001454 1440 919623.39525939862 01/05/2013


My previous view would total all the months and return the count with three times the amount.

Thank you for you reply, sorry about not being totally correct with my first post.

Adrian
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27493 Visits: 13268
Simply add the month column in the GROUP BY clause.
By the way, using HAVING on a column on which you group - thus not an aggregate - is the same as using WHERE.


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
akamorse
akamorse
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 34
Thank you for your response BigGrin

I tried Adding the Month to the Group by, and now its returning

Meter ID Count Watthours
203201582 1 55226.30078125
203201582 1 55330.86328125
203201582 1 52783.71484375
203201582 1 53696.5625
203201582 1 53230.19140625
203201582 1 54420.70703125

The data im querying looks like
MeterID TimeStamp WattHours
203201582 5/05/2013 12:00:00 AM 57736.625
203201582 5/05/2013 12:30:00 AM 57014.62890625
203201582 5/05/2013 1:00:00 AM 56760.8828125
203201582 5/05/2013 1:30:00 AM 56346.12109375
203201582 5/05/2013 2:00:00 AM 58058.109375
203201582 5/05/2013 2:30:00 AM 57952.47265625

I would like to return
Meter, Count, Watthours , Month
203201582, 1440, 137497219.28343752, JUN

So instead of counting the totaly halfhour trends and return the total count and then summing the watthours for the month, its just returning the count as i 1440 times. I need the query to return a extra colum.

Thank you for your responses BigGrin
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27493 Visits: 13268
How do you calculate the 1440?


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
akamorse
akamorse
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 34
each meter report watthours every 30mins, so in a 30 day month there are 1440 entries. BigGrin

So i Count the TimeStamp colum to get 1440.

Sorry i did a really bad job at posting my question, im new to this BigGrin
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27493 Visits: 13268
OK, so just add COUNT(Timestamp) to your query.

For next time, read the link in my signature about how to post questions. It will get you answers more accurately and more quicker.


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
akamorse
akamorse
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 34
OK, it seems i must have confused the whole thing. I managed to just solve it

SELECT MeterID, COUNT(TimeStamp) AS Count, SUM(WattHours) AS WattHours, MONTH(TimeStamp) AS Month
FROM BillingHistoryvMData
GROUP BY MeterID, MONTH(TimeStamp)

As i wanted to count the timestamp column by month and them sum the months watthours.

Not been one of the best weeks regarding concentration, pregnant partner.

Sorry and thank you
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27493 Visits: 13268
akamorse (7/5/2013)
OK, it seems i must have confused the whole thing. I managed to just solve it

SELECT MeterID, COUNT(TimeStamp) AS Count, SUM(WattHours) AS WattHours, MONTH(TimeStamp) AS Month
FROM BillingHistoryvMData
GROUP BY MeterID, MONTH(TimeStamp)

As i wanted to count the timestamp column by month and them sum the months watthours.

Not been one of the best weeks regarding concentration, pregnant partner.

Sorry and thank you



No need to apologize. :-)
Wait until after the birth, then things start to get hectic. :-)


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search