Add the two columns and then multiple rows in mysql - mysql

Below find the sample table.I want to calculate the total population(emp+nemp) of each country.Means i have to add the two columns which can be easily done and then add the data from multiple rows as well as per the same country name.
This the sample table.

Try this
SELECT
T.country,
SUM(T.emp + T.nemp) People
FROM
YourTable T
GROUP BY
T.country

Related

Merge several rows in SQL

I am trying to merge several rows with the same "OrderNumber". An order have several rows with different data records. Each time step is saved in one record, as you can see in the image below.
Now I would like to summarize the time information for the respective "OrderNumber"s. So it should look like this:
I was wondering how to make this with an SQL query..
Use GROUP BY with following query:
SELECT
OrderNumber,
MAX(DateOfReceipt),
MAX(ShippingDate),
MAX(ControlDate),
MAX(DeliveryDate)
FROM
Table
GROUP BY
OrderNumber

Sum numbers if cell contains X and store in another table

So if we have two columns 'Domain' and 'Score'. There are duplicate domains with different scores. How would I add all the scores together that are from the same domain?
and furthermore, how do I then show it separately in another table?
Thanks,
Leon.
query look like this.
SELECT Domain, sum(NZ(Score,0)) as total FROM yourtable group by Domain

in mysql my distinct query not working?

in the below image I'm using
SELECT DISTINCT(name),date,reporting,leaving from attendance where date='2016-09-01
and I'm still getting repeating names. Why?
When using DISCTINCT, MySQL uses all columns as grouping factor. If you want group by only one column and get all corresponding column values, use GROUP BY instead
SELECT name, date, reporting, leaving FROM attendance GROUP BY name WHERE ...
Actually your all rows have distinct data apart from Name column if you want only distinct names then you can get it with help of Aggregate functions, you can use MIN or MAX as per your business requirement
SELECT Name,MAX(date),MAX(reporting),MAX(leaving)
FROM attendance
WHERE date='2016-09-01'
GROUP BY Name

How to find and remove duplicate records from multiple column,keep only one full record

I am trying to find duplicates from a table based on multiple columns. I have a table which has a columns like Email(Office),Email(Personal1) & Email_Personal2, Mobile_Personal1, Mobile_Personal2, FirstName,MiddleName,LastName,CompanyName,Designation etc. It has millions of records. There are so many duplicates for a specific record.
The folowing image represents the sample Table:
Now, I want to find the record using sql query which has the full values in its columns, want to keep this record and delete remaining all.
select *,count(*) from mytable where first_name!="" group by First_Name,Email_Office,Email_Personal1,Email_Personal2,Personal_Mobile1,Personal_Mobile2 having count(*)>1
But its showing me a specific record with total number of occurrence in last count(*) column only. Guide me in above query how can I see a record which has all the details along with number of occurrences of that specific record? How do I keep that one complete record and delete remaining all from a table?
I've removed "having count(*) >1" to see each record from above query but its taking so much time to show the output,almost feel like its getting hanged.
select t.* from mytable t inner join (select first_name,middle_name,last_name,designation,company_name,email_office,email_personal1,email_personal2,personal_mobile1,personal_mobile2,count(*) as NoDuplicates from mytable group by first_name,middle_name,last_name,designation,company_name,email_office,email_personal1,email_personal2,personal_mobile1,personal_mobile2 having NoDuplicates > 1) tsum on t.first_name=tsum.first_name and t.Middle_Name=tsum.middle_name and t.Last_Name=tsum.last_name and t.Designation=tsum.designation and t.Company_Name=tsum.company_name and t.Email_Office=tsum.email_office and t.Email_Personal1=tsum.email_personal1 and t.Email_Personal2=tsum.email_personal2 and t.Personal_Mobile1=tsum.personal_mobile1 and t.Personal_Mobile2=tsum.personal_mobile2

How would I display two separate tables within the same query/report without combining each entry?

As of right now, creating a query with all records from both tables I want to display gives me every record for table b for the first record of table a, then every record of table b for the second record of table a, and so on.
SELECT *
FROM tblSales, tblRepair;
I want to be able to format these tables so that records from each table are displayed within a report, but separately (not joined). Both these tables contain sales data that need to be displayed and calculated together on a daily basis, but my problem right now is getting the data out of these tables and together in a format that doesn't join each record together.
Thanks in advance.
You can use a UNION query to combine both tables. I've added a dummy column to distinguish between the two tables:
SELECT *,'Sales' AS TheTable FROM tblSales
UNION ALL SELECT *, 'Repairs' FROM tblRepairs;
This will list all the Sales records first, followed by all the Repairs. You can add an ORDER BY clause to change this.
Alternatively, depending on the type of report you are creating, you could base the main report on one table and add a subreport based on the second.