Below is table , I need output which provide me all columns with group by date1 which has sum of salary ( > 6000) for particular date.
source table ( INput table ) :
id name date1 salary
1 JOHNSON 1990-12-17 1800
2 HARDING 1990-12-17 5200
3 TAFT 1990-12-17 2500
4 HOOVER 1990-04-02 2700
5 LINCOLN 1990-04-02 2250
6 GARFIELD 1990-04-02 5400
7 POLK 1997-09-22 2500
8 GRANT 1997-09-22 320
Desired output table (below) : ( last 2 entry 7,8 is not present)
id name date1 salary sum(salary)
1 JOHNSON 1990-12-17 1800 9500
2 HARDING 1990-12-17 5200 9500
3 TAFT 1990-12-17 2500 9500
----------
4 HOOVER 1990-04-02 2700 10350
5 LINCOLN 1990-04-02 2250 10350
6 GARFIELD 1990-04-02 5400 10350
-----------
(Last two values should not come because for that date sum of salary is < 6000)
Oracle Setup:
CREATE TABLE table_name (id, name, date1, salary ) AS
SELECT 1, 'JOHNSON', DATE '1990-12-17', 1800 FROM DUAL UNION ALL
SELECT 2, 'HARDING', DATE '1990-12-17', 5200 FROM DUAL UNION ALL
SELECT 3, 'TAFT', DATE '1990-12-17', 2500 FROM DUAL UNION ALL
SELECT 4, 'HOOVER', DATE '1990-04-02', 2700 FROM DUAL UNION ALL
SELECT 5, 'LINCOLN', DATE '1990-04-02', 2250 FROM DUAL UNION ALL
SELECT 6, 'GARFIELD', DATE '1990-04-02', 5400 FROM DUAL UNION ALL
SELECT 7, 'POLK', DATE '1997-09-22', 2500 FROM DUAL UNION ALL
SELECT 8, 'GRANT', DATE '1997-09-22', 320 FROM DUAL;
Query:
SELECT *
FROM (
SELECT t.*,
SUM( salary ) OVER ( PARTITION BY date1 ) AS sum_salary
FROM table_name t
)
WHERE sum_salary >= 6000;
Output:
ID NAME DATE1 SALARY SUM_SALARY
---------- -------- ------------------- ---------- ----------
4 HOOVER 1990-04-02 00:00:00 2700 10350
6 GARFIELD 1990-04-02 00:00:00 5400 10350
5 LINCOLN 1990-04-02 00:00:00 2250 10350
3 TAFT 1990-12-17 00:00:00 2500 9500
2 HARDING 1990-12-17 00:00:00 5200 9500
1 JOHNSON 1990-12-17 00:00:00 1800 9500
select * from
(
select id, name,
sum(salary) over (partition by date1) sum_sal
from table_name
) where sum_sal >= 6000;
Below query will work on both sql server 2008 and Mysql :-
select * from (
select a.id,a.name,a.date1,a.salary,
(select sum(salary) from table_name b where b.date1=a.date1) Sum_Salary
from
table_name a
) c
where Sum_Salary >=6000
order by id
Output :-
id name date1 salary Sum_Salary
1 JOHNSON 1990-12-17 1800.00 9500.00
2 HARDING 1990-12-17 5200.00 9500.00
3 TAFT 1990-12-17 2500.00 9500.00
4 HOOVER 1990-04-02 2700.00 10350.00
5 LINCOLN 1990-04-02 2250.00 10350.00
6 GARFIELD 1990-04-02 5400.00 10350.00
Related
I have the following table:
id post_id user_id to_user_id date time
---- ---------- -------- ------------ ------
1 100 1 2 10:00
2 100 1 2 10:30
3 100 2 2 11:00
4 100 5 2 11:30
5 100 8 2 11:45
6 105 10 50 09:00
7 105 2 50 09:30
8 105 11 50 11:00
9 105 30 50 11:30
10 105 32 50 11:45
On the following table you can see that user_id 2 has comments for post 100 and 105.
I need to get only the records per post_id that is hight than the first comment he wrote.
so for this example the result will be records 4 and 5 for post 100 and 8, 9, 10 for post 105 because 4, 5 is bigger than 3 (first record for user_id 2)
and 8, 9, 10 is bigger than 7 (user_2 first comment)
clear expected result:
id post_id user_id to_user_id date time
4 100 5 2 11:30
5 100 8 2 11:45
8 105 11 50 11:00
9 105 30 50 11:30
10 105 32 50 11:45
Could be with a subselect and an aggregation function
select * from my_table
where ( post_id, date_time) > (select post_id, max( date_time)
from my_table where user_id =2
group y post_id);
or if the tuple version donìt work properly try
select * from my_table as m
inner join (select post_id, max( date_time)
from my_table where user_id =2
group y post_id ) t on m.post_id = t.post_id
where m.date_time > t.date_time
Expenses table
1/1/2016 exp1 2000
13/1/2016 exp11 2500
1/2/2016 exp2 1500
1/3/2016 exp3 1000
10/3/2016 exp1 2000
Income table
1/1/2016 income1 2500
1/2/2016 income2 3500
1/3/2016 income3 1500
10/3/2016 income3 1000
1/4/2016 income4 5000
From single query what I need is group by month, this is what I need
Expenses Incomes Month
4500 2500 Jan
1500 3500 Feb
3000 2500 Mar
0 5000 April
I need the above query to show the data in Google graph
Terrible data structure and format, but not impossible:
SELECT
IFNULL(exp.Expenses,0) Expenses,
IFNULL(inc.Incomes,0) Incomes,
inc.`monthname` `Month`
FROM
(
SELECT
SUM(i.amount) Incomes,
MONTHNAME(STR_TO_DATE(i.`date`, '%d/%m/%Y')) `monthname`,
MONTH(STR_TO_DATE(i.`date`, '%d/%m/%Y')) `month`
FROM
incomes i
GROUP BY
MONTHNAME(STR_TO_DATE(i.`date`, '%d/%m/%Y')),
MONTH(STR_TO_DATE(i.`date`, '%d/%m/%Y'))
) inc
LEFT JOIN
(
SELECT
SUM(e.amount) Expenses,
MONTHNAME(STR_TO_DATE(e.`date`, '%d/%m/%Y')) `monthname`,
MONTH(STR_TO_DATE(e.`date`, '%d/%m/%Y')) `month`
FROM
expenses e
GROUP BY
MONTHNAME(STR_TO_DATE(e.`date`, '%d/%m/%Y')),
MONTH(STR_TO_DATE(e.`date`, '%d/%m/%Y'))
) exp
ON exp.`month` = inc.`month`
ORDER BY
inc.`month`
Output of this simplicity:
+----------+---------+----------+
| Expenses | Incomes | Month |
+----------+---------+----------+
| 4500 | 2500 | January |
| 1500 | 3500 | February |
| 3000 | 2500 | March |
| 0 | 5000 | April |
+----------+---------+----------+
4 rows in set
Anyway better thing seriously how to improve and normalize your data.
In my solution, I give the number of the month rather than text. I'll leave it to you to convert it to text (using a CASE expression, for example) if you wish:
SELECT
sum(expense) AS total_expense, sum(income) AS total_income, trans_month
FROM (
SELECT
month(trans_date) AS trans_month,
0 AS income,
sum(amount) AS expense
FROM expense
GROUP BY month(trans_date)
UNION ALL
SELECT
month(trans_date) AS trans_month,
sum(amount) AS income,
0 AS expense
FROM income
GROUP BY month(trans_date)
) AS a
GROUP BY trans_month;
Mysql query I have following tables
Company table:-
id company_name company_createddate
1 ABC 2015-01-01 12:45:23
2 LMN 2015-01-11 09:45:23
3 PQR 2015-02-16 23:45:23
User table:-
id username
1 John
2 Mary
Order table:-
id company_id user_id order_name order_createddate
1 1 1 Sales 2015-02-16 09:45:23
2 1 1 Sales3 2015-02-20 09:45:23
3 1 1 Marketing 2015-02-24 09:45:23
4 2 1 Sales2 2015-02-17 09:45:23
5 1 2 M1 2015-02-16 09:45:23
6 2 2 M2 2015-02-23 09:45:23
7 2 2 P1 2015-02-26 09:45:23
Output required:-
Week day user name Pipeline Orders
16 Feb - 22 Feb John 2 (ABC), 1(LMN)
23 Feb - 01 Mar John 3 (ABC), 1(LMN)
16 Feb - 22 Feb Mary 1 (ABC)
23 Feb - 01 Mar Mary 1 (ABC), 2(LMN)
Week days is order_createddate
In 16Feb-22Feb Week, 2 orders created for company ABC
And in 23Feb-01Mar Week, 1 order created for company ABC, I want to show total number of order generated till 01 Mar Week, So i need output of 3.
Please help!
select concat(STR_TO_DATE(concat('2015 ',wk,' 1'), '%Y %u %w'), ' to ',
STR_TO_DATE(concat('2015 ',wk,' 0'), '%Y %u %w')),
User.username, GROUP_CONCAT(som) from (
select DATE_FORMAT(order_createddate,'%u') wk,user_id,
concat(Company.company_name ,'(',CONVERT(count(1),CHAR),')') som from `Order`
join Company on `Order`.company_id = Company.id
group by wk,user_id,company_name ) t
join User on t.user_id = User.id
group by wk, User.username
the time limit should be in sub select clause t.
if you dont like the week format, change them by modify the first line's date format
sql fiddle here
I've 4 tables as shown below
doctors
id name
------------
1 Mathew
2 Praveen
3 Rosie
4 Arjun
5 Denis
doctors_appointments
id doctors_id patient_name contact date status
--------------------------------------------------------------------------------------
1 5 Nidhin 9876543210 2012-12-10 15:39:41 Registered
2 5 Sunny 9876543210 2012-12-18 15:39:48 Registered
3 5 Mani 9876543210 2012-12-12 15:39:57 Registered
4 2 John 9876543210 2012-12-24 15:40:09 Registered
5 4 Raj 9876543210 2012-12-05 15:41:57 Registered
6 3 Samuel 9876543210 2012-12-14 15:41:33 Registered
7 2 Louis 9876543210 2012-12-24 15:40:23 Registered
8 1 Federick 9876543210 2012-12-28 15:41:05 Registered
9 2 Sam 9876543210 2012-12-12 15:40:38 Registered
10 4 Sita 9876543210 2012-12-12 15:41:00 Registered
doctors_dutyplan
id doctor_id weeks time no_of_patients
------------------------------------------------------------------
1 1 3,6,7 9:00am-1:00pm 10
2 2 3,4,5 1:00pm-4:00pm 7
3 3 3,6,7 10:00am-2:00pm 10
4 4 3,4,5,6 8:30am-12:30pm 12
5 5 3,4,5,6,7 9:00am-4:00pm 30
emp_leave
id empid leavedate
--------------------------------
1 2 2012-12-05 14:42:36
2 2 2012-12-03 14:42:59
3 3 2012-12-03 14:43:06
4 3 2012-12-06 14:43:14
5 5 2012-12-04 14:43:24
My task is to find all the days in a month in which the doctor is available excluding the leave dates.
My query what is wrote is given below:
SELECT DATE_ADD( '2012-12-01', INTERVAL
ROW DAY ) AS Date,
ROW +1 AS DayOfMonth
FROM (
SELECT #row := #row +1 AS
ROW FROM (
SELECT 0
UNION ALL SELECT 1
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
)t1, (
SELECT 0
UNION ALL SELECT 1
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
)t2, (
SELECT #row := -1
)t3
LIMIT 31
)b
WHERE DATE_ADD( '2012-12-01', INTERVAL
ROW DAY )
BETWEEN '2012-12-01'
AND '2012-12-31'
AND DAYOFWEEK( DATE_ADD( '2012-12-01', INTERVAL
ROW DAY ) ) =2
AND DATE_ADD( '2012-12-01', INTERVAL
ROW DAY ) NOT
IN (
SELECT DATE_FORMAT( l.leavedate, '%Y-%m-%d' ) AS date
FROM doctors_dutyplan d
LEFT JOIN emp_leave AS l ON d.doctor_id = l.empid
WHERE doctor_id =2
)
This works fine for all doctors who took any leave in a particular day in a month (here in the example it is Decemeber 2012). and the result for the above query is shown below:
Date DayOfMonth
-----------------------
2012-12-10 10
2012-12-17 17
2012-12-24 24
2012-12-31 31
But on the other hand for the doctors who didn't took any leave , for that my query is showing empty table, example for the doctor Mathew whose id is 1, my query returns an empty result
can anyone please tell a solution for this problem.
Thanks in advance.
Your query is large, but this part looks fishy:
NOT IN (
SELECT DATE_FORMAT( l.leavedate, '%Y-%m-%d' ) AS date
FROM doctors_dutyplan d
LEFT JOIN emp_leave AS l ON d.doctor_id = l.empid
WHERE doctor_id =2
The left join means a null would be returned for doctor 1. Now, col1 not in (null) does not behave as you may expect. It translates to:
col1 <> null
Which is never true. You could solve this by changing the left join to an inner join, so an empty set instead of null is returned for a doctor without leave.
I have following data set
id name units release_date
1 salil 1 2012-04-02
2 salil 2 2012-03-21
3 salil 3 2012-04-02
4 salil 4 2012-03-02
5 salil 5 2012-04-02
6 xyz 1 2012-04-01
7 xyz 2 2012-03-30
8 xyz 3 2012-03-30
9 xyz 4 2012-04-01
I want the SUM of an unit for Maximum date for each name. something like follwing
name units Max(release_date)
salil 9 2012-04-02
xyz 5 2012-04-01
I try following but it is not working
SELECT name, MAX(release_date) as date, sum(units) as units FROM reports
GROUP BY name;
Try below :
SELECT name, sum(units) as units
FROM reports as r
LEFT JOIN (select max(date) as maxdate from reports group by name ) as mr
ON r.date=mr.maxdate
WHERE artist_name='Pacer1' GROUP BY name;