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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:43:07+00:00 2026-06-10T11:43:07+00:00

I have following query in sql server 2005 with a table select t1.id, CONVERT(VARCHAR,t1.dt,103)

  • 0

I have following query in sql server 2005 with a table

select t1.id, CONVERT(VARCHAR,t1.dt,103) date_1, CONVERT(VARCHAR,t2.dt,103) date_2, t1.hotel,
    t1.price price_1, t2.price price_2, t2.price - t1.price difference, ((t2.price - t1.price)/t1.price)*100 as Diff_percentage
from test t1
inner join
(
    select *
    from test
) t2
    on t1.hotel = t2.hotel
    and t1.dt < t2.dt and t2.dt=(SELECT MAX(dt) from TEST) and t1.dt=(SELECT MAX(dt-1) from TEST)

I want to use count if function within this query. Based on difference column. So that i can count “How many increased, How many decreased, How many same, How many unavailable”

  1. COUNT IF Difference>0 //How many increased
  2. COUNT IF Difference<0 //How many decreased
  3. COUNT IF Difference=0 //How many same
  4. COUNT IF Difference=”” //How many unavailable –Difference is blank.

DEMO : http://sqlfiddle.com/#!3/b6f37/29

  • 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-10T11:43:09+00:00Added an answer on June 10, 2026 at 11:43 am

    If I wanted your existing query result I would rewrite the query as:

    select t1.id, 
           CONVERT(VARCHAR,t1.dt,103) date_1, 
           CONVERT(VARCHAR,t2.dt,103) date_2, 
           t1.hotel,
           t1.price price_1, 
           t2.price price_2, 
           t2.price - t1.price difference, 
           ((t2.price - t1.price)/t1.price)*100 as Diff_percentage
      from test t1
      join (select max(dt) maxDt from test) d
        on t1.dt = d.maxDt-1
      join test t2
        on t2.hotel = t1.hotel
       and t2.dt = d.maxDt
    

    To extend your original query to include hotels with missing rows:

    select t1.id, 
           CONVERT(VARCHAR,t1.dt,103) date_1, 
           CONVERT(VARCHAR,t2.dt,103) date_2, 
           h.hotel,
           t1.price price_1, 
           t2.price price_2, 
           t2.price - t1.price difference, 
           ((t2.price - t1.price)/t1.price)*100 as Diff_percentage
      from (select distinct hotel from test) h
      cross join (select max(dt) maxDt from test) d
      left join test t1
        on t1.hotel = h.hotel
       and t1.dt = d.maxDt-1
      left join test t2
        on t2.hotel = h.hotel
       and t2.dt = d.maxDt
    

    The above query would be much more efficient with a normalized HOTEL table (1 row per hotel) to replace the SELECT DISTINCT subquery.

    To get your requested result, I would use:

    select count(case when (t2.price-t1.price) < 0 then 1 end) decrease_count,
           count(case when (t2.price-t1.price) > 0 then 1 end) increase_count,
           count(case when (t2.price-t1.price) = 0 then 1 end) same_count,
           count(distinct t1.hotel) - count(case when (t2.price-t1.price) is not null then 1 end) unavailable_count
      from test t1
      left join (select max(dt) maxDt from test) d
        on t1.dt = d.maxDt-1
      left join test t2
        on t2.hotel = t1.hotel
       and t2.dt = d.maxDt
    

    Again the above would be more efficient with a normalized HOTEL table. I would restructure the query more like the previous one: select from HOTEL cross joined to the MAX date query, then outer join to the TEST table twice for the data for the 2 dates. The unavailable count could then be measured more directly, counting the number of rows where the difference computation is NULL.

    Here is the SQL Fiddle for all the queries, along with some extended test data.

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

Sidebar

Related Questions

i have the following query in SQL Server 2005: SELECT distinct(user.id_user), user.name, user.familyname from
HI, Using SQL server 2005 I have the following query: SELECT contact_id ,YEAR(date_created) AS
In sql server 2008, I have the following query: select c.title as categorytitle, s.title
I have the following SQL Server query: SELECT area FROM places WHERE REPLACE(area,'-',' ')
Using SQL Server 2005 I have a table with the following columns id name
I have a table (SQL Server 2005) with the following columns: State: - StateId
I am using MSSQL 2005 Server and I have the following SQL query. IF
For instance, my query is like the following using SQL Server 2005: SELECT *
I have the following very large table in SQL Server 2005: create table Blah
I have table in Ms SQL Server 2005 (2008) in following structure ID Name

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.