简体   繁体   中英

MySQL query - cannot determine how to write specific query

I simply cannot figure out how I should write my MySQL query to get the correct result.

I have a table which contains the following structure:

在此处输入图片说明

I need to write a query which returns a total count of all distinct ronumbers which contain the following parameters:

1) Must have a serviceID of 1

2) Must also have a serviceID greater than 1

3) The serviceID 's that are greater than 1 MUST ALL have a decsvc of 1

4) If #3 is anything other than decsvc of 1, that ronumber is disqualified

In simple terms, return a count of all distinct ronumbers that have a serviceID of 1 and serviceID (s) greater than 1 that are ONLY declined services ( decsvc = 1 ).

I have tried the following queries:

1)

$query = "SELECT a.ronumber FROM servicerendered_welr a LEFT JOIN
servicerendered_welr b ON (a.ronumber = b.ronumber) 
WHERE a.serviceID > 1 AND a.decsvc = 1 AND a.addsvc = 0 AND a.dealerID = 
$dealerID 
AND b.serviceID = 1 GROUP BY a.ronumber";

2)

$query = "SELECT ronumber FROM servicerendered_welr 
WHERE addsvc = 0 AND dealerID = $dealerID AND ronumber IN 
(SELECT ronumber FROM servicerendered_welr WHERE decsvc = 1 AND dealerID =
$dealerID AND ronumber IN 
(SELECT ronumber FROM servicerendered_welr WHERE serviceID > 1 AND dealerID
= $dealerID AND ronumber IN
(SELECT ronumber FROM servicerendered_welr WHERE serviceID = 1 and dealerID
= $dealerID) 
GROUP BY ronumber) 
GROUP BY ronumber)
GROUP BY ronumber";

These both return the same result set. The results return ronumber (s) 12 and 16 ( see servicerendered_welr table ).

在此处输入图片说明

My goal would be to have the query return only ronumber 12, pertaining to the above stipulations. As you can see, ronumber 16 does meet some of the qualifications, BUT it has serviceID 's greater than 1 that have a decsvc = 0 , and this is unacceptable.

It is obvious that the query is returning ANYTHING that has a serviceID of 1, AND a serviceID greater than 1, and at least one serviceID greater than 1 with a decsvc = 1 (and thus it is not ruling out the other decsvc = 0 items).

Maybe I need to delve into virtual tables?

Please see my sqlfiddle here: http://sqlfiddle.com/#!9/5e45c/3 .

Your question is a little hard to understand but I think I get what you are trying to do. There is a link to a table you haven't included but I've removed it from the query since it is just part of the WHERE condition. What should work for you is the following:

SELECT

a.ronumber

FROM servicerendered_welr a WHERE 

serviceId = 1
AND
(SELECT COUNT(*) FROM servicerendered_welr b WHERE b.serviceId > 1 AND b.decsvc = 1 AND b.ronumber = a.ronumber) =
(SELECT COUNT(*) FROM servicerendered_welr c WHERE c.serviceId > 1 AND c.ronumber = a.ronumber)
SELECT COUNT(DISTINCT a.ronumber) AS ronumber_count
FROM servicerendered_weir AS a
JOIN (SELECT ronumber, COUNT(*) AS count, SUM(IF(decsvc = 1, 1, 0)) AS decsvc1_count
      FROM servicerendered_weir
      WHERE serviceID > 1
      GROUP BY ronumber
      HAVING count = decsvc1_count) AS b
    ON a.ronumber = b.ronumber
WHERE a.serviceId = 1

Subquery b finds all the ronumbers that meet criteria 2-4. We then join it with the rows that match criteria 1, and count them.

A set of data contains only records with service Id 1 B set of data contains only records with service ID not 1 and in set of A having a B.descsvc all records with descsvc of 1 (avg of 3 records each with 1 is 1) 1.

SELECT count(distinct A.RoNumber)
FROM ServiceRendered_welr A
LEFT JOIN (SELECT roNumber, avg(descsvc) avgDescsvc 
           FROM ServiceRendered_welr 
           GROUP BY roNumber
           HAVING avgDescsvc = 1) b 
 on A.RoNumber = B.RoNumber
and A.ServiceID = 1
and B.ServiceID > 1

I'm adding another solution to the mix, one without group bys and having. I would use the EXISTS clause with subqueries. You essentially want ronumbers that have serviceid=1 and exists at least 1 record where serviceid> 1 and decsvc=1 and does not exist any record where serviceid> 1 and decsvc<> 1:

select count(distinct s1.ronumber) from servicerendered_welr s1
where s1.serviceid=1
and exists (select 1 from servicerendered_welr s2 where s2.ronumber=s1.ronumber and s2.serviceid> 1 and s2.decsvc=1)
and not exists (select 1 from servicerendered_welr s3 where s3.ronumber=s1.ronumber and s3.serviceid> 1 and s3.decsvc=0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM