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
Related
I have two tables, 1 look like
tableA
id name value
------------------
1 Joe 22
2 John 50
3 Joe 38
4 Joe 10
5 John 20
I need to add all the value of value with same name from tableA and store in the tableB like this
tableB
id name value
---------------
1 Joe 70
2 John 70
PLEASE HELP
Assuming the tableb already exists, you can insert the data like this:
insert into tableb(id, name, value)
select min(id), name, sum(value)
from tablea
group by name;
Hi use (Select * INTO ) like this.
SELECT * INTO Table2
FROM Table1
insert into tableb(id, name, value)
select id, name, sum(value)
from tablea
group by name,id;
I have a table with 100 000 record, I want to select only the none repeated.
In another word, if the row are duplicated did not show it at all
ID Name Reslut
1 Adam 10
2 Mark 10
3 Mark 10
result
ID Name Reslut
1 Adam 10
any ideas ?
You could join a query on the table with a query that groups by the name only returns the unique names:
SELECT *
FROM mytable t
JOIN (SELECT name
FROM mytable
GROUP BY name
HAVING COUNT(*) = 1) s ON t.name = s.name
Using the same set :
ID Name Result
1 Adam 10
2 Mark 10
3 Mark 10
4 Mark 20
I'm guessing the final solution would be:
ID Name Result
1 Adam 10
4 Mark 20
Using the above query previously suggested I modified it to take the result into consideration:
SELECT t1.*
FROM myTable t1
JOIN
(
SELECT name, result
FROM myTable
GROUP BY name, result
HAVING COUNT(*) = 1
) t2
WHERE
t1.name=t2.name and
t1.result = t2.result;
I have table User, Company, ParentCompany and table Goal.
Each Company have ParentCompany, and each User inside one Company. Goal have number of action, and type of Goal, user who execute the goal, and time executed.
I want to calculate the number of action in a date range for each type of Goal, for each user, company, and parent_company. Number of action for each company equal to sum of action for user that reside in that company.
More or less after some join query, I able to get this table below, where column id is id of company, parent_id is id of companyparent, and num is number of goal for all user inside of id company.
id parent_id num
----------- -------------------- -----------------------
1 3 1
2 1 2
3 1 1
4 2 4
Now I want to make it like below:
id parent_id sum_id sum_parent
----------- -------------------- -------------- -------------
1 3 1 1
2 1 2 3
3 1 1 3
4 2 4 4
How can I make it works? I can get one of the value (sum_id or sum_parent) with GROUP BY,
SELECT id,SUM(num) AS sum_id FROM tableA GROUP BY id
or
SELECT parent_id,SUM(num) AS sum_parent FROM tableA GROUP BY parent_id
but is there any way to make it only in one query? tableA results from query with 5 join inside.
Try this:
SELECT a.id, a.parent_id, a.sum_id, b.sum_parent
FROM (SELECT id, parent_id, SUM(num) sum_id FROM tableA a GROUP BY id) a
INNER JOIN (SELECT parent_id, SUM(num) sum_parent FROM tableA a GROUP BY parent_id) b ON a.parent_id = b.parent_id
Try this :
SELECT
t1.id, t1.parent_id, t1.sum_id, t2.sum_parent
FROM
(SELECT id, parent_id, SUM(num) AS sum_id FROM mytable GROUP BY id) t1
INNER JOIN
(SELECT parent_id, SUM(num) AS sum_parent FROM mytable GROUP BY parent_id) t2
ON t1.parent_id = t2.parent_id
Apparently what I want can be done with WITH ROLLUP statement. (http://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html)
With
SELECT id,sum(num) FROM tableA GROUP BY parent_id, id WITH ROLLUP;
will results in
parent_id id sum(num)
----------- -------------------- --------------
1 2 2
1 3 1
1 null 3
2 4 4
2 null 4
3 1 2
3 null 2
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.
how can I select all from a table, and if there are identical values of column name then only select the row that has the greatest id value so If there was a table like this:
id name age country
---+------+---+-------
1 bob 24 UK
2 john 48 USA
3 janet 72 USSR
4 bob 96 Ukraine
it would only select the 'bob' with the highest id so the result would return:
id name age country
---+------+---+-------
2 john 48 USA
3 janet 72 USSR
4 bob 96 Ukraine
Thank you.
Try this query
select * from table_name where ID in(select MAX(ID) from table_name group by name)
You could use a subquery to calculate the maximum ID for every name, and then return all rows that matches the IDs returned by the subquery:
SELECT *
FROM People
WHERE id IN (SELECT MAX(id) FROM People GROUP BY Name)
Please see fiddle here.
You could use a not exists subquery that filters out rows with with the same name and a greater id:
select *
from People p1
where not exists
(
select *
from People p2
where p1.Name = p2.Name
and p2.Id > p1.Id
)
you can do it this way :
Select Table_1.* from table_1 inner join (
Select Max(ID) as ID from Table_1 Group by ID) x On Table.Id on x.ID