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

  • Home
  • SEARCH
  • 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 6591739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:27:50+00:00 2026-05-25T17:27:50+00:00

Okay, so I have this T-SQL statement: SELECT COUNT(DISTINCT RentalNo) as Count FROM RoomRental

  • 0

Okay, so I have this T-SQL statement:

SELECT 
    COUNT(DISTINCT RentalNo) as Count 
FROM 
    RoomRental AS rr 
    LEFT JOIN 
        RoomLinks AS r 
        On (r.RoomNo1 = rr.RoomNo OR r.RoomNo2 = rr.RoomNo) 
        AND r.CostCentreNo = rr.CostCentreNo
WHERE Cancelled = 0 
    AND (rr.RoomNo = @RN OR r.RoomNo2 = @RN OR r.RoomNo1 = @RN) 
    AND rr.CostCentreNo = @CCN 
    AND StartDate = @StartDate
    AND DATEADD(minute,0-SetupTime,StartTime) < @EndBlock 
    AND DATEADD(minute,BreakdownTime,EndTime) > @StartBlock)

Is there any way I can store SELECT *, of it, and then quickly (without applying the statement again), get the RentalNos, or apply another AND to the WHERE clause?

Note, this is just an example of my SQL query, I currently have a loop going around, as it goes through all time blocks, (after first querying the first ending).

Basically, there is a lot of similar SQL query repetition. And it would be great If I could store once and manipulate that instead.

Cheers,

Paul

  • 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-25T17:27:51+00:00Added an answer on May 25, 2026 at 5:27 pm

    Use SELECT INTO and store the results in a temporary table (the # prefix is used for a temporary table)

    SELECT yourcols, ...
    INTO #table
    FROM yourtables...
    

    Then you can reference #table just like any other physically stored table, join to it, etc.

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

Sidebar

Related Questions

I have a problem with a SQL statement: Using this select a.id as ID,
Okay so I have this web site search script and I'm trying to count
Okay I have this RewriteRule which is supposed to redirect any request for the
Okay, I have this program and I don't want more than one instance of
Okay this is a real headscratcher. I have an application which calls a web
Okay, this is just a crazy idea I have. Stack Overflow looks very structured
Okay, my dilemma is this. I have an admin page that I use to
This question is for SQL Server 2005. I have a table with 2 columns.
Okay this one has me stumped.. mainly because i have been working on this
Okay. I have this code on my site: <?php session_start(); include database.php; include bruger.php;

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.