CASE statements - Making the THEN display information from another column - sql

Good morning All,
I had a question regarding case statements. Let me set up a scenario that I am having a hard time with. Say we have a table with all of our providers licenses. Each provider can have anywhere from one to a number of licenses. I want to create a case statement that says basically WHEN the providers license type equals this, then display the license number from this different column holding the values for their numbers.
Hope this makes sense. I do not want it to display words such as THEN 'Primary Care Physician' , instead would like it to provide the actual license number in the THEN.
Any ideas? Thank you!

Just use the CASE syntax as it is described in the documentation: https://www.postgresql.org/docs/7.4/static/functions-conditional.html
CASE WHEN license_type = 1 THEN license_number_column_a
WHEN license_type = 2 THEN license_number_column_b
END

I Fugere that you are trying to use a code like
Case licence_type
when 1 then licence_number_column_a
when 2 then licence_number_column_b
...
End
This type of Case works exclusively when you are evaluating constants.
When you are evaluating columns, you need to move the test to "when"
Case
when licence_type = 1 then licence_number_column_a
when licence_type = 2 then licence_number_column_b
...
End

Related

How do I subtract these two columns in SQL and make an extra column that shows the difference?

Begin_Term
End_Term
2018
Current
-select-
Current
2015
2019
-select-
2018
I used using the case when SUM('End_Term') - SUM ('Begin_Term) then 1 else 0 end but it only shows 0's and 1's for the results. I'm also using DOMO so the SQL formatting is alot different.
Does selecting a new column that merely subtracts the two columns (e.g. SELECT 'End_Term' - 'Begin_Term' AS Diff) mostly meet your needs?
As comments indicated, 3 of the 4 sample rows you gave are not reasonable to expect to process, since the columns do not both contain numbers.
If you have special case strings like "Current" and "-select-" that you wish to convert to numbers, then I suggest you will want to do this first, and then do your math second.
For example:
SELECT CASE WHEN 'End_Term' = "Current" THEN year(CURDATE()) ELSE 'End_Term' as 'End_Term'
My understanding is that DOMO handles type casting / conversion transparently behind the scenes so there's no need to actually worry about whether the column itself is a varchar.
I'm not sure what "-select-" is meant to be. It seems to me like erroneous placeholder form data that shouldn't have ended up submitted or inserted into your database in the first place.

Is there an easier way than nested iifs to count mismatched conditions in SQL server

The picture below shows a table of accounts and the outcomes that I want to count. Ie every time the account number is 106844 and the outcome is "MESSAGE" or "ESCALATION EMAIL" that should count as 1 where any other outcome counts as 0. What I would normally do is a horrible mess of iifs like
sum( iif([account] = '106719' and [Outcome] in ('MESSAGE','ESCALATION_EMAIL'),1,iif([account] = '310827' and [outcome] <> 'ABORT' and 'CALL_OUTCOME_LB' in ("Call patched to Customer Care","Message Taken"),1,iif( ... , 0) as [Total Outcomes]
and so on but man it feel like there's got to be an easier way or one less prone to making a random mistake in the 7th nested iif and messing the whole thing up. Any ideas?
Don't use iif(). It is a function brought into SQL Server for back-compatibility to MS ACCESS. Why would you want to be backwards compatible to such a thing?
Use the ANSI standard CASE expression:
sum(case when account = '106719' and Outcome in ('MESSAGE', 'ESCALATION_EMAIL')
then 1
when account = '310827' and outcome <> 'ABORT' and
'CALL_OUTCOME_LB' in ("Call patched to Customer Care", "Message Taken")
then 1
. . .
else 0
end) as Total_Outcomes
I would also advise you to name your columns so they don't need to be escaped (why "Total Outcomes" became "Total_Outcomes"). That simplifies the code.
Why not use a lookup table that has the Account and Outcome and use that? Then, as requirements change, you could update the lookup table and not worry about your code.
Yeah... there is
The last parameter is for when the condition is false, everything else will fall in there.
SUM( IIF([ACCOUNT] = '106719' AND [OUTCOME] IN ('MESSAGE','ESCALATION_EMAIL'),1,0))

SQL :Write a function to return value based on business rule

I am trying to figure out the way in writing a function to do the following in the best possible way.
I need to write a function that will return me the exchange name. It will be referred in my view query below
select Exchange,ISIN,Investment_Codename,PROPORTION_NEWCASH,PROPORTION_MIDCAP,PROPORTION_SMALLCAP,PROPORTION_LARGECAP from FundPrice
Following are the rules of my function
1. If fund type=Listed Equity, AIM, Investment Company, Investment Trust, VCT or ETP and Update code=FinexDD/MM/YYYY
should rturn LSE
For anything else apart from the updatecode mentioned above with Fund Type = Listed Equity, need to take left two letters of the ISIN Code to indicate the country e.g. Microsoft=US5949181045, Michelin=FR0000121261 and infer the exchange name based on that
(CASE WHEN UpdatedCode <> 'LSE' THEN LEFT(Investment_Codename, 2)) AS [Country Code]
I think I understood your question. The above checks to see if UpdatedCode is NOT LSE, if it isn't it will take the Investment Code name, and present the first 2 characters. E.g. GB.

SQL statement only returns a single row, where it should return multiple

I have database that contains data points from various sensors. These data points are taken twice a minute.
I am using the following SQL query to attempt to get two separate data points at two different times.
SELECT *
FROM TableName
WHERE TagName = 'TagName'
AND ("DateTime" = 'X' OR "DateTime" = 'Y')
I see no reason why this should not return 2 data points, one for the first date, and one for the second, but for some reason the query only returns the row for Y.
I feel like I am missing something extremely obvious.
For a bit more context, this query is used in conjuction with a python script to grab data between two dates using a specific resolution.
Any ideas?
EDIT: I believe it has something to do with the structure of the database and how it operates in terms of giving entries a time value.
It needs more investigation on my part so im going to flag this question for deletion, thanks all for you help
Not sure if that is a mistype in your question, but if not, it is probably the " after the AND part of the statement:
AND ("DateTime" = 'X' OR "DateTime" = 'Y')
It should be AND (DateTime = 'X' OR DateTime = 'Y'). Notice no quotes.
http://sqlfiddle.com/#!2/ed00ba/3

How do I increase a counter by 1 for each repeated value?

As part of my course in university I have to make a database in Microsoft Access which is somewhat limiting me on what I'm trying to do. I have a table that has the information of whether a player in a team was present for a fixture or not using the values "P", "R", and "M" (Played, Reserves, Missed). I want to make a query that counts a value of 1 for each value of P or R and a separate one for M, so that when I make a report that prints off a membership card, it shows the amount of fixtures they've played in and the amount of fixtures that they have missed.
Sorry if this isn't clear, I'll try to explain myself further if you ask but I'm not very good with this stuff. Thank you.
Edit: I'll use screenshot links if that's okay, here is the Fixture Attendance entity that shows if a member of a team attended a game or not. I'm making a membership card based off this one. I want to be able to display the No. of fixtures played by the member and the No. of fixtures missed based off the values in the above entity and use that information in a form I'm going to create. That will be a subform inside my Membership Card form.
I'm presumably really bad at explaining this - I understand Access is rarely used in the real world so I'm not sure why I'm doing this in the first place and don't feel like I'm getting any real knowledge of working with databases.
You should use the COUNT function.
http://office.microsoft.com/en-us/access-help/count-data-by-using-a-query-HA010096311.aspx
I am guessing that you want something like this:
select playerid, sum(iif(fixture in ("P", "R"), 1, 0)) as NumPR,
sum(iif(figure = "M", 1, 0)as NumM
from table t
group by playerid;
The key idea here is putting the conditional part (iif()) inside the sum().
CASE WHEN can be used to translate the codes into 1's and 0's. Then use SUM with a GROUP BY to sum them.
SELECT player_id, SUM(Played), SUM(Reserve), SUM(Missed)
FROM
(SELECT player_id,
CASE WHEN present = 'P' THEN 1 ELSE 0 AS Played,
CASE WHEN present = 'R' THEN 1 ELSE 0 AS Reserve,
CASE WHEN present = 'M' THEN 1 ELSE 0 AS Missed
FROM fixtures)
GROUP BY player_id;