Fixed SQL Query if paramerter empty also incluce NULL - sql

I'm using the following where condition in a fixed sql query:
and ([NC_DATA].WORK_CENTER_BO LIKE '%[Param.4]%' OR '[Param.4]' IS NULL)
and its working fine when I enter a value for Param.4, but when I don't use Param.4 it's not giving the NULL paramter only data with values. How can I achieve that I get data with values and also data = NULL
Thanks for helping!
What I tried:
and ([NC_DATA].WORK_CENTER_BO LIKE '%[Param.4]%' OR '[Param.4]' IS NULL)
I get:
Column 1
WORK_CENTER_BO
ABC
123
DEF
456
I expecting:
Column 1
WORK_CENTER_BO
ABC
123
DEF
456
GHI
NULL

You can try using the COALESCE function as follows:
and (COALESCE([NC_DATA].WORK_CENTER_BO, '[Param.4]') LIKE '%[Param.4]%')
If your "WORK_CENTER_BO" is null, it will be replaced with [Param.4] to match the condition.

You could try like this:
AND (([NC_DATA].WORK_CENTER_BO LIKE '%'||NVL('[Param.4]',[NC_DATA].WORK_CENTER_BO)||'%') OR [NC_DATA].WORK_CENTER_BO IS NULL)
a rule of a thumb: All comparisons with NULL resolve to false.

Related

How to get first not null element from ARRAY<string> in hive 2.2.0

I have a hive SQL as below which returns the following output, now I want to get the first not null value from array i.e. "11".
SQ::
select cod_cust, split(reverse(concat_ws('',collect_list(case when
acc_status='Dormant' then "1" else "0" end))),'0') status from
account_status group by cod_cust;
Output::
cod_cust status
1023 ["","11","1","111","",""]
2209 ["","11","1","111","",""]
Without using split function the output would be look like:
cod_cust status
1023 01101011100
2209 01101011100
So the task is to find the first subsequence of 1, which can be solved by regexp_extract. Also you can use IF instead of CASE:
SELECT cod_cust,
regexp_extract(reverse(concat_ws('', collect_list(if((acc_status='Dormant'), '1', '0'))), '1+', 0) first_element
status
FROM account_status
GROUP BY cod_cust;

SQL-null value showing in the output when there is a value present in the database

I am extracting values from two different tables in the database. I should get only one line of output but instead I am getting two lines. One of the output line is perfect but the other line has one null value which clearly should not be null. My code is as below:
SELECT DISTINCT DEATH_RATE.COUNTRY_NAME,
DEATH_RATE.DATA_YEAR,
DEATH_RATE.DEATH_RATE_VALUE,
TIME_TO_EXPORT.EXPORT_VALUE
FROM DEATH_RATE, TIME_TO_EXPORT
WHERE TIME_TO_EXPORT.COUNTRY_NAME IN ('Belgium')
AND TIME_TO_EXPORT.COUNTRY_NAME = DEATH_RATE.COUNTRY_NAME
AND DEATH_RATE.DATA_YEAR = 2012
AND DEATH_RATE.DATA_YEAR = DEATH_RATE.DATA_YEAR ;
The output I am getting is as follows:
COUNTRY_NAME DATA_YEAR DEATH_RATE EXPORT_VALUE
1.Belgium 2012 423.5 9
2.Belgium 2012 423.5 null
First line is fine and only that should have been the output. Where is the second line and the null coming from ??
Thanks
The last condition of your query is a bit useless (the condition is always true when the value is not null):
DEATH_RATE.DATA_YEAR = DEATH_RATE.DATA_YEAR
I think you intended that :
TIME_TO_EXPORT.DATA_YEAR = DEATH_RATE.DATA_YEAR
(I assume that the fields have the same name)
In your select, you were thus getting all the TIME_TO_EXPORT records related to 'Belgium' whatever the value of DATA_YEAR.
So your query becomes :
SELECT DISTINCT DEATH_RATE.COUNTRY_NAME,
DEATH_RATE.DATA_YEAR,
DEATH_RATE.DEATH_RATE_VALUE,
TIME_TO_EXPORT.EXPORT_VALUE
FROM DEATH_RATE, TIME_TO_EXPORT
WHERE TIME_TO_EXPORT.COUNTRY_NAME IN ('Belgium')
AND TIME_TO_EXPORT.COUNTRY_NAME = DEATH_RATE.COUNTRY_NAME
AND DEATH_RATE.DATA_YEAR = 2012
AND TIME_TO_EXPORT.DATA_YEAR = DEATH_RATE.DATA_YEAR ;
Replace your last line with this:
AND TIME_TO_EXPORT.DATA_YEAR = DEATH_RATE.DATA_YEAR;
DEATH_RATE are TIME_TO_EXPORT are not properly join. and of course last line has no meaning.

Array_accum returning null values in postgres SQL when we have 350 rows in a table

I am using following query
select distinct throttle_id, array_accum(param) over (partition by throttle_id) as script_exclude from throttle_data where exclude = 't' and valve_type = 513 and throttle_id = 1270571881
and I'm getting null value in script_exclude field
Sample values I have in param column is,
airfare/pricing/launch_0tc.wql airfare/pricing/launch_0xp.wql airfare/pricing/launch_0xp_down.wql airfare/pricing/launch_0xp_up.wql
No.of rows is 351.
Please advice how could I get these all value.
Regards,
Sachin Jain
Try the following instead:
select throttle_id, array_agg(param) as script_exclude
from throttle_data where exclude = 't'
and valve_type = 513 and throttle_id = 1270571881
group by throttle_id;
If that still returns null then try:
select * from throttle_data
WHERE exclude = 't'
and valve_type = 513 and throttle_id = 1270571881;
To see what is actually aggregated.
If that doesn't return any rows, then something is wrong with your criteria in your where clause.

please correct me with the sql query

Whats wrong wrong with this query please correct me on this syntactcally it's going wrong please correct me on this how to concatenate this
string cmd = "SELECT *
FROM [tbl_students]
WHERE course_id=#courseId
AND branch_id IN ("+branchId+")
AND (#passoutYear is null or passout_year>=#passoutYear)
AND (#currentBacklog is null or current_backlog<=#currentBacklog)
AND gender=#sex
AND (#eGap is null or gapin_education<=#egap)
AND (#firstYrPercent is null or first_yea_percent>=#firstYrPercent
AND (#secondYrpercent is null or second_year_percent>=#secondYrPercent)
AND (#thirdYrPercent is null or third_year_percent>=#thirdYrPercent)
AND (#finalYrpercent is null or final_year_percent>=#finalYrpercent)
AND (#currentDegreePercentage is null current_degree_percent>=#currentDegreePercentage)
AND (#highSchoolPercentage is null high_school_percentage>=#highSchoolPercentage)
AND (#higherSchoolPercentage is null higher_school_percentage>=#higherSchoolPercentage)
AND (#graduationPercent is null graduation_percent>=#graduationPercentage)
AND (#diplomaPercentage is null diploma_percentage>=#diplomaPercenage)
AND (#noOfAtkt is null or no_of_atkt<=#noOfAtkt)
AND (#date is null or date_of_birth>=#date)";
First, if branchId is a string, then you'll need these single quotes:
branch_id IN('"+branchId+"') AND
though I don't understand why it's not parameterized like so:
branch_id = #branchId AND
Second, you may have misspelled the word 'year' in this field name:
AND (#firstYrPercent is null or first_year_percent>=#firstYrPercent
Finally, you're missing a few OR's here:
AND (#currentDegreePercentage is null OR current_degree_percent>=#currentDegreePercentage)
AND (#highSchoolPercentage is null OR high_school_percentage>=#highSchoolPercentage)
AND (#higherSchoolPercentage is null OR higher_school_percentage>=#higherSchoolPercentage)
AND (#graduationPercent is null OR graduation_percent>=#graduationPercentage)
AND (#diplomaPercentage is null OR diploma_percentage>=#diplomaPercenage)
I think I got it.
Are you getting an error while trying to COMPILE the code, as opposed to running the SQL query?
I'm going to bet that you're using C#, and that you should do multi-line strings with an #-symbol before the "-symbol. Such as this:
string blabla = #"
hello
there
";
Then of course you need it also when you open up the string after branchId

Oracle View problem with Select and division by zero

I want to create an view in Oracle which calculates an "utilization rate in percent".
AS SELECT
sw.SWITCH_ID,
sw.ASSET_ID,
sw.SYSTEMNAME,
sw.MAX_INSTALLABLE_PORTS,
sw.INSTALLED_PORTS,
sw.USED_PORTS,
(sw.INSTALLED_PORTS/sw.MAX_INSTALLABLE_PORTS)*100 AS UTIL_INSTALLED_PORTS,
sw.RES_INFRASTRUCTURE_PORTS,
sw.USED_INFRASTRUCTURE_PORTS,
sw.FREE_INFRASTRUCTURE_PORTS,
(sw.INSTALLED_PORTS/sw.MAX_INSTALLABLE_PORTS)*100 AS UTIL_INFRASTRUCTURE_PORTS,
sw.RESERVED_DEVICE_PORTS,
sw.USED_DEVICE_PORTS,
sw.FREE_DEVICE_PORTS,
(sw.FREE_DEVICE_PORTS/sw.RESERVED_DEVICE_PORTS)*100 AS UTIL_DEVICE_PORTS,
sw.RUN_DATE
Problem: sometimes sw.INSTALLED_PORTS or sw.MAX_INSTALLABLE_PORTS can be NULL (same for other UTIL Rows).
Is there any nice way to do something like:
if (sw.INSTALLED_PORTS or sw.MAX_INSTALLABLE_PORTS == null)
UTIL_INSTALLABLE_PORTS = null
else (sw.INSTALLED_PORTS/sw.MAX_INSTALLABLE_PORTS)*100 AS UTIL_INSTALLABLE_PORTS,
or a little shorter:
sw.INSTALLED_PORTS/NULLIF(sw.MAX_INSTALLABLE_PORTS,0)
Regards,
Rob.
Divizion by NULL is not the same as divizion by zero (as you reference the problem in the title).
select 1/null from dual = null
select null/null from dual = null
So you'll automatically get what you want by (sw.INSTALLED_PORTS/sw.MAX_INSTALLABLE_PORTS)*100.
I think, the problem is when sw.MAX_INSTALLABLE_PORTS is zero. In this case you can use the following:
case
when sw.MAX_INSTALLABLE_PORTS = 0 then null
else (sw.INSTALLED_PORTS/sw.MAX_INSTALLABLE_PORTS)*100
end
CASE
WHEN sw.INSTALLED_PORTS IS NULL OR sw.MAX_INSTALLABLE_PORTS IS NULL THEN NULL
ELSE (sw.INSTALLED_PORTS/sw.MAX_INSTALLABLE_PORTS)*100
END UTIL_INFRASTRUCTURE_PORTS
You could use COALESCE() which returns the first non-null from its arguments, like :
(COALESCE(sw.INSTALLED_PORTS, 0)/COALESCE(sw.MAX_INSTALLABLE_PORTS,1))*100
AS UTIL_DEVICE_PORTS
Note that I used 1 as the coalesce default in the denominator, you don't want a DIVIDE/0 instead.