This my Query
SELECT COUNT(*) as total, toys, date FROM T1
WHERE (date >= '2012-06-26'AND date < '2012-06-30') AND (Avail > '0')
UNION
SELECT COUNT(*) as total, toys, date FROM T2
WHERE (date >= '2012-06-26'AND date < '2012-06-30') AND (Avail > '0')
UNION
SELECT COUNT(*) as total, toys, date FROM T3
WHERE (date >= '2012-06-26'AND date < '2012-06-30') AND (Avail > '0')
GROUP BY RoomType
HAVING COUNT( total ) = 4
Output result
count Toys date
3 Bibi 2012-06-26
4 Baba 2012-06-26
How can i get MYSQL to show results only for count=4
SELECT * FROM (
SELECT COUNT(*) as total, toys, date FROM T1
WHERE (date >= '2012-06-26' AND date < '2012-06-30') AND (Avail > '0')
GROUP BY RoomType
UNION
SELECT COUNT(*) as total, toys, date FROM T2
WHERE (date >= '2012-06-26' AND date < '2012-06-30') AND (Avail > '0')
GROUP BY RoomType
UNION
SELECT COUNT(*) as total, toys, date FROM T3
WHERE (date >= '2012-06-26'AND date < '2012-06-30') AND (Avail > '0')
GROUP BY RoomType) AS src
WHERE total = 4;
Please, note, that for proper data groupping you must have all columns either in the GROUP BY clause or as arguments to the aggregate functions. It is MySQL feature to allow you to avoid this restriction, but it might lead you to the unexpected results.
You need group by first then union the result.
SELECT COUNT(*) as total, toys, date FROM T1
WHERE (date >= '2012-06-26'AND date < '2012-06-30') AND (Avail > '0')
GROUP BY RoomType HAVING COUNT( *) = 4
UNION
SELECT COUNT(*) as total, toys, date FROM T2
WHERE (date >= '2012-06-26'AND date < '2012-06-30') AND (Avail > '0')
GROUP BY RoomType HAVING COUNT( * ) = 4
UNION
SELECT COUNT(*) as total, toys, date FROM T3
WHERE (date >= '2012-06-26'AND date < '2012-06-30') AND (Avail > '0')
GROUP BY RoomType HAVING COUNT( * ) = 4
Related
SELECT
"Fact Order Item"."product_therapy" AS "Product_therapy"
,(select sum(total_order_value_inc_tax_uc) FROM "d2c"."fact_order"
WHERE and date_created >= DATEADD(day,-7, GETDATE()) ) as "REVENUE - T7"
,(select sum(total_order_value_inc_tax_uc) FROM "d2c"."fact_order"
WHERE and date_created >= DATEADD(day,-30, GETDATE()) ) as "REVENUE - T30"
FROM "d2c"."fact_order"
LEFT JOIN "d2c"."fact_order_item" ON "d2c"."fact_order"."order_id" = "Fact Order Item"."order_id"
WHERE "d2c"."fact_order"."is_active" = '1'
GROUP BY "Fact Order Item"."product_therapy"
im trying to take 3 sql queries and insert them into 1 table without getting the null value's and using a group by number as to not get duplicate numbers in the same column.
I have the issue where running query 1 leaves me with a bunch of null data values
and running query 2 doesnt group the numbers resulting in thousands of rows numbers only go up to 100
QUERY 1
insert into table ( number)
select number as 1day from table where date = CURDATE() - interval 1day group by number
insert into table ( number)
select number as 2day from table where date = CURDATE() - interval 1day group by number
insert into table ( number)
select number as 7day from table where date = CURDATE() - interval 1day group by number
so i try to run
QUERY 2
insert into table (number,number,number)
select
*
from
(select number as 1day from test.test where date = curdate() - interval 1 day group by
number) as 1day,
(select number as 2day from test.test where date > curdate() - interval 2 day group by
number) as 2day,
(select number as 7day from test.test where date > curdate() - interval 7 day group
by number) as 7day;
try the below:
insert into table (number,number,number)
select
table.1day,table.2day,table.7day
from
((select number from test.test where date = curdate() - interval 1 day group by
number) as 1day,
(select numberfrom test.test where date > curdate() - interval 2 day group by
number) as 2day,
(select number from test.test where date > curdate() - interval 7 day group
by number) as 7day) as table
select (case one.number when two.number then null else one.number end) as '1day',(case two.number <= third.number when true then (case one.number = two.number when true then null else two.number end) else (case one.number = two.number when false then null else two.number end) end) as '2day',(case (third.number < one.number and third.number = two.number) when true then null else third.number end) as '7day'
from (
(select x.number
from (
(select number,'1day' as 'type' from testtable where date = curdate() - interval 1 day group by number)
union all
(select number,'2day' as 'type' from testtable where date > curdate() - interval 2 day group by number)
union all
(select number,'7day' as 'type' from testtable where date > curdate() - interval 7 day group by number)) as x
where x.type='2day' order by x.number) as two,
(select x.number
from (
(select number,'1day' as 'type' from testtable where date = curdate() - interval 1 day group by number)
union all
(select number,'2day' as 'type' from testtable where date > curdate() - interval 2 day group by number)
union all
(select number,'7day' as 'type' from testtable where date > curdate() - interval 7 day group by number)) as x
where x.type='1day' order by x.number) as one,
(select x.number
from (
(select number,'1day' as 'type' from testtable where date = curdate() - interval 1 day group by number)
union all
(select number,'2day' as 'type' from testtable where date > curdate() - interval 2 day group by number)
union all
(select number,'7day' as 'type' from testtable where date > curdate() - interval 7 day group by number)) as x
where x.type='7day' order by x.number) as third
)
where ((one.number = two.number) or (one.number is null or two.number is null)) or
((third.number = two.number) or (two.number is null or third.number is null))
SELECT CONCAT(MONTH(revenue_date),"/ ", YEAR(revenue_date)) as month_year,
(SELECT COUNT(DISTINCT DATE(delivery_date)) as operating_days
FROM table2 where weekday(delivery_date) <> 5 and
weekday(delivery_date) <> 6 and delivery_date < current_time and
year(delivery_date) > 2015 GROUP BY YEAR(delivery_date), MONTH(delivery_date)),
sum(revenue) as total_revenue,
from table1
where revenue_date < current_time and year(revenue_date) > 2015
group by year(revenue_date), month(revenue_date);
This shows the error "subquery returns more than 1 value". If I don't include group by in the subquery it returns just a total of all of the months but I need it to return it separately for all months.
Try this:
SELECT CONCAT(MONTH(revenue_date),"/ ", YEAR(revenue_date)) as month_year, a.operating_days
sum(revenue) as total_revenue,
from table2 JOIN
(SELECT YEAR(delivery_date) as `year`, MONTH(delivery_date) as `month`, COUNT(DISTINCT DATE(delivery_date)) as operating_days
FROM table2 where weekday(delivery_date) <> 5 and
weekday(delivery_date) <> 6 and delivery_date < current_time and
year(delivery_date) > 2015 GROUP BY YEAR(delivery_date), MONTH(delivery_date)) a
ON year(revenue_date) = a.year AND month(revenue_date) = a.month
where revenue_date < current_time and year(revenue_date) > 2015
group by year(revenue_date), month(revenue_date);
Join with the subquery.
SELECT DATE_FORMAT('%c/%Y', revenue_date) as month_year,
sum(revenue) as total_revenue, operating_days
from table1
JOIN (SELECT YEAR(delivery_date) AS delivery_year, MONTH(delivery_date) AS delivery_month,
COUNT(DISTINCT DATE(delivery_date)) as operating_days
FROM table2
where weekday(delivery_date) <> 5 and
weekday(delivery_date) <> 6 and delivery_date < current_time and
year(delivery_date) > 2015
GROUP BY delivery_year, delivery_month) AS t2
ON YEAR(revenue_date) = delivery_year AND MONTH(revenue_date) = delivery_month
where revenue_date < current_time
group by year(revenue_date), month(revenue_date);
I'm doing this select statement:
SELECT * FROM (
SELECT COUNT(t.text) as count, COUNT(DISTINCT(t.from_user_id)) as usercount, DATE_FORMAT(t.created_at,'%Y-%m-%d %H:00') datepart
FROM TABLE1 t WHERE t.created_at >= '2015-08-12 00:00:00' AND t.created_at <= '2015-08-13 18:30:00' AND t.eliminar IS NULL
GROUP BY datepart) as t
UNION ALL
SELECT * FROM (
SELECT COUNT(b.id) as count, COUNT(DISTINCT(b.from_user_id)) as usercount, DATE_FORMAT(b.created_at,'%Y-%m-%d %H:00') datepart
FROM TABLE2 b WHERE b.created_at >= '2015-08-12 00:00:00' AND b.created_at <= '2015-08-13 18:30:00' AND b.eliminar IS NULL
GROUP BY datepart) as x GROUP BY datepart
this select gets this:
I'm trying to view with datepart grouped but I can't, any idea what I'm doing wrong?
TABLE2 only have (id,from_user_id,eliminar) and all are NULL except created_at, in this row I have entire 2015 year by day and hour, same format as TABLE1
SOLVED:
SELECT DISTINCT * FROM (
SELECT COUNT(t.text) as count, COUNT(DISTINCT(t.from_user_id)) as usercount, DATE_FORMAT(t.created_at,'%Y-%m-%d %H:00') datepart
FROM TABLE1 t WHERE t.created_at >= '2015-08-12 00:00:00' AND t.created_at <= '2015-08-13 18:30:00' AND t.eliminar IS NULL
GROUP BY datepart
UNION ALL
SELECT COUNT(t.id) as count, COUNT(DISTINCT(t.from_user_id)) as usercount, DATE_FORMAT(t.created_at,'%Y-%m-%d %H:00') datepart
FROM TABLE2 t WHERE t.created_at >= '2015-08-12 00:00:00' AND t.created_at <= '2015-08-13 18:30:00' AND t.eliminar IS NULL
GROUP BY datepart) as x GROUP BY datepart ORDER BY datepart
Hi I have below 5 mysql queries, i want to see output of all queries in by executing a single query.
please let me know how can i join all these queries.
1.
select sum(msu)
from wgr_raw
where dt
between '2013-11-01' and '2013-11-07';
2.
select *
from wgr_raw
where dt
between '2013-11-01' and '2013-11-07'
order by dt;
3.
select admin,sum(msu)
from wgr_raw
where dt
between '2013-11-01' and '2013-11-07'
group by admin;
4.
SELECT admin, sum(msu)
from wgr_raw
where dt
between '2013-11-01' and '2013-11-07'
group by admin
order by msu desc
limit 25;
Select ADMIN1,WEEK1,WEEK2,WEEK3,
(((WEEK3-WEEK2)/WEEK2)*100) as percentage_change,
(WEEK3-WEEK2) as MSU_Difference
from
((select admin as ADMIN1, sum(msu) as WEEK1
from wgr_raw
where dt >= date_sub(date('2013-11-01'),
INTERVAL 25 DAY) and dt < date_sub(date('2013-11-07'),
INTERVAL 18 DAY)
group by admin
order by WEEK1) as q1,
(select admin as ADMIN2, sum(msu) as WEEK2
from wgr_raw
where dt >= date_sub(date('2013-11-01'),
INTERVAL 18 DAY) and dt < date_sub(date('2013-11-07'),
INTERVAL 11 DAY)
group by admin
order by WEEK2) as q2,
(select admin as ADMIN3, sum(msu) as WEEK3
from wgr_raw where dt >= date_sub(date('2013-11-01'),
INTERVAL 11 DAY) and dt < date_sub(date('2013-11-07'),
INTERVAL 4 DAY)
group by admin
order by WEEK3) as q3)
where ADMIN1=ADMIN3 and ADMIN2=ADMIN3
group by admin1
order by WEEK1 desc
limit 25;
5.
select dt,sum(msu)
from wgr_raw
where dt >= date_sub(date('2013-11-01'),
INTERVAL 11 DAY)
group by dt
limit 7;
You can merge n number of select queries using UNION but there is one condition in UNION. Your all select fields should be same and order by will be common.
(
(SELECT a, b FROM tbl WHERE id >= 50 and id <= 100)
UNION
(SELECT a, b FROM tbl WHERE id >= 150 and id <= 200)
UNION
(SELECT a, b FROM tbl WHERE id >= 250 and id <= 300)
UNION
(SELECT a, b FROM tbl WHERE id >= 350 and id <= 400)
)
ORDER BY a