SELECT month,sum(pcs) as pcs,sum(carat) as carat,sum(amount) as amount from (
SELECT
DATE_FORMAT(StockInwardDate, '%b-%Y') AS Month,
COUNT(*) as pcs,
ROUND(SUM(Carat),2) as carat,
ROUND(SUM(TotalAmount),2) as amount
FROM tbl_sales
GROUP BY DATE_FORMAT(StockInwardDate, '%m-%Y')
UNION
SELECT
DATE_FORMAT(StockInwardDate, '%b-%Y') AS Month,
COUNT(*) as pcs,
ROUND(SUM(Carat),2) as carat,
ROUND(SUM(TotalAmount),2) as amount
FROM tbl_stock
GROUP BY DATE_FORMAT(StockInwardDate, '%m-%Y')
) as tbl
GROUP BY tbl.Month
ORDER by tbl.Month DESC
This above is my query but tbl.Month in order by which is equals to DATE_FORMAT(StockInwardDate, '%m-%Y') But instead of DATE_FORMAT(StockInwardDate, '%m-%Y') I want to get order by StockInwardDate only this
Is there any way to get This in order by StockInwardDate
Related
I'm working on a mysql request that make the sum of values by months including those with null values.
The request result send only the first line without making the sum operation.
SELECT SUM(IFNULL(t1.sub_total,0)) AS amount,
am.a_month AS date
FROM (
SELECT ifnull(vn.sub_total,0) as sub_total,
cast(DATE_FORMAT(order_date, '%M') as char) as mdate
FROM orders_new vn
WHERE order_status = 1
AND order_date BETWEEN '2022-01-01' AND '2022-12-31'
GROUP BY DATE_FORMAT(order_date, '%M')
) t1
RIGHT OUTER JOIN all_months am on t1.mdate = am.a_month
group by am.a_month
order by a_month_id asc;
result
below the source table
You don't need the GROUP BY clause in the subquery. Your query should be:
SELECT
SUM(IFNULL(t1.sub_total, 0)) AS amount,
am.a_month AS date
FROM
(SELECT
IFNULL(vn.sub_total, 0) AS sub_total,
CAST(DATE_FORMAT(order_date, '%M') AS char) AS mdate
FROM
orders_new vn
WHERE
order_status = 1
AND order_date BETWEEN '2022-01-01' AND '2022-12-31') t1
RIGHT OUTER JOIN
all_months am ON t1.mdate = am.a_month
GROUP BY
am.a_month
ORDER BY
a_month_id ASC;
I am using sakila.payment table.
Columns: payment_id, customer_id, staff_id, rental_id, amount, payment_date, update_date
I am using this query to get customers spending the highest amount for each month.
How can I get the Nth highest spending customer for each month?
select customer_id,`month`,max(total_amount) from
(SELECT customer_id,count(customer_id) as `count`,month(payment_date) as `month`,sum(amount) as total_amount
FROM sakila.payment
group by month(payment_date),customer_id
order by `month` asc, `total_amount` desc)t
group by `month`
Try the following, if you are using MySQL 8.0 then it will work with row_number()
select
customer_id,
month,
total_amount
from
(
select
customer_id,
month,
total_amount,
row_number() over (partition by month order by total_amount desc) as rnk
from
(
select
customer_id,
count(customer_id) as `count`,
month(payment_date) as `month`,
sum(amount) as total_amount,
from sakila.payment
group by
month(payment_date),
customer_id
) cal
) mnt
where rnk = 1
I'm trying to reduce these queries to one so it's not three separate queries. Is there a better way to do this other than what I have? I'm also trying to sum all the totals together IE (invoiced_total + refund_total + credit_memo_total). The tables have no relation other than the customer, but I'm not looking to group by customer, just date. Any help would be much appreciated.
SELECT
SUM(qbi.amount) AS invoiced_total,
DATE_FORMAT(qbi.line_item_date, '%Y-%m-01') AS date
FROM
invoices as qbi
WHERE
qbi.`line_item_date` BETWEEN '2018-12-01' AND '2019-12-31'
GROUP BY
DATE_FORMAT(qbi.`line_item_date`, '%Y-%m-01')
ORDER BY
DATE_FORMAT(qbi.`line_item_date`, '%Y-%m-01');
SELECT
SUM(amount) AS refund_total,
DATE_FORMAT(refund_date, '%Y-%m-01') AS refund_date
FROM
refunds
WHERE
refund_date BETWEEN '2018-12-01' AND '2019-12-31'
GROUP BY
DATE_FORMAT(refund_date, '%Y-%m-01')
ORDER BY
DATE_FORMAT(refund_date, '%Y-%m-01');
SELECT
SUM(amount) AS credit_memo_total,
DATE_FORMAT(credit_memo_date, '%Y-%m-01') AS credit_memo_date
FROM
credit_memos
WHERE
credit_memo_date BETWEEN '2018-12-01' AND '2019-12-31'
GROUP BY
DATE_FORMAT(credit_memo_date, '%Y-%m-01')
ORDER BY
DATE_FORMAT(credit_memo_date, '%Y-%m-01');
If you want to join these queries you must also use a 4th query which will return all the distinct months from the 3 tables just in case there is a month missing in any of them.This is why you should use left joins for the tables:
SELECT d.date, i.invoiced_total, r.refund_total, c.credit_memo_total,
COALESCE(invoiced_total, 0) + COALESCE(refund_total, 0) + COALESCE(credit_memo_total, 0) AS total
FROM (
SELECT DATE_FORMAT(qbi.`line_item_date`, '%Y-%m-01') AS date FROM invoices UNION
SELECT DATE_FORMAT(refund_date, '%Y-%m-01') FROM refunds UNION
SELECT DATE_FORMAT(credit_memo_date, '%Y-%m-01') FROM credit_memos
) AS d
LEFT JOIN (
SELECT
SUM(qbi.amount) AS invoiced_total,
DATE_FORMAT(qbi.line_item_date, '%Y-%m-01') AS date
FROM
invoices as qbi
WHERE
qbi.`line_item_date` BETWEEN '2018-12-01' AND '2019-12-31'
GROUP BY
DATE_FORMAT(qbi.`line_item_date`, '%Y-%m-01')
) i ON i.date = d.date
LEFT JOIN (
SELECT
SUM(amount) AS refund_total,
DATE_FORMAT(refund_date, '%Y-%m-01') AS refund_date
FROM
refunds
WHERE
refund_date BETWEEN '2018-12-01' AND '2019-12-31'
GROUP BY
DATE_FORMAT(refund_date, '%Y-%m-01')
) r ON r.date = d.date
LEFT JOIN (
SELECT
SUM(amount) AS credit_memo_total,
DATE_FORMAT(credit_memo_date, '%Y-%m-01') AS credit_memo_date
FROM
credit_memos
WHERE
credit_memo_date BETWEEN '2018-12-01' AND '2019-12-31'
GROUP BY
DATE_FORMAT(credit_memo_date, '%Y-%m-01')
) c ON c.date = d.date
WHERE d.date BETWEEN '2018-12-01' AND '2019-12-31'
ORDER BY d.date
You would typically join the three queries. Assuming that all monthes are available in three tables, you could do:
SELECT
i.invoice_month my_month,
i.invoiced_total,
r.refund_total,
c.credit_total,
i.invoiced_total + r.refund_total + c.credit_total amount_total
FROM (
SELECT
SUM(amount) AS invoiced_total,
DATE_FORMAT(line_item_date, '%Y-%m-01') AS invoice_month
FROM invoices
WHERE line_item_date BETWEEN '2018-12-01' AND '2019-12-31'
GROUP BY invoice_month
) i
INNER JOIN (
SELECT
SUM(amount) AS refund_total,
DATE_FORMAT(refund_date, '%Y-%m-01') AS refund_month
FROM refunds
WHERE refund_date BETWEEN '2018-12-01' AND '2019-12-31'
GROUP BY refund_month
) r
INNER JOIN (
SELECT
SUM(amount) AS credit_total,
DATE_FORMAT(credit_memo_date, '%Y-%m-01') AS credit_month
FROM credit_memos
WHERE credit_memo_date BETWEEN '2018-12-01' AND '2019-12-31'
GROUP BY credit_memo_month
) c
ORDER BY i.invoice_month
Side note: in MySQL you can use column aliases in the GROUP BY clause (and in the ORDER BY clause): this helps shortening the query.
I have a food selling website in which there is order table which record the order of every user.It column for user id ,user name,orderid ,timestamp of order.I want to know the maximum number of order that has been made in any one hour span through out the day.Give me any formula for this,or any algorithm or any sql queries for these.
SQL server:
with CTE as
(
select cast(t1.timestamp as date) as o_date, datepart(hh, t1.timestamp) as o_hour, count(*) as orders
from MyTable t1
group by cast(t1.timestamp as date), datepart(hh, t1.timestamp)
)
select o_date, o_hour, orders
from CTE
where orders = (select max(orders) from CTE)
Oracle
with CTE as
(
select to_char(t1.timestamp, 'YYYYMMDD') as o_date, to_char(t1.timestamp, 'HH24') as o_hour, count(*)
from MyTable t1
group by to_char(t1.timestamp, 'YYYYMMDD'), to_char(t1.timestamp, 'HH24')
)
select o_date, o_hour, orders
from CTE
where orders = (select max(orders) from CTE)
You can get count by day and hour like this
For SQL
SELECT TOP 1
COUNT(*)
FROM myTable
GROUP BY DATEPART(day, [column_date]), DATEPART(hour, [column_date])
ORDER BY COUNT(*) DESC;
For MySQL
SELECT
COUNT(*)
FROM myTable
GROUP BY HOUR(column_date), DAY(column_date)
ORDER BY COUNT(*) DESC
LIMIT 1;
I am trying to combine the results of a Union from
SELECT MONTHNAME(terms) AS month, COUNT(DISTINCT project_num) as total
FROM projects
WHERE terms >= '2017/01/01' AND Building_designer='SOMEPERSON'
GROUP BY MONTH(terms)
UNION
SELECT MONTHNAME(terms) AS month, COUNT(DISTINCT project_num) as total
FROM archive
WHERE terms >= '2017/01/01' AND Building_designer='SOMEPERSON'
GROUP BY MONTH(terms)
I get the following: RESULTS FROM THE SQL STATEMENT
I am trying to make it so the total will be the combination of the multiple instances of the month.
The sql tables are exactly the same.
This Is what I would like it to look like:
A FULL OUTER JOIN would be ideal. But in your case, let's do two levels of aggregation:
SELECT month, MAX(total_projects) as total_projects, MAX(total_archive) as total_archive
FROM ((SELECT MONTHNAME(terms) AS month, COUNT(DISTINCT project_num) as total_projects, 0 as total_archive
FROM projects
WHERE terms >= '2017/01/01' AND Building_designer = 'SOMEPERSON'
GROUP BY MONTH(terms)
) UNION ALL
(SELECT MONTHNAME(terms) AS month, 0, COUNT(DISTINCT project_num
FROM archive
WHERE terms >= '2017/01/01' AND Building_designer = 'SOMEPERSON'
GROUP BY MONTH(terms)
)
) pa
GROUP BY month
ORDER BY month;
EDIT:
Oops. You only want one column. If you want to count the number of distinct projects for each month, then do a union all and then combine the results at the next higher level:
SELECT month, COUNT(DISTINCT project_num) as total
FROM ((SELECT MONTHNAME(terms) AS month, project_num
FROM projects
WHERE terms >= '2017/01/01' AND Building_designer = 'SOMEPERSON'
) UNION ALL
(SELECT MONTHNAME(terms) AS month, project_num
FROM archive
WHERE terms >= '2017/01/01' AND Building_designer = 'SOMEPERSON'
)
) pa
GROUP BY month
ORDER BY month;
A quick thought would be to just do something like this. You essentially want to sum the counts from each table.
select month, sum(total) from
(
SELECT MONTHNAME(terms) AS month, COUNT(DISTINCT project_num) as total FROM projects WHERE terms >= '2017/01/01' AND Building_designer='SOMEPERSON' GROUP BY MONTH(terms)
UNION
SELECT MONTHNAME(terms) AS month, COUNT(DISTINCT project_num) as total FROM archive WHERE terms >= '2017/01/01' AND Building_designer='SOMEPERSON' GROUP BY MONTH(terms)
) group by month;
transform into a derived table, put an alias then aggregate
select
x.month,
sum(x.total) [Total]
from (
SELECT
MONTHNAME(terms) AS month,
COUNT(DISTINCT project_num) AS total
FROM projects
WHERE terms >= '2017/01/01'
AND Building_designer = 'SOMEPERSON'
GROUP BY MONTH(terms)
UNION
SELECT
MONTHNAME(terms) AS month,
COUNT(DISTINCT project_num) AS total
FROM archive
WHERE terms >= '2017/01/01'
AND Building_designer = 'SOMEPERSON'
GROUP BY MONTH(terms)
) x
group by x.month
You can try creating a sum expression on the entire query.
SELECT month, SUM (total) FROM
(SELECT MONTHNAME(terms) AS month, COUNT(DISTINCT project_num) as total FROM projects WHERE terms >= '2017/01/01' AND Building_designer='SOMEPERSON' GROUP BY MONTH(terms)
UNION
SELECT MONTHNAME(terms) AS month, COUNT(DISTINCT project_num) as total FROM archive WHERE terms >= '2017/01/01' AND Building_designer='SOMEPERSON' GROUP BY MONTH(terms))
GROUP BY month