how to select data on sql using group by - mysql

i have table
i would like to select like this
how to get data count_attendance?

you can use subquery to do that:
select name, gol, count(attendance) as count_data,(Select count(attendance) from tablename n where n.attendance='yes' and n.name=tablename.name and n.gol=tablename.gol) as count_attendance from tablename group by name,gol
Just replace tablename with the name of your table.

Related

I need to fetch Row twice in SQL from Single Table

[Expected Result]
I Need this result from Single table dont know how to do this.
You can use union all to duplicate each and every row in the resultset:
select custname, city from mytable
union all select custname, city from mytable
An order by clause might be useful:
select custname, city from mytable
union all select custname, city from mytable
order by custname, city
One simple method is:
select t.*
from t
union all
select t.*
from t;
If you care about the ordering, then you need an order by clause, so add:
order by custname;
Add ORDER BY to the UNION. For example:
select * from t union all select * from t order by id
See running example at DB Fiddle.

from the sample database , How could I make a query where I could search all the region ( region id) where Kivuto Id is duplicated?

I need to fetch the 3 lines as highlighted in the result with green i.e separate region id but same kivuto id.I need to rectify such products so that I could correct the kivuto id's
Try this.
select * from table_name
where kivuto_id in (
select email from table_name
group by kivuto_id
having count(*) > 1
)
You can refer to this as well: Find rows that have the same value on a column in MySQL
You can simply use exists:
select t.*
from t
where exists (select 1
from t t2
where t2.kivuto_id = t.kivuto_id and
t2.region_id <> t.region_id
);
For performance, you want an index on (kivuto_id, region_id).

MySQL: Select all rows from select distinct

I would like to do a specific query
select distinct name
from tablename
to remove duplicates. But this gives me only the names. From 'select distinct name from table' I would like all columns returned with one where condition:
select *
from tablename
where value= 1
I tried this:
select *
from tablename
where value = 1 and exists (select distinct name
from tablename)
Unfortunately it returns the same data as:
select *
from tablename
where value = 1
Which means that there is a fundamental flaw in my query.
Could someone help me with my query. Thank you in advance.
Can you try this select field1, field2, field3 from tablename group by field2
This query will give you the id where you have duplicates:
select distinct id
from table1
group by id
having count(id)>1
and you can put
having count(id)=1
if you want to know the ones that don have duplicates

get all unique records and their corresponding column

Is it possible to get all unique records as well as their corresponding column in a database?
something like:
SELECT DISTINCT *
FROM table_name
?where?
I want to get all unique records with their corresponding column.
I tried:
SELECT distinct(column_name), other_column
FROM table_name
?where?
I still get duplicate records.
I tried:
SELECT distinct(column_name)
FROM table_name
?where?
I get unique records but incomplete column. How can I get all unique records w/ their column?
Are you looking for something like this?
SELECT t.*
FROM
(
SELECT MIN(pk_id) pk_id
FROM table_name
GROUP BY fk_id
) q JOIN table_name t
ON q.pk_id = t.pk_id
Here is SQLFiddle demo
In Postgres you can use DISTINCT ON
SELECT DISTINCT ON (fk_id) t.*
FROM table_name t
ORDER BY fk_id
Here is SQLFiddle demo

where not in subquery - SQL Server 2008

The inner query in the following SQL statement is to normalize part of the database (code1, code2, code3, etc.) With the outer query I want to select the codes that aren't in the lookup table (tblicd)
select primarycode from
(
select id, primarycode from myTable
union
select id, secondarycode from myTable
union
select id, tertiarycode from myTable) as t
order by id
where primarycode not in tblicd.icd_id
The query above doesn't run, I'm wondering what I'm doing wrong. The error I get is the multi-part identifier tblicd.icd_id could not be bound
One problem is your ORDER BY and WHERE clauses are reversed. The ORDER BY clause must come after the WHERE clause.
Your WHERE clause is also incorrect. It should be like this:
WHERE primarycode NOT IN (SELECT icd_id FROM tblicd)
ORDER BY id
where primarycode not in tblicd.icd_id
might be
where primarycode not in (SELECT icd_id FROM tblicd )