Hi I have a following table Sales
Country
Sales
Sale_Date
US
2
12-06-2022
JP
2
15-06-2022
I have to write a SQL query to update the paticular cell in dbt. I want to change Sale_Date for US.
Now my query is-
UPDATE `sales`
SET
Sale_Date = '2022-06-16'
WHERE
Country = 'US'
However, In dbt I get following error
Server error: Database Error in rpc request (from remote system)
Syntax error: Expected end of input but got keyword LIMIT at [4:1]
What am I missing? I am fairly new to dbt.
dbt is thought to mainly be used with SELECT statements. You can find a bit more context here.
What you might want to do is to just apply that transformation using a SELECT statement as follows:
select
[...],
case when country = 'US' then '2022-06-16'::date else sale_date end as <new_column_name>
from {{ ref('your_staging_model') }}. -- or {{ source('your_schema', 'your_source_table')}}
So now, you will have a dbt model materialized either as a view, a table, or whatever you prefer, that contains a column with the UPDATE-like transformation that you needed.
Related
I am trying to use below query as source for my data flow but I keep getting errors. Is the fuctionality not supported in data flow?
SELECT customer.customerid AS 'customerid',
customer.customer_fname AS 'fname',
customer.customer_lname AS 'lname',
customer.customer_phone AS 'Phone',
address.customer_addressid as 'addressid',
address.Address_type as 'addresstype',
address.street1 as 'street1'
FROM customer customer
INNER JOIN customer_address address
ON customer.customerid = address.customerid
order by customer.customerid
FOR JSON AUTO, ROOT('customer')
I get the following error:
Notifications
Column name needs to be specified in the query, set an alias if using a SQL function
ADF V2, Data Flows, Source
The error is cause by that Data Flow Query doesn't support order by statement, not the 'FOR JOSN AUTO'.
See the error bellow:
Please refence Data Flow Source transformation:
Query: If you select Query in the input field, enter a SQL query for your source. This setting overrides any table that you've chosen in the dataset. Order By clauses aren't supported here, but you can set a full SELECT FROM statement. You can also use user-defined table functions. select * from udfGetData() is a UDF in SQL that returns a table. This query will produce a source table that you can use in your data flow. Using queries is also a great way to reduce rows for testing or for lookups.
SQL Example: Select * from MyTable where customerId > 1000 and customerId < 2000
The query work well in Copy active but false in Data Flow. You need to change the query.
I am using APEX collections to store some values and pass them between pages in Oracle Application Express 4.2.3.
I would like to then perform an update statement on a table called "project" with the values from the collection.
My code so far is as follows:
update project
SET name=c.c002,
description=c.c007,
start_date=c.c004,
timeframe=c.c005,
status=c.c009
FROM
apex_collections c
WHERE
c.collection_name = 'PROJECT_DETAILS_COLLECTION'
and id = :p14_id;
where :p14_id is the value of a page item.
However, I am getting the following error:
ORA-00933: SQL command not properly ended
Anyone have any idea on how to approach this?
Thanks!
The UPDATE syntax you are using is not valid in Oracle; it does not allow you to use FROM in the way you are attempting.
The simplest way to do this in Oracle would with a subquery:
update project
set (name, description, start_date, timeframe, status) =
(select c.c002, c.c007, c.c004, c.c005, c.c009
FROM
apex_collections c
WHERE
c.collection_name = 'PROJECT_DETAILS_COLLECTION'
)
WHERE
id = :p14_id
;
Note that if the subquery returns no rows, the columns in the target table will be updated to NULL; this could be avoided by adding a similar EXISTS condition in the predicate for the update. It could also be avoided by using a MERGE statement instead of an UPDATE.
If the subquery returns multiple rows, the statement will throw an error. It looks like tthat should not be the case here.
Lets assume (for simplicity) i have two tables:
Product
product_id
CreatedDate
LastEditedBy
EditedDate
Entity
entity_id
CreatedDate
LastEditedBy
EditedDate
Both tables have the same column names but only the ID column has a different name. I would like to run a query so that when i run it from SQL plus i just give it a parameter and it gets the data from one of the tables. In the above example i could run the query like this
#archiveHistory.sql product
#archiveHistory.sql entity
Here is my attempt but it always failed to recognise one of the columns, i.e. if i run it with product, it says entity_id does not exist. if i run it with entity it says product_id does not exist.
Note that i am using the passed in parameter on both the column selection and the table name selection.
define identifier = '&1'
Select * from (
Select case lower('&identifier')
when product then product_id
when entity then entity_id
end ID, CreatedDate, LastEditedBy, EditedDate
From &identifier
)
I think it will work if the column list in the CASE statement were all from the same table.
Questions
What do i need to do so the query ignores the column that is not relevant i.e. ignore product_id if the argument is entity
I thought about using an anonymous PL/SQL block (i.e. Begin End) but i am not sure how i can display the output without using dbms_output.put_line.
For this particular case, I think the following SQL-only solution might work best:
SELECT product_id AS the_field
, CreatedDate, LastEditedBy, EditedDate
FROM Product
WHERE LOWER('&identifier') = 'product'
UNION ALL
SELECT entity_id AS the_field
, CreatedDate, LastEditedBy, EditedDate
FROM Entity
WHERE LOWER('&identifier') = 'entity'
The query planner will pre-evaluate your '&identifier' = ... predicates, which prevents execution of the unneeded union subquery.
If that's not an option (because your real-world use-case is much more complex), there are plenty of answers on Stack Overflow already regarding the execution of dynamic SQL from PL/SQL:
Executing a dynamic sql statement into a SYS_REFCURSOR
Cursor For Loop with dynamic SQL-Statement
Binding Parameters to Oracle Dynamic SQL
You could use dynamic SQL to insert your data into a temp table, and then simply SELECT * FROM temp_table
I have the following query in a stored procedure in SQL server:
SELECT TLI.LESNumber
,COUNT(TLT.PL)
INTO #PWCM
FROM #tmpLESImport TLI
INNER JOIN tbl_LES L
on TLI.LESNumber=L.NUMB
WHERE ISNULL(L.DELT_FLAG,0)=0
AND L.SCHL_PK=#SCHL_PK
AND TLI.PL IS NOT NULL
AND LEN(TLI.PL)>0
GROUP BY LESNumber
HAVING COUNT(PL)>1
When the query is run I get the following error:
An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.
Can anyone tell me why? #PWCM does not appear anywhere until this query.
When you SELECT INTO a table, it creates the table (in this case, a temp table). In order to create a table, each column needs a name, which your count column does not. You just need to give it a name:
SELECT TLI.LESNumber,COUNT(TLT.PL) [NumRecords]
INTO #PWCM
FROM #tmpLESImport TLI
...
I had this error for this query
SELECT
CASE WHEN COALESCE([dbo].[my-table].[field],"") = '...' THEN 'A'
WHEN COALESCE([dbo].[my-table].[field],"") = '...' THEN 'B'
...
END AS aaa INTO ##TEMPTABLE
FROM [dbo].[my-table]
Turns out I had to change the "" inside the COALSCE into ''.
Solved it for me
I just came across this, and the core reason was actually related to an errant set of brackets in the code which made the engine think there was a missing alias. Something along the lines of:
select *
from SOME_TABLE
where x = 1
[]
A stringified version of the query included a parameter list for logging, but that was being issued as the query instead of the actual query object. Deleting [] at the end resolved it.
I am using SQL Server 2008 and have a very large CASE statement that is also used in the GROUP By clause. I would like to set the CASE statement to a variable in order to minimize code maintenance and maximize reuse. The problem is that I get this error:
Each GROUP BY expression must contain at least one column that is not an outer reference.
This CASED column is not the only column referenced in the GROUP By clause so I'm not sure why I get this error.
I've searched the site but didn't find a problem quite like mine (surprisingly). So, how do I go about getting around this?
UPDATE: I have included the DB type. As far as adding the code for what I have, I'm not sure that would add anything but bulk as it is over 200 lines. It's not a complex statement at all. It just takes various country codes and maps them to their full country names. For instance, the U.S. has over 50 codes so I am using the CASE statement to consolidate them. This allows me to group my info by country.
The best way to do this is with a subquery:
select var, count(*)
from (select t.*,
(case <nasty expressions go here>
end) var
from t
) t
group by var
The error that you are getting is because the variables in the group by is a constant. I'm not sure why the error message is not clearer.
And, in the event that you actually do want to include a constant in the group by for some reason (as I have had occasion to do), then a column helps:
group by (case when coalesce(col, '') = coalesce(col, '') then 'some constant' end)
At least in SQL Server 2008, the engine does not recognize the expression as a constant.