PIVOT function not working from SQLCMD while exactly same query is running fine
from SSMS
SQL server version 2008. while running from sqlcmd it is giving syntax error near PIVOT
I am putting the query in a .sql file and supplying that via -i option of sqlcmd. no
SELECT * FROM (
SELECT
ROW_NUMBER() OVER(PARTITION BY EMP-ID,Date order by EMP-ID ) AS rno,
EMP-ID,Date, EMP-CAT
FROM DB.table1
where EMP-CAT != 'XXX'
) as L
PIVOT
(
MAX(EMP-CAT)
FOR rno IN ("1","2","3")
)AS pvt1
Related
Need your help. I am trying to convert below SQL query into RedShift, but getting error message "Invalid operation: This type of correlated subquery pattern is not supported yet"
SELECT
Comp_Key,
Comp_Reading_Key,
Row_Num,
Prev_Reading_Date,
( SELECT MAX(X) FROM (
SELECT CAST(dateadd(day, 1, Prev_Reading_Date) AS DATE) AS X
UNION ALL
SELECT dim_date.calendar_date
) a
) as start_dt
FROM stage5
JOIN dim_date ON calendar_date BETWEEN '2020-04-01' and '2020-04-15'
WHERE Comp_Key =50906055
The same query works fine in SQL Server. Could you please help me to run it in RedShift?
Regards,
Kiru
Kiru - you need to convert the correlated query into a join structure. Not knowing the data content of your tables and the exact expected out put I'm just guessing but here's a swag:
SELECT
Comp_Key,
Comp_Reading_Key,
Row_Num,
Prev_Reading_Date,
Max_X
FROM stage5
JOIN dim_date ON calendar_date BETWEEN '2020-04-01' and '2020-04-15'
JOIN ( SELECT MAX(X) as Max_X, MAX(calendar_date) as date FROM (
SELECT CAST(dateadd(day, 1, Prev_Reading_Date) AS DATE) AS X FROM stage5
cross join
SELECT dim_date.calendar_date from dim_date
) a
) as start_dt ON a.date = dim_date.calendar_date
WHERE Comp_Key =50906055
This is just a starting guess but might get you started.
However, you are likely better off rewriting this query to use window functions as they are the fastest way to perform these types of looping queries in Redshift.
Thanks Bill. It won't work in RedShift as it still has correalted sub-query.
However I have modified query in another method and it works fine.
I am closing ticket.
Am getting an error that goes like this:
Insert values statement can contain only constant literal values or variable references.
these are the statements in which I am getting the errors:
INSERT INTO val.summary_numbers (metric_name, metric_val, dt_create) VALUES ('Total IP Enconters',
(SELECT
count(DISTINCT encounter_id)
FROM prod.encounter
WHERE encounter_type = 'Inpatient')
,
(SELECT min(mod_loadidentifier)
FROM ccsm.stg_demographics_baseline)
);
INSERT INTO val.summary_numbers (metric_name, metric_val, dt_create) VALUES ('Total 30d Readmits',
(SELECT
count(DISTINCT encounter_id)
FROM prod.encounter_attr
WHERE
attr_name = 'day_30_readmit' AND attr_value = 1)
,
(SELECT min(mod_loadidentifier)
FROM ccsm.stg_demographics_baseline));
Change your query like this:
insert into val.summary_numbers
select
'Total IP Enconters',
(select count(distinct encounter_id)
from prod.encounter
where encounter_type = 'Inpatient'),
(select min(mod_loadidentifier)
from ccsm.stg_demographics_baseline)
When using the ADW service, I would recommend that you consider using the CTAS operation possibly combined with a RENAME. The RENAME is a metadata operation so it is fast and the CTAS is parallel where the INSERT INTO will be row by row.
You may still have a data related issue that can be hard to determine with out the create table statement.
Thanks
Not sure why the following does not work (it's not recognizing the last select i.e., syntax error near SELECT). Thanks.
SELECT * INTO #temp FROM
(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE')
SELECT * FROM #temp
If you're using MySQL, then you can't use SELECT * INTO because MySQL doesn't support it.
Instead, you could do this:
CREATE TEMPORARY TABLE IF NOT EXISTS `#temp` AS (
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
);
SELECT * FROM `#temp`;
Also, if your table name has special characters in it you should quote it with backticks.
I have configured connection to pgsql in excel (using ODBC driver).
When i execute sql query then i get an error "ODBC escape convert error"
Below is query (pseudocode)
SELECT
data1,
data2
FROM
(table) A
LEFT JOIN (
SELECT
*
FROM
schema.procedure (
'{1706}',
'2014-09-30 00:00:00',
'2014-10-02 00:00:00',
'{1,2}'
)
) b ON A .some1 = b.some1
AND A .some2 = b.some2
When i run first and second part of query (from excel connection) separately then everything is ok.
Some solutions suggest that i need to replace { with ( but it does not work.
I am waiting for support:)
I can give more informations if you need.
Best regards.
I want to create a temporary table in linq query. I have searched for the solution but didn't succeeded.
here "Node" is temporary table,and "Organization.TblOrganizationUnits" is table in my database.
In linq, How can i create a temporary table and how can I perform different joins and union operation of the above query.
my sql query is:
string query=string.Format(#"WITH Node (OrganizationUnitId, UnitName,ParentUnitId)
AS (
SELECT Organization.TblOrganizationUnits.OrganizationUnitId, Organization.TblOrganizationUnits.UnitName , Organization.TblOrganizationUnits.ParentUnitId
FROM Organization.TblOrganizationUnits
WHERE OrganizationUnitId ={0}
UNION ALL
SELECT Organization.TblOrganizationUnits.OrganizationUnitId, Organization.TblOrganizationUnits.UnitName, Organization.TblOrganizationUnits.ParentUnitId
FROM Organization.TblOrganizationUnits
INNER JOIN Node
ON Organization.TblOrganizationUnits.ParentUnitId = Node.OrganizationUnitId
)
SELECT OrganizationUnitId, UnitName,ParentUnitId FROM Node
where OrganizationUnitId not in (SELECT ParentUnitId FROM Node)
option (maxrecursion 0); ", OrganizationUnitId);