Nikhil Gupta
Nikhil Gupta

Reputation: 386

How to make the mysql query using group by

Below is the table structure I have

user_id    quote_id    bill_type(2=>proforma,1=>invoice)
474        676          2
674        676          2
474        676          1
445        680          2
422        120          2
411        240          2
411        240          2
410        240          2
410        240          1

Basically in my portal we have Buyer and Seller(user_id), and both of them can buy and sell with a specific quote_id as shown in the above table. A specific pair of buyer and seller can have multiple quote_id. (you can consider the case of Amazon where specific buyer can purchase multiple products from a specific seller. here product will be quote_id)

Here bill_type refers to the invoice(1) or proforma(2). Invoice will be sent to the user only once and proforma can be sent multiple times for a specific quote_id. Now the problem is that I want to return the data such that users whose invoice is not yet generated for a specific quote_id should be returned.

If we consider the above case then returned data should be:

user_id    quote_id    bill_type(2=>proforma,1=>invoice)
    674        676          2
    445        680          2
    422        120          2
    411        240          2

This are the users whose bill is not yet generated for a specific quote_id.

The query that I have managed to make so far is:

select *, GROUP_CONCAT(bill_type) as bt,GROUP_CONCAT(user_id)
  from quote 
  GROUP BY quote_id

Now I am deleting the user whose invoice is generated with help of program. but that is not the right way to do I need the query to achieve this.

Upvotes: 1

Views: 86

Answers (2)

Nick
Nick

Reputation: 147166

You can do this with a WHERE NOT EXISTS clause, to check that there is no matching invoice sent for a particular user_id/quote_id combination:

SELECT DISTINCT q1.user_id, q1.quote_id, q1.bill_type
FROM quote q1
WHERE NOT EXISTS (SELECT *
                  FROM quote q2
                  WHERE q2.user_id = q1.user_id AND q2.quote_id = q2.quote_id AND q2.bill_type = 1)

Output:

user_id  quote_id  bill_type
674      676       2 
445      680       2 
422      120       2 
411      240       2 

The DISTINCT on the SELECT ensures you don't get multiple rows in situations where more than one proforma invoice is sent (e.g. combination 411/240).

To deal with the case where quote_id is NULL and you don't want those lines in the output, change the query to:

SELECT DISTINCT q1.user_id, q1.quote_id, q1.bill_type
FROM quote q1
WHERE NOT EXISTS (SELECT *
                  FROM quote q2
                  WHERE q2.user_id = q1.user_id AND q2.quote_id = q2.quote_id AND q2.bill_type = 1)
    AND q1.quote_id IS NOT NULL

SQLFiddle

Upvotes: 3

Olim Saidov
Olim Saidov

Reputation: 2844

Try this query:

select distinct
  t1.user_id,
  t1.quote_id,
  t1.bill_type
from
  quote t1
left join  quote t2 on t1.user_id = t2.user_id and
                       t1.user_id = t2.user_id and
                       t2.bill_type = 1
where
  t1.bill_type = 2 and 
  t2.bill_type is null;

Demo on SQL Fiddle

Upvotes: 0

Related Questions