SELECT "public"."mv_tags_per_org"."count" AS "count", "public"."mv_tags_per_org"."tag_name" AS "Tag Name",
CASE
WHEN "public"."mv_tags_per_org"."ngo_id" = 30 then 'SSS'
WHEN "public"."mv_tags_per_org"."ngo_id" = 33 then 'PF'
WHEN "public"."mv_tags_per_org"."ngo_id" = 34 then 'DS'
ELSE 'Maybe'
END AS "NPO"
FROM "public"."mv_tags_per_org"
WHERE "NPO???" = "SSS"
Above you can see my code. It is currently returning exactly the output I want when you remove the "WHERE" function. I'm adding the "WHERE" function and attempting to access the new column I made called "NPO". It seems as if the column does not exist to the SQL editor, but it does exist when the query is ran. How do I access it?
Thanks!
Enclose your query into a "table expression" so you can produce a named column. Then you can use it in the WHERE clause:
select *
from ( -- table expression 'x' starts here
SELECT
"public"."mv_tags_per_org"."count" AS "count",
"public"."mv_tags_per_org"."tag_name" AS "Tag Name",
CASE
WHEN "public"."mv_tags_per_org"."ngo_id" = 30 then 'SSS'
WHEN "public"."mv_tags_per_org"."ngo_id" = 33 then 'PF'
WHEN "public"."mv_tags_per_org"."ngo_id" = 34 then 'DS'
ELSE 'Maybe'
END AS "NPO"
FROM "public"."mv_tags_per_org"
) x
WHERE "NPO" = 'SSS'
Note: "table expressions" are also called "derived tables" and "inline views" by different teams of people.
The WHERE clause cannot relate to a column alias defined in the SELECT clause (because the former it is evaluated before the latter).
This does not really matter for your use case, which can be simplified as:
SELECT
"public"."mv_tags_per_org"."count" AS "count",
"public"."mv_tags_per_org"."tag_name" AS "Tag Name",
CASE
WHEN "public"."mv_tags_per_org"."ngo_id" = 30 then 'SSS'
WHEN "public"."mv_tags_per_org"."ngo_id" = 33 then 'PF'
WHEN "public"."mv_tags_per_org"."ngo_id" = 34 then 'DS'
ELSE 'Maybe'
END AS "NPO"
FROM "public"."mv_tags_per_org"
WHERE "public"."mv_tags_per_org"."ngo_id" = 30
Related
I wanted to do a condition wherein I put values (000000) in DATE_COMPLETED if it see's the FLAG_CLOSED = Y and if its not Y then do nothing
SELECT
"JOB",
"SUFFIX",
"SUFFIX",
"DATE_COMPLETED",
"FLAG_CLOSED",
CASE "DATE_COMPLETED"
WHEN "FLAG_CLOSED"='Y'
THEN "DATE_COMPLETED"='000000'
END "DATE_COMPLETED"
FROM "JOB_OPERATIONS"
What I got
SQL Execution Error
[LNA][PSQL][SQL Engine]Syntax Error: SELECT
"JOB",
"SUFFIX",
"SUFFIX",
"DATE_COMPLETED",
"FLAG_CLOSED",
CASE "DATE_COMPLETED" WHEN "FLAG_CLOSED" << ??? >> = 'Y' THEN "DATE_COMPLETED" = '000000' END "DATE_COMPLETED"
FROM JOB_OPERATIONS
It looks like you're attempting to change the DATE_COMPLETED column in your table. You can't do that with a SELECT statement. CASE / WHEN / THEN helps construct output. UPDATE statements allow clauses like DATE_COMPLETED='000000' that change columns.
Try something like this.
SELECT "JOB", "SUFFIX", "SUFFIX", "DATE_COMPLETED", "FLAG_CLOSED",
CASE WHEN "FLAG_CLOSED"='Y' THEN '000000'
ELSE "DATE_COMPLETED" END "CLOSED_DATE_COMPLETED"
FROM "JOB_OPERATIONS"
I named your CASE-computed output column CLOSED_DATE_COMPLETED so it won't collide with the DATE_COMPLETED colum you already mentioned.
Syntax is either:
CASE a WHEN b
... or:
CASE when a=b
To return the value of DATE_COMPLETED depending on the flag, you can do this:
CASE "FLAG_CLOSED"
WHEN 'Y' THEN '000000'
ELSE "DATE_COMPLETED"
END AS "DATE_COMPLETED"
Beware that you need to produce a coherent column type. If DATE_COMPLETED is not text, you'll need to cast it.
I have the following sql query
SELECT "SortingAttempts"."StartedAt", "ExternalId" as "ARTID", "Barcode", "LoadStationId", "CheckInLocationId", "Weight", "Length", "Height", "Width", "CheckInStatus"
FROM "SortingAttempts"
FULL OUTER JOIN "SortingAttemptBarcodes"
ON "SortingAttempts"."Id" = "SortingAttemptBarcodes"."SortingAttemptId"
FULL OUTER JOIN "Packages"
ON "SortingAttempts"."PackageId" = "Packages"."Id"
WHERE "StartedAt" >= '${yesterday} 8:00'
AND "EndedAt" <= '${today} 8:00'
ORDER BY 1
And the following result result
I wand to get rid of the "CheckInStatus" column, but i need it's values. So i need to check, if ARTID's value is null - then i put CheckInStatus's value in ARTIS's cell. Important - i don't want to rewrite anything in DB, i just need a specific query result.
I tried to use SQL CASE WHEN, THEN and DO IF ELSE END, but, seems like i do smth wrong, or it's just impossible to do with sql query.
I tried something for you I hope you get an idea ;
select StartedAt,
case when ARTID is null then CheckInStatus else
ARTID end as ARTID
from SortingAttempts;
http://sqlfiddle.com/#!9/306388/3
so you can try like this ;
SELECT "SortingAttempts"."StartedAt", case when "ExternalId" is null then "CheckInStatus" else "ExternalId" end as "ARTID", "Barcode", "LoadStationId", "CheckInLocationId", "Weight", "Length", "Height", "Width"
FROM "SortingAttempts"
FULL OUTER JOIN "SortingAttemptBarcodes"
ON "SortingAttempts"."Id" = "SortingAttemptBarcodes"."SortingAttemptId"
FULL OUTER JOIN "Packages"
ON "SortingAttempts"."PackageId" = "Packages"."Id"
WHERE "StartedAt" >= '${yesterday} 8:00'
AND "EndedAt" <= '${today} 8:00'
ORDER BY 1
I want to make an if statement with conditions that have 2 result
Example
if (id = "25" OR "36") then
print "The id is 25 or 36"
else
print "the id is not 25 or 36"
end if
My concern is in the if condition statement for "OR"
I try with "AND" but this only takes id = 25 as true whereas id = 36 as false
I try with "OR" "ORELSE" "XOR" this takes everything as true.
I try || sign but I got syntax error
You are missing the comparision after the or:
If (id = "25" Or id = "36") Then
You need to provide the variable to check each time
If id = "25" or id = "36" Then
Alternatively, if this is likely to expand to include other numbers, you could use
If {"25","36"}.Contains(id) Then
Alternatively, you could use a Select Case statement:
Select Case id
Case "25", "36"
Print("The id is 25 or 36")
Case Else
Print("The id is not 25 or 36")
End Select
This works the same way as an if..else statement, but allows you to easily supply different test expressions.
I cant figure out why this code executes as a query but when I execute it in a view it throws a the multi-part identifier could not be bound, error.
When I take out the case statement It works in the view, so I believe it is something that has to do with the case statement.
Any suggestions are appreciated.
WITH [cteFrostSum] AS
(
SELECT ID AS ID, theMonth as Mo,
SUM(dbo.Frost.[DRAmount]) AS [DRAmount]
FROM dbo.Frost
GROUP BY [ID], theMonth
)
SELECT DISTINCT
TOP (100) PERCENT
dbo.ternean.MemberID,
dbo.ternean.SSN,
dbo.ternean.GroupName,
dbo.ternean.CustomerID,
dbo.ternean.GroupNumber,
dbo.ternean.LastName,
dbo.Frost.DRAmount,
dbo.Frost.HittheBank,
dbo.Frost.MonthofPremium,
cte.[DRAmount] AS [SUM_Frost_Balance],
dbo.ternean.TotalCost,
cte.[DRAmount] - dbo.ternean.TotalCost AS Diff,
dbo.ternean.ACH_RoutingNo,
dbo.Frost.RTNum,
dbo.ternean.ACH_AcctNo,
dbo.Frost.AccountNumber,
CASE
WHEN dbo.Frost.RTNum <> SUBSTRING(dbo.ternean.ACH_RoutingNo, 2, 20)
THEN 'DO not match'
WHEN dbo.Frost.RTNum = SUBSTRING(dbo.ternean.ACH_RoutingNo, 2, 20)
THEN 'match'
END AS [Routing # match],
CASE
WHEN SUBSTRING(dbo.ternean.ACH_AcctNo, 2, 20) <> dbo.Frost.AccountNumber
THEN 'DO not match'
WHEN SUBSTRING(dbo.ternean.ACH_AcctNo, 2, 20) = dbo.Frost.AccountNumber
THEN 'match'
END AS [Account # match],
dbo.Frost.theMonth
FROM dbo.Frost
INNER JOIN dbo.ternean ON dbo.Frost.ID = dbo.ternean.CustomerID
AND dbo.Frost.theMonth = dbo.ternean.theMonth
INNER JOIN [cteFrostSum] cte ON dbo.Frost.ID = cte.ID
AND dbo.Frost.theMonth = cte.Mo
ORDER BY dbo.ternean.theMonth
I tried to replicate your error but couldn't.
Why are you using multi-part identifiers the field names anyway? The list of fields in the select statement can only refer to the tables in the from clause, at first reading this query seems to be referring to the tables directly in the dbo schema.
Give your tables some nice easy aliases i.e.
FROM dbo.Frost AS F
and use them like this
F.RTNum
Secondly you can simplify your case statements and only do one test i.e.:
CASE WHEN SUBSTRING(T.ACH_AcctNo, 2, 20) <> F.AccountNumber
THEN 'DO not match'
ELSE 'match'
END AS [Account # match]
I have a column called DayShift in a table which is of Yes/No data type (Boolean). The result Output I want is: if the value is true, display "Day" else display night.
I have tried the following:
SELECT iif(DayShift=Yes,"Day","Night") as Shift FROM table1;
SELECT iif(DayShift,"Day","Night") as Shift FROM table1;
SELECT iif(DayShift=True,"Day","Night") as Shift FROM table1;
SELECT iif(DayShift=1,"Day","Night") as Shift FROM table1;
But none of the above work. It just gives me a list of blank check boxes in the output datasheet window. I am using Ms Access 2003. Any help appreciated.
Update:
After a bit of research that the yes/no data type in Ms Access 2003 cannot handle null values appropriately. Hence, the error. Check this link for details.
Update 2
Real Query with the joins. Didnt mention it since i though the information provided above would work.
SELECT tblovertime.contfirstname AS [First Name],
tblovertime.contlastname AS [Last Name],
tblovertime.employeenumber AS [Employee Number],
tblsignup.thedate AS [Sign Up Date],
Iif([tblOvertime.DayShift] =- 1, "Day", "Night") AS shift,
(SELECT Mid(MIN(Iif(sector = 1, "," & sector, NULL)) & MIN(
Iif(sector = 2, "," & sector, NULL)) & MIN(
Iif(sector = 3, "," & sector, NULL)) & MIN(
Iif(sector = 4, "," & sector, NULL)), 2) AS concat
FROM tblempsectorlist
WHERE tblempsectorlist.empnum = tblsignup.employeenumber
GROUP BY empnum) AS sectors,
tblovertime.timedatecontact AS [Date Contacted],
tblovertimestatus.name AS status
FROM (tblsignup
INNER JOIN tblovertime
ON ( tblsignup.thedate = tblovertime.otdate )
AND ( tblsignup.employeenumber = tblovertime.employeenumber ))
INNER JOIN tblovertimestatus
ON Clng(tblovertime.statusid) = tblovertimestatus.statusid
WHERE (( ( tblsignup.thedate ) ># 1 / 1 / 2011 # ))
ORDER BY tblsignup.thedate;
Your second one has it right
SELECT iif(DayShift,"Day","Night") as Shift FROM table1;
I suggest trying the following to see what's actually being evaluated
SELECT iif(DayShift,"Day","Night") as Shift, DayShift FROM table1;
You could equally do
SELECT iif(DayShift = -1,"Day","Night") as Shift FROM table1;
Since MS Access is storing true as -1 and false as 0 (it's not as intuitive as true = 1, but it's probably faster to evaluate in twos-compliment)
-- edit --
Since you appear to be using a join, which can result in Nul's for Yes/No's, use the nz() function.
select iff(nz(DayShift, 0), "Day","Night") as Shift FROM table1;
When DayShift comes out null, this will return 0 (false/no) as a result instead.
This one might be stupid, but ....
In case you have some Null values in the DayShift field, Access will not be able to evaluate the formula. You could write your test this way:
iif(Nz(DayShift,0)=-1,"Day","Night")
I ran the following query successfully:
SELECT IIf([Table1]![isDayShift]=True,"Day","Night") AS Shift
FROM Table1;
I built this in Access 2007 (sorry, I didn't have a copy of 2003 lying around). The only difference I saw was that it gave the full path to the field instead of just the field name. However, that shouldn't be an issue. If it is giving you checkboxes, it would seem that it is not seeing this field as a text field but rather as a checkbox field still.
Bottom line is the above query should work.
You are binding to "DayShift" rather then the derived "Shift" in the form.
The 3rd query (=True) show when run in isolation should show Day/Night.
The Access database engine (ACE, Jet, whatever) has a clever/stupid feature that allows an expression in the SELECT clause to refer to an AS clause ("column alias") in the same SELECT clause if that AS clause is to the left of the expression. This makes it easy to test expressions with test data without using a table at all (assuming ANSI-92 Query Mode, I think) e.g. try executing any of the following statements individually: all should work and produce the expected result:
SELECT CBOOL(TRUE) AS DayShift,
IIF(DayShift = TRUE, 'Day', 'Night') AS Shift;
SELECT CBOOL(FALSE) AS DayShift,
IIF(DayShift = TRUE, 'Day', 'Night') AS Shift;
SELECT NULL AS DayShift,
IIF(DayShift = TRUE, 'Day', 'Night') AS Shift;
SELECT CBOOL(TRUE) AS DayShift,
IIF(DayShift, 'Day', 'Night') AS Shift;
SELECT CBOOL(FALSE) AS DayShift,
IIF(DayShift, 'Day', 'Night') AS Shift;
SELECT NULL AS DayShift,
IIF(DayShift, 'Day', 'Night') AS Shift;