Using an equality check between columns in a SELECT clause - sql

I am using Microsoft SQL Server 2012 and I would like to run this seemingly simple query:
SELECT
FirstEvent.id AS firstEventID,
SecondEvent.id AS secondEventID,
DATEDIFF(second, FirstEvent.WndFGEnd, SecondEvent.WndFGStart) AS gap,
FirstEvent.TitleID = SecondEvent.TitleID AS titlesSameCheck
FROM VibeFGEvents AS FirstEvent
RIGHT OUTER JOIN VibeFGEvents AS SecondEvent
ON
FirstEvent.intervalMode = SecondEvent.intervalMode
AND FirstEvent.id = SecondEvent.id - 1
AND FirstEvent.logID = SecondEvent.logID
However FirstEvent.TitleID = SecondEvent.TitleID AS titlesSameCheck in the SELECT clause is incorrect syntax. But the SELECT Clause (Transact-SQL) documentation includes this syntax:
SELECT [ ALL | DISTINCT ]
[ TOP ( expression ) [ PERCENT ] [ WITH TIES ] ]
<select_list>
<select_list> ::=
{
*
| { table_name | view_name | table_alias }.*
| {
[ { table_name | view_name | table_alias }. ]
{ column_name | $IDENTITY | $ROWGUID }
| udt_column_name [ { . | :: } { { property_name | field_name }
| method_name ( argument [ ,...n] ) } ]
| expression
[ [ AS ] column_alias ]
}
| column_alias = expression
} [ ,...n ]
I think that means an expression is valid in the select clause and indeed the examples given include things like 1 + 2. Looking at the documentation for expressions:
{ constant | scalar_function | [ table_name. ] column | variable
| ( expression ) | ( scalar_subquery )
| { unary_operator } expression
| expression { binary_operator } expression
| ranking_windowed_function | aggregate_windowed_function
}
boolean equality checks are valid expressions and indeed the example expression given in the = (Equals) (Transact-SQL) documentation includes one:
SELECT DepartmentID, Name
FROM HumanResources.Department
WHERE GroupName = 'Manufacturing'
albeit in the WHERE clause not the SELECT clause. It looks like I cannot use = the equality operator to compare expressions in my SELECT clause as they are being wrongly interpreted as assignment.
How do I include a Boolean equality column comparison equivalent to FirstEvent.TitleID = SecondEvent.TitleID AS titlesSameCheck in my SELECT clause?

Like this:
case when FirstEvent.TitleID = SecondEvent.TitleID then 1 else 0 end as titlesSameCheck

You cannot use the Boolean type directly except in conditional statements (case, where, having, etc.)
Best way to solve your problem is to do something like
select case when x = y then 'true' else 'false' end
The bit type is probably the closest to boolean.
select CAST(case when x = y then 1 else 0 end as bit)
Of course, use whichever two values best represent what you are after.

As the two existing answers state, boolean values can't be returned as a column value. This is documented in the Comparison Operators section:
Unlike other SQL Server data types, a Boolean data type cannot be
specified as the data type of a table column or variable, and cannot
be returned in a result set.
Given that restriction, using CASE to transform the value to something that can be displayed is your best alternative.

Related

SQL: Expression order in SQL compare

Is the expression order relevant in a compare statement?
For example:
select * from person where name = 'John'
and
select * from person where 'John' = name
Is there another reason other than readability to have the column first instead of the value?
Most SQL database systems will have sections of their documentation that explicitly describe their syntax. I'll use SQL Servers Search Condition1, since that's my most common reference:
<search_condition> ::=
MATCH(<graph_search_pattern>) | <search_condition_without_match> | <search_condition> AND <search_condition>
<search_condition_without_match> ::=
{ [ NOT ] <predicate> | ( <search_condition_without_match> ) }
[ { AND | OR } [ NOT ] { <predicate> | ( <search_condition_without_match> ) } ]
[ ...n ]
<predicate> ::=
{ expression { = | < > | ! = | > | > = | ! > | < | < = | ! < } expression
Here, we can see that the items on both side of the = (as well as many other operators) are described as expression - that is, anything that can appear on the left can equally well appear on the right.
Compare this, for example, with IN2:
| expression [ NOT ] IN ( subquery | expression [ ,...n ] )
where we can clearly see that what's allowed on the left and right are subtly different.
It's well worth, when you have question on syntax, to consult the relevant documentation for your database product. For standard SQL, as here, just about any product's documentation will do.
1Which in turn was reached from the documentation for WHERE
2Same page as <predicate>

Get value from json dimensional array in oracle

I have below JSON from which i need to fetch the value of issuedIdentValue where issuedIdentType = PANCARD
{
"issuedIdent": [
{"issuedIdentType":"DriversLicense","issuedIdentValue":"9797979797979797"},
{"issuedIdentType":"SclSctyNb","issuedIdentValue":"078-01-8877"},
{"issuedIdentType":"PANCARD","issuedIdentValue":"078-01-8877"}
]
}
I can not hard-code the index value [2] in my below query as the order of these records can be changed. So want to get rid off any hardcoded index.
select json_value(
'{"issuedIdent": [{"issuedIdentType":"DriversLicense","issuedIdentValue":"9797979797979797"},{"issuedIdentType":"SclSctyNb","issuedIdentValue":"078-01-8877"}, {"issuedIdentType":"PANCARDSctyNb","issuedIdentValue":"078-01-8877"}]}',
'$.issuedIdent[2].issuedIdentValue'
) as output
from d1entzendev.ExternalEventLog
where
eventname = 'CustomerDetailsInqSVC'
and applname = 'digitalBANKING'
and requid = '4fe1fa1b-abd4-47cf-834b-858332c31618';
What changes will need to apply in json_value function to achieve the expected result
In Oracle 12c or higher, you can use JSON_TABLE() for this:
select value
from json_table(
'{"issuedIdent": [{"issuedIdentType":"DriversLicense","issuedIdentValue":"9797979797979797"},{"issuedIdentType":"SclSctyNb","issuedIdentValue":"078-01-8877"}, {"issuedIdentType":"PANCARD","issuedIdentValue":"078-01-8877"}]}',
'$.issuedIdent[*]' columns
type varchar(50) path '$.issuedIdentType',
value varchar(50) path '$.issuedIdentValue'
) t
where type = 'PANCARD'
This returns:
| VALUE |
| :---------- |
| 078-01-8877 |

What does 'AS' mean in SQL?

Below is the synopsis of SELECT from the PostgreSQL documentation. It seems to me that sometimes we write <expression> AS <name> and sometimes it's <name> AS <expression>. In ordinary English, I tend to think <expression> AS <name> is much more common (e.g. "Address her as. Doctor Smith, please., and I'm having trouble understanding how to think about <name> AS <expression>.
How can we distinguish between where to use <name> AS <expression> and <expression> as <name>?
What are minimal obvious examples of each?
Are there parallels of each kind in ordinary language, which would make it intuitively obvious when to use what?
[ WITH [ RECURSIVE ] with_query [, ...] ]
SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]
* | expression [ [ AS ] output_name ] [, ...]
[ FROM from_item [, ...] ]
[ WHERE condition ]
[ GROUP BY expression [, ...] ]
[ HAVING condition [, ...] ]
[ WINDOW window_name AS ( window_definition ) [, ...] ]
[ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] select ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ LIMIT { count | ALL } ]
[ OFFSET start [ ROW | ROWS ] ]
[ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY ]
[ FOR { UPDATE | SHARE } [ OF table_name [, ...] ] [ NOWAIT ] [...] ]
where from_item can be one of:
[ ONLY ] table_name [ * ] [ [ AS ] alias [ ( column_alias [, ...] ) ] ]
( select ) [ AS ] alias [ ( column_alias [, ...] ) ]
with_query_name [ [ AS ] alias [ ( column_alias [, ...] ) ] ]
function_name ( [ argument [, ...] ] ) [ AS ] alias [ ( column_alias [, ...] | column_definition [, ...] ) ]
function_name ( [ argument [, ...] ] ) AS ( column_definition [, ...] )
from_item [ NATURAL ] join_type from_item [ ON join_condition | USING ( join_column [, ...] ) ]
and with_query is:
with_query_name [ ( column_name [, ...] ) ] AS ( select | values | insert | update | delete )
TABLE [ ONLY ] table_name [ * ]
I like the question.
Here is how I see it and how I explain it to people, hope it helps:
Let's start with <expression> as <name>. The simplest analogy from real life is an abbreviation. It was created to make the code cleaner, easier to read and simply shorter. Let's imagine a scenario: we have data on all students from the Massachusetts Institute of Technology as well as Massachusetts Department of Motor Vehicles in our database and we want to find students that have speeding tickets and how much they have paid.
SELECT
government.SocialSecurityAdministration.FirstName,
government.SocialSecurityAdministration.LastName,
education.MasachusettsInstituteOfTechnology.Faculty
government.DepartmentOfMotorVehiclesTickets.TicketTotal,
government.SocialSecurityAdministration.SocialSecurityNumber
FROM
education.MasachusettsInstituteOfTechnology
INNER JOIN government.DepartmentOfMotorVehiclesTickets ON education.MasachusettsInstituteOfTechnology.SocialSecurityNumber = government.DepartmentOfMotorVehicles.SocialSecurityNumber
INNER JOIN government.SocialSecurityAdministration ON government.DepartmentOfMotorVehicles.SocialSecurityNumber = government.SocialSecurityAdministration.SocialSecurityNumber
Looks ugly, doesn't it? In real life, we've abbreviated the Massachusetts Institute of Technology to be MIT and the Department of Motor Vehicles to be DMV. I'm not aware of the official abbreviation for Social Security Administration (but we can come up with one) though we say SSN when we mean Social Security Number. Let's implement this idea:
SELECT
ssnAdm.FirstName,
ssnAdm.LastName,
ssnAdm.Faculty
dmv.TicketTotal,
ssnAdm.SocialSecurityNumber AS ssn
FROM
education.MasachusettsInstituteOfTechnology AS mit
INNER JOIN government.DepartmentOfMotorVehiclesTickets AS dmv ON mit.SocialSecurityNumber = dmv.SocialSecurityNumber
INNER JOIN government.SocialSecurityAdministration AS ssAdm ON dmv.SocialSecurityNumber = dmv.SocialSecurityNumber
Looks better now, doesn't it?
Now to the <name> as <expression> portion of it. This is done to simplify the code as well as some performance optimizations but let's focus on simplification for now. Using the same example I've used above, you might want to get/ask the following: "For every MIT student that has received a ticket I need to know the last 4 digits of their SSN, their last name, the amount of money in their bank account and their last VISA transaction amount". Yes, you work for CIA.
Let's write it:
SELECT
RIGHT(4,ts.ssn) as LastFourDigitsSsn,
ts.LastName,
bad.TotalAmount,
ISNULL(visa.TransactionAmt,'Student uses MasterCard') AS VisaTransaction
FROM
(SELECT
ssnAdm.FirstName,
ssnAdm.LastName,
ssnAdm.Faculty
dmv.TicketTotal,
ssnAdm.SocialSecurityNumber AS ssn
FROM
education.MasachusettsInstituteOfTechnology AS mit
INNER JOIN government.DepartmentOfMotorVehiclesTickets AS dmv ON mit.SocialSecurityNumber = dmv.SocialSecurityNumber
INNER JOIN government.SocialSecurityAdministration AS ssAdm ON dmv.SocialSecurityNumber = dmv.SocialSecurityNumber
) AS ts
INNER JOIN business.BankAccountsData AS bad ON ts.ssn = bad.SocialSecurityNumber
OUTER APPLY (SELECT TOP 1 TransactionAmt FROM business.VisaProcessingData vpd WHERE vpd.BankAccountID = bad.ID ORDER BY TransactionDateTime DESC) as visa
Well, looks ugly again. But what if we simplify it a bit and express certain things outside of the actual statement? That's when <name> as <expression> comes in. Let's do it:
WITH MitTicketedStudents AS (
SELECT
ssnAdm.LastName,
ssnAdm.SocialSecurityNumber as ssn,
RIGHT(4,ssnAdm.SocialSecurityNumber) as LastFourDigitsSsn
FROM
education.MasachusettsInstituteOfTechnology AS mit
INNER JOIN government.DepartmentOfMotorVehiclesTickets AS dmv ON mit.SocialSecurityNumber = dmv.SocialSecurityNumber
INNER JOIN government.SocialSecurityAdministration AS ssAdm ON dmv.SocialSecurityNumber = dmv.SocialSecurityNumber
),
LatestVisaTransactions AS (
SELECT DISTINCT
BankAccountID,
FIRST_VALUE(TransactionAmt) OVER (PARTITION BY BankAccountId ORDER BY TransactionDateTime DESC) as TransactionAmt
FROM
business.VisaProcessingData
)
-- And let's use our expressions now
SELECT
mts.LastFourDigitsSsn,
mts.LastName,
bad.TotalAmount,
ISNULL(lvt.TransactionAmt,'Student uses MasterCard') AS VisaTransaction
FROM
MitTicketedStudents mts
INNER JOIN business.BankAccountsData AS bad ON mts.ssn = bad.SocialSecurityNumber
LEFT OUTER JOIN LatestVisaTransactions lvt ON bad.ID = lvt.BankAccountID;
Looks better, doesn't it?
Conclusion: when you want to separate code you use <name> as <expression>, when you want to give something an alias to simplify code you use <expression> as <name>.
What matters is where it appears.
mytable:
mycolumn myothercolumn
----------------------
1 a
2 b
SELECT myrow.mycolumn * 2 AS mylabel
FROM (SELECT * FROM mytable) AS myrow
WHERE myrow.mycolumn > 1
mylabel
-------
4
In SELECT, we refer to the value of an expression AS some output column name ("column alias"). In FROM, we refer to (a typical row of) the value of a table expression AS some name ("table alias", "correlation name").
(It turns out that because of details of the grammar typos are less problematic if we use AS in SELECT clauses but don't use AS in FROM clauses.)
There are other uses of AS. The context also determines what they mean, and they also correspond to using using a name to refer to something.
In technical contexts it turns out not to be helpful to try to make sense of what something means based on the everyday meanings of technical terms, including making sense of what a thing is based on its name. The SQL language designers [sic] didn't choose to always have either <expression> AS <name> or <name> AS <expression>. That is just how it is. That is just how you write stuff to get your program to do stuff to stuff. (Accepted but more modern principles of computer language design do suggest more regular notations.)

PostgreSQL: Sub-select inside insert

I have a table called map_tags:
map_id | map_license | map_desc
And another table (widgets) whose records contains a foreign key reference (1 to 1) to a map_tags record:
widget_id | map_id | widget_name
Given the constraint that all map_licenses are unique (however are not set up as keys on map_tags), then if I have a map_license and a widget_name, I'd like to perform an insert on widgets all inside of the same SQL statement:
INSERT INTO
widgets w
(
map_id,
widget_name
)
VALUES (
(
SELECT
mt.map_id
FROM
map_tags mt
WHERE
// This should work and return a single record because map_license is unique
mt.map_license = '12345'
),
'Bupo'
)
I believe I'm on the right track but know right off the bat that this is incorrect SQL for Postgres. Does anybody know the proper way to achieve such a single query?
Use the INSERT INTO SELECT variant, including whatever constants right into the SELECT statement.
The PostgreSQL INSERT syntax is:
INSERT INTO table [ ( column [, ...] ) ]
{ DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) [, ...] | query }
[ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]
Take note of the query option at the end of the second line above.
Here is an example for you.
INSERT INTO
widgets
(
map_id,
widget_name
)
SELECT
mt.map_id,
'Bupo'
FROM
map_tags mt
WHERE
mt.map_license = '12345'
INSERT INTO widgets
(
map_id,
widget_name
)
SELECT
mt.map_id, 'Bupo'
FROM
map_tags mt
WHERE
mt.map_license = '12345'
Quick Answer:
You don't have "a single record" you have a "set with 1 record"
If this were javascript: You have an "array with 1 value" not "1 value".
In your example, one record may be returned in the sub-query,
but you are still trying to unpack an "array" of records into separate
actual parameters into a place that takes only 1 parameter.
It took me a few hours to wrap my head around the "why not".
As I was trying to do something very similiar:
Here are my notes:
tb_table01: (no records)
+---+---+---+
| a | b | c | << column names
+---+---+---+
tb_table02:
+---+---+---+
| a | b | c | << column names
+---+---+---+
|'d'|'d'|'d'| << record #1
+---+---+---+
|'e'|'e'|'e'| << record #2
+---+---+---+
|'f'|'f'|'f'| << record #3
+---+---+---+
--This statement will fail:
INSERT into tb_table01
( a, b, c )
VALUES
( 'record_1.a', 'record_1.b', 'record_1.c' ),
( 'record_2.a', 'record_2.b', 'record_2.c' ),
-- This sub query has multiple
-- rows returned. And they are NOT
-- automatically unpacked like in
-- javascript were you can send an
-- array to a variadic function.
(
SELECT a,b,c from tb_table02
)
;
Basically, don't think of "VALUES" as a variadic
function that can unpack an array of records. There is
no argument unpacking here like you would have in a javascript
function. Such as:
function takeValues( ...values ){
values.forEach((v)=>{ console.log( v ) });
};
var records = [ [1,2,3],[4,5,6],[7,8,9] ];
takeValues( records );
//:RESULT:
//: console.log #1 : [1,2,3]
//: console.log #2 : [4,5,7]
//: console.log #3 : [7,8,9]
Back to your SQL question:
The reality of this functionality not existing does not change
just because your sub-selection contains only one result. It is
a "set with one record" not "a single record".

Pervasive SQL Update with top

I am looking to do this:
update aTable
set aField = 'value'
where aTable.Id in (select top 400 Id from aTable order by dateField) .
-- This will run, but it only updates the first id it gets to in the IN clause
or this:
update top 400 aTable set aField = 'value' order by dateField
-- This will not run
But, it has to work in V10 of Pervasive...
A similiar workaround would be sufficient too!
Background, I am trying to update a (number(which is variable) of items)'s field with a value ordered by a date field.
Here is Pervasive's Update help (I do not see the TOP reserved word in there):
UPDATE < table-name | view-name > [ alias-name ]
SET column-name = < NULL | DEFAULT
| expression | subquery-
expression > [ , column-name = ... ]
[ FROM table-reference [, table-reference ] ...
[ WHERE search-condition ]
table-name ::= user-defined-name
view-name ::= user-defined-name
alias-name ::= user-defined-name (Alias-name is not allowed if a
FROM clause is used. See FROM Clause .)
table-reference ::= { OJ outer-join-definition }
| [db-name.]table-name [ [ AS ] alias-name ]
| [db-name.]view-name [ [ AS ] alias-name ]
| join-definition | ( join-definition )
| ( table-subquery )[ AS ] alias-name [ (column-name [ , column-name
]... ) ]
I did some testing and the best way to get this working is to create a view:
create view View1 as select Id from
aTable order by dateField;
Then use the view in the Update:
update aTable set aField = 'value'
where aTable.Id in (select top 400
a.Id from View1 a );
I've tested with PSQL v11 but it should also work with PSQL v10.