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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:49:52+00:00 2026-06-18T19:49:52+00:00

I am having trouble getting this complicated select to work for me. Here is

  • 0

I am having trouble getting this complicated select to work for me.

Here is a graphic of my structure.
http://www.bandwise.com/files/tester_db.png

SQL file of the structure and sampe data.
http://www.bandwise.com/files/tester_db.sql

To put is simply, I have a table called “leases” and one called “apartments.” There is a one to many relationship between leases and apartments. Apartments identify a U.S. state and county in which they belong. Therefore, a given lease may have many apartments scattered across the country.

What I need to do is select ONLY U.S. states which contain one or more apartments and return a count of how many leases apply. Because some leases may have apartments in multiple states, a lease may need to be counted in multiple states.

Here is a test query that I’m working with.

SET @timenow = 1360855314;

SELECT l.lid, c.aid, c.leaseid, c.serialnum, c.state, c.county
FROM leases l, apartments c
WHERE c.leaseid = l.lid
    AND l.closebidding > @timenow AND l.status = 1;

I can see here that I have 6 leases in state #1 (Texas) and 2 leases in state #2 (Louisiana). How do I go about querying from the states table outward to get the number of leases based on where their apartments are located?

The return should look like this.

---------------------------
|  state      |   total   |
---------------------------
|  Texas      |     6     |
---------------------------
|  Louisiana  |     3     |
---------------------------
  • 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-18T19:49:54+00:00Added an answer on June 18, 2026 at 7:49 pm

    Use a count(distinct l.lid):

    SELECT s.state_name,count(distinct l.lid) 
    FROM 
      leases l
      JOIN apartments c ON c.leaseid = l.lid
      JOIN states s ON s.state_id = c.state
    WHERE
      l.closebidding > @timenow AND l.status = 1 
    GROUP BY s.state_name;
    

    SQL Fiddle

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

Sidebar

Related Questions

I am having trouble getting this invisibilityOfElementLocated() method to work. Here is the code
Im having trouble getting this to work, here´s a quick overview of the idea.
hi im having trouble getting this to work im getting an error here with
I am having trouble getting this to work: http://jsfiddle.net/2jg2F/1/ I basically want to make
Ok this seems so silly but I'm having some trouble getting this to work.
Having trouble getting this to work. What's strange is that I have 10 bookmarks
I am having trouble getting this too work, for some strange reason the DIV's
i am having trouble getting this to work, can any one spot the issue?
I'm having trouble getting this to work...it just replaces the entire page with the
I am having trouble getting this to work properly I only have one check

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.