Multiple rows with null values - i want one row with not null - sql

My query result below:
ID desc Year pid
0006845503 tes1 null null
0006845503 null 2017 null
0006845503 null null 90
0006845503 null null null
I want to show these results:
ID desc year pid
0006845503 TEST1 2017 90

If a value for a column appear only once(only one value available) , then simple group by should do the task :
SELECT t.id,t.year,t.code,MAX(t.desc) as desc,MAX(t.year) as year,MAX(t.pid) as pid
FROM YourTable t
GROUP BY t.id,t.year,t.code

If you know that only one row per column would have a value, you can use an aggregate function like min or max that would skip the nulls and return the only value in the column. E.g.:
SELECT id, year, code, MAX("desc"), MAX("year"), MAX(pid)
FROM mytable
GROUP BY id, year, code

Related

SQL query to allow for latest datasets per items

I have this table in an SQL server database:
and I would like a query that gives me the values of cw1, cw2,cw3 for a restricted date condition.
I would like a query giving me the "latest" values of cw1, cw2, cw3 giving me previous values of cw1, cw2, cw3, if they are null for the last plan_date. This would be with a date condition.
So if the condition is plan_date between "02.01.2020" and "04.01.2020" then the result should be
1 04.01.2020 null, 9, 4
2 03.01.2020 30 , 15, 2
where, for example, the "30" is from the last previous date for item_nr 2.
You can get the last value using first_value(). Unfortunately, that is a window function, but select distinct solves that:
select distinct item_nr,
first_value(cw1) over (partition by item_nr
order by (case when cw1 is not null then 1 else 2 end), plan_date desc
) as imputed_cw1,
first_value(cw2) over (partition by item_nr
order by (case when cw2 is not null then 1 else 2 end), plan_date desc
) as imputed_cw2,
first_value(cw3) over (partition by item_nr
order by (case when cw3 is not null then 1 else 2 end), plan_date desc
) as imputed_cw3
from t;
You can add a where clause after the from.
The first_value() window function returns the first value from each partition. The partition is ordered to put the non-NULL values first, and then order by time descending. So, the most recent non-NULL value is first.
The only downside is that it is a window function, so the select distinct is needed to get the most recent value for each item_nr.

SQL Server find results within partition

I have the following table:
ID Date
-------------------
1 Null
1 1/2/2020
2 Null
2 12/2/2020
3 Null
For every ID which has at least one non-null date, I need to classify as 'accounted'.
Result set should look like below:
id Date AccountFlag
----------------------------
1 Null Accounted
1 1/2/2020 Accounted
2 Null Accounted
2 12/2/2020 Accounted
3 Null Unaccounted
You can use window functions to check if the same id has at least one non-null date, and a case expression to set the flag accordingly. Window aggregate functions come handy for this:
select id, date,
case when max(date) over(partition by id) is not null
then 'Accounted'
ese 'Unaccounted'
end as accountflag
from mytable
max() ignores null values, so it returns null if and only if all values in the partition are null. This would work just the same with min().

If one value is null get previous value in that quarter, in sql select query

I have data as shown below,
Now i want to get result as ,
DateDisplayName Active
Q2(Jun)-2015 736
Q3(Sep)-2015 734
Q4(Dec)-2015 NULL
Q1(Mar)-2016 NULL
So if last month data is null in that quarter then get last but one data.
Ex: in Q3 Active is null for Sep so i shoul show Aug data.
You'd rank your records. Use ROW_NUMBER to give the best record per quarter row number 1 and then only keep those.
select
date_display_name,
active
from
(
select
date_display_name,
active,
row_number() over
(
partition by date_display_name
order by
case when active is null then 2 else 1 end,
defaultdate desc
) as rn
from mytable
) ranked
where rn = 1;

Eliminate NULL records in distinct select statement

In SQL SERVER 2008
Relation : Employee
empid clock-in clock-out date Cmpid
1 10 11 17-06-2015 001
1 11 12 17-06-2015 NULL
1 12 1 NULL 001
2 10 11 NULL 002
2 11 12 NULL 002
I need to populate table temp :
insert into temp
select distinct empid,date from employee
This gives all
3 records since they are distinct but what
I need is
empid date CMPID
1 17-06-2015 001
2 NULL 002
Depending on the size and scope of your table, it might just be more prudent to add
WHERE columnName is not null AND columnName2 is not null to the end of your query.
Null is different from other date value. If you wont exclude null record you have to add a and condition like table.filed is not null.
It sounds like what you want is a result table containing a row or tuple (relational databases don't have records) for every employee with a date column showing the date on which the worked or null if they didn't work. Right?
Something like this should do you:
select e.employee_id
from ( select distinct
empid
from employee
) master
left join employee detail on detail.empid = master.empid
and detail.date is not null
The master virtual table gives you the set of destinct employees; the detail gives you employees with non-null dates on which they worked. The left join gives you everything from master with any matches from detail blended in.
Rows in master with no matching rows in details, are returned once with the contributing columns from detail set to null. Rows in master with matching rows in detailare repeated once for each such match, with the detail columns reflecting the matching row's values.
This will give you the lowest date or null for each empid
SELECT empid,
MIN(date) date,
MIN(cmpid) cmpid
FROM employee
GROUP BY empid
try this
select distinct empid,date from employee where date is not null

SQL select COUNT issue

I have a table
num
----
NULL
NULL
NULL
NULL
55
NULL
NULL
NULL
99
when I wrote
select COUNT(*)
from tbl
where num is null
the output was 7
but when I wrote
select COUNT(num)
from tbl
where num is null
the output was 0
what's the difference between these two queries ??
Difference is in the field you select.
When counting COUNT(*) NULL values are taken into account (count all rows returned).
When counting COUNT(num) NULL values are NOT taken into account (count all non-null fields).
That is a standard behavior in SQL, whatever the DBMS used
Source. look at COUNT(DISTINCT expr,[expr...])
count(*) returns number of rows, count(num) returns number of rows where num is not null. Change your last query to select count(*) from test where num is null to get the result you expect.
In second case first count values are eliminated and then where clause comes in picture. While in first case when you are using * row with null is not eliminated.
If you are counting on a coll which contains null and you want rows with null to be included in count than use
Count(ISNULL(col,0))
Count(*) counts the number of rows, COUNT(num) counts the number of not-null values in column num.
Considering the output given above, the result of the query count(num) should be 2.