How to get single row when foreign key is similar - mysql

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

Related

Deduplicating rows using GROUP BY. Issue with Max()

SELECT BVDID, max(case when CompanynameLatinalphabet='' then NULL else CompanynameLatinalphabet end) as CompanynameLatinalphabet, idField1, idField2 FROM `imported_companies_BVD` where BVDID=103327
group by BVDID, CompanynameLatinalphabet
Hi everyone. In the below statement, I am getting:
BVDID | CompanynameLatinalphabet | idField1 | idField2
103327 | NULL | 100 | 1
103327 | CASEIFICIO SOCIALE DI RAV... | NULL | 1
How can I prevent the row with NULL be retrieved? I can not use IS NOT NULL in the where statement, as there are other columns that have data in the row.
The whole idea is merging the whole thing, there are values in BOTH ROWS that should be merged into as single row.
Thanks if you can help!
The expected result should be:
BVDID | CompanynameLatinalphabet | idfield1 | idfield2
103327 | CASEIFICIO SOCIALE DI RAV... | 100 | 1
Why use case statements in max function? If value is blank just return blank instead of null
If you want to exclude the rows with NULL in the column CompanynameLatinalphabet you can do:
select *
from (
SELECT
BVDID,
max(case when CompanynameLatinalphabet='' then NULL
else CompanynameLatinalphabet end) as CompanynameLatinalphabet
FROM `imported_companies_BVD`
where BVDID=103327
group by BVDID, CompanynameLatinalphabet
) x
where CompanynameLatinalphabet is not null

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

Select group multiple records by specific condition

I have table:
+----+-------+-------------+
| id | code | value_check |
| 1 | p-01 | OK |
| 2 | p-01 | NOT OK |
| 3 | p-01 | OK |
| 4 | p-02 | OK |
| 5 | p-02 | OK |
| 6 | p-02 | OK |
+----+-------+-------------+
How can I select record which having 'OK' group by code,but if there is one or more 'NOT OK' on value_check then don't need to select
expected result:
code
p-02
i have tried my query can get the result but its very slow
this is my query :
SELECT code FROM table
WHERE code
NOT IN (SELECT code FROM table
WHERE value_check = 'NOT OK' GROUP BY code)
GROUP BY code
any other solution?
Check whether the total count is equal to the count of rows having value as OK using HAVING clause.
Query
select `code` from `your_table_name`
group by `code`
having count(*) = sum(`value_check` = 'OK');
Find a demo here
Try below with conditional aggregation
select code from table
group by code
having sum(case when value_check='NOT OK' then 1 else 0 end)=0
You can try it also with correlated subquery:
demo
SELECT distinct code FROM cte1 a
WHERE NOT exists (SELECT 1 FROM cte1 b where a.code=b.code and val = 'NOT OK')
SELECT DISTINCT x.code
FROM my_table x
LEFT
JOIN my_table y
ON y.code = x.code
AND y.value_check = 'not ok'
WHERE x.value_check = 'ok'
AND y.id IS NULL

SQL WHERE clause logic mismatch

I have a database table with the following information
+----+--------+--------+--------+
| id | status | action | reason |
+----+--------+--------+--------+
| 1 | CN | WAPP | BDEC |
| 2 | CN | DENY | PREF |
| 3 | AP | APPL | MI |
| 4 | AP | MATR | AUTO |
+----+--------+--------+--------+
A MySQL query to select data from this table has been written with a specific filter to exclude all records matching CN-WAPP-BDEC.
SELECT *
FROM `table`
WHERE (`status` != 'CN' AND `action` != 'WAPP' AND `reason` != 'BDEC');
However, it only returns records 3 and 4, record #2 is also excluded.
If I change the query to:
SELECT *
FROM `table`
WHERE CONCAT(`status`, `action`, `reason`) != 'CNWAPPBDEC';
Only then, do I get the expected result: records 2, 3, and 4.
As far as I can tell, the queries should both do the exact same thing, but they obviously aren't, and I'm confused as to why that is. Any insight would be appreciated.
Record 2 is excluded because status = CN, which makes the first condition status != 'CN' false.
Try this:
SELECT *
FROM `table`
WHERE (NOT (`status` == 'CN' AND `action` == 'WAPP' AND `reason` == 'BDEC'));
I think you want OR, not AND:
SELECT t.*
FROM `table` t
WHERE `status` <> 'CN' OR `action` <> 'WAPP' OR `reason` <> 'BDEC';
You can also phrase this using tuples:
WHERE (status, action, reason) not in ( ('CN', 'WAPP', 'BDEC') );

MySQL Query that will group records

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