is their any way or a query to prevent data from displaying if it has same incident_id and the pnt_id is NULL using
in my attached image. the highlighted data must not be displayed
is it possible? if so. can anyone help me.? and Thanks in Advance
Try this query:
SELECT respond_id, incident_id, pnt_id FROM TBL
WHERE pnt_id IS NOT NULL
UNION
SELECT respond_id, incident_id, pnt_id FROM TBL
WHERE incident_id in (SELECT distinct(incident_id)
FROM TBL
GROUP BY incident_id
HAVING count(id) = 1);
The first part of the union will get all the not null pnt_ids
and the second part will get all the records that may be null but go in the result.
Union merges the two results eliminating duplicates
Related
Image1ImageHelp me to merge these 2 given queries
SELECT DISTINCT vehicle_no FROM petty_cash;
SELECT COUNT(variation) as bal FROM petty_cash WHERE variation<0;
Thanks in advance.
I'm not sure about your output but i can combine those 2 queries as below.
SELECT DISTINCT vehicle_no, COUNT(variation) as bal FROM petty_cash WHERE variation < 0;
Do you want to execute two statment in one query? Is answer is yes then it
SELECT (
SELECT DISTINCT vehicle_no FROM petty_cash
) AS firstStatment,
(
SELECT COUNT(variation) as bal FROM petty_cash WHERE variation<0
) AS secondStatment
You can use case to get your desired result,
like,
SELECT DISTINCT vehicle_no,CASE WHEN variation==0 THEN 'HIDE ROW' END FROM petty_cash;
After executing this query you can get the number of vehicle with HIDE ROW value where variation is zero.
In the place of 'HIDE ROW' you can set 0 or 1 for your understanding.
I Hope this will help you.
I need help :( I have table like this...
ID|code|item|user|
01|aaaa|1111|0001|
02|bbbb|1111|0001|
03|cccc|1111|0001|
04|dddd|1111|0001|
05|aaaa|1111|0002|
06|eeee|1111|0002|
07|ffff|1111|0001|
I'm user 0002 and I know my item numer (for example 1111). I don't know other users, ids and other codes, but i have to get only 02,03,04,07 results (for this example). Any sinle and duplicated rows (for code column) with user 002 should be ignored... if you know what i mean. Any ideas how? :(
This can be done with a subquery filter
select *
from myTable
where code not in (
select code
from myTable
where user = '0002'
)
Try with:
select ID
from myTable
where user <> '0002'
group by ID, code
having count(code) = 1
Is there any way to do that in a single query? Or do I have to manage it externally? It is not a JOIN of any kind.
SELECT
IF (
(SELECT indicator FROM configuration_table) = 1,
(SELECT series_id FROM series_table LIMIT 1),
''
) as if_exp
FROM
series_table
This executes but returns the first ID over and over, and if I take out the LIMIT 1, it doesn't work as it expects only one result. But what I need is that, if this condition is met:
(SELECT indicator FROM configuration_table) = 1,
Then I need all this data returned:
SELECT series_id, series_code, series_name FROM series_table
Is it possible somehow? Should I be doing two queries and managing the data from php? Thank you very much.
The easiest way would be:
IF ((SELECT indicator FROM configuration_table) = 1) THEN
SELECT series_id, series_code, series_name FROM series_table
END IF
You did not show us what to do, when the condition is false. We do not know the relationship between configuration_table and series_table, so we can't find a way to make it in a single query.
I have copied this answer from IF Condition Perform Query, Else Perform Other Query this answer.
SELECT CASE WHEN ( (SELECT indicator FROM configuration_table) = 1 )
THEN
SELECT series_id, series_code, series_name FROM series_table
ELSE
<QUERY B>
END
Here Query B should replaced by your desired query.
ID Site
a www.google.com
a
b www.qq.com
b
c www.hodes.com
.
.
.
I have a table like the one above, I'd like to extract the the site value is not blank, such as:
ID Site
a www.google.com
b www.qq.com
c www.hodes.com
You just have to check if the content of the column site is null or not. In order to select the rows with content:
SELECT * FROM TABLE_NAME WHERE SITE IS NOT NULL
Read more here.
select * from table where Site <> '' or Site is not null
will work if the blank ones are ALWAYS duplicates.
otherwise
select max(id), max(site) from table group by id
Should be okay
select
ID,
max(if(trim(site) = '', null, site)) as site
from tbl
group by ID
;
You could substitute GROUP_CONCAT for max if more than one site per ID.
I'm gonna go on a limb and assume that you want to remove the empty rows from your database. If that's not the case, just move the where clause to a Select statement.
Delete from TableName where ISNULL(Site, '')=''
If you must return one and only one record per id, then your code must
cope with potential multiple records per id -- and you need the max() and group by .
I would suggest:
Select id, max(site)
from table_name
where nvl(site,0) <> 0
group by id
Else if you know that you'd never encounter multiple records with the same id:
Select id, site
from table_name
where nvl(site,0) <> 0
I have a very simple question.
I want to know if a certain database row exists.
I generally use :
SELECT 1 FROM `my_table` WHERE `field_x` = 'something'
Then I fetch the result with :
$row = self::$QueryObject->fetch();
And check if any results :
if(isset($row[1]) === true){
return(true);
}
You can do this also with :
COUNT 1 FROM `my_table` WHERE `field_x` = 'something'
And similar to COUNT * FROMmy_tableandCOUNT field_id FROM `my_table
But I was wondering.. How does this relate to performance?
Are there any cons to using SELECT 1 or COUNT 1??
My feeling says that select INTEGER 1 means the lowest load.
But is this actually true??
Can anyone enlighten me?
Actually all your solutions are suboptimal :) What you do with your queries is reading every row there is to be found, even if you add limit. Do it like this:
SELECT EXISTS ( SELECT 1 FROM `my_table` WHERE `field_x` = 'something');
EXISTS returns 1 if something was found, 0 if not. It stops searching as soon as an entry was found. What you select in the subquery doesn't matter, you can even select null.
Also keep in mind, that COUNT(*) or COUNT(1) are very different from COUNT(column_name). COUNT(*) counts every row, while COUNT(column_name) only count the rows that are not null.
If you add the LIMIT 1 to the end of the query then SELECT works better than COUNT especially when you have a large table.