i want to select booktitle with status paid
but if there no booktitle with status paid display other booktitle with other status
ITEMS
ID bookname code
1 cats 222
2 dogs 555
3 cows 777
4 goat 888
5 rubbit 999
PRODUCTS
ID booktitle status
1 222 paid
2 555 paid
3 777 notpyed
4 888 waitpyed
5 999 notmoney
Query
SELECT
snd.code, m.booktitle FROM
products as m
JOIN items as snd ON snd.code = m.booktitle WHERE CASE WHEN (m.status = 'paid') > 0 THEN m.status = 'paid' ELSE m.status = 'notpyed' OR m.status = 'waitpyed'
OR m.status = 'notmoney' END
You use a subquery to get the sum of books paid
The else part could be m.status <> 'paid' , but i don't know how many sti you have
SELECT
snd.code, m.booktitle FROM
PRODUCTS as m
JOIN ITEMS as snd ON snd.code = m.booktitle
WHERE CASE WHEN (SELECT SUM(status = 'paid') FROM PRODUCTS) > 0 THEN m.status = 'paid'
ELSE m.status = 'notpyed' OR m.status = 'waitpyed'
OR m.status = 'notmoney' END
code | booktitle
---: | --------:
222 | 222
555 | 555
db<>fiddle here
Related
I have some problem in mysql, I have data like this
id
date
renewaltime
111
2020-01-01
1
111
2020-01-02
1
111
2020-01-03
1
111
2020-01-04
1
111
2020-01-05
1
but I wanna output be like this in mysql
id
date
renewaltime
renewal_by_date
111
2020-01-01
1
1
111
2020-01-02
1
2
111
2020-01-03
1
3
111
2020-01-04
1
4
111
2020-01-05
1
5
adding the value with value before in diffrent datetime
SELECT
h.id as ID,
i.date as DATE,
count(it.relid) as RENEWALTIME
FROM tblhosting h
INNER JOIN tblinvoiceitems it on h.id=it.relid
INNER JOIN tblinvoices i on it.invoiceid=i.id
WHERE h.id =305864
and i.status = 'Paid'
AND it.type = 'Hosting'
AND h.regdate <> it.duedate
GROUP BY i.date
please if you have same problem and share withme
thanks
On MySQL 8+, this is a cinch with analytic functions:
SELECT h.id, i.date,
SUM(COUNT(it.relid)) OVER (ORDER BY i.date) RENEWALTIME
FROM tblhosting h
INNER JOIN tblinvoiceitems it ON h.id = it.relid
INNER JOIN tblinvoices i ON it.invoiceid = i.id
WHERE
h.id = 305864 AND
i.status = 'Paid' AND
it.type = 'Hosting' AND
h.regdate <> it.duedate
GROUP BY
i.date
ORDER BY
i.date;
This question already has an answer here:
MySQL pivot row into dynamic number of columns
(1 answer)
Closed 4 years ago.
I have three tables e.g client, product and purchase.
Purchase:
id productId clientId amount
1 1 2 2500
2 2 3 3500
3 3 4 4500
4 6 1 5500
5 1 2 1500
6 3 3 2000
7 3 2 1000
Client:
id name
1 A
2 B
3 C
4 B
Product:
id product
1 Apple
2 Banana
3 Mango
6 Sweet
I am able to query this
SELECT client.id, client.client_name, product.product, purchase.amount from client INNER JOIN purchase ON client.id=purchase.clientId INNER JOIN product ON product.id=purchase.productId GROUP BY client.id
My output is:
id client_name product amount
1 A Sweet 5500
2 B Apple 2500
3 c Mango 3500
4 D Banana 4500
But I want output like where is the amount of each client purchased
Desired Output:
id client_name Apple Banana Mango Sweet
1 A x x x x
2 B X x x x
3 c X x x x
4 D x x x x
How can I do that with query. thanks
If you have a limited products then i would use conditional aggregation :
SELECT c.id, c.client_name,
SUM(CASE WHEN pd.id = 1 THEN p.Amount ELSE 0 END) AS Apple,
SUM(CASE WHEN pd.id = 2 THEN p.Amount ELSE 0 END) AS Banana,
SUM(CASE WHEN pd.id = 3 THEN p.Amount ELSE 0 END) AS Mango,
SUM(CASE WHEN pd.id = 6 THEN p.Amount ELSE 0 END) AS Sweet
FROM client c INNER JOIN
purchase p
ON c.id = p.clientId INNER JOIN
product pd
ON pd.id = p.productId
GROUP BY c.id, c.client_name;
The easiest way if your Purchase table is static
select
c.id,
c.name,
(select SUM(p.amount) from Purchase p where p.productId = 1 and p.clientId = c.id) as Apple,
(select SUM(p.amount) from Purchase p where p.productId = 2 and p.clientId = c.id) as Banana,
(select SUM(p.amount) from Purchase p where p.productId = 3 and p.clientId = c.id) as Mango,
(select SUM(p.amount) from Purchase p where p.productId = 6 and p.clientId = c.id) as Sweet
from Client c
I have a Source table as shown below..
Name Subject Marks Year
A Science 88 2015
A Social 75 2015
A Social 75 2015
A Maths 22 2015
B Social 75 2015
B Maths 50 2014
C Science 88 2015
C Social 75 2014
D Science 88 2015
D Social 75 2015
A Social 75 2015
B Maths 50 2014
and I have a requirement as below like if any student has satisfies both as in below requirements then he should be awarded as respected requirement set name in the another table B
Set1
Social 75 2015
Science 88 2015
Set2
Social 75 2015
Maths 50 2014
The expected output in the table B is as below
Name Status
A Set1
B Set2
C None
D Set1
try this :
SELECT NAME,
CASE WHEN SUM(SCIENCE) + SUM(SOCIAL) = 2 THEN 'GOOD' ELSE 'BAD' END AS Status
FROM (SELECT NAME,
CASE
WHEN SUBJECT = 'Science' AND MARKS = 88 AND YEAR = 2015
THEN 1 ELSE 0
END
AS SCIENCE,
CASE
WHEN SUBJECT = 'Social' AND MARKS = 75 AND YEAR = 2015
THEN 1 ELSE 0
END
AS SOCIAL
FROM A) group by Name;
SQLFiddle
[EDIT] if you have some rules to add, add a case in subquery, and update the top case :
SELECT NAME,
CASE WHEN MAX(SCIENCE) + MAX(SOCIAL) = 2 THEN 'Set1'
WHEN MAX(SOCIAL) + MAX(MATHS) =2 THEN 'Set2'
ELSE 'None'END AS Status
FROM (SELECT NAME,
CASE
WHEN SUBJECT = 'Science' AND MARKS = 88 AND YEAR = 2015
THEN 1 ELSE 0
END
AS SCIENCE,
CASE
WHEN SUBJECT = 'Social' AND MARKS = 75 AND YEAR = 2015
THEN 1 ELSE 0
END
AS SOCIAL,
CASE
WHEN SUBJECT = 'Maths' AND MARKS = 50 AND YEAR = 2014
THEN 1 ELSE 0
END AS MATHS
FROM A)x group by Name;
See the SQLFiddle
Try this query:
SELECT t2.Name AS Name, CASE WHEN SUM(CASE WHEN t2.Status = 'Set1' THEN 1 ELSE 0 END) >= 2 THEN 'Set1'
WHEN SUM(CASE WHEN t2.Status = 'Set2' THEN 1 ELSE 0 END) >= 2 THEN 'Set2'
ELSE 'None' END AS Status
FROM
(
SELECT DISTINCT
CASE WHEN (t.Subject = 'Social' AND t.Marks >= 75 AND t.Year = '2015') THEN 'Set1'
WHEN (t.Subject = 'Science' AND t.Marks >= 88 AND t.Year = '2015') THEN 'Set1'
WHEN (t.Subject = 'Social' AND t.Marks >= 75 AND t.Year = '2015') THEN 'Set2'
WHEN (t.Subject = 'Maths' AND t.Marks >= 50 AND t.Year = '2014') THEN 'Set2'
ELSE 'None' END AS Status,
t.Name, t.Subject, t.Marks, t.Year
FROM A t
) t2
GROUP BY t2.Name
SQLFiddle
Output:
+------+--------+
| Name | Status |
+------+--------+
| A | Set1 |
| B | Set2 |
| C | None |
| D | Set1 |
+------+--------+
I have table payments(Actual there is about 100k records, different categories, clients etc):
Client Dt Amt Category
1 201312 10 Tax
1 201401 10 Tax
1 201405 10 Tax
1 201406 10 Tax
2 201311 10 Tax
And i want to make cumulative sum YTD for category tax for every client. So the result will be like this:
Client Dt Amt Category
1 201312 10 Tax
1 201401 10 Tax
1 201405 20 Tax
1 201406 30 Tax
2 201311 10 Tax
Thank you
Try this:
SELECT a.Client, a.Dt, SUM(b.Amt) AS Amt, a.Category
FROM payments a
JOIN payments b ON b.Client = a.Client
AND b.Category = a.Category
AND b.Dt <= a.Dt
AND YEAR(b.Dt) = YEAR(a.Dt)
WHERE a.Category = 'Tax'
GROUP BY a.Client, a.Dt, a.Category
I have join with a multiple tables. You can find the tables and MySQL query in this demo.
These are the tables:
users
id name
1 abc
2 xyz
3 pqr
friend
user_id friend_id
1 2
1 3
2 3
collection
id user_id friend_id amount
1 1 2 100
2 2 1 -100
3 2 3 200
4 3 2 -200
5 1 3 300
6 3 1 -300
bill
id use_id(bill_creator)
1 1
2 2
3 2
bill_person
id bill_id user_id
1 1 1
2 1 2
3 1 3
4 2 2
5 2 3
6 3 2
6 3 1
So far I have made this query:
SELECT mf.id
, mf.name
, c.amount AS amount,count(bp.user_id) AS no_common_bills
FROM (
SELECT fr.user_id AS user_id
, fr.friend_id AS friend_id
FROM friend fr
JOIN users fru
ON fru.id = fr.user_id
WHERE fru.id IN (1)
UNION
SELECT fl.friend_id AS user_id
, fl.user_id AS friend_id
FROM friend fl
JOIN users flf
ON flf.id = fl.friend_id
WHERE flf.id IN (1)
) f
JOIN users mf
ON mf.id = f.friend_id
LEFT
JOIN collection c
ON c.friend_id = mf.id
AND c.user_id = f.user_id
LEFT JOIN bill_person bp
ON bp.user_id=f.user_id AND c.friend_id = mf.id
GROUP BY mf.id
ORDER BY mf.id
and my output of this query is
id NAME AMOUNT NO_COMMON_BILLS
2 XYZ 100 2
3 PQR 300 2
but I want this result:
id NAME AMOUNT NO_COMMON_BILLS
2 XYZ 100 2
3 PQR 300 1
I am getting wrong output at NO_COMMON_BILLS. Other than that, all values are correct.
Third time's a charm...
http://sqlfiddle.com/#!2/338dd/12
SELECT u.name friend
, COUNT(bp2.user_id) no_common_bills
FROM users u
LEFT
JOIN
( SELECT user_id me, friend_id them FROM friend WHERE user_id = 1
UNION
SELECT friend_id,user_id FROM friend WHERE friend_id = 1
) x
ON x.them = u.id
LEFT
JOIN bill_person bp1
ON bp1.user_id = x.them
LEFT
JOIN bill_person bp2
ON bp2.bill_id = bp1.bill_id
AND bp2.user_id = x.me
WHERE u.id <> 1
GROUP
BY friend;
FRIEND NO_COMMON_BILLS
jkl 0
mno 0
pqr 1
xyz 2