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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:34:23+00:00 2026-06-05T05:34:23+00:00

I’m new to SQL and I want to implement the following query: I’ve got

  • 0

I’m new to SQL and I want to implement the following query:

I’ve got two tables, LicenseTbl and UnlockTbl:

LicenseTbl contains information about a purchased software license:

LicenseID, ProgramID, Owner, Location, OrderNo, BlockTime

UnlockTbl contains information about a specific software registration:

UnlockID, LicenseID (foreign key into LicenseTbl), Timestamp, SerialNo, Key, UninstallTime

where BlockTime and UninstallTime contain a timestamp if the license was blocked or the software uninstalled and NULL otherwise.

I want to devise a query that gives me ALL LicenseIDs for which the following conditions hold:

  • belongs to a given customer,
  • is not blocked,
  • is either not listed in the UnlockTbl or there are < X different SerialNo’s in lines which are not marked as uninstalled.

I have written this, but I’m not sure if it is absolutely correct (it’s one of my first SQL queries ever):

SELECT LicenseID FROM LicenseTbl
JOIN UnlockTbl
   ON (LicenseTbl.LicenseID = UnlockTbl.LicenseID) 
WHERE  LicenseTbl.OrderNo   = '$givenOrderNo'
   AND LicenseTbl.Owner     = '$givenOwner'
   AND LicenseTbl.Location  = '$givenLocation'
   AND LicenseTbl.BlockTime IS NULL
   AND UnlockTbl.UninstallTime IS NULL
GROUP BY LicenseTbl.LicenseID, UnlockTbl.Key
HAVING COUNT(*) < $X

(which is supposed to mean, list all licenses which have only been used less than X times simultaneously. I would prefer those that have been used the least first but don’t know how to sort like that.)

  • 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-05T05:34:25+00:00Added an answer on June 5, 2026 at 5:34 am

    This is a good start, but I would change the query to the following…

    SELECT
      LicenseID
    FROM
      LicenseTbl
    LEFT JOIN
      UnlockTbl
        ON  UnlockTbl.LicenseID = LicenseTbl.LicenseID
        AND UnlockTbl.UninstallTime IS NULL
    WHERE
          LicenseTbl.OrderNo   = '$givenOrderNo'
      AND LicenseTbl.Owner     = '$givenOwner'
      AND LicenseTbl.Location  = '$givenLocation'
      AND LicenseTbl.BlockTime IS NULL
    GROUP BY
      LicenseTbl.LicenseID
    HAVING
      COUNT(DISTINCT UnlockTbl.SerialNo) < $X
    ORDER BY
      COUNT(DISTINCT UnlockTbl.SerialNo)
    

    1). LEFT JOIN

    A LEFT JOIN ensures that all rows in LicenseTbl are returned, even if there are no matches in the UnlockTbl table. (If there are no matches, the UnlockTbl table’s values are all represented as NULL.)

    2). UnlockTbl.UninstallTime IS NULL in the JOIN and not the WHERE

    The WHERE clause is applied after the JOIN. This means that any records in UnlockTbl where UninstallTime have a real value (NOT NULL) get joined and then get filtered out. This in turn means that if all the relevant records in UnlockTbl have a non-NULL value in UninstallTime, all the rows for that License will get filtered.

    3). GROUP BY on just the license, not the Key.

    Simply, I don’t know why you had it there, and it doesn’t appear in the English description of what you want.

    As you want a list of LicenseIDs, grouping by only that field ensures that you get one record per LicenseID.

    4). HAVING clause modified to look at COUNT(DISTINCT SerialNo)

    COUNT(*) counts all records. Even if there was no match (All the UnlockTbl values appearing as NULL), this would return 1.

    COUNT(SerialNo) counts only records where SerialNo is NOT NULL. If there was no match (All the UnlockTbl values appearing as NULL), this would return 0.

    COUNT(DISTINCT SerialNo) also counts only records where SerialNo is NOT NULL, but treats duplicates of the sme value as just 1 entry.

    5). ORDER BY COUNT(DISTINCT SerialNo)

    Takes the same value as is being filtered in the HAVING clause, and orders by it.

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

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns 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.