MySQL Query that will group records - mysql

I have this table [Table 1]
cid | arrived | date_arrived
The [arrived field can have a value of [T] or [F], the value is [F] the date arrived field is NULL
1 records may appear only up to maximum of 2 (1 record for arrived=T and another record for arrived=F) But there are also records that may appear only once
1 | T | 2012-02-01
2 | F | [Null]
1 | F | [Null]
3 | T | 2012-03-05
I need a query that will show something like this
cid | arrived | not_arrived
1 Yes Yes
2 No Yes
3 Yes No

This works:
SELECT
cid,
SUM(arrived = 'T') > 0 as arrived,
SUM(arrived = 'F') > 0 as not_arrived
FROM [Table 1]
GROUP BY cid;
You can try it here: http://sqlfiddle.com/#!2/2b5a7/1/0

try
select cid,
case when find_in_set('T', group_concat(arrived)) > 0 then 'yes' else 'no' end as arrived,
case when find_in_set('F', group_concat(arrived)) > 0 then 'yes' else 'no' end as not_arrived
from table1
group by cid

Related

How to manipulate SQL Query output

I would like to know if it's possible for example I have one sql table with the following results
+----+---------+--------+--------+
| ID | Name | Number | Active |
+----+---------+--------+--------+
| 1 | Jessica | 12 | 0 |
| 2 | Andrew | 23 | 1 |
| 3 | Jason | 53 | 0 |
+----+---------+--------+--------+
And I would like to change the active field to 0 = No | 1 = Yes but only in the results, I don't want to change the value of the row, is it possible to make one query that can do it?
Well with the answers bellow I managed to get it changed but now how can I echo the value in php?
SELECT *, case when Active =0 then 'No' when Active =1 then 'Yes' end as Expr1,
FROM table
Should it be like: $isActive = $rows['Expr1'];
NVM the line above is working.
Just use a case statement for translating 1 = yes and 0 = No like this
select ID
,Name
,Number
,case when Active=0 then 'No'
when Active=1 then 'Yes'
end as active_y_n
from table
use case when
select Id,name,number,
case Active when 0 then 'No'
when 1 then 'Yes' end as active_status
from t
A particularly simple way would use elt():
select Id, name, number,
elt(Active + 1, 'No', 'Yes') as as active_status
from t

How to get single row when foreign key is similar

I have a table tbl_issue with columns
> serial_no.
Issue_no. (f.k)
From_Section
To_Section
+-----+---------------+-------------+-------------+
| id | issue no | from section| to_section |
+-----+---------------+-------------+-------------+
| 1 | 223 | MFA | N/A |
| 2 | 223 | N/A | LOG |
+----------+----------+-------------+--------------+
When I query the table on issue no. I get two rows, can anyone kindly help how can I get a single record and no 'N/A'
For the example you gave this would work:
WITH combined AS
(
SELECT i.issue_no,
CASE WHEN i.from_section = 'N/A' THEN i2.from_section ELSE i.from_section END from_section,
CASE WHEN i.to_section = 'N/A' THEN i2.to_section ELSE i.to_section END to_section
FROM dbo.tbl_issue i
INNER JOIN dbo.tbl_issue i2
ON i2.issue_no = i.issue_no
)
SELECT DISTINCT *
FROM combined c
WHERE c.from_section <> 'N/A' AND c.to_section <> 'N/A'
This is supposing that the 'N/A' is does not mean NULL...If you meant NULL replace "= 'N/A'" with IS NULL and replace "<> 'N/A'" with IS NOT NULL

Rows to columns in mysql

I'm doing the select:
select id,
status,
count(status) as qtd
from user
group by id, status;
The return is:
id | status | qtd
1 YES 5
1 NO 3
2 YES 3
2 NO 1
I want this:
id | YES | NO
1 5 3
2 3 1
Thanks.
NOTE:
you can use case logic to do what you want.. basically you want to pivot the results and to pivot them you have to use aggregates with conditionals to fake a pivot table since mysql doesn't have a way to accomplish that
QUERY:
SELECT
id,
SUM(CASE status WHEN 'Yes' THEN 1 ELSE 0 END) as 'YES',
SUM(CASE status WHEN 'No' THEN 1 ELSE 0 END) as 'NO'
FROM user
GROUP BY id;
DEMO
OUTPUT:
+----+-----+----+
| id | YES | NO |
+----+-----+----+
| 1 | 5 | 3 |
| 2 | 3 | 1 |
+----+-----+----+
You can do so,using expression in sum() like sum(status ='Yes') will result as boolean (0/1) and thus you can have your count based on your criteria you provide in sum function
select id,
sum(status ='Yes') as `YES`,
sum(status ='No') as `NO`
from user
group by id;

SQL Query Summary

i would create a particular sql string.
I have this tables :
cities
-----------
city varchar not null primary key
etc
weeks
-----------
week varchar not null primary key
Insert_data
-----------
id int not null primary key
cityID varchar
week varchar
value1 int
etc
I would to bind asp.net gridview with this table structure:
+-----+-------+-------+-------+
| city| week1 | week2 | week3 |
+-----+-------+-------+-------+
| 1 | (y/n) | (y/n) | (y/n) |
| 2 | (y/n) | (y/n) | (y/n) |
| 3 | (y/n) | (y/n) | (y/n) |
| 4 | (y/n) | (y/n) | (y/n) |
| 5 | (y/n) | (y/n) | (y/n) |
+----+-------+-------+-------+
if there are records related to that week in insert_data the value will be Y.
It's possible to create a query string for this?
As always, when you need to create columns from rows with a "group by argument" (city), you can use the PIVOT operator.
http://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx
Above only applies to MS-SQL, doesn't apply to MySQL.
Or you can do it the old-fashioned way
SELECT
cities.city
,SUM(CASE WHEN weeks.week = 1 THEN Insert_data.value1 ELSE 0 END) AS week1
,SUM(CASE WHEN weeks.week = 2 THEN Insert_data.value1 ELSE 0 END) AS week2
,SUM(CASE WHEN weeks.week = 3 THEN Insert_data.value1 ELSE 0 END) AS week3
,SUM(CASE WHEN weeks.week = 4 THEN Insert_data.value1 ELSE 0 END) AS week4
-- ,etc. for each week (select distinct week from insert_data order by week)
FROM Insert_data
left join cities
cities.city= Insert_data.cityID
left join weeks
on weeks.week = Insert_data.week
group by cities.city
Either way, you need to dynamically hardcode the weeks in your SQL string.
You would now need an additional condition for y and n, and then you can concat the values.
,'(' + CAST(SUM(CASE WHEN weeks.week = 1 AND value1 = 1 THEN 1 ELSE 0 END) as varchar(10))
+ '/'
CAST(SUM(CASE WHEN weeks.week = 1 AND value1 = 0 THEN 1 ELSE 0 END) as varchar(10))
+ ')'
AS week1
or if it's just y or no, then
,
CASE
WHEN SUM(CASE WHEN weeks.week = 1 THEN Insert_data.value1 ELSE 0 END) > 0
THEN 'y'
ELSE 'n'
END AS week1

combining multiple different sql into one

cusID | Name | status | Date
---------------------------------
1 | AA | 0 | 2013-01-25
2 | BB | 1 | 2013-01-23
3 | CC | 1 | 2013-01-20
SELECT COUNT(cusID) FROM customer WHERE STATUS=0;
SELECT COUNT(cusID) FROM customer WHERE STATUS=1;
Is there a way of combing such two sql and return the results as one. Because want to avoid calling to DB everytime. I tried UNION of two statments, but only showing one result.
This is the shortest possible solution in MySQL.
SELECT SUM(status = 1) totalActive,
SUM(status = 0) totalInactive
FROM tableName
SQLFiddle Demo
and this is the CASE version
SELECT SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) totalActive,
SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) totalInactive
FROM tableName
SQLFiddle Demo