I have 3 tables, lets say tables A, B, C to obfuscate my software :). A and B have two columns with numeric values and Table C has a Boolean column.
What I want is to create a view with a single column where depending on the column in C, either the value in A or B is selected.
Example:
Input:
| A.val | | B.val | | C.val |
--------- --------- ---------
entry1 | 1 | | 6 | | T |
entry2 | 2 | | 8 | | F |
Output:
| D |
-----
entry1 | 1 |
entry2 | 8 |
I'm wondering if there is a way to do this in a SQL statement(s) since I am currently doing it programmatically which eats up unnecessary resources.
Using the GROUP BY clause to group all rows by the target column(s) – i.e. the column(s) you want to check for duplicate values on. Using the COUNT function in the HAVING clause to check if any of the groups have more than 1 entry; those would be the duplicate values.
In SQL, problems require us to compare two columns for equality to achieve certain desired results. This can be achieved through the use of the =(equal to) operator between 2 columns names to be compared.
If you're trying to select A if C = T or B if C = F then you can just use a Case Statement
Select (Case When C.val = TRUE Then A.val Else B.Val END) AS D
From Table
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With