Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Query Help Expand / Collapse
Author
Message
Posted Wednesday, July 3, 2013 11:06 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, June 23, 2014 7:34 AM
Points: 7, Visits: 34
Thanks for reading :D

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.
Post #1470300
Posted Thursday, July 4, 2013 12:46 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:21 PM
Points: 13,639, Visits: 10,533
akamorse (7/3/2013)
Thanks for reading :D

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?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1470320
Posted Thursday, July 4, 2013 1:14 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, June 23, 2014 7:34 AM
Points: 7, Visits: 34
Oh im a idiot

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

Post #1470329
Posted Thursday, July 4, 2013 3:39 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:21 PM
Points: 13,639, Visits: 10,533
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?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1470367
Posted Thursday, July 4, 2013 9:42 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, June 23, 2014 7:34 AM
Points: 7, Visits: 34
Thank you for your response :D

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 :D
Post #1470551
Posted Thursday, July 4, 2013 11:48 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:21 PM
Points: 13,639, Visits: 10,533
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?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1470560
Posted Thursday, July 4, 2013 11:53 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, June 23, 2014 7:34 AM
Points: 7, Visits: 34
each meter report watthours every 30mins, so in a 30 day month there are 1440 entries. :D

So i Count the TimeStamp colum to get 1440.

Sorry i did a really bad job at posting my question, im new to this :D
Post #1470564
Posted Thursday, July 4, 2013 11:58 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:21 PM
Points: 13,639, Visits: 10,533
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?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1470567
Posted Friday, July 5, 2013 12:10 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, June 23, 2014 7:34 AM
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

Post #1470574
Posted Friday, July 5, 2013 12:18 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:21 PM
Points: 13,639, Visits: 10,533
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?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1470579
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse