calculate value based on 2 fields in SQL database - sql

I can do this with linq easy but i got a situation where i have to create a stored procedure to return true or false based on 2 fields(minrange,maxrange) in table B. So, the goal is given an id from table A, i select the range value from table A and compare this value to the 2 ranges in table B. If the value is within range(minrange,maxrange) return true. Thanks.

I'm assuming you have a field that allows you to join records from Table A to Table B. I'll call it "CategoryID". Try this:
SELECT
CASE WHEN TableA.Value BETWEEN TableB.MinValue AND TableB.MaxValue
THEN 1 ELSE 0 END
FROM TableA
INNER JOIN TableB ON TableA.CategoryID = TableB.CategoryID
WHERE TableA.ID = "TheID"
Good luck!
-Michael

Without knowing specifics (as to foreign keys), this syntax should work.
SELECT
CAST((CASE
WHEN tableAValue < tableB.maxRange and tableAValue > tableB.minRange
THEN 1
ELSE
0
END) AS BIT)
FROM TableA
INNER JOIN TableB
ON TableA.ID = TableB.TableAID
WHERE TableA.ID = #yourID

Related

Select from table where another value doesn't exist in any of the joined tables

I am trying to retrieve the IDs from a table in Oracle only IF another column value doesn't exist in any of the joined tables. Let me give you an example:
example
As you can see in the sketch, Table A is joined to tables B via the ID. I would like to get the IDs from Table A only if all statuses in any joined Table B DO NOT contain the value 2.
Here is my SQL statement:
SELECT ID FROM TABLE A
LEFT JOIN TABLE B
ON A.ID = B.REF_ID
WHERE B.STATUS NOT IN (2)
Unfortunately, I still get all IDs (which makes sense) and am not able to come up with a method to retrieve only the IDs without a certain value in the Status column of a joined table. Hence, I would only like to get ID 1, since all joined tables do not contain the value 2 in their Status.
Many thanks for any inputs.
Use aggregation
SELECT ID
FROM TABLE A LEFT JOIN
TABLE B
ON A.ID = B.REF_ID
GROUP BY A.ID
HAVING SUM(CASE WHEN B.STATUS IN (2) THEN 1 ELSE 0 END) = 0;
Or, simply use NOT EXISTS:
SELECT A.ID
FROM TABLE A
WHERE NOT EXISTS (SELECT 1
FROM B
WHERE A.ID = B.REF_ID AND B.STATUS IN (2)
);

PROC SQL - Case when with multiple tables

I need to create a dataset (TABLE3) in order to check if some variables from other two tables are equals. If so, the code must return 0, otherwise 1. But, as I'm a new user of SAS and SQL I'm struggling to figure out how to do that.
I'm trying something like that but it's not working.
PROC SQL;
CREATE TABLE TABLE3 AS
SELECT A.*, B.*
CASE WHEN B.VARIABLE1 = A.VARIABLE2 THEN 0 ELSE 1 END AS VARIABLE_1_2,
CASE WHEN B.VARIABLE3 = A.VARIABLE4 THEN 0 ELSE 1 END AS VARIABLE_3_4
FROM TABLE1 AS A
LEFT JOIN TABLE2 AS B;
P.S.: Variables 1, 2, 3 and 4 are all character variables.
In addition to the tables relation you must add after the join, You miss a coma "," just before the first case.
For a JOIN condition to work, there must a ON clause
PROC SQL;
CREATE TABLE TABLE3 AS
SELECT A.*, B.*,
CASE WHEN B.VARIABLE1 = A.VARIABLE2 THEN 0 ELSE 1 END AS VARIABLE_1_2,
CASE WHEN B.VARIABLE3 = A.VARIABLE4 THEN 0 ELSE 1 END AS VARIABLE_3_4
FROM TABLE1 AS A LEFT JOIN TABLE2 AS B
**ON TABLE1.SOME_COLUMN_NAME = TABLE2.SOME_COLUMN_NAME**
;

SQL Conditional filter with different values

I have found lots of posts on coniditonal filtering in the where clause, but they all seem to be based off of using the same value, such as:
WHERE (o.OrderID = #orderid OR #orderid IS NULL)
I need to do something slightly different, I need to remove a filter and its value completely base on another value, so something like:
select *
from tableA
where 1 = 1
case when a = 1 then
and b in (select b from tableB)
else
-- do nothing
end
I know that the above is not allowed, and I am just writing as an example of what I am trying to do. does Anyone have any idea of a good way to do this? I know i could use if statements and duplicate the query, but it is a large one, and i am trying to avoid that.
Thanks
SELECT *
FROM tableA
WHERE a <> 1
OR (a = 1 AND EXISTS(SELECT b from TableB WHERE tableA.b = TableB.b))
You could also write this as:
SELECT tableA.*
FROM tableA
LEFT JOIN tableB
ON tableA.b = tableB.b
WHERE tableA.a <> 1
OR (tableA.a = 1 AND tableB.b IS NOT NULL)
"Correcting" your WHERE clause:
select *
from tableA
where 'T' = case
when a = 1 then case
when b in (select b from tableB) then 'T'
end
else 'T'
end;

Multiple counts in a multi table query SQL

I would like to count The different requests by survey Id's and grouping it by SubjectValue
I have done this on just the one table with a sub query, but I'm not too sure to do it with several. Could anyone help me out?
This is how the 3 tables are joined. The only values of note are
subjectValue - Table A
Request_Id - Table A
Survey_Id - Table C
SELECT TableA.SubjectValue
FROM TableB INNER JOIN
TableA ON TableB.ID = TableA.Request_ID INNER JOIN
Table C ON TableB.Details_ID = TableC.ID
May I also add that all counts should be returned in the same row.
there are 3 different survey Id's so the count will need a where clause on the survey_id.
Hope that makes sense.
Many thanks in advance.
You can use generic Cross-tab method
select
TableA.SubjectValue,
SUM(case when somecol='request1' then 1 else 0 end) as request1,
SUM(case when somecol='request2' then 1 else 0 end) as request2,
.
TableB INNER JOIN
TableA ON TableB.ID = TableA.Request_ID INNER JOIN
Table C ON TableB.Details_ID = TableC.ID
group by
TableA.SubjectValue
You probably dont need to join to table C (surveys) assuming you have a foreign key to it on table B (Requests).
try this.
SELECT TableA.SubjectValue, COUNT(TableB.SurveyID)
FROM TableB
INNER JOIN TableA ON TableB.ID = TableA.Request_ID
Group by TableA.SubjectValue
EDIT: To Include SurveyID use this..
SELECT TableC.SurveyID, TableA.SubjectValue, COUNT(TableB.RequestId)
FROM TableA
INNER JOIN TableB ON TableB.SurveyID = TableA.SurveyID
INNER JOIN TableC ON TableC.RequestID = TableB.RequestID
Group by TableA.SubjectValue, TableC.SurveyID
(hope i didnt get my A, B's and C's mixed up.)

Update Field based on another table's values

Is there a more elegant way to write the following Microsoft SQL Server 2008 command?
UPDATE TableB
SET TableBField2=0
WHERE TableBID IN(
SELECT TableBID
FROM TableB
JOIN TableA on TableB.TableAID=TableA.TableAID
WHERE TableBField2 < 0
AND TableAField1 = 0
)
In plain speak, what I'm doing is updating a table based on the value of a field in a joined table. I wonder if my use of IN() is considered inefficient.
This should be more efficient:
UPDATE TableB b
SET TableBField2=0
WHERE exists (
SELECT 1
FROM TableA
WHERE b.TableAID=TableA.TableAID
AND b.TableBField2 < 0
AND TableAField1 = 0
)
You can try something like this
UPDATE TableB
SET Field2 = 0
FROM TableB b INNER JOIN
TableA a ON b.TableB.TableAID=a.TableAID
WHERE b.Field2 < 0
AND a.Field1 = 0