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 9271627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:40:05+00:00 2026-06-18T15:40:05+00:00

I use a Linq-2-Sql at the moment in order to perform a fairly complicated

  • 0

I use a Linq-2-Sql at the moment in order to perform a fairly complicated query. It is however very inefficient and results in multiple round trips being made to the server.

Using some joins I have managed to get the data into a format like this:

Sensor -- Timestamp        -- Value
A      -- 12/02/2013 09:00 -- 10.4
A      -- 12/02/2013 10:00 -- 10.3
A      -- 12/02/2013 11:00 -- 10.1
B      -- 12/02/2013 09:00 -- 15.3
B      -- 12/02/2013 10:00 -- 16.4
B      -- 12/02/2013 11:00 -- 15.4

I desire this in an output like

TimeStamp        -- SensorA -- SensorB
12/02/2013 09:00 -- 10.4    -- 15.3
12/02/2013 10:00 -- 10.3    -- 16.4
12/02/2013 11:00 -- 10.1    -- 15.4

I am guessing I need a dynamic pivot? Can anybody help point me in the right direction?

EDIT – Obviously there are more than the 2 sensors…ideally I need this to be able to scale out in terms of columns. I don’t even know if SQL Server can do this so any help is appreciated.

  • 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-18T15:40:06+00:00Added an answer on June 18, 2026 at 3:40 pm

    You will want to use the PIVOT function to transform the data.

    If you have a limited number of Sensor values, then you can hard-code the query:

    select *
    from
    (
      select sensor, timestamp, value
      from yourtable
    ) src
    pivot
    (
      max(value)
      for sensor in (A, B)
    ) piv
    

    See SQL Fiddle with Demo

    But if the values are unknown then you will want to implement dynamic SQL:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME(Sensor) 
                        from yourtable  -- table containing Sensor values
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT timestamp, ' + @cols + ' from 
                 (
                    select sensor, timestamp, value
                    from yourtable
                ) x
                pivot 
                (
                    max(value)
                    for sensor in (' + @cols + ')
                ) p '
    
    execute(@query)
    

    See SQL Fiddle with Demo

    Both will give the result:

    |           TIMESTAMP |    A |    B |
    -------------------------------------
    | 2013-12-02 09:00:00 | 10.4 | 15.3 |
    | 2013-12-02 10:00:00 | 10.3 | 16.4 |
    | 2013-12-02 11:00:00 | 10.1 | 15.4 |
    

    Note, you will replace the yourtable with your current query.

    Edit #1, if you want to filter by date, you could use the following dynamic sql:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX),
        @startdate datetime,
        @enddate datetime
    
    set @startdate = '2013-12-01'
    set @enddate = '2013-12-03'
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME(Sensor) 
                        from yourtable
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT timestamp, ' + @cols + ' from 
                 (
                    select sensor, timestamp, value
                    from yourtable
                    where timestamp >= '''+convert(varchar(10), @startdate, 120)+'''
                      and timestamp <= '''+convert(varchar(10), @enddate, 120)+'''
                ) x
                pivot 
                (
                    max(value)
                    for sensor in (' + @cols + ')
                ) p '
    
    
    execute(@query)
    

    See SQL Fiddle with Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use linq to sql for my project (very short deadline), and
I'm trying to use linq-to-sql, and this is all very new to me. I
I'm trying to use Linq to SQL to search a text column for multiple
I use Linq To Sql Join Query in DAL layer, How can I define
How can I use LINQ to SQL to query a table with an XML
I use LINQ-SQL as my DAL, I then have a project called DB which
Trying to use Linq to SQL for a small project I'm working on at
I'm trying to use LINQ to SQL to select a few specific columns from
I'd like to use Linq to SQL in my windows form application. The data
I'm trying to use LINQ to SQL Server in C#/.NET on Visual Studio 2010.

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.