[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

01/04/2005 at 06:59AM PST, ID: 21261150
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.2

NESTED SQL SELECT STATEMENT QUESTION

Asked by pjordanna in MS SQL Server

Tags: nested, sql, select

Hi Experts,

I have the following table:

rowID      country      destination      bkgDateStart      bkgDateEnd      pickUpStart      pickUpEnd           carCode      band      supplier
1      ESP      IBZ      01/12/2004      31/12/2005      01/01/2005      04/01/2005            A      ACH1
2      ESP      IBZ      01/12/2004      31/12/2005      05/01/2005      08/01/2005            B      ACH1
3      ESP      IBZ      01/12/2004      31/12/2005      09/01/2005      12/01/2005            C      ACH1
4      ESP      IBZ      01/12/2004      31/12/2005      13/01/2005      16/01/2005            D      ACH1
5      ESP      IBZ      01/12/2004      31/12/2005      17/01/2005      20/01/2005            E      ACH1
6      ESP      IBZ      01/12/2004      31/12/2005      21/01/2005      25/01/2005            F      ACH1
7      ESP      IBZ      01/01/2005      10/01/2005      01/01/2005      31/12/2005            G      ACH1
8      ESP      IBZ      04/01/2005      06/01/2005      01/01/2005      31/12/2005            H      ACH1
9      ESP      IBZ      01/12/2004      31/12/2005      01/01/2005      04/01/2005            A      ACH2
10      ESP      IBZ      01/12/2004      31/12/2005      05/01/2005      08/01/2005            B      ACH2
11      ESP      IBZ      01/12/2004      31/12/2005      09/01/2005      12/01/2005            C      ACH2
12      ESP      IBZ      01/12/2004      31/12/2005      13/01/2005      16/01/2005            D      ACH2
13      ESP      IBZ      01/12/2004      31/12/2005      17/01/2005      20/01/2005            E      ACH2
14      ESP      IBZ      01/12/2004      31/12/2005      21/01/2005      25/01/2005            F      ACH2


And I am querying it using the nested SQL statement below. The statement first chooses the rows where "pickUpStart" and "pickUpEnd" are closest to the date specified (i.e. the date specified falls between those dates), then it chooses the closest match for each of those rows to "bkgDateStart" and "bkgDateEnd" and returns the result. The statement is as follows:



select top 100 c.* from ch_bandChoice_ESP_IBZ c inner join

      (select a.pickUpStart, a.pickUpEnd, a.bkgDateStart, a.bkgDateEnd AS bkgDateEnd FROM
      
            (select * from ch_bandChoice_ESP_IBZ where (pickUpStart <= CONVERT(DATETIME, '2005-01-11 00:00:00', 102)) and (pickUpEnd >= CONVERT(DATETIME, '2005-01-11 00:00:00', 102)))
      
      a where CONVERT(DATETIME, '2005-01-06 00:00:00', 102) between a.bkgDateStart  and a.bkgDateEnd group by a.pickUpStart,a.pickUpEnd,a.bkgDateStart,a.bkgDateEnd)

b on c.pickUpStart=b.pickUpStart and c.pickUpEnd=b.pickUpEnd and c.bkgDateStart=b.bkgDateStart and c.bkgDateEnd=b.bkgDateEnd order by (c.bkgDateEnd-c.bkgDateStart), supplier


This particular statement returns the following result:


rowID      country      destination      bkgDateStart      bkgDateEnd      pickUpStart      pickUpEnd                      carCode      band      supplier
8      ESP      IBZ      2005-01-04 00:00:00      2005-01-06 00:00:00      2005-01-01 00:00:00      2005-12-31 00:00:00             H      ACH1
7      ESP      IBZ      2005-01-01 00:00:00      2005-01-10 00:00:00      2005-01-01 00:00:00      2005-12-31 00:00:00             G      ACH1
3      ESP      IBZ      2004-12-01 00:00:00      2005-12-31 00:00:00      2005-01-09 00:00:00      2005-01-12 00:00:00             C      ACH1
11      ESP      IBZ      2004-12-01 00:00:00      2005-12-31 00:00:00      2005-01-09 00:00:00      2005-01-12 00:00:00             C      ACH2



I now need to continue with the nested statement in order to filter the result down to distinct supplier codes returning only the top row for each supplier code. In this example the final returned rows should be:

rowID      country      destination      bkgDateStart      bkgDateEnd      pickUpStart      pickUpEnd                      carCode      band      supplier
8      ESP      IBZ      2005-01-04 00:00:00      2005-01-06 00:00:00      2005-01-01 00:00:00      2005-12-31 00:00:00             H      ACH1
11      ESP      IBZ      2004-12-01 00:00:00      2005-12-31 00:00:00      2005-01-09 00:00:00      2005-01-12 00:00:00             C      ACH2



I.E. the first row for supplier ACH1 and the first row for supplier ACH2


I am having difficulty in doing this. Do any of you have any ideas?



Thanks,




PJORDANNA





                                                      
[+][-]01/04/05 07:03 AM, ID: 12952600

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01/04/05 07:20 AM, ID: 12952768

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01/04/05 07:34 AM, ID: 12952927

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01/04/05 07:35 AM, ID: 12952945

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01/04/05 07:39 AM, ID: 12952993

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01/04/05 07:41 AM, ID: 12953024

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01/04/05 07:47 AM, ID: 12953094

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01/04/05 07:48 AM, ID: 12953107

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01/04/05 07:49 AM, ID: 12953123

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: MS SQL Server
Tags: nested, sql, select
Sign Up Now!
Solution Provided By: Hilaire
Participating Experts: 5
Solution Grade: A
 
 
[+][-]01/04/05 07:52 AM, ID: 12953161

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01/04/05 07:59 AM, ID: 12953250

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01/04/05 08:03 AM, ID: 12953305

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05/15/05 10:49 AM, ID: 14006618

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-91