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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:00:51+00:00 2026-05-14T01:00:51+00:00

I am trying to achieve the following without using sub query. For a funding,

  • 0

I am trying to achieve the following without using sub query.

For a funding, I would like to select the latest Letter created date and the ‘earliest worklist created since letter created’ date for a funding.

FundingId Leter (1, 1/1/2009 )(1, 5/5/2009) (1, 8/8/2009) (2, 3/3/2009) 

FundingId WorkList (1, 5/5/2009 ) (1, 9/9/2009) (1, 10/10/2009) (2, 2/2/2009) 

Expected Result –

FundingId Leter WorkList (1, 8/8/2009, 9/9/2009)

I wrote a query as follows. It has a bug. It will omit those FundingId for which the minimum WorkList date is less than latest Letter date (even though it has another worklist with greater than letter created date).

CREATE TABLE #Funding(
[Funding_ID] [int] IDENTITY(1,1) NOT NULL,
[Funding_No] [int] NOT NULL,
CONSTRAINT [PK_Center_Center_ID] PRIMARY KEY NONCLUSTERED ([Funding_ID] ASC)
) ON [PRIMARY]

CREATE TABLE #Letter(
[Letter_ID] [int] IDENTITY(1,1) NOT NULL,
[Funding_ID] [int] NOT NULL,
[CreatedDt] [SMALLDATETIME],
CONSTRAINT [PK_Letter_Letter_ID] PRIMARY KEY NONCLUSTERED ([Letter_ID] ASC)
) ON [PRIMARY]

CREATE TABLE #WorkList(
[WorkList_ID] [int] IDENTITY(1,1) NOT NULL,
[Funding_ID] [int] NOT NULL,
[CreatedDt] [SMALLDATETIME],
CONSTRAINT [PK_WorkList_WorkList_ID] PRIMARY KEY NONCLUSTERED ([WorkList_ID] ASC)
) ON [PRIMARY]

SELECT F.Funding_ID,
Funding_No, 
MAX (L.CreatedDt),
MIN(W.CreatedDt)
FROM #Funding F
INNER JOIN #Letter L ON L.Funding_ID = F.Funding_ID
LEFT OUTER JOIN #WorkList W ON W.Funding_ID = F.Funding_ID
GROUP BY F.Funding_ID,Funding_No
HAVING MIN(W.CreatedDt) > MAX (L.CreatedDt)

How can I write a correct query without using subquery?

Please help

Thanks

Lijo

  • 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-14T01:00:51+00:00Added an answer on May 14, 2026 at 1:00 am

    using derived tables is as good as it gets:

    OP’s tables:

    CREATE TABLE #Funding(
    [Funding_ID] [int] IDENTITY(1,1) NOT NULL,
    [Funding_No] [int] NOT NULL,
    CONSTRAINT [PK_Center_Center_ID] PRIMARY KEY NONCLUSTERED ([Funding_ID] ASC)
    ) ON [PRIMARY]
    
    CREATE TABLE #Letter(
    [Letter_ID] [int] IDENTITY(1,1) NOT NULL,
    [Funding_ID] [int] NOT NULL,
    [CreatedDt] [SMALLDATETIME],
    CONSTRAINT [PK_Letter_Letter_ID] PRIMARY KEY NONCLUSTERED ([Letter_ID] ASC)
    ) ON [PRIMARY]
    
    CREATE TABLE #WorkList(
    [WorkList_ID] [int] IDENTITY(1,1) NOT NULL,
    [Funding_ID] [int] NOT NULL,
    [CreatedDt] [SMALLDATETIME],
    CONSTRAINT [PK_WorkList_WorkList_ID] PRIMARY KEY NONCLUSTERED ([WorkList_ID] ASC)
    ) ON [PRIMARY]
    

    OP’s sample data:

    INSERT INTO #Funding (Funding_No) VALUES (1)
    INSERT INTO #Funding (Funding_No) VALUES (2)
    
    INSERT INTO #Letter (Funding_ID,CreatedDt) VALUES (1,'1/1/2009')
    INSERT INTO #Letter (Funding_ID,CreatedDt) VALUES (1,'5/5/2009')
    INSERT INTO #Letter (Funding_ID,CreatedDt) VALUES (1,'8/8/2009')
    INSERT INTO #Letter (Funding_ID,CreatedDt) VALUES (2,'3/3/2009')
    
    INSERT INTO #WorkList (Funding_ID,CreatedDt) VALUES (1, '5/5/2009')
    INSERT INTO #WorkList (Funding_ID,CreatedDt) VALUES (1, '9/9/2009')
    INSERT INTO #WorkList (Funding_ID,CreatedDt) VALUES (1, '10/10/2009')
    INSERT INTO #WorkList (Funding_ID,CreatedDt) VALUES (2, '2/2/2009')
    

    The table CREATEs look like TSQL, but no version was given, so a CTE could have been used as well. However, this uses derived tables:

    SELECT
        dt.Funding_ID,LCreatedDt,MIN(CreatedDt) AS WCreatedDt
        FROM (SELECT
                  f.Funding_Id,l.LCreatedDt
                  FROM #Funding    f
                  LEFT OUTER JOIN (SELECT
                                       Funding_ID,MAX(CreatedDt) AS LCreatedDt
                                       FROM #Letter
                                       GROUP BY Funding_ID
                                  ) l ON f.Funding_ID=l.Funding_ID
             ) dt
        LEFT OUTER JOIN #WorkList w ON dt.Funding_ID=w.Funding_ID
        WHERE w.CreatedDt>dt.LCreatedDt
        GROUP BY dt.Funding_ID,LCreatedDt
    

    OUTPUT:

    Funding_ID  LCreatedDt              WCreatedDt
    ----------- ----------------------- -----------------------
    1           2009-08-08 00:00:00     2009-09-09 00:00:00
    
    (1 row(s) affected)
    

    to preempt any people claiming that my query uses a subquery, read this article first on
    Subquery Fundamentals: http://msdn.microsoft.com/en-us/library/aa213252(SQL.80).aspx

    A subquery is a SELECT query that
    returns a single value and is nested
    inside a SELECT, INSERT, UPDATE, or
    DELETE statement, or inside another
    subquery. A subquery can be used
    anywhere an expression is allowed.

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

Sidebar

Related Questions

I am trying to achieve the following MySQL query in Zend Framework 1.11: SELECT
I'm trying to achieve the following layout without tables or JavaScript. It's basically the
I'm trying to achieve following: class A { def foo() { foo } }
I am trying to achieve the following: <Style TargetType=ListBoxItem> <Setter Property=ContextMenu> <Setter.Value> <ContextMenu> <MenuItem
I am trying to achieve the following... _4S.NJB_Request request = (from r in db.NJB_Requests
I am trying to achieve the same behavior as indicated in the following post.
I'm trying to implement the folowing scenario, using JQuery deferred, without much luck. What
I'm trying to achieve the following but with no problems in spacing. The image
hopefully this is a really easy question. I'm trying to achieve the following style
I am trying to achieve the following: I have a server-side script that generates

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.