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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:51:07+00:00 2026-06-08T12:51:07+00:00

I need to change the following procedure if else statement into a single query

  • 0

I need to change the following procedure if else statement into a single query using the select case statement with the subquery…

 if((select COUNT(*) from pseb.dbo.meterattributedetails where meterid=@meterid)=0)
  select @emf=EMF from pseb.dbo.METERMASTER where MeterID=@meterid
  else  if((select COUNT(*) from pseb.dbo.meterattributedetails where meterid=@meterid and dateadd(day,1,@fromdate)<DateTime)>0) 
    select top 1 @emf=oldvalue from pseb.dbo.meterattributedetails  where MeterID=@meterid and dateadd(day,1,@fromdate)<datetime order by DateTime 
     else 
  select top 1 @emf=newvalue from pseb.dbo.meterattributedetails  where meterid=@meterid and DateTime<@fromdate order by DateTime desc

meterattribute table structure as like this:

AttributeID AttributeName   Oldvalue    newvalue    DateTime    meterid
1           EMF         2.00000         4.00000        2012-07-05   4756
1           EMF         4.00000         6.00000        2012-07-10   4756
1           EMF         6.00000         8.00000        2012-07-15   4756
1           EMF         8.00000        10.00000        2012-07-25   4756

in that table datetime refers the emf changed date, if my input is ‘2012-07-17’ means i have to fetch he newvalue for that particular date,,, in between the july 15-25 the emf value is 8.00000.


I formatted your code using http://www.dpriver.com/pp/sqlformat.htm — Michael

IF( (SELECT Count(*) 
     FROM   pseb.dbo.meterattributedetails 
     WHERE  meterid = @meterid) = 0 ) 
  SELECT @emf = emf 
  FROM   pseb.dbo.metermaster 
  WHERE  meterid = @meterid 
ELSE IF( (SELECT Count(*) 
     FROM   pseb.dbo.meterattributedetails 
     WHERE  meterid = @meterid 
            AND Dateadd(day, 1, @fromdate) < datetime) > 0 ) 
  SELECT TOP 1 @emf = oldvalue 
  FROM   pseb.dbo.meterattributedetails 
  WHERE  meterid = @meterid 
         AND Dateadd(day, 1, @fromdate) < datetime 
  ORDER  BY datetime 
ELSE 
  SELECT TOP 1 @emf = newvalue 
  FROM   pseb.dbo.meterattributedetails 
  WHERE  meterid = @meterid 
         AND datetime < @fromdate 
  ORDER  BY datetime DESC 
  • 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-08T12:51:10+00:00Added an answer on June 8, 2026 at 12:51 pm

    @Michael – thanks for the link to http://www.dpriver.com/pp/sqlformat.htm.

    Try this (untested, of course)

    SELECT @emf = CASE 
                WHEN ( (SELECT Count(*) 
                        FROM   pseb.dbo.meterattributedetails 
                        WHERE  meterid = @meterid) = 0 ) THEN 
                (SELECT emf 
                 FROM   pseb.dbo.metermaster 
                 WHERE  meterid = @meterid) 
                WHEN ( (SELECT Count(*) 
                        FROM   pseb.dbo.meterattributedetails 
                        WHERE  meterid = @meterid 
                               AND Dateadd(day, 1, @fromdate) < datetime) > 0 ) 
              THEN ( 
                SELECT 
                TOP 1 oldvalue 
                FROM 
              pseb.dbo.meterattributedetails 
                WHERE 
              meterid = @meterid 
              AND Dateadd(day, 1, @fromdate) < datetime 
                ORDER 
              BY datetime) 
                ELSE (SELECT TOP 1 newvalue 
                      FROM   pseb.dbo.meterattributedetails 
                      WHERE  meterid = @meterid 
                             AND datetime < @fromdate 
                      ORDER  BY datetime DESC) 
              END 
    

    @Saranya: if this doesn’t work, how about setting up a test case on http://sqlfiddle.com/

    Good luck!

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

Sidebar

Related Questions

I need to change tab icon if selected or not. I am using following
I need advice on how to change the file formatted following way file1: A
I need change background of all text that have two spaces from the start
I use Pjax with tutorial from http://railscasts.com/episodes/294-playing-with-pjax?view=comments I don't need change url and this
I want to do following: Make Oracle stored procedure for read all data from
How can I change following query, so that I'm able to parameterize the SparePartNames
I have the following code: procedure TCellBlock.GeneratePtoQ; var x,y: integer; i: integer; Change: cardinal;
I am using following code: Note that i HAVE TO SEND SQL Query so
I have a bit of a weird use case where i need to change
all I need to change the position of a ImageView dynamically and iam using

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.