Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7962269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:17:38+00:00 2026-06-04T05:17:38+00:00

I am partly on my way to solving this, but have hit a stumbling

  • 0

I am partly on my way to solving this, but have hit a stumbling block, which I think can be solved with pivot(s).

I have the following SQL query, combining two temporary table variables (may change these to temporary tables, as I think performance maybe come a problem as they will be hit a large number of times):

SELECT MeterId, MeterDataOutput.BuildingId, MeterDataOutput.Value, 
            MeterDataOutput.TimeStamp, UtilityId, SnapshotId 
        FROM @MeterDataOutput as MeterDataOutput INNER JOIN @InsertOutput AS InsertOutput 
                ON MeterDataOutput.BuildingId = InsertOutput.BuildingId
                AND MeterDataOutput.[Timestamp] = InsertOutput.[TimeStamp]

This produces the following table:

Step 1

I have then modified the query to group by BuildingId, SnapshotId, Timestamp, Utility and applied the SUM() function to aggregate the Value field (and dropped the MeterId as its not required), as follows:

SELECT MeterDataOutput.BuildingId, SUM(MeterDataOutput.Value) AS Value, MeterDataOutput.TimeStamp, UtilityId, SnapshotId 
FROM @MeterDataOutput as MeterDataOutput 
INNER JOIN @InsertOutput AS InsertOutput 
    ON MeterDataOutput.BuildingId = InsertOutput.BuildingId
    AND MeterDataOutput.[Timestamp] = InsertOutput.[TimeStamp]
GROUP BY MeterDataOutput.BuildingId, MeterDataOutput.TimeStamp, UtilityId, SnapshotId

This query the provides me with the following table:

Step 2

Now the bit I’m having trouble with is transforming the UtilityId values to columns, and placing the values from the Value field under each column. I.e:

My problem

For reference buildingId, Timestamp, Snapshot and Value are variable. UtilityId value 6 is always ‘Electricity’, 7 is always ‘Gas’ and 8 is always ‘Water’.

I’m actually starting to get the hand of the SQL lark 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:17:40+00:00Added an answer on June 4, 2026 at 5:17 am

    Maybe something like this:

    SELECT
        pvt.BuildingId,
        pvt.SnapshotId,
        pvt.TimeStamp,
        pvt.[6] AS Electricity,
        pvt.[7] AS Gas,
        pvt.[8] AS Water
    FROM
        (
        SELECT 
            MeterDataOutput.BuildingId, 
            MeterDataOutput.Value, 
            MeterDataOutput.TimeStamp, 
            UtilityId, 
            SnapshotId 
        FROM @MeterDataOutput as MeterDataOutput 
        INNER JOIN @InsertOutput AS InsertOutput 
            ON MeterDataOutput.BuildingId = InsertOutput.BuildingId
            AND MeterDataOutput.[Timestamp] = InsertOutput.[TimeStamp]
        ) AS SourceTable
    PIVOT
    (
        SUM(Value)
        FOR UtilityId IN ([6],[7],[8])
    ) AS pvt
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is partly an extension to a Previous Question of mine: I can now
I'm stuck in a script I have to write and can't find a way
I ask this partly because I want to know the best practice way of
I'm sure there's more than one way to do this, but what's a good
Partly following on from this question. Hopefully the example speaks for itself: there's a
This is partly related to this SO question . I have two classes, both
I have a NET 2.0 Winforms app based partly on this code . It
I know this question has been partly answered here on S.O. , but there
I have the following code, partly in Java, partly in Scala: Java: public interface
I have a partly inherited web application in PHP and after poking around with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.