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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:35:51+00:00 2026-05-25T13:35:51+00:00

This situation seems like it should be a simple task but I can’t come

  • 0

This situation seems like it should be a simple task but I can’t come up with the solution. Imagine eight columns, four of which are bit columns (on/off) and the other four are reading values.

I want to create a view from this table, with another column for the range of the four reading values, based on those where their corresponding sensor alarm <> 1..

For examples purposes, we can use the following values.

Column1 – 12.44
Column1Alarm – 0

Column2 – 99.43
Column2Alarm – 0

Column3 – 4.43
Column3Alarm – 1

Column4 – 43.33
Column4Alarm – 0

For this example, the only values included in the range should be those coming from Column1, 2, and 4.

Thanks,
Tom

  • 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-05-25T13:35:51+00:00Added an answer on May 25, 2026 at 1:35 pm

    Editted as per @HLGM comments to make it a bit more robust.

    Note that in it’s current form, I assume that when

    • all alarms equal 1, the range should be NULL
    • only one alarm equals 0, the range is the value of this alarm.

    If this does not suffice, OP might clarify what should be returned instead.

    SQL Statement

        ;WITH Alarm (C1, C1Alarm, C2, C2Alarm, C3, C3Alarm, C4, C4Alarm) AS (
            SELECT  12.44, 0, 99.43, 0, 4.43, 1, 43.33, 0
            UNION ALL SELECT 12.44, 1, 99.43, 0, 4.43, 1, 43.33, 0
            UNION ALL SELECT 1, 0, 2, 1, 3, 1, 4, 1
            UNION ALL SELECT 1, 1, 2, 1, 3, 1, 4, 1
        )
        , AddRowNumbers AS (
            SELECT  rowNumber = ROW_NUMBER() OVER (ORDER BY C1)
                    , C1, C1Alarm
                    , C2, C2Alarm
                    , C3, C3Alarm
                    , C4, C4Alarm
            FROM    Alarm   
        )
        , UnPivotColumns AS (
            SELECT  rowNumber, value = C1 FROM AddRowNumbers WHERE C1Alarm = 0
            UNION ALL SELECT rowNumber, C2 FROM AddRowNumbers WHERE C2Alarm = 0
            UNION ALL SELECT rowNumber, C3 FROM AddRowNumbers WHERE C3Alarm = 0
            UNION ALL SELECT rowNumber, C4 FROM AddRowNumbers WHERE C4Alarm = 0
        )
        SELECT  C1, C1Alarm
                , C2, C2Alarm
                , C3, C3Alarm
                , C4, C4Alarm
                , COALESCE(range1.range, range2.range)
        FROM    AddRowNumbers rowNumber
                LEFT OUTER JOIN (SELECT rowNumber, range = MAX(value) - MIN(value) FROM UnPivotColumns GROUP BY rowNumber HAVING COUNT(*) > 1) range1 ON range1.rowNumber = rowNumber.rowNumber
                LEFT OUTER JOIN (SELECT rowNumber, range = AVG(value) FROM UnPivotColumns GROUP BY rowNumber HAVING COUNT(*) = 1) range2 ON range2.rowNumber = rowNumber.rowNumber  
    

    Test script

    ;WITH Alarm (C1, C1Alarm, C2, C2Alarm, C3, C3Alarm, C4, C4Alarm) AS (
        SELECT  12.44, 0, 99.43, 0, 4.43, 1, 43.33, 0
        UNION ALL SELECT 12.44, 1, 99.43, 0, 4.43, 1, 43.33, 0
        UNION ALL SELECT 1, 0, 2, 1, 3, 1, 4, 1
        UNION ALL SELECT 1, 1, 2, 1, 3, 1, 4, 1
    )
    , AddRowNumbers AS (
        SELECT  rowNumber = ROW_NUMBER() OVER (ORDER BY C1)
                , C1, C1Alarm
                , C2, C2Alarm
                , C3, C3Alarm
                , C4, C4Alarm
        FROM    Alarm   
    )
    , UnPivotColumns AS (
        SELECT  rowNumber, value = C1 FROM AddRowNumbers WHERE C1Alarm = 0
        UNION ALL SELECT rowNumber, C2 FROM AddRowNumbers WHERE C2Alarm = 0
        UNION ALL SELECT rowNumber, C3 FROM AddRowNumbers WHERE C3Alarm = 0
        UNION ALL SELECT rowNumber, C4 FROM AddRowNumbers WHERE C4Alarm = 0
    )
    SELECT  C1, C1Alarm
            , C2, C2Alarm
            , C3, C3Alarm
            , C4, C4Alarm
            , COALESCE(range1.range, range2.range)
    FROM    AddRowNumbers rowNumber
            LEFT OUTER JOIN (SELECT rowNumber, range = MAX(value) - MIN(value) FROM UnPivotColumns GROUP BY rowNumber HAVING COUNT(*) > 1) range1 ON range1.rowNumber = rowNumber.rowNumber
            LEFT OUTER JOIN (SELECT rowNumber, range = AVG(value) FROM UnPivotColumns GROUP BY rowNumber HAVING COUNT(*) = 1) range2 ON range2.rowNumber = rowNumber.rowNumber  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like it should be simple but I can't work out how to
Hi this may seem like a weird question, but here's my situation: I have
So, in a non-class type of situation, I can do something like this: int
This may seem like a dumb question, but I'm in a head-> wall situation
I've run into a really weird situation. This should be so simple, yet I'm
I bet this has been answered many times over, but... For a simple situation
I've come upon this situation a few times before. I wish to reference a
This question has been brought up many times, but I'd like to ask it
I have come across an awkward situation where I would like to have a
This situation arises from someone wanting to create their own pages in their web

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.