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.
Related
I'm using MySql. I have a table with 2 column id (Primary Key) and id_of (Foreign Key). Number of id can have the same id_of. I want to get all the id and get the count of rows having the id_of related to the id. How to make this sql query/queries? So far I could only get this:
SELECT id, (SELECT COUNT(id_of) FROM test_table) AS count FROM test_table;
database's table:
id | id_of
----------------
abasb | 2131233
hdafd | 2131233
fajdf | 3546541
pogad | 3546541
afdaj | 2131233
fafda | 8661565
the results I want:
id | count
----------------
abasb | 3
hdafd | 3
fajdf | 2
pogad | 2
afdaj | 3
fafda | 1
just need a bit of correction your query
SELECT id,
(SELECT COUNT(*) FROM test_table t2 where t2.id_of=t1.id_of) AS count
FROM test_table t1
You may try this...
; with cte as ( select distinct id_of, count(*) as Coun from testtable )
select t.id , c.coun from testtable as t inner join cte as c on t.id_of=c.id_of
You can try this:
select id , count(*) over (partition by id_of) id_of from Yourtable
You could use ajoin on subqiery for count
select a.id, t.count_of_id_of
from test_table a
inner join (
select id_of, count(*) count_of_id_of
from test_table
group by id_of
) t on t.id_of= a.id_of
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;
I have a table like this in MYSQL:
ID | NAME | VALUE |
----------------------------
1 | Bob | 1 |
2 | Bob | 2 |
3 | Jack | 5 |
4 | Jack | 8 |
5 | Jack | 10 |
and I'm trying to update the VALUE column to the highest value of rows with same NAME. So the result should be:
ID | NAME | VALUE |
----------------------------
1 | Bob | 2 |
2 | Bob | 2 |
3 | Jack | 10 |
4 | Jack | 10 |
5 | Jack | 10 |
I managed to get the max value like this:
SELECT MAX(Value) max FROM `table` GROUP BY Name having count(*) >1 AND MAX(Value) != MIN(Value)
But can't figure out how to put it in my update
Update table set Value = (SELECT MAX(Value) max FROM `table` GROUP BY Name having count(*) >1 AND MAX(Value) != MIN(Value))
Doesn't work. I'd appreciate any help.
This is easier than other answers are making it.
UPDATE MyTable AS t1 INNER JOIN MyTable AS t2 USING (Name)
SET Value = GREATEST(t1.Value, t2.Value);
You don't have to find the largest value. You just have to join each row to the set of rows with the same name, and set the Value to the greater Value of the two joined rows. This is a no-op on some rows, but it will apply to every row in turn.
http://sqlfiddle.com/#!9/f79a3/1
UPDATE t1
INNER JOIN (SELECT name, MAX(`value`) max_value
FROM t1 GROUP BY name) t2
ON t1.name = t2.name
SET t1.value = t2.max_value;
Create a temporary table consisting of ID NAME and MAX VALUE as follows:
CREATE TEMP TABLE TABLE1 AS
(SELECT NAME,MAX(Value) value FROM `table` GROUP BY Name having count(*) >1
AND MAX(Value) != MIN(Value)
);
Use this temporary table to do your update as follows:
UPDATE
Table_A
SET
Table_A.value = Table_B.value
FROM
`table` AS Table_A
INNER JOIN TABLE1 AS Table_B
ON Table_A.NAME = Table_B.NAME
Also this code is somewhat of an approximation as i am not familiar with mysql but i am familiar with sql.
Let me know if this doesn't help.
Simple left join would do the trick.
Try this out and let me know in case of any queries.
select a.id,a.name,b.value
from
table a
left join
(select name,max(value) as value from table group by name) b
on a.name=b.name;
You may use this query. The table is joined with a subquery (table t2) that contains the results you want to update your table with:
UPDATE `table` t1,
(SELECT Name, MAX(Value) maxv, MIN(Value) minv
FROM `table`
GROUP BY Name
HAVING COUNT(*)>1 AND maxv != minv) t2
SET t1.Value = t2.maxv
WHERE t1.Name = t2.Name;
If you want to know how will the values be updated, you can first run an equivalent SELECT query:
SELECT t1.*, t2.maxv
FROM `table` t1,
(SELECT Name, MAX(Value) maxv, MIN(Value) minv
FROM `table`
GROUP BY Name
HAVING COUNT(*)>1 AND maxv != minv) t2
WHERE t1.Name = t2.Name;
This query will display all the fields of table, followed by the new value maxv. You can check the current value and the new value, and if it looks fine, you may run the UPDATE query.
I really don't know how to title this problem properly.
Heres the table structure:
ID | CLIENT_ID | …
ID is primary and auto increment. CLIENT_ID on the other hand can occur multiple times.
What i want is to fetch the rows by CLIENT_ID with highest ID ... Heres a example
ID | CLIENT_ID
1 | 1
2 | 1
3 | 2
4 | 3
5 | 2
So here CLIENT_ID 1 and 2 occurs multiple times (because there is a newer version).
After the query i want the following IDs in the results: 2,4,5 (Because the highest ID in rows with CLIENT_ID 1 is the row with ID 2 and so on)
If you need all the columns you can use a select in
select * from my_table
where (id, client_id) in ( select max(id), client_id
from my_table
group by client_id);
but if you need only the id
select id from my_table
where (id, client_id) in ( select max(id), client_id
from my_table
group by client_id);
or more simple
select max(id)
from my_table
group by client_id;
SELECT * FROM table GROUP BY client_id HAVING max(id)
this should be more efficient than a sub select
I have a query that returns data in the following format:
id | name | number
1 John 12545
1 John 50496
2 Mary 23443
3 Mark 54
3 Mark 5600
3 Mark 50206
I would like to find out the number of distinct ids that appear in the result set. For example, for the result above. I would like to obtain the value 3.
Is there any way to add a column so the result looks like this instead?
count | id | name | number
3 1 John 12545
3 1 John 50496
3 2 Mary 23443
3 3 Mark 54
3 3 Mark 5600
3 3 Mark 50206
My query is:
SELECT * FROM (
SELECT id FROM tableA
WHERE xyz
) as t1
JOIN tableB using (id)
SELECT (SELECT COUNT(DISTINCT id) FROM tableName) totalCount,
id,name,number
FROM tableName
or by using CROSS JOIN
SELECT x.totalCount,
a.id, a.name, a.number
FROM tableName a, (SELECT COUNT(DISTINCT id) totalCount
FROM tableName) x
You should try :
SELECT id,name,number, (SELECT COUNT(DISTINCT name) FROM YourTableName) FROM YourTableName
Good luck
SELECT COUNT(DISTINCT id) would be faster than using column name.
SELECT (SELECT COUNT(DISTINCT id) FROM tableName) as 'count',
id,name,number
FROM tableName
SELECT COUNT(id) AS count , id, name, number
FROM
(
SELECT id
FROM tableA
WHERE xyz
) as t1
JOIN tableB using (id)
GROUP BY id, name, number