payday
id day employee income expenses tax
1 7 3 600 100 30
2 14 3 650 150 35
3 14 2 680 200 38
SELECT p.income, p.tax, ps.expenses
FROM paydays p
LEFT JOIN paydays ps ON ps.day+7 = p.day
WHERE p.day = 14 AND p.employee = 3
this gives what i want, 650 income and 35 tax from row with day 14,
and 100 expenses from row with day 7
now the problem,
SELECT SUM(p.income), SUM(p.tax), SUM(ps.expenses)
FROM paydays p
LEFT JOIN paydays ps ON ps.day+7 = p.day
WHERE p.day = 14
it should give
income = 1330
tax = 73
expenses = 100
but it multiplies fields by 2, income = 2660, tax = 146.....
Maybe I am missing something in your explanation but can't you use a subquery to get the expenses, similar to this:
SELECT SUM(p.income) TotalIncome,
SUM(p.tax) TotalTax,
ps.expenses Expenses
FROM paydays p
INNER JOIN
(
select sum(expenses) expenses, day
from paydays
where day = 7
group by day
) ps
ON ps.day+7 = p.day
WHERE p.day = 14
GROUP BY ps.expenses
See SQL Fiddle with Demo
The result is:
| TOTALINCOME | TOTALTAX | EXPENSES |
-------------------------------------
| 1330 | 73 | 100 |
SELECT
SUM(payday.income) as Income ,
SUM(payday.tax) as Tax,
p.expenses as Expanses
FROM payday
LEFT JOIN payday as p ON p.id = payday.id - 1
WHERE day = 14
Related
I have 3 mysql tables:
appointments
id slot_id patient_name doctor_id deleted_at
1 11 Tasin 23 2019-10-10
2 12 Nawaz 22 null
3 13 Rakib 23 null
4 14 Hossen 23 null
5 15 Aritra 24 null
6 16 Anik 22 null
7 17 Manik 22 null
doctors
id status doctor_name
22 1 Khaled
23 1 Hasan
24 0 Rumi
slots
id date duration time
11 2019-10-10 2900 01:01
12 2019-10-11 1200 02:01
13 2019-10-18 1100 03:01
14 2019-09-08 200 11:01
15 2019-08-01 500 01:31
16 2019-10-07 300 02:31
17 2019-10-02 1200 03:31
Now, I want to show a list of doctors with their total appointment durations in decreasing order using SQL query.
Unfortunately, I don't have any idea about this SQL query. Can you assist me?
SELECT DOCTOR_NAME, SUM(DURATION) FROM APPOINTMENTS A
JOIN DOCTORS D ON D.ID = A.DOCTOR_ID
JOIN SLOTS S ON S.ID = A.SLOT_ID
GROUP BY D.ID, DOCTOR_NAME
ORDER BY SUM(DURATION) DESC;
select d.doctor_id, d.doctor_name, sum(apt.duration) as total_duration from
doctors as d
join appointments as apt on apt.doctor_id = d.doctor_id
join slots as s on s.id = apt.slot_id
group by d.doctor_id, d.doctor_name
The above query should work fine.
There might be some typo as I didn't write it in the SQL management studio.
I have 2 table on my transaction
Table One
id | date | cust_id | driver_number
1 2019-01-02 1 F 3350 NN
2 2019-04-02 2 AX 111 Z
3 2019-05-02 3 S 787 X
4 2019-05-02 4 T 9090 M
5 2019-06-02 3 P 8989 L
Table Two
driver_number | price
F 3350 NN 350000
AX 111 Z 400000
S 787 X 375000
T 9090 M 900000
P 8989 L 500000
How do I count total transaction from two tables above in one month as per requested .
In example, request for total transaction in May so the result is like below
period | total
May 1275000
Thank you
Using MONTH(T1.date) = 5 and SUM(price) the expected result is achievable
SELECT MONTH(T1.date) AS Period, SUM(price) AS `Total`
FROM TableOne T1
JOIN TableTwo T2 ON T2.driver_number = T1.driver_number
WHERE MONTH(T1.date) = 5
GROUP BY MONTH(T1.date)
I've got a table of sales transactions and I've been asked to report only on items that have two sales within a 12 month period. For example:
id | Item | Sale Date
---------------------
1 | A | 2017-02-03
2 | C | 2016-05-04
3 | A | 2016-08-23
4 | B | 2016-03-25
5 | D | 2015-07-30
6 | A | 2013-04-19
7 | E | 2011-03-12
8 | B | 2017-05-20
9 | E | 2011-05-04
Item A has three sales records, but only transactions 1 & 3 should be returned as transaction 6 is more than 12 months from transaction 3.
Item B has two sales, but greater than 12 months apart so should be excluded.
Item E has two sales within 12 months of each other so should be included.
I'm using this to find entries with multiple sales:
SELECT * FROM salesdata.sales
INNER JOIN
(SELECT Item_Code, COUNT(*) c from salesdata.sales
GROUP BY Item_Code HAVING c > 1) as vals
ON sales.Item_Code = vals.Item_Code;
But can't figure out how to test if the two sales for an item are within 12 months of each other.
SELECT DISTINCT x.*
FROM my_table x
JOIN my_table y
ON y.id <> x.id
AND y.item = x.item
AND DATEDIFF(GREATEST(y.sale_date,x.sale_date),LEAST(y.sale_date,x.sale_date)) < 365;
You can get a list of items that have had a sale within the previous 12 months:
select s.*
from salesdata.sales s
where exists (select 1
from salesdata s2
where s2.item_code = s.item_code and
s2.sale_date >= s.sale_date - interval 1 year and
s2.sale_date < s.sale_date
);
Your question is unclear what you want to do with this information, but this retrieves all items.
SELECT s.*
from salesdata.sales s
INNER JOIN (
SELECT COUNT(item), item
FROM salesdata.sales
WHERE Sale Date >= DATEADD(year, -1, GETDATE())
GROUP BY item
HAVING COUNT(item) > 1
) b
on s.item = b.item
and s.Sale Date >= DATEADD(year, -1, GETDATE())
Id MedId ShipId AvailableQuant DefaultQuant MinQuant MedExpiry LastUsed
------ ------ ------ -------------- ------------ -------- ------------------- ---------------------
1 1 2918 20 30 15 2015-02-05 11:37:24 2014-12-01 11:37:32
4 2 2918 50 55 30 2015-03-26 11:57:14 2014-12-03 11:57:22
5 3 2918 15 40 20 2014-12-10 16:58:58 2014-12-10 16:59:02
6 4 2918 30 75 30 2015-03-31 11:58:26 2014-12-03 11:58:32
7 5 2918 22 50 20 2015-01-01 11:59:05 2014-12-03 11:59:09
9 6 3095 5 35 10 2014-12-03 11:59:51 2014-09-01 11:59:55
10 7 2918 30 60 35 2014-12-01 12:00:43 2014-10-22 12:00:57
11 8 3095 25 30 20 2014-12-31 17:48:58 2014-12-01 17:49:12
And there are 2 queries that i have written
1)To give me count of critical Items
SELECT SUM(IF(m.AvailableQuant <= m.MinQuant,1, 0)) AS criticalFROM tbl_vesselmaster vs
INNER JOIN comp_login cl ON vs.co_id = cl.id
INNER JOIN m_shipinv m ON vs.id = m.ShipId
WHERE vs.co_id=$co_id;
2)To give me medicine that have excedeed expiry date
SELECT COUNT(MedId) as count FROM `m_shipinv` WHERE DATE(MedExpiry) < DATE(NOW());
i want a query to get the count intersection of this both query. means if the item is critical and its medicine is expired then it should count only 1
this should be the output
count
-------
4
I think you can just add the where clause to the first query:
SELECT SUM(m.AvailableQuant <= m.MinQuant) AS critical
FROM tbl_vesselmaster vs INNER JOIN
comp_login cl
ON vs.co_id = cl.id INNER JOIN
m_shipinv m
ON vs.id = m.ShipId
WHERE vs.co_id = $co_id OR DATE(MedExpiry) < DATE(NOW());
Note that I simplified the SUM() calculation. The if is not needed because MySQL treats booleans as integers in a numeric context.
Can't you just put the two conditions in the where clause ?
I don't understand all you inner joins as you only describe one table. I just altered the select of Gordon Linoff:
SELECT count(*) AS critical
FROM tbl_vesselmaster vs
INNER JOIN comp_login cl ON vs.co_id = cl.id
INNER JOIN m_shipinv m ON vs.id = m.ShipId
WHERE vs.co_id = $co_id
AND DATE(MedExpiry) < DATE(NOW())
AND m.availableQuant <= m.minQuant;
I think the result should be 3: only records 5, 9 and 10 meet both criteria.
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