I have one table of records like
╔═════╦══════╦══════════╦══════════╗
║ PID ║ NAME ║ DATE1 ║ DATE2 ║
╠═════╬══════╬══════════╬══════════╣
║ 123 ║ john ║ 20110401 ║ 19700101 ║
║ 123 ║ john ║ 20110401 ║ 19700101 ║
║ 123 ║ john ║ 20110401 ║ 19700101 ║
║ 323 ║ mike ║ 20110401 ║ 19900101 ║
║ 323 ║ mike ║ 20110401 ║ 19900101 ║
╚═════╩══════╩══════════╩══════════╝
query1 is count on pid
query2 is if all the four fields are same the record is to be taken as one record else individual records and records to be counted on pid order.
The above result is:
╔═════╦══════════════╦═════════════╗
║ PID ║ TOTALRECORDS ║ TOTALUNIQUE ║
╠═════╬══════════════╬═════════════╣
║ 123 ║ 3 ║ 1 ║
║ 323 ║ 2 ║ 1 ║
╚═════╩══════════════╩═════════════╝
I did select pid,count(pid),(select count(pid) group by pid,name,date1,date2) from <table> group by pid;
But I get an error... kindly correct my code.
SELECT pid,
COUNT(*) totalRecords,
COUNT(DISTINCT name, date1, date2) totalUnique
FROM TableName
GROUP BY pid
SQLFiddle Demo
SQLFiddle Demo (with not unique record)
Result
╔═════╦══════════════╦═════════════╗
║ PID ║ TOTALRECORDS ║ TOTALUNIQUE ║
╠═════╬══════════════╬═════════════╣
║ 123 ║ 3 ║ 1 ║
║ 323 ║ 2 ║ 1 ║
╚═════╩══════════════╩═════════════╝
Related
I am learning sql query.
Here is my table simplified
╔════╦══════════════╦══════════════╦════════╦
║ id ║ user_id ║ department_id║ salary ║
╠════╬══════════════╬══════════════╬════════╬
║ 1 ║ 1 ║ 3 ║ 100 ║
║ 2 ║ 2 ║ 3 ║ 50 ║
║ 3 ║ 1 ║ 3 ║ 30 ║
║ 4 ║ 2 ║ 3 ║ 20 ║
║ 5 ║ 2 ║ 3 ║ 20 ║
╚════╩══════════════╩══════════════╩════════╩
Here is what I want to have below
╦══════════════╦══════════════╦════════╦
║ user_id ║ department_id║ salary ║
╠══════════════╬══════════════╬════════╬
║ 1 ║ 3 ║ 130 ║
║ 2 ║ 3 ║ 90 ║
╚══════════════╩══════════════╩════════╩
I want to add user salary if they are in the same department.
I have nooo ideas how to start.
Does anyone have good feedback that I can start with?
Thank you in advance
Try this query:
Select user_id, department_id, sum(salary) from table
Group by user_id, department_id
In the select clause, you have the columns you wish to select and your group by clause contains the grouping.
I have three tables named "users","user_hobbies" and "hobbies". Below is the sample tables with values;Below is the users table with fields id, name and age
╔════╦══════╦═════╗
║ ID ║ NAME ║ AGE ║
╠════╬══════╬═════╣
║ 1 ║ abc ║ 23 ║
║ 2 ║ xyz ║ 24 ║
║ 3 ║ pqr ║ 21 ║
╚════╩══════╩═════╝
and below is user_hobbies table with fields id, user_id and hobby_id
╔════╦═════════╦══════════╗
║ ID ║ USER_ID ║ HOBBY_ID ║
╠════╬═════════╬══════════╣
║ 1 ║ 1 ║ 1 ║
║ 2 ║ 1 ║ 2 ║
║ 3 ║ 1 ║ 3 ║
║ 4 ║ 2 ║ 4 ║
║ 5 ║ 2 ║ 3 ║
║ 6 ║ 2 ║ 5 ║
║ 7 ║ 3 ║ 2 ║
║ 8 ║ 4 ║ 6 ║
╚════╩═════════╩══════════╝
. Below is the hobbies table with fields id and desc
╔════╦═══════════╗
║ ID ║ DESC ║
╠════╬═══════════╣
║ 1 ║ music ║
║ 2 ║ chatting ║
║ 3 ║ cricket ║
║ 4 ║ badminton ║
║ 5 ║ chess ║
║ 6 ║ cooking ║
╚════╩═══════════╝
. The actual requirement is that i need a query to retrieve name, age, hobby_id and desc (see an example below)
╔══════╦═════╦══════════╦═════════════════════════╗
║ NAME ║ AGE ║ HOBBYID ║ DESC ║
╠══════╬═════╬══════════╬═════════════════════════╣
║ abc ║ 23 ║ 1,2,3 ║ music,chatting,cricket ║
║ pqr ║ 21 ║ 2 ║ chatting ║
║ xyz ║ 24 ║ 4,3,5 ║ badminton,cricket,chess ║
╚══════╩═════╩══════════╩═════════════════════════╝
You need to join the tables first and use an aggregate function called GROUP_CONCAT().
SELECT a.Name,
a.Age,
GROUP_CONCAT(c.ID) hobbyIDs,
GROUP_CONCAT(c.desc) descList
FROM users a
INNER JOIN user_hobbies b
ON a.ID = b.user_ID
INNER JOIN hobbies c
ON b.hobby_ID = c.ID
GROUP BY a.Name, a.Age
SQLFiddle Demo
MySQL GROUP_CONCAT()
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
OUTPUT
╔══════╦═════╦══════════╦═════════════════════════╗
║ NAME ║ AGE ║ HOBBYIDS ║ DESCLIST ║
╠══════╬═════╬══════════╬═════════════════════════╣
║ abc ║ 23 ║ 1,2,3 ║ music,chatting,cricket ║
║ pqr ║ 21 ║ 2 ║ chatting ║
║ xyz ║ 24 ║ 4,3,5 ║ badminton,cricket,chess ║
╚══════╩═════╩══════════╩═════════════════════════╝
I ran into some problems while structuring my database, and I will ask two questions.
First question: below table needs to be merged by the same IDs
╔═════╦═══════╦═════╦═══════╗
║ id* ║ name ║ age ║ grade ║
╠═════╬═══════╬═════╬═══════╣
║ 0 ║ John ║ ║ ║
║ 0 ║ ║ 11 ║ ║
║ 0 ║ ║ ║ 6 ║
║ 1 ║ Dave ║ ║ ║
║ 1 ║ ║ 12 ║ ║
║ 1 ║ ║ ║ 7 ║
╚═════╩═══════╩═════╩═══════╝
so it should look like this;
╔═════╦═══════╦═════╦═══════╗
║ id* ║ name ║ age ║ grade ║
╠═════╬═══════╬═════╬═══════╣
║ 0 ║ John ║ 11 ║ 6 ║
║ 0 ║ Dave ║ 12 ║ 7 ║
╚═════╩═══════╩═════╩═══════╝
NOTE: id* is not AUTO_INCREMENT
Second question: You probably think that the former database structure is poor. The good thing is, I haven't created the database yet and I have been looking for a solution to add data to an existing row without removing old information, but if there is no old information, it would create a new row. Thanks in advance.
Second question explained
Virgin table
╔═════╦═══════╦═════╦═══════╗
║ id* ║ name ║ age ║ grade ║
╠═════╬═══════╬═════╬═══════╣
║ ║ ║ ║ ║
╚═════╩═══════╩═════╩═══════╝
some SQL statement
╔═════╦═══════╦═════╦═══════╗
║ id* ║ name ║ age ║ grade ║
╠═════╬═══════╬═════╬═══════╣
║ 0 ║ John ║ ║ ║
╚═════╩═══════╩═════╩═══════╝
the same SQL statement with different parameters
╔═════╦═══════╦═════╦═══════╗
║ id* ║ name ║ age ║ grade ║
╠═════╬═══════╬═════╬═══════╣
║ 0 ║ John ║ ║ ║
║ 1 ║ Dave ║ ║ ║
╚═════╩═══════╩═════╩═══════╝
another SQL statement
╔═════╦═══════╦═════╦═══════╗
║ id* ║ name ║ age ║ grade ║
╠═════╬═══════╬═════╬═══════╣
║ 0 ║ John ║ ║ ║
║ 1 ║ Dave ║ 12 ║ ║
╚═════╩═══════╩═════╩═══════╝
another SQL statement
╔═════╦═══════╦═════╦═══════╗
║ id* ║ name ║ age ║ grade ║
╠═════╬═══════╬═════╬═══════╣
║ 0 ║ John ║ ║ 6 ║
║ 1 ║ Dave ║ 12 ║ ║
╚═════╩═══════╩═════╩═══════╝
... and so on.
You should be able to apply an aggregate function to all the columns and then GROUP BY id:
select id,
max(name) name,
max(age) age,
max(grade) grade
from yourtable
group by id
See SQL Fiddle with Demo
As far as the DB structure, the only issue that I see is that you are inserting multiple records for the same user. You should be using an UPDATE statement to use the values instead of inserting.
It sounds like you want to use the REPLACE function in MySQL (here is a tutorial).
So the query would be similar to this:
REPLACE
INTO yourtable (`id`, `name`, `age`, `grade`)
VALUES (0, 'john', 11, null);
See SQL Fiddle with Demo
You could group by id, and use any aggregate function to pick the non-null row. This example uses max, but min would work too:
select id
, max(name) as name
, max(age) as age
, max(grade) as grade
from YourTable
group by
id
Hi i have following table on mysql DB.
╔═══════════╦═════════╦════════╦════════════════╗
║ REVIEW_ID ║ USER_ID ║ STATUS ║ DATE_ADDED ║
╠═══════════╬═════════╬════════╬════════════════╣
║ 218 ║ 2 ║ cool ║ 20130121134811 ║
║ 218 ║ 2 ║ cool ║ 20130121134812 ║
║ 218 ║ 2 ║ lame ║ 20130121134813 ║
║ 218 ║ 2 ║ funny ║ 20130121134814 ║
║ 218 ║ 2 ║ funny ║ 20130121134815 ║
║ 218 ║ 2 ║ funny ║ 20130121134816 ║
║ 218 ║ 2 ║ lame ║ 20130121134817 ║
╚═══════════╩═════════╩════════╩════════════════╝
how can i get a result where when i do query based on user_id i need to get result of total status for each type:
╔════════╦════════════╗
║ STATUS ║ TOTALCOUNT ║
╠════════╬════════════╣
║ cool ║ 2 ║
║ funny ║ 3 ║
║ lame ║ 2 ║
╚════════╩════════════╝
Thanks
Use COUNT() which is an aggregate function, and group them according to their status
SELECT status, COUNT(*) totalCount
FROM tableName
GROUP BY status
SQLFiddle Demo
OTHER(s)
MySQL GROUP BY with some Aggregate Functions List
SELECT status, count(*)
FROM your_table
GROUP BY status
SELECT status, count(*)
FROM yourtable
GROUP BY status
;
SELECT STATUS, COUNT(*) AS TOTALCOUNT
FROM tableName
GROUP BY STATUS
HAVING USER_ID = user_id you need
I have two table
The table structure goes something like this
Table A
╔══════╦════════╗
║ P_ID ║ P_NAME ║
╠══════╬════════╣
║ 1 ║ name1 ║
║ 2 ║ name2 ║
║ 3 ║ name3 ║
║ 4 ║ name5 ║
╚══════╩════════╝
Table B
╔═════╦════════╦════════╦═══════════╦═══════╗
║ ID ║ P_ID ║ C_ID ║ C_PRICE ║ TIME ║
╠═════╬════════╬════════╬═══════════╬═══════╣
║ 1 ║ 1 ║ 3 ║ 11 ║ 11111 ║
║ 2 ║ 2 ║ 4 ║ 22 ║ 22222 ║
║ 3 ║ 3 ║ 5 ║ 33 ║ 33333 ║
║ 4 ║ 3 ║ 6 ║ 44 ║ 44444 ║
║ 5 ║ 3 ║ 6 ║ 55 ║ 55555 ║
║ 6 ║ 4 ║ 7 ║ 66 ║ 66666 ║
╚═════╩════════╩════════╩═══════════╩═══════╝
The requirement is
1. Join two table
2. Group By C_ID
3. The latest rows in Group By
I tried to modify the answer By Bill Karwin from How do I join the most recent row in one table to another table?
SELECT e.*, s1.*
FROM table_a e
INNER JOIN
table_b s1
ON (e.p_id = s1.p_id)
LEFT OUTER JOIN table_b s2
ON (e.p_id = s2.p_id AND s1.id < s2.id)
WHERE s2.p_id IS NULL;
but I could not achieve what I want. From his answer I will get
╔═════╦════════╦════════╦═══════════╦═══════╦═════════╗
║ ID ║ P_ID ║ C_ID ║ C_PRICE ║ TIME ║ P_NAME ║
╠═════╬════════╬════════╬═══════════╬═══════╬═════════╣
║ 1 ║ 1 ║ 3 ║ 11 ║ 11111 ║ name1 ║
║ 2 ║ 2 ║ 4 ║ 22 ║ 22222 ║ name2 ║
║ 5 ║ 3 ║ 6 ║ 55 ║ 55555 ║ name3 ║
║ 6 ║ 4 ║ 7 ║ 66 ║ 66666 ║ name5 ║
╚═════╩════════╩════════╩═══════════╩═══════╩═════════╝
But the output what I want is as below ( dublicate P_ID is ok but for each dublicate P_ID should not have dublicate C_ID )
╔═════╦════════╦════════╦═══════════╦═══════╦═════════╗
║ ID ║ P_ID ║ C_ID ║ C_PRICE ║ TIME ║ P_NAME ║
╠═════╬════════╬════════╬═══════════╬═══════╬═════════╣
║ 1 ║ 1 ║ 3 ║ 11 ║ 11111 ║ name1 ║
║ 2 ║ 2 ║ 4 ║ 22 ║ 22222 ║ name2 ║
║ 3 ║ 3 ║ 5 ║ 33 ║ 33333 ║ name3 ║
║ 5 ║ 3 ║ 6 ║ 55 ║ 55555 ║ name3 ║
║ 6 ║ 4 ║ 7 ║ 66 ║ 66666 ║ name5 ║
╚═════╩════════╩════════╩═══════════╩═══════╩═════════╝
SELECT e.*, s1.*
FROM table_a e INNER JOIN
(SELECT * FROM (SELECT * FROM table_b ORDER BY time DESC) temp GROUP BY c_id) s1;
The innermost ORDER BY ensures the for the same c_id the desired row always comes first, the outter GROUP BY will group on c_id, and having not specified otherwise will return the first row found for each group.
You must
Join two table
Group By C_ID
Number row for each group desc ordered by chosen field (last imply order!)
Filter by row number = 1.
This can help you for numbering rows.