Query Help

  • Thanks for reading 😀

    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.

  • akamorse (7/3/2013)


    Thanks for reading 😀

    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.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • 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 1440137497219.28343752

    203201588 144018056670.550566405

    204001454 1440919623.39525939862

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

    203201582 1440137497219.28343752 01/06/2013

    203201588 144018056670.550566405 01/06/2013

    204001454 1440919623.39525939862 01/06/2013

    203201582 1440137497219.28343752 01/05/2013

    203201588 144018056670.550566405 01/05/2013

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

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

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Thank you for your response 😀

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

    Meter ID Count Watthours

    203201582 155226.30078125

    203201582 155330.86328125

    203201582 152783.71484375

    203201582 153696.5625

    203201582 153230.19140625

    203201582 154420.70703125

    The data im querying looks like

    MeterID TimeStamp WattHours

    203201582 5/05/2013 12:00:00 AM57736.625

    203201582 5/05/2013 12:30:00 AM57014.62890625

    203201582 5/05/2013 1:00:00 AM56760.8828125

    203201582 5/05/2013 1:30:00 AM56346.12109375

    203201582 5/05/2013 2:00:00 AM58058.109375

    203201582 5/05/2013 2:30:00 AM57952.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 😀

  • How do you calculate the 1440?

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • each meter report watthours every 30mins, so in a 30 day month there are 1440 entries. 😀

    So i Count the TimeStamp colum to get 1440.

    Sorry i did a really bad job at posting my question, im new to this 😀

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

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • 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

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

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply