Here are two queries with results, I need to run one query to get same result.
1-Total slots
2-created Date
3-start date
4-end date
5-Unused Slots
First Query:-
SELECT COUNT( id ) as total_slots ,
created_date, MIN( DATE ) as start_date ,
MAX( DATE ) as end_date
FROM slots
GROUP BY created_date;
Query 1(Result)
Here is image with query result
Can I get unused slots in same query as I am getting from below query?
But here
SELECT COUNT( id ) AS unused
FROM slots
WHERE user_id =0
AND created_date = '2016-10-01 20:20:20'
Result Query with created date 2016-10-01 20:20:20
unused
79
SELECT COUNT( id ) AS unused
FROM slots
WHERE user_id =0
AND created_date = '2016-10-01 20:24:45'
Result Query with created date 2016-10-01 20:24:45
unused
51
Try
SELECT
COUNT( id ) as total_slots,
created_date,
MIN( DATE ) as start_date,
MAX( DATE ) as end_date,
COUNT(CASE WHEN user_id = 0 THEN 1 END) as unused_slots
FROM slots
GROUP BY created_date;
Related
I would like to know the first date of the first 3 consecutive entries that are between two dates. Based on my SQLFiddle, I would expect the output to be '2021-01-24'.
I've looked at many examples but can't get them to work.
This query is not working how I want it to, I can't figure out the missing piece of my query. Here is the SQLFIDDLE: http://sqlfiddle.com/#!9/935fbd/1
SELECT DISTINCT
logDate
FROM
FoodLog
WHERE
studentID = '1329' AND logDate BETWEEN '2021-01-01' AND '2021-05-01'
GROUP BY
logDate
HAVING
COUNT(logDate) = 3
I've tried working with the following, but can't figure out how to limit the search to studentID='1329' or my date range:
SELECT DISTINCT
f.id,
f.logDate
FROM
FoodLog f,
(
SELECT
f1.logDate START,
f2.logDate NEXT
FROM
FoodLog f1,
FoodLog f2
WHERE
f2.logDate <= DATE_ADD(f1.logDate, INTERVAL 1 DAY) AND f2.logDate > f1.logDate
) f2
WHERE
f.logDate = f2.start OR(
f.logDate = f2.next AND f2.start IS NOT NULL
)
LIMIT 1
WITH
cte1 AS (
SELECT DISTINCT logDate
FROM FoodLog
WHERE logDate BETWEEN '2021-01-01' AND '2021-05-01'),
cte2 AS (
SELECT logDate, LEAD(logDate, 2) OVER (ORDER BY logDate) next2date
FROM cte1
)
SELECT MIN(logDate) logDate
FROM cte2
WHERE DATEDIFF(next2date, logDate) = 2;
fiddle
I have three separate queries and need to combine it in one to prepare data for a chart. So the common parameter for all 3 datasets is the date.
Please help me to do it.
1)
SELECT COUNT(id) Registrations, regdate
FROM wpu_users
WHERE regdate >= '2020-01-09'
GROUP BY regdate;
2)
SELECT ROUND(SUM(total)) as amount, DATE(datetime) as date
FROM wp_payments
WHERE site_id=4 AND status=1
GROUP BY date
ORDER BY date DESC
3)
SELECT ROUND(SUM(cnt)) as Matrix, DATE(datetime) as date
FROM wp_payments
WHERE site_id=4
AND status=1
AND currency='USD'
AND description LIKE '% + %'
AND datetime IS NOT NULL
GROUP BY date
The result should be a table with columns:
Registrations
amount
Matrix
date
Based on your requirement. try this
SELECT
tab1.Registrations as "Registrations",
tab2.amount as "Amount",
tab3.Matrix as "Matrix",
tab1.regdate as "Date"
FROM
( SELECT COUNT( id ) Registrations, regdate FROM wpu_users where regdate >= '2020-01-09' GROUP BY regdate ) tab1
INNER JOIN (
SELECT
ROUND( SUM( total ) ) AS amount,
DATE( datetime ) AS date
FROM
wp_payments
WHERE
site_id = 4
AND STATUS = 1
GROUP BY
date
) tab2 ON tab1.regdate = tab2.date
INNER JOIN (
SELECT
ROUND( SUM( cnt ) ) AS Matrix,
DATE( datetime ) AS date
FROM
wp_payments
WHERE
site_id = 4
AND STATUS = 1
AND currency = 'USD'
AND description LIKE '% + %'
AND datetime IS NOT NULL
GROUP BY
date
) tab3 ON tab1.regdate = tab3.date
I have 2 queries right now for which I am looking to combine into 1 if possible.
I have open tickets stored in the Tickets_Open table and closed tickets in Tickets_Closed. Both tables have "Date_Requested" and "Date_Completed" columns. I need to count the number of tickets requested and completed each day.
My tickets requested count query is the following:
SELECT SUM(Count) AS TotalOpen, Date FROM(
SELECT COUNT(Ticket_Request_Code) AS Count, Date_Requested AS Date
FROM Tickets_Closed
WHERE Date_Requested >='2018-01-01 00:00:00'
GROUP BY(Date_Requested)
UNION
SELECT COUNT(Work_Request_Code) AS Count, Date_Requested AS Date
FROM Tickets_Open
WHERE Date_Requested >='2018-01-01 00:00:00'
GROUP BY(Date_Requested)
) AS t1 GROUP BY Date ORDER BY `t1`.`Date` DESC
My tickets completed count query is the following:
SELECT COUNT(Ticket_Request_Code) AS CountClosed, Date_Completed AS Date
FROM Tickets_Closed
Where Date_Completed >='2018-01-01 00:00:00'
GROUP BY(Date_Completed)
Both queries return the correct result. For open it returns with the column headings Date and TotalOpen. For close it returns with the column headings Date and CountClosed.
Is it possible to return it with the following column headings Date, TotalOpen, CountClosed.
You can combine these as:
SELECT Date, SUM(isopen) as isopen, SUM(isclose) as isclose
FROM ((SELECT date_requested as date, 1 as isopen, 0 as isclose
FROM Tickets_Closed
WHERE Date_Requested >= '2018-01-01'
) UNION ALL
(SELECT date_requested, 1 as isopen, 0 as isclose
FROM Tickets_Open
WHERE Date_Requested >= '2018-01-01'
) UNION ALL
(SELECT date_closed as date, 0 as isopen, 1 as isclose
FROM Tickets_Closed
WHERE date_closed >= '2018-01-01'
)
) t
GROUP BY Date
ORDER BY Date DESC;
This assumes that Ticket_Request_Code and Work_Request_Code are not NULL. If COUNT() is really being used to check for NULL values, then add the condition to the WHERE clause in each subquery.
This query uses the FULL OUTER JOIN on the Dates as well, but it correctly adds the Open/Closed counts together to give you a TotalOpen Count. This will also handle possible NULL values for cases where you have a day that doesn't close any tickets.
WITH open AS
(
SELECT COUNT(Work_Request_Code) AS OpenCount, Date_Requested AS Date
FROM Tickets_Open
WHERE Date_Requested >='2018-01-01 00:00:00'
GROUP BY(Date_Requested)
)
, close AS
(
SELECT COUNT(Ticket_Request_Code) AS ClosedCount, Date_Requested AS Date
FROM Tickets_Closed
WHERE Date_Requested >='2018-01-01 00:00:00'
GROUP BY(Date_Requested)
)
SELECT
COALESCE(c.Date, o.Date) AS Date
, IFNULL(o.OpenCount, 0) + IFNULL(c.ClosedCount, 0) AS TotalOpen
, IFNULL(c.CountClosed, 0) AS CountClosed
FROM open o
FULL OUTER JOIN closed c ON o.Date = c.Date
I need to get a table that contains the most popular product sold per day. All data is stored in Magento, and I use MySQL to write the query. The only table I need is Sales_flat_order_item table.
The final table should have 3 columns: Date, Product SKU, and number of units sold of the most popular product that day - MaxQty.
I came up with the query that works for me, but I would like to know how it can be improved since I use the same subquery twice in my code:
1 Select Date, Product Id, Sku, and Quantity from sales_flat_order_item - Subquery1
2 Select Date and Maximum Quantity from Subquery1 - Subquery2
3 Join them together knowing that dates should be the same, and Quantity from Subquery1 should be equal to Maximum Quantity from Subquery2
SELECT DATE( sq2.created_at ) AS CreatedAt, sq0.sku AS SKU, sq2.MaxQty
FROM (
SELECT created_at, product_id, sku, SUM( qty_ordered ) AS qty
FROM `sales_flat_order_item`
GROUP BY DATE( created_at ) , product_id
) AS sq0
JOIN (
SELECT sq.created_at, MAX( sq.qty ) AS MaxQty
FROM (
SELECT created_at, product_id, SUM( qty_ordered ) AS qty
FROM `sales_flat_order_item`
GROUP BY DATE( created_at ) , product_id
) AS sq
GROUP BY DATE( sq.created_at )
) AS sq2 ON DATE( sq2.created_at ) = DATE( sq0.created_at )
AND sq2.MaxQty = sq0.qty
GROUP BY DATE( CreatedAt )
I believe this should do what you want.
I added a WHERE clause to run it only for this month, in case you have a huge database so it should not take much time.
SELECT day, sku, MAX(qty_total) AS qty FROM (
SELECT DATE(created_at) AS day, sku, SUM(qty_ordered) AS qty_total
FROM `sales_flat_order_item`
WHERE created_at > '2015-07%'
GROUP BY sku, day
ORDER BY qty_total DESC
) AS item_count
GROUP BY day
I have this query:
SELECT DATE( a.created_at ) AS order_date, count( * ) as cnt_order
FROM `sales_order_item` AS a
WHERE MONTH( a.created_at ) = MONTH( now())-1
GROUP BY order_date
which will return result something like this (snapshot only otherwise will return per 31 days):
order_date cnt_order
2012-08-29 580
2012-08-30 839
2012-08-31 1075
and my full query is selecting based on above selection:
SELECT order_date
, MAX(cnt_order) AS highest_order
FROM (
SELECT DATE (a.created_at) AS order_date
, count(*) AS cnt_order
FROM `sales_order_item` AS a
WHERE MONTH(a.created_at) = MONTH(now()) - 1
GROUP BY order_date
) AS tmax
But it result :
order_date highest_order
2012-08-01 1075
Which has the date wrong and always pick the first row of date where it suppose 2012-08-31. Maybe this is a simple error that I dont know. So how to get the date right point to 2012-08-31? Any help would be great.
You could try ordering the sub query result set; something like:
SELECT
DATE (a.created_at) AS order_date,
COUNT(*) AS cnt_order
FROM
`sales_order_item` AS a
WHERE
MONTH(a.created_at) = MONTH(now()) - 1
GROUP BY
order_date
ORDER BY
cnt_order DESC
You can add ORDER BY order_date DESC in subquery.