I'm trying to convert 4 date columns titled Created, Approved, Processed & Realized to a single column with all 4 dates AND a second column with the status of each of those dates.
The image at the end shows the data issue visually (apologies I'm still figuring out how to attach tables in textual form on stackoverflow)
To solve this, I successfully executed the CROSS APPLY
function in SQL server (see below), but I now need to do the same in AWS Simba
Athena or the Presto language. Can someone please guide me on what is the AWS/Presto equivalent of a CROSS APPLY function? Thank you in advance
SELECT
V.Date,
V.Status
From Table C
CROSS APPLY
(VALUES
(C.Created, 'Opened'),
(C.Approved, 'Approved'),
(C.Processed, 'Processed'),
(C.Realized, 'Realized')
) AS V([Date], Status)
I want to convert the following table:
You should be able to use UNNEST for this:
SELECT v.date, v.status
FROM m_table
CROSS JOIN UNNEST(ARRAY[
ROW(C.Created, 'Opened'),
ROW(C.Approved, 'Approved'),
ROW(C.Processed, 'Processed'),
ROW(C.Realized, 'Realized')
]) AS v(date, status);
This works in the latest Presto version, 337.
In Athena you probably still cannot UNNEST array or ROW
in ANSI SQL manner, so you may need some modifications.
Related
I am trying to extract individual dates from a varchar column in a SQL Server 2016 tablet that are stored comma separated and am not sure how to proceed. The data is setup like this:
article Consolidation_Order_Cut_Off_Final_Allocation
------------------ ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
011040 01/13/2021,03/10/2021
019099 01/13/2021,01/27/2021,02/24/2021,03/24/2021,04/28/2021,05/26/2021,06/23/2021,07/28/2021
019310 01/27/2021,02/03/2021,03/10/2021,04/14/2021,05/12/2021,06/09/2021,07/14/2021,08/11/2021
059611 01/13/2021
Ideally - I would have each date split out into a new row. I have seen a few similar questions that use very complex functions but those seem to be for SQL Server 2008. I have also found the new function STRING_SPLIT but that would seem to be table valued and thus have to come in the FROM. One thought I had was to declare a variable to hold this column and then use something like select * FROM string_split(#dates,','); however since there is more than one value in that list that returns an error. I am very new to the 2016 version of SQL Server and curious if anyone has ran into a way to solve this.
String_Split() is a table valued function, so you can call it with a CROSS APPLY
Example or dbFiddle
Select A.article
,B.Value
From YourTable A
Cross Apply string_split(Consolidation_Order_Cut_Off_Final_Allocation,',') B
I have a table where it contains data in below format
How to achieve this in MS SQL Server.
This uses DelimitedSplit8K, as information on the ordinal position is required (something STRING_SPLIT and many other splitters don't supply). The below is Pseudo SQL as well, as the OP has provided images, rather that textual data:
SELECT {YourColumns}
FROM YourTable YT
CROSS APPLY dbo.DelimitedSplit8K(YT.Qualification,',') DSq
CROSS APPLY dbo.DelimitedSplit8K(YT.Instituion,',') DSi
WHERE DSq.ItemNumber = DSi.ItemNumber;
The true answer here, as has been mentioned in the comments, however, is to fix the data model.
An alternative method would be to use OPENJSON. This is something I have only been introduced to recently, and I don't have access to a SQL Server 2016 instance to test this against (I have used SQL Fiddle to test it runs though, but not against the image provided for my same reason above). I beleive this should also achieve your goal though:
SELECT OJq.[value], OJi.[Value]
FROM YourTable YT
CROSS APPLY (SELECT ca.[Key], ca.[value]
FROM OPENJSON('["' + REPLACE(YT.Qualification,',','","') + '"]') ca) OJq
CROSS APPLY (SELECT ca.[Key], ca.[value]
FROM OPENJSON('["' + REPLACE(YT.Instituion,',','","') + '"]') ca) OJi
WHERE OJq.[Key] = OJi.[Key];
I have a situation which prevent me of updating rows in a table in MSSQL getting the data from ORACLE. I can INSERT fine from ORACLE to MS SQL using a SELECT statement like:
SELECT XRECORDACTIVATIONDATE, XRECORDCUTOFFDATE, XRECORDREVIEWDATE,
XRECORDFILINGDATE, XNOLATESTREVISIONDATE, XNEWREVISIONDATE, XDATERECEIVEDDOC,
XINACTIVEDATE, DCREATEDATE, DINDATE, DRELEASEDATE, DLASTMODIFIEDDATE
FROM STELLENT.V_EXPORT_TO_MSSQL V
But when I try to update the rows based on an unique ID using:
UPDATE D
SET D.XRECORDACTIVATIONDATE = V.XRECORDACTIVATIONDATE
FROM DBO.DOCUMENT D
INNER JOIN STELLENT."V_EXPORT_TO_MSSQL" V ON D.DID = V.DID
I get the following error:
ORA-00933: SQL command not properly ended
(System.Data.OracleClient)
DBO.DOCUMENT is a MSSQL table.
STELLENT.V_EXPORT_TO_MSSQL is a View in ORACLE
I might be writing wrong the query I will appreciate some help. thank you.
Lukasz is correct - a select statement is a lot different from an insert statement.
The ORA-00933 error means your query is not formed properly. This is because in Oracle, the database expects queries to follow a certain format/standard. Typically, queries within Oracle will have a form of SELECT [columns] FROM [tables] WHERE [conditions]. This can vary - for example if you wanted to select all data from a table, that query might look like "SELECT * FROM [table];" and the WHERE clause can be omitted because you do not need to define a condition for the database to return all rows. While queries can vary in form, in general, they will follow some type of format.
You are receiving this error because your query does not conform to the expected form, and it is because you have an INNER JOIN that directly follows your FROM clause. To fix this, I would recommend creating a query that you use to select the records you want to update, and then using that select statement to form your update statement by replacing the "SELECT" with "UPDATE".
For more on SQL Standards and how to format your queries, I would recommend taking a look at Oracle documentation. https://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_1001.htm#SQLRF52344
I am trying to pull data from a MS Access report (repInvoices and form frmInvoice) and keep it in MS Access query(qryQuote). I know how it can be done from form but not from a report. I did research in stack exchange and google but I didn't find anything that is helpful. Can any of you help me out? Following is the query that I had been trying to work on. I kept form on following example:
SELECT MAX([oi].[Quote])
FROM ORDER_Shipment AS os
INNER JOIN ORDER_Items AS oi
ON os.Id = oi.Id
AND oi.MaterialId = [Reports]![repInvoices]![MaterialId]
WHERE os.InvoiceNumber= CAST('& Me.InvoiceID' AS VARCHAR)
Two last lines in above query is the place where I need to get data from the report.Also, I know both of above are not correct right now.
[Reports]![repInvoices]![MaterialId] It gives me an error JOIN expression not supported.
Also, I have to use report InvoiceID here CAST('& Me.InvoiceID' AS VARCHAR)
Hi and thanks in advance.
I would like to know why my code got changed after i create a new view, this happend with two different IDE, Server Studio and RazorSQL, this is an example.
Original Code:
SELECT T_USER.ID IDUSER, T_DEP.DESCRIPTION DEPARTMENT
FROM TABLE1 T_USER
INNER JOIN TABLE2 T_DEP ON TABLE2.ID = TABLE1.ID
After create a view
DROP
VIEW orales:vw_test;
CREATE
view "owner".vw_test (id, description) as
SELECT
x0.id
x1.description ,
FROM
("owner".table1 x0 JOIN "owner".table2 x1
ON
((x1.id = x0.id)));
I want to know how to prevent the compiler or something overwrite the name of my tables's alias and the clause of my inner join.
Thank you again :)
You can't prevent this behaviour.
Actually, all databases do this. Your raw query is parsed into an abstract syntax tree which is stored in the database in a proprietary format.
What you are seeing is the rendering of that AST as a valid SQL statement.
Your original query is long gone.
You shouldn't be relying on the database to "manage" the source for your queries. Your original query is "code" and should be managed by a version control system, just like all your other code in your project should be. Doing so means the internal representation of your query in the database is irrelevant, and of course gives you all the other benefits of a VCS.