Use rownum to always get non null value - sql

select case when (CUST.ADDRESS_TYPE='OFFICE') then
(Select MOBILE
FROM cust_table CUST
where CID = Deal.CID
and ADDRESS_TYPE = 'CURRES'
and rownum = 1)
else
CUST.MOBILE
end as MOBILE
FROM cust_table CUST
RIGHT OUTER JOIN (SELECT CID CID
, WNAME
, APPLICANT_TYPE
FROM deal_table ) DEAL
ON DEAL.CID = CUST.CID
AND APPLICANT_TYPE = 'P'
and mailing_add = 'true'
WHERE WNAME='22135'
and rownum = 1
#MOBILE#
NULL
647432923
OR
#MOBILE#
74238423
NULL
This query returns a column named 'MOBILE ' with two rows, one of the entries being always null when I dont use rownum = 1 at the end, but if I put rownum = 1 towards the end then in some cases it returns null value and in some cases non null value. How can I use rownum so that the query always returns non null value.

Do not use rownum for this. rownum will give you the number of the row after your query has been run. Use a properly defined ORDER BY clause to get your NULLS at the end and only fetch first row.
<your query>
WHERE wname='22135'
ORDER BY mobile NULLS LAST
FETCH FIRST 1 ROWS ONLY

Related

Remove the duplicate rows based on Presence of Number of NULL values in a row

I was able to remove the duplicate rows, but I would like to remove the duplicate rows based on one more constraint. I want to keep only a row with a smaller number of NULL values.
Original Table
Ran the SQL Server Query
WITH CTE AS(
SELECT *,
RN = ROW_NUMBER()OVER(PARTITION BY Premise_ID ORDER BY Premise_ID)
FROM sde.Premise_Test
)
DELETE FROM CTE WHERE RN > 1
Result:
But I want to get this result
I have modified the SQL script as per the comment from Aaron. but the result is still the same. DB fiddle is showing NULL from IS NULL getting highlighted.
Update the ROW_NUMBER() function like this (no, there is no shorter way):
RN = ROW_NUMBER() OVER (
PARTITION BY Premise_ID
ORDER BY Premise_ID,
CASE WHEN Division IS NULL THEN 1 ELSE 0 END
+ CASE WHEN InstallationType IS NULL THEN 1 ELSE 0 END
+ CASE WHEN OtherColumn IS NULL THEN 1 ELSE 0 END
...
)

Finding rows in SQL where changes but only certain changes while keeping others

I have this scenario where I want each occurrence of an active row to bring back that row in my result set and also inactive if there is only 1 inactive record for that IDENTIFIER and also if there are more than 1 active also show those. I've used Row_Number function and then in another query show where the row = '1' but if I do that row 1s only come back and then I lose some of my desired results. To restate my issue is I want all active records to come back and only inactive where IDENTIFIER is unique. The row that is bold should not be shown in the results.
1 has 1 active record in the DB.
2 has 2 active and 1 inactive records.
3 has no active records.
4 has only 2 active records, no inactive.
You can use a windowed conditional count, this has the benfit of only scanning the table once
SELECT
t.IDENTIFIER,
t.DB_ID,
t.Status
FROM (
SELECT *,
HasActive = COUNT(CASE WHEN t.Status = 'Active' THEN 1 END) OVER (PARTITION BY t.IDENTIFIER)
FROM YourTable t
) t
WHERE t.Status = 'Active' OR t.HasActive = 0;
One way to do this is with NOT EXISTS:
SELECT t1.*
FROM tablename t1
WHERE t1.Status = 'Active'
OR NOT EXISTS (
SELECT 1
FROM tablename t2
WHERE t2.identifier = t1.identifier AND t2.db_id <> t1.db_id
);
I assume that the column db_id is unique, at least for the same identifier.
If I understood you correctly, this is my variant.
select IDENTIFIER, [DB_ID], [Status]
from Tab
where [Status]='Active'
union
select IDENTIFIER, [DB_ID], [Status]
from Tab as t
where [Status]='Inactive' And 1=(select Count(*) from Tab where
IDENTIFIER=t.IDENTIFIER)
Order by IDENTIFIER, [DB_ID]
you can do it like this, because (rank=1 and Status=Inactive) only if there are no active rows for a particular Identifier
select * from (
select *,
DENSE_RANK() OVER (PARTITION BY identifier order by status) AS rank
from some_table
)
where rank=1 or status = 'Active'

Random sorting with ORDER BY with CASE clause

I am testing ORDER BY clause with CASE, and came across this problem.
My test select statement:
SELECT to_date as "One", field1 as "Two"
FROM(
SELECT to_date('yyyy-mm-dd', '2017-10-10'), '333' union all
SELECT to_date('yyyy-mm-dd', '2017-09-09'), '111' union all
SELECT to_date('yyyy-mm-dd', '2017-09-09'), '222' union all
SELECT to_date('yyyy-mm-dd', '2017-09-09'), '' union all
SELECT to_date('yyyy-mm-dd', '2017-09-09'), ''
)
ORDER BY One DESC,
CASE when Two = '' then 1
else 0 end DESC
And it's result may vary in a way, that sorting by second column is random:
How should I modify CASE clause to avoid it?
In Oracle, an empty string '' is the identical to NULL so your query is:
ORDER BY
One DESC,
CASE when Two = NULL then 1 else 0 end DESC
When comparing values, the are possible states are:
Equality Result
------------------------ ------
value = value TRUE
value = other_value FALSE
value = NULL NULL
NULL = NULL NULL
Your CASE expression will only evaluate to 1 when the equality evaluates to TRUE and this will never be the result when at least one side of the equality is NULL.
What you want is to use IS NULL rather than = '':
ORDER BY
One DESC,
CASE WHEN Two IS NULL THEN 1 ELSE 0 END DESC,
Two DESC;
Which you can simplify to:
ORDER BY
One DESC,
Two DESC NULLS FIRST;
The default for DESC ordering is NULLS FIRST so you could further simplify it to:
ORDER BY
One DESC,
Two DESC;
However, I would not take it this far as you are better explicitly stating that you are expecting NULL values to be ordered before non-NULL so future developers know that that is your intended ordering (rather than just an unintentional side-effect of your query).
Add the column two as third order condition
ORDER BY One DESC,
CASE when Two = '' then 1 else 0 end DESC,
Two DESC
The second order condition only puts empty entries first and not more.

Replace NULL with values

Here is my challenge:
I have a log table which every time a record is changed adds a new record but puts a NULL value for each non-changed value in each record. In other words only the changed value is set, the rest unchanged fields in each row simply has a NULL value.
Now I would like to replace each NULL value with the value above it that is NOT a NULL value like below:
Source table: Task_log
ID Owner Status Flag
1 Bob Registrar T
2 Sue NULL NULL
3 NULL NULL F
4 Frank Admission T
5 NULL NULL F
6 NULL NULL T
Desired output table: Task_log
ID Owner Status Flag
1 Bob Registrar T
2 Sue Registrar T
3 Sue Registrar F
4 Frank Admission T
5 Frank Admission F
6 Frank Admission T
How do I write a query which will generate the desired output table?
One the new windowed function of SQLServer 2012 is FIRST_VALUE, wich have quite a direct name, it can be partitioned through the OVER clause, before using it is necessary to divide every column in data block, a block for a column begin when a value is found.
With Block As (
Select ID
, Owner
, OBlockID = SUM(Case When Owner Is Null Then 0 Else 1 End)
OVER (ORDER BY ID)
, Status
, SBlockID = SUM(Case When Status Is Null Then 0 Else 1 End)
OVER (ORDER BY ID)
, Flag
, FBlockID = SUM(Case When Flag Is Null Then 0 Else 1 End)
OVER (ORDER BY ID)
From Task_log
)
Select ID
, Owner = FIRST_VALUE(Owner) OVER (PARTITION BY OBlockID ORDER BY ID)
, Status = FIRST_VALUE(Status) OVER (PARTITION BY SBlockID ORDER BY ID)
, Flag = FIRST_VALUE(Flag) OVER (PARTITION BY FBlockID ORDER BY ID)
FROM Block
SQLFiddle demo
The UPDATE query is easily derived
As I mentioned in my comment, I would try to fix the process that is creating the records rather than fixing the junk data. If that is not an option, the code below should get you pointed in the right direction.
UPDATE t1
set t1.owner = COALESCE(t1.owner, t2.owner),
t1.Status = COALESCE(t1.status, t2.status),
t1.Flag = COALESCE(t1.flag, t2.flag)
FROM Task_log as t1
INNER JOIN Task_log as t2
ON t1.id = (t1.id + 1)
where t1.owner is null
OR t1.status is null
OR t1.flag is null
I can think of several approaches.
You could use a combination of COALESCE with an array aggregate function. Unfortunately it doesn't look like SQL Server supports array_agg natively (although some nice people have developed some workarounds).
You could also use a subselect for each column.
SELECT id,
(SELECT TOP 1 FROM (SELECT owner FROM ... WHERE id = outer_id AND owner IS NOT NULL order by ID desc )) AS owner,
-- other columns
You could probably do something with window functions, too.
A vanilla solution would be:
select id
, owner
, coalesce(owner, ( select owner from t t2
where id = (select max(id) from t t3
where id < t1.id and owner is not null))
) as new_owner
, flag
, coalesce(flag, ( select flag from t t2
where id = (select max(id) from t t3
where id < t1.id and flag is not null))
) as new_flag
from t t1
Rather inefficient, but should work on most DBMS

SQL SELECT with condition and unique name

Say I have the following table. If I want to return ONLY the inactive rows (Active = 0) but disregard a field that has the same FileName. For instance, the query should only return Helper0990329 since it is inactive and has no other filename in another row that is the same.
VinnyVincenzo1345090457296 should not be included in the results.
PATH | FileName | Active
C:\Vinny\ VinnyVincenzo1345090457296.mp3 0
C:\Vinny\ VinnyVincenzo1345090457296.mp3 1
C:\Vinny\ VinnyVincenzo1345137702505.mp3 1
C:\Helper\ Helper0990329.mp3 0
I tried the following but I ended up deactivating (and later deleting) files that I shoulnt have:
SELECT
[Path],
[FileName]
FROM [Flows].[dbo].[Flows_Flows]
Where [Active] = '0' AND [Created] > '8/18/2012'
Group By Path, FileName
Having count(FileName) = 1
GO
Assuming you are using SQL 2005 or greater, you can use ranking functions to solve this:
select *
from (SELECT [Path], [FileName],
count(*) over (partition by path, filename) as numFiles
FROM [Flows].[dbo].[Flows_Flows]
Where [Created] > '8/18/2012'
) t
where active = 0 and numfiles = 1
The problem with our query is that the WHERE clause was evaluated before the HAVING clause. So, the active rows were never seen.
Or, you can do this with a simple group by but a more complicated HAVING clause:
SELECT [Path], [FileName]
FROM [Flows].[dbo].[Flows_Flows]
Where [Created] > '8/18/2012'
Group By Path, FileName
Having count(FileName) = 1 and
sum(case when Active = '0' then 1 else 0 end) = 1
Try it by counting its number of instance inside SubQuery
SELECT [Path], [FileName]
FROM Flows_Flows a
INNER JOIN
(
SELECT [FileName],
COUNT([FileName]) TotalCount
FROM Flows_Flows
GROUP BY [FileName]
) b ON a.[FileName] = b.[FileName]
WHERE a.[Active] = 0 AND
a.[Created] > '8/18/2012' AND
b.TotalCount = 1
You can use a LEFT OUTER JOIN and when the join finds a duplicate row, exclude it (dup.path is null).
SELECT
f.[Path],
f.[FileName]
FROM [Flows].[dbo].[Flows_Flows] f
LEFT OUTER JOIN [Flows].[dbo].[Flows_Flows] dup on f.Path = dup.Path and dup.Active = '1' --Active must be set otherwise, we found ourselves
Where f.[Active] = '0' AND f.[Created] > '8/18/2012'
AND dup.Path is null -- here we exculde all rows that have a duplicate
The advantage being that this solution has no sub query nor aggregate. That being said, not having looked at the query plan, I cannot say if it is indeed faster. (I also just prefer this syntax, I suppose.)
I'm guessing you have an id field as well right?
I'm also assuming that you dont necessarily want only ones that aren't duplicate rather ones users that don't have ANY active entries.
You could use IN in the WHERE clause.
example:
SELECT Path, FileName FROM Flows_Flows WHERE Active = 0 AND id NOT IN
( SELECT id FROM Flows_Flows WHERE Active = 1 )