How to change result row into column in mysql - mysql

I have below result set
Name | ID | Total | CityName
--------------------------------
A 1 2 ABC
--------------------------------
B 2 1 XYZ
--------------------------------
C 3 1 ABC
--------------------------------
How I can show below result
Name | ID | ABC | XYZ
---------------------------
A 1 2 0
---------------------------
B 2 0 1
---------------------------
C 3 1 0
---------------------------

Use conditional aggregation
select name, id, max(case when CityName='ABC' then total else 0 end) as ABC
max(case when CityName='XYZ' then total else 0 end) as XYZ
from tablename
group by name,id

Well it seems like a subquery, or procedure would be a good idea.
Something like:
select Name,ID,
(select Total from tets where CityName ='ABC' and Id = t.Id) as 'ABC',
(select Total from tets where CityName ='XYZ' and Id = t.Id) as 'XYZ'
from tets t

Related

MYSQL count only those students with number 1

i would like construct a query to count the number of student who don't have number 0.
assuming i have 2 tables
students
*student_id
*name
Number
*number_id
*student_id
*number
| Student 1 |
1
1
1
1
| Student 2 |
1
1
1
1
| Student 3 |
1
0
1
0
|Student 4 |
0
1
1
1
So the result should be. student without zero = 2
student with zero = 2
This answer assumes that your table has the following structure:
id | value
1 | 1
1 | 1
1 | 1
1 | 1
2 | 1
etc...
SELECT id
FROM yourTable
GROUP BY id
HAVING SUM(CASE WHEN value = 0 THEN 1 ELSE 0 END) = 0
Something like this might return the specified result:
SELECT CONCAT('student without zero = '
, ( SELECT SUM(1)
FROM ( SELECT t.`some_identifier`
FROM `sometable` t
GROUP BY t.`some_identifier`
HAVING SUM(IF(t.`some_col`=0,1,0)) = 0
)
)
, '\n'
, 'student with zero = '
, ( SELECT SUM(1)
FROM ( SELECT z.`some_identifier`
FROM `sometable` z
WHERE z.`some_col` = 0
GROUP BY z.`some_identifier`
)
)
) AS `result`
Just replace some_table with the name of the table, some_col with the name of the column that contains the 1 and 0 values, and some_identifier with the column that contains the Student 1, Student 2, etc. values.
This should return a single row, something like this:
result
--------------------------------
student without zero = 2
student with zero = 2

sql hierarchy count

I have a sql table named Parentids of ids and their parent's id (where 0 represents no parent) like follows:
id | parentid
--------------
1 | 0
2 | 0
3 | 1
4 | 0
5 | 2
6 | 2
7 | 3
8 | 1
From this table I need to a sql query to return a table with the number of children for each id that should result in the following:
id | childrenCnt
--------------
1 | 2
2 | 2
3 | 1
4 | 0
5 | 0
6 | 0
7 | 0
8 | 0
i have the following sql query but it does not seem to work:
SELECT id
,sum(CASE
WHEN parentid = tid
THEN 1
ELSE 0
END) AS childrenCnt
FROM Parentids
You could do it with group by on parentId,
Only members with children:
select
parentId,
COUNT(*)
from Parentids
where
parentId <> 0
group by
parentId
EDIT:
All members, to match exact OP expected result:
select
parentId,
COUNT(*)
from Parentids
group by
parentId
order by
2,1
One method is using a left join and aggregation. However, a correlated subquery might even work better:
select p.id,
(select count(*)
from parentids p2
where p2.parentid = p.id
) as childrenCnt
from parentids p;
You can GROUP BY parentids and then remove the record that id = 0 (first row). So try this code:
select parentid as id, count(*) as childrenCnt
from Parentids
where id <> 0
group by id
You could use the following:
SELECT p.id,
COUNT(DISTINCT ch.id) AS childrenCnt
FROM Parentids p
LEFT JOIN Parentids ch ON p.id = ch.parentid
GROUP BY p.id;
It produces the output you specified.
SQL Fiddle

How to get one value from other categories in php mysql?

I have a sample data with table is test
name | catid | date
------------
abc | 1
def | 2
ghi | 1
jkl | 2
mno | 1
pqr | 3
And my query
SELECT * FROM test WHERE catid = 1 AND catid = 2 AND catid = 3 ORDER BY date DESC
How to get value with result is
name | catid
------------
abc | 1
def | 2
pqr | 3
SELECT a.*
FROM TableName a
INNER JOIN
(
SELECT catid, MAX(DATE) max_date
FROM tableName
GROUP BY catID
) b ON a.catID = b.catID AND
a.date = b.max_date
SELECT name, catid FROM test WHERE catid IN (1, 2, 3) GROUP BY catid
I think you need the IN operator which is simplier than catid = X OR catid... (and you need OR, not AND)
Based on your desired output, this might be the query:
select catid, min(name)
from yourtable
where catid between 1 and 3
group by catid
But it's hard to know what you want based on the info provided in your question.

MySQL multi join / group query

idI have a table like this
|----|-----------|--------|
| ID | Ethnicity | Gender |
| 1 | 2 | 1 |
| 2 | 3 | 1 |
| 3 | 4 | 2 |
|----|-----------|--------|
etc ....
And I'm trying to get back a set of results that show me ethnicities group by male(1) and female(2)
So the result row would in this example would be:
Ethnicity Male Female
2 1 0
3 1 0
4 0 1
So far I'm close with to what I want with:
SELECT ethnicity,
(SELECT
count(id)
FROM
table_name
WHERE
gender = '2' ) as female,
(SELECT
count(id)
FROM
table_name
WHERE
gender = '1') as male
FROM table_name
GROUP BY ethnicity
Which gives me:
Ethnicity Male Female
2 2 1
3 2 1
4 2 1
But need the count(id) to only be a count of the adno of that ethnicity row if that makes sense.
Try this instead:
SELECT
ethnicity,
SUM(CASE WHEN gender = 1 THEN 1 ELSE 0 END) AS female,
SUM(CASE WHEN gender = 2 THEN 1 ELSE 0 END) AS Male
FROM students
GROUP BY ethnicity;
The above should work in virtually any SQL product, not just in MySQL. If you like, however, you can also use this version, which employs MySQL's implicit conversion of booleans to ints (true -> 1, false -> 0):
SELECT
ethnicity,
SUM(gender = 1) AS female,
SUM(gender = 2) AS Male
FROM students
GROUP BY ethnicity;
SELECT
ethnicity,
CASE WHEN gender = 1 THEN 1 ELSE 0 END AS Male,
CASE WHEN gender = 2 THEN 1 ELSE 0 END AS Female
FROM students

Double group by

I've got table with two columns: name and grade. It looks sth like this:
NAME | GRADE
Adam | 1
Adam | 2
Adam | 2
Adam | 3
Frank | 2
Frank | 1
Now I want create view that will looks like this:
NAME | GRADE 1 | GRADE 2 | GRADE 3
Adam | 1 | 2 | 1
Frank | 1 | 1 | 0
I've wrote this:
SELECT Name,
(SELECT COUNT(Grade)
FROM dbo.Rodzaj
WHERE Grade = '1') as Grade_1,
(SELECT COUNT(Grade)
FROM dbo.Rodzaj
WHERE Grade = '2) as Grade_2,
(SELECT COUNT(Grade)
FROM dbo.Rodzaj
WHERE Grade = '3') as Grade_3
FROM dbo.Rodzaj
GROUP BY Name
but it doesn't work...
I would appreciate any help
What you are looking for is called a "pivot table" and it is done with a chain of CASE statements which apply a 1 or 0 for each condition, then SUM() up the ones and zeros to retrieve a count.
SELECT
NAME,
SUM(CASE WHEN GRADE = 1 THEN 1 ELSE 0 END) AS GRADE1,
SUM(CASE WHEN GRADE = 2 THEN 1 ELSE 0 END) AS GRADE2,
SUM(CASE WHEN GRADE = 3 THEN 1 ELSE 0 END) AS GRADE3
FROM Rodzaj
GROUP BY NAME
Note that if you need this to have a dynamic number of columns, you will have to construct the query using a scripting language and a loop. (Or a loop inside a stored procedure)