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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:45:44+00:00 2026-05-25T19:45:44+00:00

I am stuck with rather confusing query. Assume I have a ProductLending table that

  • 0

I am stuck with rather confusing query.

Assume I have a ProductLending table that tracks what product each user has borrowed, when it was renewed, was it returned or not etc.. Given a user, I want to be able to select, all unique products that are still with the user.

table example:

userid    DateRenewed    ProductId    isReturned
````````````````````````````````````````````````
1        2011-07-21      15           0
1        2011-08-20      16           0
1        2011-09-21      15           1
2        2011-09-21      17           0
1        2011-09-21      15           0

This is a mock up so sorry if it’s not accurate.

Now, given userId = 1, I want to select just unique productId that are NOT returned, but are with the user. So this should give me 15, 16 as result, as even though 15 was returned, it was re-borrowed. If we delete the last row, then the result would just be 16, since user has only 16 with him.

I tried ordering by dateRenewed and selecting top 1 but it did totally something else.. how do I construct a query for this please?

  • 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-25T19:45:45+00:00Added an answer on May 25, 2026 at 7:45 pm

    If product is not returned by user, then the sum of bought products must be larger than sum of returned products

    SELECT userid,ProductId FROM <table>
    GROUP BY userid,ProductId HAVING SUM(CASE CAST(isReturned AS INT) WHEN 0 THEN 1 ELSE 0 END)-SUM(CAST(isReturned AS INT))>0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm stuck in something that I'm sure rather easy I have a field in
So we have a PHP+Zend Framework+Doctrine 1.2 application that has the following structure: Controller
I have a menu on my site that changes depending on whether the user
Evening folks!! Stuck with this rather dull problem. I have deployed my website on
I have a rather simple informational website that's being run by Django. Its' pages(views)
Being stuck with a legacy database schema that no longer reflects your data model
Kinda stuck here... I have an application with lets say 5000 rows of data
Being new to the Entity Framework, I'm really rather stuck on how to proceed
I am a bit stuck implementing a rather simple layout requirement. I want to
I would like to thank contributors in advanced, I am rather stuck on this

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.