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
Related
I need to retrieve rows that have a numeric or null value in the HomeID column and finally return the value with a numeric value if there is a row with the same symbol
// my table
+--------+---------+-------+
| Symbol | Home ID | Value |
+--------+---------+-------+
| test | 1 | value |
| test | NULL | value |
| test1 | 2 | value |
| test2 | 3 | vlaue |
+--------+---------+-------+
Actually, I did something like that. It added up the symbols for me, but I don't know how to return the poem I need
SELECT
[Symbol],
COUNT(*) AS CNT
FROM [DB].[dbo].[Table]
GROUP BY
[Symbol]
HAVING COUNT(*) > 1;
One method is:
select t.*
from t
where t.homeid is not null or
not exists (select 1
from t t2
where t2.symbol = t.symbol and t2.homeid is not null
);
You can also do this with aggregation, if you just have these three columns and you want exactly one row per symbol:
select symbol, max(homeid) as homeid,
coalesce(max(case when homeid is not null then value end),
max(value)
) as value
from t
group by symbol;
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
I have three table which i want to count each row with group from another table column.
The problem is count with group will return nothing when no record found.
So i want to add null value for each group that no record found.
Here is the query:
select monster.monster_name,count(*) as count
from monster right join monster_ability
on monster.monster_id= monster_ability.monster_id where
isnull(monster_ability.used)
group by monster.monster_id
here is the fiddle: fiddle
I want the result should look like this:
| monster_name | count |
|--------------|-------|
| kora | 1 |
| lowdowu | 3 |
| ngjengeh| null|
| lortyu | 1 |
| foh du fy| null|
Use case when to get null when count is 0:
select
m.monster_name,
case when count(a.ability_id) = 0 then null else count(a.ability_id) end as `count`
from monster m
left join monster_ability ma on m.monster_id = ma.monster_id and ma.used is null
left join ability a on ma.ability_id = a.ability_id
group by m.monster_id
SQLFiddle demo here.
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
I have data in a table that might look like so:
id | streetnum | name | item
-----------------------------
1 | 100 | a | 0
2 | 100 | b | NULL
3 | 100 | c | NULL
4 | 101 | d | NULL
5 | 101 | e | NULL
6 | 102 | f | 1
I'm trying to put together a query which would identify the identical streenum's where the item column has both a value and one or more NULL's. In the example above, the query's result should be:
100
My first instinct is to put together a nested query involving count(*) but I want to see what other ideas you guys come up with.
Also possible with a self join:
SELECT DISTINCT streetnum FROM atable AS a1,atable AS a2 WHERE a1.streetnum=a2.streenum AND a1.item IS NULL AND a2.item IS NOT NULL;
Here is a query that works in SQLServer. I haven't tested the syntax for mysql.
SELECT streetnum FROM YourTable
WHERE streetnum IN
(SELECT streetnum FROM YourTable
WHERE item IS NULL
GROUP BY streetnum)
AND streetnum IN
(SELECT streetnum FROM YourTable
WHERE item IS NOT NULL
GROUP BY streetnum)
GROUP BY streetnum
SELECT streetnum
FROM atable
GROUP BY streetnum
HAVING MAX(item) IS NOT NULL
AND COUNT(CASE WHEN item IS NULL THEN 1 END) > 0
MAX(item) can be replaced by MIN(item) or SUM or AVG. Also this part of condition can be replaced by COUNT(item) > 0.
The more tricky part is where you must account for the presence of NULLs as well. Here you'll have to use CASE, because you need to turn the NULL into a value to be able to use it in an aggregate. Once it is a value, you can COUNT or SUM it (MAX, MIN etc. would do as well).