MSSQL Return 0 for null blank - sql

select COALESCE([ItemSlotX],0) as [ItemSlotX],COALESCE([ItemSlotY], 0 ) as [ItemSlotY] from [PowerUP_Items] where [ItemIndex]=16 and [ItemGroup]=255
In my case, is no record.
How can I return 0 for ItemSlotX and 0 for ItemSlotY if there is no record found?

If you want the results, even when there is no matching row, use this:
select COALESCE([ItemSlotX],0) as [ItemSlotX],COALESCE([ItemSlotY], 0 ) as [ItemSlotY]
from (select null dummy ) d
left
outer
join [PowerUP_Items]
on [ItemIndex]=16 and [ItemGroup]=255

This?
select ItemSlotX, ItemSlotY from PowerUP_Items where ItemIndex=16 and ItemGroup=255
if ##rowcount = 0
select 0 as ItemSlotX, 0 as ItemSlotY
Or more general approach:
if exists (select * from PowerUP_Items where ItemIndex=16 and ItemGroup=255)
select ItemSlotX, ItemSlotY from PowerUP_Items where ItemIndex=16 and ItemGroup=255
else
select 0 as ItemSlotX, 0 as ItemSlotY

If your query must return not more than one row, try this
select COALESCE(max([ItemSlotX]),0) as [ItemSlotX],COALESCE(max([ItemSlotY]), 0 ) as [ItemSlotY]
from [PowerUP_Items] where [ItemIndex]=16 and [ItemGroup]=255

Related

returning the column values as a list or else 0

I had a query where i am trying to get the results of a query, the query can have multiple rows or it can be empty, i am trying if it is empty, it should return me 0 for a column i am looking which is called as sequence
My query is like this:
select CASE WHEN COUNT(1) > 0 THEN 1 ELSE 0 END AS Sequence
from dbo.mytable
it returns me the either 1 or 0, for 1 i want that column should return me values or it should combine all the rows and return me the value of that column as list like 1,2,3,4,5,6,7
This should work.
SELECT
CASE WHEN MY_COUNT > 0 THEN 1 ELSE 0 END AS SEQUENCE
FROM
(SELECT COUNT(*) AS MY_COUNT
FROM
DBO.MYTABLE);
If you want only one row in the result set, simply do:
select (case when count(*) > 0 then 1 else 0 end) as sequence
from mytable;
If you care at all about performance, the more efficient method is:
select (case when exists (select 1 from dbo.mytable) then 1 else 0
end) as sequence

How to check all records have some value in oracle query?

I have query to check some exists like:
SELECT CASE WHEN (exists (select * from "Customer" where length(CustomerID) >
0) ) then 1 else 0 end val from dual
How do I know all records of Customer table have length of the field(CustomerID) bigger than 3. If all records have value bigger than 3 then 1 or 0 .
Thanks in advance.
Joon
Something like this?
SELECT CASE WHEN MIN(LENGTH(CustomerID)) > 3 THEN 1 ELSE 0 END AS Val
FROM Customer;
Try this.
SELECT CASE
WHEN COUNT(CASE
WHEN LENGTH(CustomerID) > 3
THEN 1
END) = COUNT(*)
THEN 1
ELSE 0
END as res
FROM Customer;
Demo

How to execute multiple case when statements for each iteration in SAS?

I'm using proc sql, and using multiple case when statements to add columns with either a 0 or 1 if the condition is met. It's a big bottleneck right now since it has to scan through each id for each case when statement. So I'm trying to figure out a way to somehow nest the case statements to perform each iteration, instead of having to iterate for all case statements.
This is an example of my code that is taking too long right now.
SELECT *,
CASE WHEN loannumber IN (
SELECT loannumber FROM PREPAY_LOAN_IDS
) THEN 1
ELSE 0 END AS PREPAY_FLAG,
CASE WHEN loannumber IN (
SELECT loannumber FROM DPD_30_IDS
) THEN 1
ELSE 0 END AS DPD_30_FLAG,
CASE WHEN loannumber IN (
SELECT loannumber FROM DPD_60_IDS
) THEN 1
ELSE 0 END AS DPD_60_FLAG,
CASE WHEN loannumber IN (
SELECT loannumber FROM DPD_90_IDS
) THEN 1
ELSE 0 END AS DPD_90_FLAG,
CASE WHEN loannumber IN (
SELECT loannumber FROM DPD_120_IDS
) THEN 1
ELSE 0 END AS DPD_120_FLAG,
CASE WHEN loannumber IN (
SELECT loannumber FROM FORECLOSURE_IDS
) THEN 1
ELSE 0 END AS FORECLOSURE_FLAG
FROM(
SELECT *
FROM MORTGAGES
)
The below query will work faster than the one you have posted as the input table is not completely access to retrieve the results. Try running this query and see how it performs.
SELECT M.*,
CASE WHEN PLI.loannumber IS NOT NULL THEN 1
ELSE 0 END AS PREPAY_FLAG,
CASE WHEN D3I.loannumber IS NOT NULL THEN 1
ELSE 0 END AS DPD_30_FLAG,
CASE WHEN D6I.loannumber IS NOT NULL THEN 1
ELSE 0 END AS DPD_60_FLAG,
CASE WHEN D9I.loannumber IS NOT NULL THEN 1
ELSE 0 END AS DPD_90_FLAG,
CASE WHEN D12I.loannumber IS NOT NULL THEN 1
ELSE 0 END AS DPD_120_FLAG,
CASE WHEN FCI.loannumber IS NOT NULL THEN 1
ELSE 0 END AS FORECLOSURE_FLAG
FROM MORTGAGES M
LEFT JOIN
PREPAY_LOAN_IDS PLI
ON M.loannumber = PLI.loannumber
LEFT JOIN
DPD_30_IDS D3I
ON M.loannumber = D3I.loannumber
LEFT JOIN
DPD_30_IDS D6I
ON M.loannumber = D6I.loannumber
LEFT JOIN
DPD_90_IDS D9I
ON M.loannumber = D9I.loannumber
LEFT JOIN
DPD_90_IDS D12I
ON M.loannumber = D12I.loannumber
LEFT JOIN
FORECLOSURE_IDS FCI
ON M.loannumber = FCI.loannumber
;
Since you're using SAS, here's a data step alternative, assuming that each of your datasets is already either sorted by or has an index on loannumber:
data want;
merge MORTGAGES(in = Mortgages)
PREPAY_LOAN_IDS(in = PLIDs keep = loannumber)
/*etc*/
;
by loannumber;
if Mortgages;
PREPAY_FLAG = PLIDs;
/*etc*/
run;
N.B. You will get duplicated records from MORTGAGES if you have duplicates in any of your other tables.

Return 1 or 0 as a subquery field with EXISTS?

I'm struggling to figure this one out.
What I want to do is like so:
select [fields],
((select <criteria>) return 0 if no rows returned, return 1 if any rows returned) as SubqueryResult
where a=b
Is this possible?
Please try:
select [fields],
case when (select COUNT(*) from YourTable with criteria)>0 then
1
else
0
end
as SubqueryResult
where a=b
In T-sql you can use Exists clause for the given requirement as:
select [fields],
case when exists (select <criteria> from <tablename> ) then 1
else 0
end as SubqueryResult
from <tablename>
where a=b
Also in TSQL you can do it using ISNULL() and SELECT TOP 1:
select [fields],
ISNULL((SELECT TOP 1 1 FROM YourTable WHERE <criteria> ),0)
as SubqueryResult
where a=b

SQL if select statement returns no rows then perform alternative select statement

Basically, what syntex would allow me to achieve the title statement?
If (select statement 1) returns 0 rows THEN (select statement 2) else (select statement 3)
So that the sql returns results from either statement 2 or 3
I've looked for a way to do this but nothing I've found so far seems to exactly address the if requirements.
IF EXISTS (SELECT field FROM table)
BEGIN
SELECT field FROM table2
END
ELSE
BEGIN
SELECT field FROM table3
END
Here you go...
IF ((select count(*) from table1)= 0)
BEGIN
Select * from table2
END
ELSE
BEGIN
SELECT * from table3
END
Sorry for the lack of feedback. Someone else in the office took an interest and came up with this:
select * from (
select *
, (SELECT Count(*)
FROM users
WHERE version_replace = 59 AND moderated = 1) AS Counter
FROM users WHERE version_replace = 59 AND moderated in (0,1)
) AS y
where Counter = 0 and Moderated = 0
or Counter > 0 and Moderated = 1
ORDER By ID DESC
Which does what I need.