I have the following query:
SELECT SUM(COUNTED) AS TCOUNTED
FROM (SELECT COUNTED FROM `clicks`.`t1`
UNION ALL
SELECT COUNTED FROM `clicks`.`t2`
UNION ALL
SELECT COUNTED FROM `clicks`.`t3`
) AS TMP
Where and how do I add a time constraint? I want it to count only between certain dates... Sorry for the MySQL noobness.
Thanks!
SELECT SUM(COUNTED) AS TCOUNTED
FROM (SELECT COUNTED FROM `clicks`.`t1` WHERE somedatecol BETWEEN somedate AND somedate
UNION ALL
SELECT COUNTED FROM `clicks`.`t2` WHERE somedatecol BETWEEN somedate AND somedate
UNION ALL
SELECT COUNTED FROM `clicks`.`t3` WHERE somedatecol BETWEEN somedate AND somedate
) AS TMP`
http://dev.mysql.com/doc/refman/5.0/en/select.html
I would think your best bet is to include the where clause inside each sub-select before the UNION statements.
SELECT SUM(COUNTED) AS TCOUNTED FROM
(SELECT COUNTED FROM clicks.t1 WHERE <fieldname> BETWEEN '<date1>' AND '<date2>'
UNION ALL
SELECT COUNTED FROM clicks.t2 WHERE <fieldname> BETWEEN '<date1>' AND '<date2>'
UNION ALL
SELECT COUNTED FROM clicks.t3 WHERE <fieldname> BETWEEN '<date1>' AND '<date2>')
AS TMP;
Of course it as all relative to what you are trying to accomplish with the query.
Related
I have this query, it works but I'm not sure if it's the best approach and I don't get what I want.
I need to select the query contained in the "IN" clause first, then union with others. Entire row returned must be 40.
SELECT *
FROM (
SELECT * FROM tbl_x a WHERE id IN(11,20,30)
UNION ALL
SELECT * FROM tbl_x b WHERE exam_group='jpx' AND subject='chemistry'
) ab
GROUP BY id LIMIT 40
The next query should to return same data in simple way:
SELECT *
FROM tbl_x
WHERE
id IN (11,20,30)
OR (exam_group='jpx' AND subject='chemistry')
ORDER BY id IN (11,20,30) DESC, id
LIMIT 40;
I would like to select all number of years that occur more than once in between two columns.
Here is what I have so far:
SELECT YEAR(`Date1`), COUNT(*) as Counter
from (SELECT YEAR(`Date1`)
from table1 UNION
SELECT YEAR(`date2`)
from table1
) as year
GROUP by YEAR(`date1`)
WHERE Counter > 2;
I appreciate any advice!
Thanks.
When you are using GROUP BY , you need to use HAVING not WHERE like following.
SELECT Y ,
COUNT(*) AS Counter
FROM (
SELECT DISTINCT YEAR(`Date1`) Y
FROM table1
UNION ALL
SELECT DISTINCT YEAR(`date2`) Y
FROM table1) AS YEAR
GROUP BY Y
HAVING COUNT(*) > 2;
Note: You don't need to put YEAR again in your outer query, also you can put DISTINCT, for each column so that you don't get duplicates for the column.
I think your problem is the UNION. It needs to be UNION ALL, or you will never find duplicates.
Then, you can simply do:
SELECT yyyy, COUNT(*) as Counter
FROM (SELECT YEAR(`Date1`) as yyyy
FROM table1
UNION ALL
SELECT YEAR(`date2`) as yyyy
FROM table1
) y
GROUP BY yyyy
HAVING Counter >= 2;
Note the changes to the query:
UNION ALL instead of UNION so the subquery does not eliminate duplicates.
Giving a column alias to the year, in this case, yyyy.
Using the column alias in the outer query.
Using HAVING instead of WHERE.
I have 3 tables in 3 different databaes; Currently the goal here is to find the duplicates unique ID in the three databases and then find the lowest price value of the duplicates unique ID.
Currently I'm using a INNER JOIN to query between only 2 database... Can anyone advise on how to add the third one?
set #a = (SELECT db1.tb1.var1 from db1.tb1
INNER JOIN db2.tb1 ON db2.tb1.var1 = db1.tb1.var1
UNION );
Also, once I have the #a variable set to the duplicate, I wanted to grab a secondary value here.
SELECT price
FROM db1.tb1
WHERE asin=#a
UNION ALL
SELECT price
FROM db2.tb1
WHERE asin=#a
UNION ALL
SELECT price
FROM db3.tb1
WHERE asin=#a
However, the result I'd get would return multiple rows (obviously), How do I query only for the MIN() number from this ?
Any help is appreciated.
Thanks,
Put your query into a subquery, and then use MIN() in the main query.
SELECT MIN(price)
FROM (
SELECT price
FROM db1.tb1
WHERE asin=#a
UNION ALL
SELECT price
FROM db2.tb1
WHERE asin=#a
UNION ALL
SELECT price
FROM db3.tb1
WHERE asin=#a) AS x
You can use order by and limit:
SELECT price
FROM db1.tb1
WHERE asin = #a
UNION ALL
SELECT price
FROM db2.tb1
WHERE asin = #a
UNION ALL
SELECT price
FROM db3.tb1
WHERE asin = #a
ORDER BY price
LIMIT 1;
select count(*) as total FROM ( SELECT * FROM database1.orders WHERE number LIKE "11111111111111111" UNION ALL SELECT * FROM database2.orders WHERE number LIKE "11111111111111111" ) AS b
but i got error :
The used SELECT statements have a different number of columns
because run SELECT * FROM database2.orders WHERE number LIKE "11111111111111111" give me a result is null.
How to merge it with a query because with a query to help me process the pagination
Thank for helps !
Just do the aggregation before the union all:
select sum(cnt) as total
FROM ((SELECT count(*) as cnt
FROM database1.orders
WHERE number LIKE '11111111111111111'
)
UNION ALL
(SELECT count(*) as cnt
FROM database2.orders
WHERE number LIKE '11111111111111111'
)
) t;
Note I changed the string delimiter to be a single quote rather than a double quote. It is good practice to use single quotes for string and date constants (and nothing else).
By the way, you can also do this using a join:
select o1.cnt1, o2.cnt1, (o1.cnt1 + o2.cnt1) as total
FROM (SELECT count(*) as cnt1
FROM database1.orders
WHERE number LIKE '11111111111111111'
) o1 cross join
(SELECT count(*) as cnt2
FROM database2.orders
WHERE number LIKE '11111111111111111'
) o2;
This makes it easier to get the individual counts for the two databases.
The orders table in database1 probably has a different number of columns than the table by the same name in database2.
Instead of using select *, select the columns you're interested in, like select userid, productid, deliveryaddress, .... Make sure you specify the same columns in both parts of the union.
For a count(*), you could choose no columns at all, and select the value 1 for each row, like:
select count(*)
from (
select 1
from database1.orders
where number like '111'
union all
select 1
from database2.orders
where number like '111'
) as SubQueryAlias
Or you can add the result of two subqueries without a union:
select (
select count(*)
from database1.orders
where number like '111'
)
+
(
select count(*)
from database2.orders
where number like '111'
)
Please help me figure a single query that will transform the data below...
|id |status_a |status_b |
|+++++++++++++++++++++++|
| 1|active |inactive |
...into this one.
|status_group |count|
|++++++++++++++++++++++++|
|status_a.active | 1|
|status_b.inactive | 1|
edit: If a single pass query is possible then that will be better. Also, does a query with unions does a single pass?
If status can be either only active or inactive, I'd suggest a different approach:
SELECT
sum(if(status_a='active',1,0)) AS status_a_active,
sum(if(status_a='inactive',1,0)) AS status_a_inactive,
sum(if(status_b='active',1,0)) AS status_b_active,
sum(if(status_b='inactive',1,0)) AS status_b_inactive
FROM table
Otherwise you need to use the UNION approach, but I'd do it a little differently. First, you can use UNION ALL, because you don't need to remove duplicates in the result. I'd also use GROUP BY only once like this:
SELECT status_group, count(id)
FROM (
SELECT CONCAT('status_a.', status_a) AS status_group, id FROM table
UNION ALL
SELECT CONCAT('status_b.', status_b) AS status_group, id FROM table
) a
GROUP BY status_group
I have a solution that uses UNIONs. Shown here:
SELECT 'status_a.active' AS status_group, COUNT(*) AS count FROM `test` WHERE status_a = 'active'
UNION
SELECT 'status_a.inactive' AS status_group, COUNT(*) AS count FROM `test` WHERE status_a = 'inactive'
UNION
SELECT 'status_b.active' AS status_group, COUNT(*) AS count FROM `test` WHERE status_b = 'active'
UNION
SELECT 'status_b.inactive' AS status_group, COUNT(*) AS count FROM `test` WHERE status_b = 'inactive'
Basically, it queries each condition for status_a or status_b being active or not. We get four such queries and we apply UNION to all of them.
I suppose, I've to move my comment a while ago which is also a shorter solution here than hw's.
SELECT CONCAT('status_a.', status_a) AS stat, COUNT(id) FROM base GROUP BY stat
UNION
SELECT CONCAT('status_b.', status_b) AS stat, COUNT(id) FROM base GROUP BY stat