I want to select from two identical tables using UNION ALL and GROUP BY. However, the Group BY doesn't work. Here is my query:
SELECT type , COUNT(subscription.id) as number ,SUM(subscription.amount) as total
FROM subscription
WHERE DATE(subscription.timestamp) BETWEEN '2022-10-18' AND '2022-10-18'
UNION ALL
SELECT type , COUNT(archive_subscription.id) as number ,SUM(archive_subscription.amount) as total
FROM archive_subscription
WHERE DATE(archive_subscription.timestamp) BETWEEN '2022-10-18' AND '2022-10-18'
GROUP BY type
The result is like the following:
type
number
amount
1
2
180000
1
1
80000
What I want to do is two merge both table using GROUP BY but it won't work:
type
number
amount
1
3
260000
Please, any suggestions? Thanks
The first subquery in your union is missing its GROUP BY clause. Instead, try taking a union first and then aggregate:
SELECT type, COUNT(*) AS number, SUM(amount) AS total
FROM
(
SELECT type, amount
FROM subscription
WHERE timestamp BETWEEN '2022-10-18' AND '2022-10-18'
UNION ALL
SELECT type, amount
FROM archive_subscription
WHERE timestamp BETWEEN '2022-10-18' AND '2022-10-18'
) t
GROUP BY type;
Note that the ranges in your WHERE clauses are trivial. If you really want to restrict to a single date, just use WHERE timestamp = '2022-10-18' instead.
Related
I have a very basic table, consisting of an integer column and a timestamp column.
What's the query to count how many entries there are for each day?
When I use SELECT COUNT(*) FROM taps GROUP BY(DATE(time_stamp)) , I get the total number of rows int he table, rather than the number of rows for each DISTINCT date.
How do I need to modify the query?
Pretty straightforward.
SELECT
DATE(time_stamp),
COUNT(1)
FROM taps
GROUP BY DATE(time_stamp)
This is what I last tried.
(
SELECT
`offers`.`id` AS `offer`, `offers`.`date`
from `offers`
WHERE `offers`.`expired`='0'
ORDER BY `offers`.`date` DESC LIMIT 10
)
UNION ALL
(
SELECT
`vlog`.`video`,
`vlog`.`updated`
from `vlog`
ORDER BY `vlog`.`date` DESC LIMIT 10
)
For simplicity's sake, I have only two columns. I need to sort them by the date (not in the same results the code above provides) and show which is an offer and which is a video. Is this possible with no columns that link the tables.?
I'm assuming that video.video is an ID, since it lines up with the id column in the top query. You can select a literal string in both queries to indicate which query it came from:
SQL Fiddle
SELECT
'offer' AS type,
id,
date
FROM
offers
WHERE
expired = 0
UNION
SELECT
'video' AS type,
video AS id,
updated AS date
FROM
vlog
ORDER BY
date
I am querying a mysql table and want results group by date, and one column name is type. There are two value for the type call and email. I want to find count for call and email for each day.
Find the SQL Fiddle here
I am trying with this query. Which only gets me total counts:
SELECT Date(date) date,
COUNT(type) total,
COUNT(type='email') emails,
COUNT(type='call') calls
from leads
where user_id = 1
GROUP BY Date(date)
Use SUM() instead. The type='email' in the function returns either 0 (false) or 1 (true).
SELECT Date(date) date,
COUNT(type) total,
SUM(type='email') emails,
SUM(type='call') calls
from leads
where user_id = 1
GROUP BY Date(date)
Let's say that I have a table of race results. The table consists of seven columns as follows: Date ( MySql Date format of xxxx-xx-xx ), and one column each for the names of the top six finishers named First, Second, Third, Fourth, Fifth, and Sixth. I have a several sets of results and maybe 100 or so different names in the various finisher columns. I need a query that would allow me to list each person whose name has appeared in any of the finisher columns ( First, Second, Third, Fourth, Fifth, Sixth ) along with only the most recent date that their name appeared. I do NOT need separate results based on finish place, so I need all six of the finisher columns lumped together. Most of the names will appear on dozens of different dates, but I only need the most recent date that each name appeared. Ideally the result would generate a list of each name and their most recent finish date, sorted from least recent to most recent. I tried to create a fiddle to demonstrate this but for whatever reason I could not get the date to work correctly in the fiddle. Anyway, anyone who can offer even a shred of help on this would be greatly appreciated.
SELECT <table>.date, <table>.first as name FROM <table> GROUP BY name
UNION DISTINCT
SELECT <table>.date, <table>.second as name FROM <table> GROUP BY name
UNION DISTINCT
SELECT <table>.date, <table>.third as name FROM <table> GROUP BY name
UNION DISTINCT
SELECT <table>.date, <table>.fourth as name FROM <table> GROUP BY name
UNION DISTINCT
SELECT <table>.date, <table>.fifth as name FROM <table> GROUP BY name
UNION DISTINCT
SELECT <table>.date, <table>.sixth as name FROM <table> GROUP BY name
ORDER BY date
This gave me exactly the results I needed. I basically just used "name" to catch the finishers, and "MAX(Date) as last" to pick out only the most recent showing for each "name" when I queried all six columns.
SELECT name, MAX(Date) as last
FROM (
SELECT first name,Date FROM Results
UNION ALL
SELECT second, Date FROM Results
UNION ALL
SELECT third, Date FROM Results
UNION ALL
SELECT fourth, Date FROM Results
UNION ALL
SELECT fifth, Date FROM Results
UNION ALL
SELECT sixth, Date FROM Results) Q
GROUP BY name
ORDER BY last ASC;
select t.first, a.date from (select distinct(first) from race union select distinct(second) from race union select distinct(third) from race union select distinct(fourth) from race union select distinct(fifth) from race union select distinct(sixth) from race) as t, race a where t.first in (a.first, a.second, a.third, a.fourth, a.fifth, a.sixth)
I have "users" table with fields
user_name, user_id
I have data tables like
data_table_2012_10
data_table_2012_11
data_table_2012_12
data_table_2013_01
data_table_2013_02
each table contains the following fields
user_id, type ('ALARM', 'EMERGENCY', 'ALIVE', 'DEAD'), date_time
There will be millions of records in each table.
I have to select the count of type from the data_tables within the time frame given by the user, as well as have to get the corresponding name of the user with the help of user_id.
Can some one help me out with the best solution.
Try this query where DATE1 and DATE2 is your date range. You should union all tables in the inner query. Also you can try to make a query dynamically to include in the inner query only those tables that are in a date range you use:
select t.user_id,t.type, MAX(users.user_name), SUM(t.cnt)
from
(
select user_id,type,count(*) cnt
from data_table_2012_10 where date_time between DATE1 and DATE2
group by user_id,type
union all
select user_id,type,count(*) cnt
from data_table_2012_11 where date_time between DATE1 and DATE2
group by user_id,type
union all
.........................................
union all
select user_id,type,count(*) cnt
from data_table_2013_02 where date_time between DATE1 and DATE2
group by user_id,type
) t
left join users on (t.user_id=users.user_id)
group by t.user_id,t.type
Remember not to use UNION, but UNION ALL as UNION will return only merge similar rows into one and that may cause problem