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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:17:04+00:00 2026-06-11T17:17:04+00:00

I have a select statement and want to return all values only if at

  • 0

I have a select statement and want to return all values only if at least one of them has a date with 60 days of difference from today.
The problem is that i have an outer apply which returns the column i want to compare to, and they come from different tables (one belongs to cash items, and the other to card items).
Considering I have the following:

OUTER APPLY (
    SELECT COUNT(*) AS quantity, MIN(date) AS item_date
        FROM dbo.get_cash_items(loans.id_cash) AS cash_item
        HAVING loans.id_product_type = 1 --Cash
    UNION
    SELECT COUNT(*) AS quantity, MIN(date) AS item_date
        FROM dbo.get_card_items(loans.id_card) AS card_item
        HAVING loans.id_product_type = 2 --Card
) AS items

I want to return all the rows only when DATEDIFF(DAY, MIN(items.item_date), GETDATE()) >= 60, but I want them all even if only one matches this condition.
What would be the best approach to do this?

EDIT
To make it clearer, I’ll explain the use case:
I need to show the items of every loan, only if the client is late for more than 60 days of the due date on any of it

  • 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-11T17:17:06+00:00Added an answer on June 11, 2026 at 5:17 pm

    I am also not sure, what do you expect, but how about that:

    WITH items
         AS (SELECT Count(*)  AS quantity,
                    Min(date) AS item_date
             FROM   dbo.Get_cash_items(loans.id_cash) AS cash_item
             HAVING loans.id_product_type = 1
             UNION
             SELECT Count(*)  AS quantity,
                    Min(date) AS item_date
             FROM   dbo.Get_card_items(loans.id_card) AS card_item
             HAVING loans.id_product_type = 2)
    SELECT a.*
    FROM   items AS a,
           (SELECT TOP 1 *
            FROM   items AS b
            WHERE  Datediff(day, b.item_date, Getdate()) >= 60) AS c  
    

    It’s a sort of CROSS JOIN, where table C will have one or zero rows depending on that if the condition is met – it will than join to every row in other table.

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

Sidebar

Related Questions

I want to execute one mysql statement where I have a select that is
I'm having trouble with my SQL SELECT statement. Basically I want to return all
I have a select statement and in a couple of the fields, I want
I have a select statement, retrieve about 1000 record I want to modify it
I have to following SQL Statement that I want to conver to LINQ Select
I want to get all the image_name values from one of my tables where
I have a SQL statement. SELECT ID, LOCATION, CODE,MAX(DATE),FLAG FROM TABLE1 WHERE DATE <=
I am trying to find a way to have a SELECT statement return the
I have a select statement running which pulls data into $row. $result = mysql_query(Select
I have a SELECT statement like this: SELECT T1.COD, T1.NAME, (SELECT MAX(T2.DATA) FROM dbo.TAB2

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.