I want to test two TaskChangeLogIDs where ID '28' has a NewRollback of 0, and ID '31' has a NewRollback of 1.
I want make sure that depending which ID is called, it selects the correct NewRollback number. I get an error applying the code below in a new window so just need help tweaking this and learn on how to test things like this in the future. I'm getting an error in the IF statement line only.
SELECT isnull(NewRollback, 0) FROM Core.TaskChangeLog
WHERE TaskChangeLogID = 28
SELECT isnull(NewRollback, 0) FROM Core.TaskChangeLog
WHERE TaskChangeLogID = 31
IF (NewRollback = 1)
Select * from Core.TaskChangeLog where NewRollback = 1
ELSE
BEGIN
Select * from Core.TaskChangeLog where NewRollback = 0
END
You don't need IF condition to do this just AND/OR logic will do the job
Try this way
Select * from Core.TaskChangeLog
where (NewRollback = 1 and askChangeLogID = 28)
OR (NewRollback = 0 and askChangeLogID = 31)
To do some action based on select result try this
IF EXISTS (Select * from Core.TaskChangeLog
where NewRollback = 1 and askChangeLogID = 28) -- Replace with your ID
BEGIN
--some action
END
IF EXISTS (Select * from Core.TaskChangeLog
where NewRollback = 0 and askChangeLogID = 28)
BEGIN
--some action
END
How about putting all this in one query? This is my best guess as to the logic you want:
Select *
from Core.TaskChangeLog
where NewRollback = (SELECT COALESCE(NewRollback, 0)
FROM Core.TaskChangeLog
WHERE TaskChangeLogID = XX
);
Your code fails because the NewRollback in the IF is undefined. You could use a variable, but that would just complicate the code. And, the code should be simplified rather than made more complicated.
Related
I want to write this query using all, but it is not valid SQL:
SELECT *
FROM OrderCustPlankLoad ocl
WHERE ocl.PlankLoadStatusId = 2 and all
(Select PlankLoadStatusId from OrderAccountPlankLoad oal where oal.PlankLoadStatusId = 2
and ocl.PlankClientId = oal.PlankClientId
)
What I want to ensure is that all related records in the related table have a status of 2.
But I don't think I am writing this correctly - SSMS does not like the "All" as well as "ocl.PlankClientId" in the query.
What I am doing is ensuring that all the records are valid before I start processing them. I don't want to process the rows in ocl if there are related rows in oal that are not valid.
How do I write this correctly?
select *
FROM OrderCustPlankLoad ocl
WHERE ocl.PlankLoadStatusId = 2 and not exists
(Select 1 from OrderAccountPlankLoad oal where oal.PlankLoadStatusId <>2
and ocl.PlankClientId = oal.PlankClientId
)
I think this is what you mean:
SELECT *
FROM OrderCustPlankLoad ocl
WHERE ocl.PlankLoadStatusId = 2
AND ocl.PlankClientId IN
(SELECT oal.PlankClientId
FROM OrderAccountPlankLoad oal
WHERE oal.PlankLoadStatusId = 2
)
I came up with this which seems to be working:
SELECT *
FROM OrderCustPlankLoad ocl
WHERE ocl.PlankLoadStatusId = 2
AND NOT EXISTS (
SELECT 1
FROM OrderAccountPlankLoad oal
WHERE oal.PlankLoadStatusId = 5
AND ocl.PlankClientId = oal.PlankClientId
)
I have 2 similar tables, one with all the data and the other contains a subset of the first. Every 2-3 days I need to insert in the second table the missing values, and I use this code
INSERT INTO [SRVDB2].[dbt].[curve].[curve_value]
SELECT *
FROM [SRVDB1].[dbt].[curve].[curve_value] as DB01
WHERE TargetDate >= '20150505'
and NOT EXISTS (SELECT *
FROM [SRVDB2].[dbt].[curve].[curve_value] as DB02
WHERE DB02.TargetDate = DB01.TargetDate
and DB02.[Hour] = DB01.[Hour]
and DB02.[id_Mkt] = DB01.[id_Mkt]
and DB02.[Price] = DB01.[Price]
and DB02.VoSe = DB01.VoSe
and DB02.VoBu = DB01.VoBu
)
It always worked but now I have some rows with NULL in column VoSe or VoBu and those values are not inserted correctly (even if executing only the SELECT statement seems to return all the differences). How can I handle these?
Add explicit check for NULL for both of these columns:
INSERT INTO [SRVDB2].[dbt].[curve].[curve_value]
SELECT *
FROM [SRVDB1].[dbt].[curve].[curve_value] as DB01
WHERE TargetDate >= '20150505'
and NOT EXISTS (SELECT *
FROM [SRVDB2].[dbt].[curve].[curve_value] as DB02
WHERE DB02.TargetDate = DB01.TargetDate
and DB02.[Hour] = DB01.[Hour]
and DB02.[id_Mkt] = DB01.[id_Mkt]
and DB02.[Price] = DB01.[Price]
and ((DB02.VoSe IS NULL AND DB01.VoSe IS NULL) OR DB02.VoSe = DB01.VoSe)
and ((DB02.VoBu IS NULL AND DB01.VoBu IS NULL) OR DB02.VoBu = DB01.VoBu)
)
#dotnetom's answer (+1) should work for your problem. However, making some assumptions on the problem you describe, I suspect the following would work just as well:
INSERT INTO [SRVDB2].[dbt].[curve].[curve_value]
SELECT *
FROM [SRVDB1].[dbt].[curve].[curve_value]
WHERE TargetDate >= '20150505'
EXCEPT SELECT *
FROM [SRVDB2].[dbt].[curve].[curve_value]
Use ISNULL to handle the NULL values.
NOTE: Use some random value in ISNULL function which will not come in those two columns. For example i have kept 'AAA'
SELECT *
FROM [SRVDB1].[dbt].[curve].[curve_value] as DB01
WHERE TargetDate >= '20150505'
and NOT EXISTS (SELECT *
FROM [SRVDB2].[dbt].[curve].[curve_value] as DB02
WHERE DB02.TargetDate = DB01.TargetDate
and DB02.[Hour] = DB01.[Hour]
and DB02.[id_Mkt] = DB01.[id_Mkt]
and DB02.[Price] = DB01.[Price]
and ISNULL(DB02.VoSe,'AAA') = ISNULL(DB01.VoSe,'AAA')
and ISNULL(DB02.VoBu,'AAA') = ISNULL(DB01.VoBu,'AAA')
)
I have a stored procedure which gets a list of items attached to a given configuration (each item is attached to one configuration ; if it's not (i.e. the field is NULL) then we consider it attaced to the default configuration (wich has its field ISDEFAULT to 1). I would like to get the items using pagination. I remembered that the ROWNUM field starts at one, so I wrote (using 0 for the first page, and ten items per page):
SELECT *
FROM
ITEMS item
WHERE
(ROWNUM - 1) >= 0 and
(ROWNUM - 1) < (0 + 10) and
(item.CONFIGID = 0 or
item.CONFIGID is null and exists (SELECT config.CONFIGID FROM CONFIGURATION config WHERE config.CONFIGID = 0 and config.ISDEFAULT = 1));
This request returned me zero results.
I changed it to:
SELECT item.NAME,
item.ITEMID,
item.CONFIGID,
ROWNUM
FROM
ITEMS item
WHERE
(ROWNUM - 1) >= 0 and
(ROWNUM - 1) < (0 + 10) and
(item.CONFIGID = 0 or
item.CONFIGID is null and exists (SELECT config.CONFIGID FROM CONFIGURATION config WHERE config.CONFIGID = 0 and config.ISDEFAULT = 1));
And it worked! Adding the ROWNUM in the SELECT statement fixed the issue.
Then I tried:
SELECT *
FROM
ITEMS item
WHERE
ROWNUM >= (0 + 1) and
ROWNUM < (0 + 1 + 10) and
(item.CONFIGID = 0 or
item.CONFIGID is null and exists (SELECT config.CONFIGID FROM CONFIGURATION config WHERE config.CONFIGID = 0 and config.ISDEFAULT = 1));
I wrote back the SELECT * statement and moved the - 1 to the other sides of the comparison operators (therefore they became + 1). This works as well.
Can someone explain why my first query does not work and yields zero results whereas the other two work like a charm?
I think there is nothing wrong with ROWNUM.
Can you try to execute:
SELECT *
FROM ITEMS item
WHERE
(ROWNUM - 1) >= 0 and
(ROWNUM - 1) < (0 + 10);
SELECT item.NAME,
item.ITEMID,
item.CONFIGID,
ROWNUM
FROM ITEMS item
WHERE
(ROWNUM - 1) >= 0 and
(ROWNUM - 1) < (0 + 10);
Both should work ok. Which means you have problem with second part of the filter.
The following subquery
(SELECT config.CONFIGID FROM CONFIGURATION config WHERE config.CONFIGID = 0 and config.ISDEFAULT = 1));
is uncorreleated. So it whould always return same result for any record. I think you have to add brackets into condition by item.CONFIGID:
(item.CONFIGID = 0) or
(
item.CONFIGID is null and
exists (SELECT ...)
)
or
(item.CONFIGID = 0 or
item.CONFIGID is null) and
exists (SELECT ...)
I think that is your problem. But not sure without seeing data.
Please, observe:
DECLARE #UseFastLane BIT
SELECT TOP 1 #UseFastLane = 1
FROM BackgroundJobService
WHERE IsFastLane = 1;
SELECT TOP 1 bjs.HostName AllocatedAgentHostName,
bjs.ServiceName AllocatedAgentServiceName,
bjs.IsFastLane,
SUM(CASE
WHEN bjw.WorkStatusTypeId IN ( 2, 3, 4, 10 ) THEN 1
ELSE 0
END) AS InProgress
FROM BackgroundJobService bjs
LEFT JOIN BackgroundJobWork bjw
ON bjw.AllocatedAgentHostName = bjs.HostName
AND bjw.AllocatedAgentServiceName = bjs.ServiceName
WHERE bjs.AgentStatusTypeId = 2
AND bjs.IsFastLane = COALESCE(#UseFastLane, 0)
GROUP BY bjs.HostName,
bjs.ServiceName,
bjs.IsFastLane
ORDER BY IsFastLane DESC,
InProgress
I am using two SQL select statements here. Is it possible to use just one top level SQL select statement, nesting another one within?
You can replace the text AND bjs.IsFastLane = COALESCE(#UseFastLane, 0) with this:
AND bjs.IsFastLane = (SELECT Max(IsFastLane)
FROM BackgroundJobService)
which should give you an equivalent query assuming that there are rows in the BackgroundJobService.
If there might be zero rows in BackgroundJobService then you can wrap the select with a COALESCE function to return 0, like this:
COALESCE((SELECT Max(IsFastLane) FROM BackgroundJobService), 0)
how to limit an integer query result to 1. a return of 2 to be 1, a return 1 to be 1, and a return of 0.5 to be 0.5 because it is <= 1. i don't want to modify the tables, i just want to modify the results.
This is my exact query.
select ((select "V01" from sports where "UID" = '1') * 1.0 ) /
(select "V01" from master where "BALL" = 'REQUIREMENT') ;
I'm using postgres.
To limit, you'd do something like this:
select
case
when yourNumber >= 1 then 1
else yourNumber
end
...
Then you just apply this concept to your query.
As noted by Wiseguy, you could also do:
select LEAST(yourNumber, 1)
, since this is postgresql.
The first solution will work with any ANSI SQL compatible database.
Update
Applied to your query, I think (if I understood what you want correctly) it would be like this:
select LEAST(1,
((select "V01" from sports where "UID" = '1') * 1.0 ) /
(select "V01" from master where "BALL" = 'REQUIREMENT')
);
use the LEAST function , docs: http://www.postgresql.org/docs/8.3/static/functions-conditional.html. Also, check out GREATEST too
SELECT LEAST(1, <your value>)
EDIT replaced GREATEST with LEAST
try this:
select CASE
WHEN ((select V01 from sports where UID = '1') * 1.0 ) /
(select V01 from master where BALL = 'REQUIREMENT') >= 1
THEN 1
ELSE ((select V01 from sports where UID = '1') * 1.0 ) /
(select V01 from master where BALL = 'REQUIREMENT')
END;