I have 2 tables
tableA:
id
dateA
colA
...
1
2022-11-11 12:00:00
A
2
2022-11-12 12:00:00
B
3
2022-11-14 12:00:00
C
tableB:
id
dateB
colB
...
3
2022-11-05 12:00:00
D
4
2022-11-06 12:00:00
E
5
2022-11-13 12:00:00
F
and I want put all rows to one result and sort it by column date
Wanted result (rows from both tables sorted by column date DESC):
id
date
colA
colB
...
...
3
2022-11-14 12:00:00
C
5
2022-11-13 12:00:00
F
2
2022-11-12 12:00:00
B
1
2022-11-11 12:00:00
A
4
2022-11-06 12:00:00
E
3
2022-11-05 12:00:00
D
I can combine tables, but tables are "squashed"...
SELECT
COALESCE(a.id, b.id) AS id,
COALESCE(a.dateA, b.dateB) AS date,
a.colA,
b.colB
FROM tableA AS a, tableB AS b
ORDER BY date DESC
Use UNION ALL and ORDER BY. This requires enumerating the columns:
select id, dateA as dateX, colA, null as colB from tableA
union all
select id, dateB, null, colB from tableB
order by dateX
union all combines two datasets. Since we have different columns in the two tables, we need to arrange both select clauses so that they return the same set of columns ; null values can be used to fill in "missing" columns in a given dataset.
As for sorting the resultset : in MySQL, a single ORDER BY in a UNION ALL query applies to the whole resultset (after it was unioned). The query just does that (using dateX, the column alias that was created in the subqueries).
In case you want to show the rows that share same id and date as one row, you can build on top of GMB's answer:
select id, dateX as date, max(colA) as colA, max(colB) as colB
from (
select id, dateA as dateX, colA, null as colB from tableA
union all
select id, dateB, null, colB from tableB
) as q
group by id, dateX
order by dateX
See db-fiddle
Related
I've two tables like this:
First :
id
num
1
a
2
b
3
c
Second:
id
first_id
value
11
1
a1
12
1
a2
13
1
a3
And I need to get result like this:
id
value
1
a1-a2-a3
I've tried with query:
SELECT first.id, (SELECT second.value FROM second
WHERE second.first_id = first.id) AS value
FROM first
But I've got #1242 error. How I can do it?
You can use group_concat()
select first_id as id,
group_concat(`value` order by `value` separator '-') as combined_values
FROM second_table
group by first_id
How can I count the occurrence of the field/column in SQL?
Example dataset:
A
A
A
A
B
B
C
I want:
A | 4
A | 4
A | 4
A | 4
B | 2
B | 2
C | 1
Is there anyway to do it without using GROUP BY? So far all answer I get my query retuns the following:
A | 4
B | 2
C | 1
select value, count(*) from table group by value
Use HAVING to further reduce the results, e.g. only values that occur more than 3 times:
select value, count(*) from table group by value having count(*) > 3
You could use a nested sub-select for this desired result set.
If the example table name is my_table and the column called col1:
select col1,
(select count(*) from my_table where col1 = t.col1) as Count
from my_table t;
Or if you want to remove the duplicates, use the distinct statement. It removes the duplicates of your result set.
select distinct col1,
(select count(*) from my_table where col1 = t.col1) as Count
from my_table t;
TO Plot Each Input table I have Separate query, need to apply functionality on that queries and want to create Single query for Output Table
Select Distinct Names, SUM(count) from
(Select Query table 1
union
Select Query table 2
union
Select Query table 3) table group by Names;
this query Not adding count properly Niether Sorting Names properly Whats wrong with this ?
Input Table 1 :-
Names count
bob 3
pol 4
Input Table 2 :-
Names count
bob 5
0 - name may be missing here neglect this entry
Input Table 3 :-
Names count
james 4
pol 7
bob 1
Expected output table :-
Names count
bob 9
pol 11
james 4
You can use UNION and them sum of those.
select sum(a), sum(b) from
(select 2 as a, 1 as b
union select 3 as a, 6 as b
union select 4 as a, 1 as b) as b
Try this query
select `Name`,sum(`Count`) total from ( select `Name`,`Count` from `table1` union all select `Name`,`Count` from `table2` union all select `Name`,`Count` from `table3` ) tot group by `Name`
May this help you.
My current table looks like this:
ID TYPE QUANTITY
1 A1 3
2 B1 2
3 A1 2
4 B1 8
And after doing the query I want to get that:
ID TYPE QUANTITY SUM
1 A1 3 5
2 B1 2 10
3 A1 2 5
4 B1 8 10
The SUM column consist of summed quantities of items with the same type.
My approach is to use a derived table which aggregates the quantity by type first and then join this result with the original data:
select
t.id,
t.type,
t.quantity,
tmp.overall
from
table t join (
select
table.type,
sum(table.quantity) as overall
from
table
group by
table.type
) tmp on t.type = tmp.type
SELECT t.ID,t.TYPE,t.QUANTITY,x.SUM FROM TABLE t LEFT JOIN
(SELECT ID,TYPE,QUANTITY,SUM(QUANTITY) AS SUM FROM TABLE GROUP BY TYPE)x
ON t.type=x.type
SQL Fiddle
I haven't tried the query but see what happens if you do this:
SELECT
ID,
myTable.TYPE,
QUANTITY,
aaa.summy
FROM myTable
JOIN
(
SELECT
TYPE,
SUM(QUANTITY) summy
FROM myTable
GROUP BY TYPE
) aaa
ON aaa.TYPE = myTable.TYPE
I have different tables (per region) with some columns that are the same. Now I need to have a count for each value that is placed in one column over multiple tables. I'm trying to get a sum for this so I don't have to use 4 separate queries and outputs. Furthermore the values are matched with a lookup table
Region table(s) - Table1:
id | Column1 |
---------|----------|
1 | 1
2 | 2
3 | 3
etc
Lookup table
id | Description |
---------|-------------|
1 | Description1
2 | Description2
3 | Description3
The query I'm using to get the count from 1 of the tables is :
SELECT Description, Count(*) as Number from Table1, LookupTable
WHERE Column1 = LookupTable.id GROUP BY Column1 ORDER BY Number Desc
The output is
Description | Number
---------------|--------
Description1 | Number
Description2 | Number
Etc.
Any idea on how to sum up the counts for each Description/value of Column1 from 4 tables that generates the output as displayed above (but then with the sum value for each description)?
It's not clear but I guess you can use:
select LookupTable.id,LookupTable.Description, SUM(Cnt) as Number
from LookupTable
JOIN
(
SELECT Column1 as CId, count(*) as Cnt from Table1 group by Column1
union all
SELECT Column2 as CId, count(*) as Cnt from Table2 group by Column2
union all
SELECT Column3 as CId, count(*) as Cnt from Table3 group by Column3
union all
SELECT Column4 as CId, count(*) as Cnt from Table4 group by Column4
) T1 on LookupTable.id =T1.Cid
GROUP BY LookupTable.id,LookupTable.Description
ORDER BY Number Desc
Use this query:
SELECT LookupTable.Description, Count(*) as Number
FROM Table1, LookupTable
WHERE Table1.Column1 = LookupTable.id
GROUP BY Table1.Column1;
You have leave the name of table or its alias to call the column.