Select statement to return constant when no records found in table in SQL Server 2008 - sql

I am have a table with data and now i need to return zero in select statement if there is no records in table for example. I need to use it in Stored Procedure.
-- If no records exists in below select statement
SELECT ID,Text,Date FROM tblData WHERE ID = 12
IF (##ROWCOUNT = 0)
BEGIN
SELECT -5 AS ID
END
Output:
ID Text Date
ID
-5
Expected output
ID
-5

If you want to return 1 row even when there is no match, you can use aggregation:
SELECT (CASE WHEN COUNT(*) = 0 THEN -5 ELSE MAX(ID) END) as ID
FROM tblData
WHERE ID = 12;

I always use an Exists statment.
if exists(SELECT ID FROM tblData WHERE ID = 12)
select 0 as RowsExist
else
select 1 as RowsExist

For a single scalar value you could use something like;
SELECT ISNULL((SELECT ID FROM tblData WHERE ID = 12), 0) as ID
Rhys

SELECT (CASE WHEN Ta.ID IS NULL THEN TBL.ID
ELSE Ta.ID END) AS ID,Ta.Text,Ta.Date
FROM (VALUES(-5)) AS TBL(ID)
LEFT JOIN
(
SELECT ID,Text,Date FROM tblData WHERE ID = 12
)
AS Ta ON Ta.ID = Ta.ID

Related

How best to Count(*) with a CASE STATEMENT?

The following SQL (on SQL Server) returns an error of:
Incorrect syntax near '*'
Is there something inherently wrong with using the following SELECT statement?:
SELECT
COUNT(CASE WHEN <conditions> THEN * ELSE NULL END) as conditionalcountall
FROM TABLE
I tried this variation which also failed:
SELECT
CASE WHEN <conditions> THEN COUNT(*) ELSE NULL END as conditionalcountall
FROM TABLE
I tend to like sum()
SELECT
SUM(CASE WHEN <conditions> THEN 1 ELSE 0 END) as conditionalcountall
FROM TABLE
Try This, it is Tested
SELECT
CASE WHEN 1 = 1 THEN COUNT(*) ELSE NULL END as conditionalcountall
FROM TABLE
1 = 1is example conditions
Demo:-
Create table #temp (id int , col1 varchar (10))
go
insert into #temp values (1 , 'aaaa')
insert into #temp values (2 , 'bbbb')
insert into #temp values (3 , 'cccc')
SELECT
CASE WHEN 1 = 1 THEN COUNT(*) ELSE NULL END as conditionalcountall
FROM #temp
Result:
In Case Condation like that id = 1 you should select Count(*) in CASE cluse in your query
like this:
SELECT
CASE WHEN id = 1 THEN (select COUNT(*) from #temp) ELSE NULL END as conditionalcountall
FROM #temp
Result:-
Note: if You used Count(*) directly, you counted the id column, so you should use group by as next:
SELECT
CASE WHEN id = 1 THEN COUNT(*) ELSE NULL END as conditionalcountall
FROM #temp
group by id
Result:
SELECT
CASE WHEN X THEN Y
ELSE Z
END *NEW COLUMN NAME*
, COUNT(*)
FROM
TABLE
GROUP BY
*NEW COLUMN NAME*
This should return two columns, one with the buckets/case statement and one with the count of the columns for each one of your buckets
This method was the most straightforward for myself
If you REALLY, REALLY want to use COUNT, then you can do this:
SELECT
COUNT(*)
FROM table
WHERE <conditions>

Sql Case Statement Error Cannot select More Than one Column

I need to get the Supplier ID from the SQL Select statement inside Case statement. Once I put the A.SUPPLIER_ID to Select Statement I get an Error. How to do this?
Select
CASE
WHEN TYPE = 1 THEN
(
SELECT A.name
from BIZZXE_V2_SCH.SUPPLIERS A
where A.SUPPLIER_ID = 30
)
ELSE
(
SELECT A.name
from BIZZXE_V2_SCH.STOCK_SUPPLIER A
where A.SUPPLIER_ID = 31
)
END name
from DUAL;
You can't put complete queries into a case statement. But this should work
SELECT name
from BIZZXE_V2_SCH.SUPPLIERS
where SUPPLIER_ID = 30 and TYPE = 1
union all
SELECT name
from BIZZXE_V2_SCH.STOCK_SUPPLIER
where SUPPLIER_ID = 31 and TYPE <> 1
Using IF/ELSE statement
DECLARE #type NUMBER;
SELECT TYPE INTO #type FROM DUAL; -- Make sure it always returns one row
IF #type = 1 THEN
SELECT A.name
FROM BIZZXE_V2_SCH.SUPPLIERS A
WHERE A.SUPPLIER_ID = 30;
ELSE
SELECT A.name
FROM BIZZXE_V2_SCH.STOCK_SUPPLIER A
WHERE A.SUPPLIER_ID = 31
END IF;
You should be able to handle this in your WHERE statement, like this.
SELECT A.name
FROM BIZZXE_V2_SCH.STOCK_SUPPLIER A
WHERE (A.SUPPLIER_ID = 30 AND TYPE = 1) OR
A.SUPPLIER_ID = 31

SQL Specific Item on top

I am having this query for a photo contest:
SELECT * FROM `users` ORDER BY entry_id DESC
The result gives 10 records with entry_id 10, 9, 8, 7, ......1
What can I do to pick a specific entry on the top?
As there is a requirement if there is a refer ID, entry show first.
So the expected result should be: 4,10,9,8,7,6,5,3,2,1 if 4 if a refer ID.
Try this:
SELECT *
FROM `users`
ORDER BY (CASE WHEN entry_id = 4 THEN 0 ELSE 1 END), entry_id DESC;
for more Dynamic Approach create table value Function
create function OrderData(#a int)
returns #t table ( id int)
as
begin
insert into #t
SELECT *
FROM ab
ORDER BY (CASE WHEN id = #a THEN 0 ELSE 1 END), id DESC
return;
end;
select * from dbo.abc(4)
output
4,10,9,8,7,6,5,3,2,1
select * from dbo.abc(5)
output
5,10,9,8,7,6,4,3,2,1

sql query sum count

I have a table
id date state
1 10.01.01 reg
2 08.01.01 reg
3 05.01.01 check
4 02.01.01 check
5 01.01.01 reg
and want to show result like this
count reg
5 1
e.g sum of "reg" statuses should be counted only if the previous status was "check".
Please, help me or give the right direction to solve it
In SQL Server 2012 and above you could use LAG to access the previous row value:
SQL SERVER 2012
;WITH MyCTE AS
(
select id, date,state,lag(state,1) over(order by id) as prevstate
from Table1
)
SELECT COUNT(*),SUM(CASE WHEN state = 'reg' AND prevstate = 'check' THEN 1 ELSE 0 END)
FROM MyCTE
Fiddler Demo
ORACLE
With T AS
(
select "id", "date", "state", lag("state",1) over(order by "id") as "prevstate"
from Table1
)
SELECT COUNT(*),SUM(CASE WHEN "state" = 'reg' AND "prevstate" = 'check' THEN 1 ELSE 0 END)
FROM T
Fiddler Demo
MySQL
SELECT COUNT(*),SUM(CASE WHEN state = 'reg' AND prevstate = 'check' THEN 1 ELSE 0 END)
FROM
(
SELECT T1.ID,T1.Date,T1.state,T2.state AS prevstate
FROM Table1 T1
LEFT JOIN Table1 T2
ON T1.ID - 1 = T2.ID
) AS T
Fiddler Demo
Actually the third case would work in prety much everything.

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.