Open SQL statement with a subtraction in WHERE condition - abap

SELECT * FROM dbtab
WHERE a - b GE c.
The problem is in ABAP with the a - b part. Is there a way to make such a WHERE?

As far as I can see, that's not possible with Open SQL. And btw, you don't select from an internal table (but usually into it).

Related

Add column with substring of other column in SQL (Snowflake)

I feel like this should be simple but I'm relatively unskilled in SQL and I can't seem to figure it out. I'm used to wrangling data in python (pandas) or Spark (usually pyspark) and this would be a one-liner in either of those. Specifically, I'm using Snowflake SQL, but I think this is probably relevant to a lot of flavors of SQL.
Essentially I just want to trim the first character off of a specific column. More generally, what I'm trying to do is replace a column with a substring of the same column. I would even settle for creating a new column that's a substring of an existing column. I can't figure out how to do any of these things.
On obvious solution would be to create a temporary table with something like
CREATE TEMPORARY TABLE tmp_sub AS
SELECT id_col, substr(id_col, 2, 10) AS id_col_sub FROM table1
and then join it back and write a new table
CREATE TABLE table2 AS
SELECT
b.id_col_sub as id_col,
a.some_col1, a.some_col2, ...
FROM table1 a
JOIN tmp_sub b
ON a.id_col = b.id_col
My tables have roughly a billion rows though and this feels extremely inefficient. Maybe I'm wrong? Maybe this is just the right way to do it? I guess I could replace the CREATE TABLE table2 AS... to INSERT OVERWRITE INTO table1 ... and at least that wouldn't store an extra copy of the whole thing.
Any thoughts and ideas are most welcome. I come at this humbly from the perspective of someone who is baffled by a language that so many people seem to have mastery over.
I'm not sure the exact syntax/functions in Snowflake but generally speaking there's a few different ways of achieving this.
I guess the general approach that would work universally is using the SUBSTRING function that's available in any database.
Assuming you have a table called Table1 with the following data:
+-------+-----------------------------------------+
Code | Desc
+-------+-----------------------------------------+
0001 | 1First Character Will be Removed
0002 | xCharacter to be Removed
+-------+-----------------------------------------+
The SQL code to remove the first character would be:
select SUBSTRING(Desc,2,len(desc)) from Table1
Please note that the "SUBSTRING" function may vary according to different databases. In Oracle for example the function is "SUBSTR". You just need to find the Snowflake correspondent.
Another approach that would work at least in SQLServer and MySQL would be using the "RIGHT" function
select RIGHT(Desc,len(Desc) - 1) from Table1
Based on your question I assume you actually want to update the actual data within the table. In that case you can use the same function above in an update statement.
update Table1 set Desc = SUBSTRING(Desc,2,len(desc))
You didn't try this?
UPDATE tableX
SET columnY = substr(columnY, 2, 10 ) ;
-Paul-
There is no need to specify the length, as is evidenced from the following simple test harness:
SELECT $1
,SUBSTR($1, 2)
,RIGHT($1, -2)
FROM VALUES
('abcde')
,('bcd')
,('cdef')
,('defghi')
,('e')
,('fg')
,('')
;
Both expressions here - SUBSTR(<col>, 2) and RIGHT(<col>, -2) - effectively remove the first character of the <col> column value.
As for the strategy of using UPDATE versus INSERT OVERWRITE, I do not believe that there will be any difference in performance or outcome, so I might opt for the UPDATE since it is simpler. So, in conclusion, I would use:
UPDATE tableX
SET columnY = SUBSTR(columnY, 2)
;

Get column names of a table in 1 row without data from that table - Firebird

Hello StackOverflow community,
Could you please tell me can I get information about column names of a table in 1 row using Firebird? [*]
What I'd like to achieve is the following using Postgres:
SELECT * FROM table_name LIMIT 0;
Above statement would return 0 rows, but will contain the header
id | data
---+-------
(0 rows)
[*] I need a similar way (taken from example above) in Firebird to obtain information about columns, since I'm using it external tool, so I need to provide a dynamically built schema for dynamic queries. This implies, that I'm not looking for a solution using system tables like rdb$relation_fields.
Adding WHERE condition with dummy value does not apply in my case. I'm looking for something like SELECT FIRST 0 .... Does it even exist?
edited: Yes, it does. Interactive Firebird Client doesn't show up anything, but it does indeed return the "header". This may be confusing for some of us that use psql console which yields 0 rows, but does contain the "header" itself.
Firebird supports limiting the result set - but the syntax is a bit different:
SELECT FIRST 0 *
FROM table_name;
This is part of the Firebird FAQ: http://www.firebirdfaq.org/faq111/
What about this?
select * from (
[any query here]
) where 1=0

Sub-Queries in Sybase SQL

We have an application which indexes data using user-written SQL statements. We place those statements within parenthesis so we can limit that query to a certain criteria. For example:
select * from (select F_Name from table_1)q where ID > 25
Though we have discovered that this format does not function using a Sybase database. Reporting a syntax error around the parenthesis. I've tried playing around on a test instance but haven't been able to find a way to achieve this result. I'm not directly involved in the development and my SQL knowledge is limited. I'm assuming the 'q' is to give the subresult an alias for the application to use.
Does Sybase have a specific syntax? If so, how could this query be adapted for it?
Thanks in advance.
Sybase ASE is case sensitive w.r.t. all identifiers and the query shall work:
as per #HannoBinder query :
select id from ... is not the same as select ID from... so make sure of the case.
Also make sure that the column ID is returned by the Q query in order to be used in where clause .
If the table and column names are in Upper case the following query shall work:
select * from (select F_NAME, ID from TABLE_1) Q where ID > 25

understanding existing SQL query

I am trying to read some exiting SQL queries written for MS SQL server.
I don't have access to database, table names etc.. Just raw query format...And I need to do some analysis on the fields required..
I need some help in understanding what certain query statements are doing...such as in the following block...
select FIELD1, x2.FIELD2
into #temp
from #temp1 x1 join #temp2 x2
on x1.FIELD1 = x2.FIELD2
and x1.FIELD3 = x2.MAXOCCUR
I have basic SQL understanding.. But I need to understand couple of things....Why does 'into' and 'from' statements have a '#' infront of table names.....what are x1 and x2 in this case. Why not just say
temp1.FIELD1 = temp2.FIELD2 instead of
x1.FIELD1 = x2.FIELD2
.....Am I missing something or is this query formed weird to begin with....I understand joins etc...
Can someone help me out...
Thanks
That is selecting from two already temp existing temp tables into a new temp table. The x1.FIELD1 is called aliasing. It's used so you don't have to type full table names when writing the query
As mentioned, the # signs indicate a TEMPORARY table.
x1 and x2 are used as "table alias" in this query. Yes, you could write
temp1.FIELD1 = temp2.FIELD2 instead of x1.FIELD1 = x2.FIELD2
but, consider if the tables had long names. Then using an alias makes the query easier to read (for humans. the computer doesn't really care).

Access 2007 Nest parameterful query

My problem is straight forward:
I have Query A:
SELECT Old, New
FROM MAPPING
WHERE Old = [Param];
I now need a Query B that calls Query A by giving it a value for [Param].
Is that possible without VBA ?
Thanks in advance
Miloud
I don't think you can do that. But you can replace your parameter in QueryA by a reference to an unbound control. This way you can set the value of the control and queryB can silently call queryA. In the same way, you could also replace your parameter by a UDF.
It soundn like you want to create a nested select query. If you are doing a select query in B, just put it inside parentheses where [Param]; is.
It sounds as though you want to pull the results from Query A, based on data from a Query B, is that correct?
A nested query is probably your best bet. What B looks like will depend on what you want to do, but try a model similar to this:
SELECT Old, New FROM MAPPING WHERE Old
= (SELECT somefield FROM sometable WHERE somefield = somevalue);