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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:09:36+00:00 2026-06-04T08:09:36+00:00

I know that there are several posts about how BAD it is to try

  • 0

I know that there are several posts about how BAD it is to try to loop in SQL Server in a stored procedure. But I haven’t quite found what I am trying to do. We are using data connectivity that can be linked internally directly into excel.

I have seen some posts where a few people have said they could convert most loops to a standard query. But for the life of me I am having trouble with this one.

I need all custIDs who have orders right before an event of type 38,40. But only get them if there is no other order between the event and the order in the first query.

So there are 3 parts. I first query for all orders (orders table) based on a time frame into a temporary table.

Select into temp1 odate, custId from orders where odate>'5/1/12'

Then I could use the temp table to inner join on the secondary table to get a customer event (LogEvent table) that may have occurred some time in the past prior to the current order.

Select into temp2 eventdate, temp1.custID from LogEvent inner join temp1 on 
temp1.custID=LogEvent.custID where EventType in (38,40) and temp1.odate>eventdate
order by eventdate desc

The problem here is that the queries I am trying to run will return all rows for each of the customers from the first query where I only want the latest for each customer. So this is where on the client side I would loop to only get one Event instead of all the old ones. But as all the query has to run inside of Excel I can’t really loop client side.

The third step then could use the results from the second query to make check if the event occurred between most current order and any previous order. I only want the data where the event precedes the order and no other orders are in between.

Select ordernum, shopcart.custID from shopcart right outer join temp2 on 
shopcart.custID=temp2.custID where shopcart.odate >= temp2.eventdate and
ordernum is null

Is there a way to simplify this and make it set-based to run in SQL Server instead of some kind of loop that I is perform at the client?

  • 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-04T08:09:37+00:00Added an answer on June 4, 2026 at 8:09 am

    THis is a great example of switching to set-based notation.

    First, I combined all three of your queries into a single query. In general, having a single query let’s the query optimizer do what it does best — determine execution paths. It also prevents accidental serialization of queries on a multithreaded/multiprocessor machine.

    The key is row_number() for ordering the events so the most recent has a value of 1. You’ll see this in the final WHERE clause.

    select ordernum, shopcart.custID
    from (Select eventdate, temp1.custID,
                 row_number() over (partition by temp1.CustID order by EventDate desc) as seqnum
          from LogEvent inner join
               (Select odate, custId
                from order
                where odate>'5/1/12'
               ) temp1 
               on temp1.custID=LogEvent.custID
          where EventType in (38,40) and temp1.odate>eventdate order by eventdate desc 
         ) temp2 left outer join
         ShopCart
         on shopcart.custID=temp2.custID
     where seqnum = 1 and shopcart.odate >= temp2.eventdate and ordernum is null
    

    I kept your naming conventions, even though I think “from order” should generate a syntax error. Even if it doesn’t it is bad practice to name tables and columns with reserved SQL words.

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

Sidebar

Related Questions

I know there have been several posts about random word generation based on large
Possible Duplicate: Objective C for Windows I know there are several posts about this
I know, there are several posts with nearly the same question, but all the
I know that there are lots of examples out there on this, but they
I know that there can be multiple values for an email, but I'm not
I know that there are many Delphi database related questions available, but I'm considering
i know that there is some rules and standards in css handling but i
I know that it seems a duplicate entry, but I red all the posts
I've read several of the posts on stack overflow already about this topic, but
I've read several posts about changing Fragments dynamically using a Fragment transaction, but they

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.