Accees table attribute in Snowflake CTE - snowflake-cloud-data-platform

use database DQ_MART;
use schema WORKING;
WITH ASCENDER_EMPLOYEE AS (
**SELECT DISTINCT EMPLOYEE_ID FROM RECONCILLIATION_ASCENDER_WORKER_TIMESHEET**
),
WORKDAY_EMPLOYEE AS (
**SELECT DISTINCT EMPLOYEE_ID FROM RECONCILLIATION_WORKDAY_WORKER_TIMESHEET**
)
SELECT 'Missing employee in Ascender' DQ_RULE_NAME,
RECONCILLIATION_WORKDAY_WORKER_TIMESHEET.EMPLOYEE_ID KEY
FROM WORKDAY_EMPLOYEE WORKDAY
LEFT OUTER JOIN ASCENDER_EMPLOYEE ASCENDER
ON ASCENDER.EMPLOYEE_ID = WORKDAY.EMPLOYEE_ID
;
Hi All, I am bit new to Snowflake SQL CTE. In the above query, I am getting an error, Error: invalid identifier 'RECONCILLIATION_WORKDAY_WORKER_TIMESHEET.EMPLOYEE_ID' (line 16) in this line
RECONCILLIATION_WORKDAY_WORKER_TIMESHEET.EMPLOYEE_ID
The select statements where the same table is accessed runs properly.
Te database and schema where the table resides is set correctly and I do have SELECT grant on the tables.
Is there a scope visibility in Snowflake which is causing the error to occur. Any suggestions will be welcome.

"RECONCILLIATION_WORKDAY_WORKER_TIMESHEET.EMPLOYEE_ID" means the column "EMPLOYEE_ID" in the table "RECONCILLIATION_WORKDAY_WORKER_TIMESHEET".
Your select statement that uses this column is:
SELECT <column list>
FROM WORKDAY_EMPLOYEE WORKDAY
LEFT OUTER JOIN ASCENDER_EMPLOYEE ASCENDER
ON ASCENDER.EMPLOYEE_ID = WORKDAY.EMPLOYEE_ID
which doesn't include a table called RECONCILLIATION_WORKDAY_WORKER_TIMESHEET - which is why you are getting the error

Related

What is the "lifespan" of a postgres CTE expression? e.g. WITH... AS

I have a CTE I am using to pull some data from two tables then stick in an intermediate table called cte_list, something like
with cte_list as (
select pl.col_val from prune_list pl join employees.employee emp on pl.col_val::uuid = emp.id
where pl.col_nm = 'employee_ref_id' limit 100
)
Then, I am doing an insert to move records from the cte_list to another archive table (if they don't exist) called employee_arch_test
insert into employees.employee_arch_test (
select * from employees.employee where id in (select col_val::uuid from cte_list)
and not exists (select 1 from employees.employee_arch_test where employees.employee_arch_test.id=employees.employee.id)
);
This seems to work fine. The problem is when I add another statement after, to do some deletions from the main employee table using this aforementioned cte_list - the cte_list apparently no longer exists?
SQL Error [42P01]: ERROR: relation "cte_list" does not exist
the actual delete query:
delete from employees.employee where id in (select col_val::uuid from cte_list);
Can the cte_list CTE table only be used once or something? I'm running these statements in a LOOP and I need to run the exact same calls for about 2 or 3 other tables but hit a sticking point here.
A CTE only exists for the duration of the statement of which it's a part. I gather you have an INSERT statement with the CTE preceding it:
with cte_list
as (select pl.col_val
from prune_list pl
join employees.employee emp
on pl.col_val::uuid = emp.id
where pl.col_nm = 'employee_ref_id'
limit 100
)
insert into employees.employee_arch_test
(select *
from employees.employee
where id in (select col_val::uuid from cte_list)
and not exists (select 1
from employees.employee_arch_test
where employees.employee_arch_test.id = employees.employee.id)
);
The CTE is part of the INSERT statement - it is not a separate statement by itself. It only exists for the duration of the INSERT statement.
If you need something which lasts longer your options are:
Add the same CTE to each of your following statements. Note that because data may be changing in your database each invocation of the CTE may return different data.
Create a view which performs the same operations as the CTE, then use the view in place of the CTE. Note that because data may be changing in your database each invocation of the view may return different data.
Create a temporary table to hold the data from your CTE query, then use the temporary table in place of the CTE. This has the advantage of providing a consistent set of data to all operations.

String or binary data would be truncated error in SQL server. How to know the column name throwing this error

I have an insert Query and inserting data using SELECT query and certain joins between tables.
While running that query, it is giving error "String or binary data would be truncated".
There are thousands of rows and multiple columns I am trying to insert in that table.
So it is not possible to visualize all data and see what data is throwing this error.
Is there any specific way to identify which column is throwing this error? or any specific record not getting inserted properly and resulted into this error?
I found one article on this:
RareSQL
But this is when we insert data using some values and that insert is one by one.
I am inserting multiple rows at the same time using SELECT statements.
E.g.,
INSERT INTO TABLE1 VALUES (COLUMN1, COLUMN2,..) SELECT COLUMN1, COLUMN2,.., FROM TABLE2 JOIN TABLE3
Also, in my case, I am having multiple inserts and update statements and even not sure which statement is throwing this error.
You can do a selection like this:
select TABLE2.ID, TABLE3.ID TABLE1.COLUMN1, TABLE1.COLUMN2, ...
FROM TABLE2
JOIN TABLE3
ON TABLE2.JOINCOLUMN1 = TABLE3.JOINCOLUMN2
LEFT JOIN TABLE1
ON TABLE1.COLUMN1 = TABLE2.COLUMN1 and TABLE1.COLUMN2 = TABLE2.COLUMN2, ...
WHERE TABLE1.ID = NULL
The first join reproduces the selection you have been using for the insert and the second join is a left join, which will yield null values for TABLE1 if a row having the exact column values you wanted to insert does not exist. You can apply this logic to your other queries, which were not given in the question.
You might just have to do it the hard way. To make it a little simpler, you can do this
Temporarily remove the insert command from the query, so you are getting a result set out of it. You might need to give some of the columns aliases if they don't come with one. Then wrap that select query as a subquery, and test likely columns (nvarchars, etc) like this
Select top 5 len(Col1), *
from (Select col1, col2, ... your query (without insert) here) A
Order by 1 desc
This will sort the rows with the largest values in the specified column first and just return the rows with the top 5 values - enough to see if you've got a big problem or just one or two rows with an issue. You can quickly change which column you're checking simply by changing the column name in the len(Col1) part of the first line.
If the subquery takes a long time to run, create a temp table with the same columns but with the string sizes large (like varchar(max) or something) so there are no errors, and then you can do the insert just once to that table, and run your tests on that table instead of running the subquery a lot
From this answer,
you can use temp table and compare with target table.
for example this
Insert into dbo.MyTable (columns)
Select columns
from MyDataSource ;
Become this
Select columns
into #T
from MyDataSource;
select *
from tempdb.sys.columns as TempCols
full outer join MyDb.sys.columns as RealCols
on TempCols.name = RealCols.name
and TempCols.object_id = Object_ID(N'tempdb..#T')
and RealCols.object_id = Object_ID(N'MyDb.dbo.MyTable)
where TempCols.name is null -- no match for real target name
or RealCols.name is null -- no match for temp target name
or RealCols.system_type_id != TempCols.system_type_id
or RealCols.max_length < TempCols.max_length ;

MS SQL Server cannot call methods on ntext

I am trying to create a view in MS SQL server from a table. The table name is Account_Plan and I am trying to create a view as Account_Plan_vw. While executing the DDL to create the view, I am getting the error as shown below.
Msg 258, Level 15, State 1, Procedure Account_Plan_vw, Line 56
Cannot call methods on ntext
Msg 207, Level 16, State 1, Procedure Account_Plan_vw, Line 22
Invalid column name 'How_the_CU_will_achieve_these_objective2__c'.
The error message shows the column 'How_the_CU_will_achieve_these_objective2__c' as invalid. However, this is a valid column in the Account_Plan table of ntext type.
Can someone help? I just removed the extra columns from the Create view statement.
CREATE VIEW [dbo].[Account_Plan_vw]
AS
SELECT
Results_1.Account__c
,Results_1.How_the_CU_will_achieve_these_objectives__c
,Results_1.How_the_CU_will_achieve_these_objective2__c
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY apc1.Account__c ORDER BY apc1.Year__c DESC, apc1.CreatedDate DESC) AS RN_1
,apc1.Account__c
,apc1.How_the_CU_will_achieve_these_objectives__c
,apc1.How_the_CU_will_achieve_these_objective2__c
FROM Account_Plan apc1
INNER JOIN RecordType rtp1
ON apc1.RecordTypeId=rtp1.[Id]
AND rtp1.DeveloperName = 'Account_Plan'
INNER JOIN Account acc1
ON acc1.[Id] = apc1.Account__c
WHERE apc1.Year__c <= YEAR(GETDATE())
) AS Results_1
WHERE RN_1 = 1
NTEXT is deprecated, convert it to NVARCHAR(MAX) instead
see: ntext, text, and image (Transact-SQL)
You should consider altering the table not just casting in the view, but:
CREATE VIEW [dbo].[Account_Plan_vw]
AS
SELECT
results_1.Account__c
, results_1.How_the_CU_will_achieve_these_objectives__c
, results_1.How_the_CU_will_achieve_these_objective2__c
FROM (
SELECT
ROW_NUMBER() OVER (PARTITION BY apc1.Account__c ORDER BY apc1.Year__c DESC, apc1.CreatedDate DESC) AS rn_1
, apc1.Account__c
, apc1.How_the_CU_will_achieve_these_objectives__c
, cast(apc1.How_the_CU_will_achieve_these_objective2__c as nvarchar(max)) as How_the_CU_will_achieve_these_objective2__c
FROM Account_Plan apc1
INNER JOIN RecordType rtp1 ON apc1.RecordTypeId = rtp1.[Id]
AND rtp1.DeveloperName = 'Account_Plan'
INNER JOIN Account acc1 ON acc1.[Id] = apc1.Account__c
WHERE apc1.Year__c <= YEAR(GETDATE())
) AS results_1
WHERE RN_1 = 1
the issue and it was somewhat cryptic to find. The salesforce object had a field as last_peer_review_date__c for which no permissions were given to anybody. As a result, DBAMP user was not able to see the field and hence missed to create this field in SQL server when I used the SF_Replicate command. The create view SQL was created by me couple of weeks ago and it did work at that time. Now, when I used the same SQL, it failed because the SQL had the last_peer_review_date field, but the Account_Plan table does not.
Balaji Pooruli

Why i get error when want insert to table in sql server?

I'm beginner in write query,i write this query for insert any data to table:
insert into ScoreTable (UserID,Score,PhoneNumber,date_)
select id,sum(Duration) / 60 ,phoneNumber,Date_ from CDR p
where p.phoneNumber=phoneNumber
but when i execute the that query,i get this error:
Msg 8120, Level 16, State 1, Line 27
Column 'CDR.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
How can i solve that problem?
Your issue is with the select statement. Run that independently and you will get the same issue. Because you're aggregating (SUM) you require a group by clause. Give this a go;
SELECT p.id
,sum(p.Duration) / 60
,p.phoneNumber
,p.Date_
FROM CDR p
WHERE p.phoneNumber = phoneNumber
GROUP BY p.id, p.phoneNumber, p.Date
Also remember to use your table aliases in the queries to avoid ambiguity.

How to join columns in SQL Server?

I have 2 tables
Table 1:
create table Tb1
(
id int identity(1,1) primary key,
repoted int foreign key references Tb2(id)
)
go
Table 2
create table Tb2
(
id int identity(1,1) primary key,
name nvarchar(100)
)
go
Procedure:
create procedure test
#reported int
as
select
reported, name, count(reported) as numberofreport
from
Tb1
cross join
Tb2
where
reported = #reported
group by
reported
When I execute the query, it returns an error:
Msg 8120, Level 16, State 1, Procedure test, Line 4
Column 'Tb2.name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Then I enter edit the group by to reported, name. The error gone but it shows all of my records in the table, only change the name.
Any solution for this problem?
If you are using groupby you need to mention all the columns present in select statement in groupby.
You can have any of below query :-
create procedure test
#reported int
as
select reported,name,count(reported) as numberofreport from Tb1 cross join Tb2 where reported = #reported group by reported,name
or
select reported,count(reported) as numberofreport from Tb1 cross join Tb2 where reported = #reported group by reported
Reason behind this rule is , in case of group by, query suppose to return one row for each group (mentioned in group by clause), so if you have two columns in select then it may have more records. Consider scenario you have two rows for one reported value with different name, how database will decide which to return.
It may be one to one mapping in your scenario between reported and name, but rules are generic for everyone to avoid runtime failures.

Resources