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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:56:02+00:00 2026-06-09T16:56:02+00:00

SELECT a.lead_id, c.state_name AS COL1DATA, count( c.state_name ) AS leadcount, ( SELECT count( won_loss

  • 0
SELECT a.lead_id, c.state_name AS COL1DATA, count( c.state_name ) AS leadcount, (

SELECT count( won_loss ) AS wonlosscount
FROM lead_status
WHERE (won_loss = 'loss')
AND lead_id = a.lead_id
) AS losscount
FROM lead AS a
JOIN states AS c ON a.state_id = c.states_id
GROUP BY c.state_name
ORDER BY losscount DESC

the answer i get is

lead_id     COL1DATA    leadcount   losscount
1           Queensland  7            0
8           Victoria    3            0

lead status

lead_id     won_loss    won_price   won_mainreason  loss_mainreason     loss_attachment_id  lost_dont_sell_note     add_note    dealer_satisfaction
      5     win         4655        pricing                                                                         fghfg       somewhat
      8     won         34543       pricing                                                                         sfdgs       satisfied
      7     loss                                    service                                                         Additional Notes    verygood
      9     loss                                    not_in_stock                                                    Additi      satisfied

but the loss count should be 1 and 1

any help is appricated

  • 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-09T16:56:03+00:00Added an answer on June 9, 2026 at 4:56 pm

    I’m guessing that there’s a problem with mixing the non-aggregated lead_id in the correlated query all the while grouping on state_name. Perhaps you can describe what you’re looking to get back.

    EDIT: Based on OP feedback in comment below.

    EDIT 2: Changed to left outer joins based on chat session. Not all leads have a lead_status.

    SELECT
        s.state_name AS COL1DATA, count(c.state_name) AS leadcount,
        sum(case when ls.won_loss = 'loss' then 1 else 0 end) as losscount
    FROM
        lead AS l
        INNER JOIN states AS s ON s.state_id = l.states_id
        LEFT OUTER JOIN lead_status as ls on ls.lead_id = l.lead_id
    GROUP BY s.state_name
    ORDER BY losscount DESC
    

    I might argue that this version is slightly better. But I didn’t want to totally change your query. (I did change the aliases because A and C were confusing.)

    SELECT
        min(s.state_name) AS COL1DATA,
        count(l.lead_id) AS leadcount, /* counting non-nullable key on the outer side */
        sum(case when ls.won_loss = 'loss' then 1 else 0 end) as losscount
    FROM
        lead AS l
        INNER JOIN states AS s ON s.state_id = l.states_id
        LEFT OUTER JOIN lead_status as ls on ls.lead_id = l.lead_id
    GROUP BY s.state_id /* might be better to group on the id */
    ORDER BY losscount DESC
    

    The lead_id column you have included in the output is unpredictable unless the group only has one row. Based on what you’ve said, I doubt that you really want it.

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

Sidebar

Related Questions

I have the following query: SELECT l.id, (SELECT amount FROM lead_status WHERE lead_id =
SELECT * From `users` AS `User` LEFT JOIN `selections` AS `Selections` ON (`Selections`.`user_id` =
Example: select * from component inner join component_supplier on component.id = component_supplier.component_id inner join
I have a mysql query SELECT * FROM lead LIMIT 5 OFFSET 0 to
I have the following sql query: SELECT field_number, COUNT(*) AS c FROM lead_detail WHERE
I have an sql query: DELETE **x** FROM $table **x** JOIN ( SELECT vendors
SELECT REPLACE([strUrl], '/myoldurl', '/mynewurl') FROM [UrlRewrite] If strUrl is /myoldurl/myoldurl_something_else , it returns /mynewurl/mynewurl_something_else
SELECT Id,Date,Name FROM people WHERE DATEPART(hh,Date) >= 7 AND DATEPART(hh,Date) <= 8 Order by
select tf.Id,tf.Name,tf.LName,tf.Rank,tf.Category where tf.Id not in (select fighter_id from tbl_player_fighter where league_id=91) and tf.Status
SELECT * FROM list WHERE list_item LIKE %_1375_% returns results it should not. For

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.