Receiving error when converting text to date in Redshift - amazon-redshift

I am running a query that Redshift is throwing an error on "SQL Error [500310][57014]:Amazon Invalid operation: Error converting text to date;"
However, when I remove the Joined tables, or remove the conversions from the WHERE clause, it no longer throws an error. Thus I suspect something else is going on and this error is misleading.
I have looked at the data and it none of the dates look strange or out of range.
Here is the query that throws an error:
SELECT m.member_id,
t.tracker_id,
DATE(ma.yyyymmdd) AS tracked_date,
DATE(ma.updated_date) AS updated_date
FROM import_genesis.member_activities ma
INNER JOIN master."member" m
ON ma.memberid = m.member_legacy_id
INNER JOIN master.action_activity aa
ON ma.activity_type = aa.activity_type
INNER JOIN master.tracker t
ON t.action_id = aa.action_id
WHERE DATE(ma.yyyymmdd) > DATE(DATEADD(DAY, -14, GETDATE()))
AND DATE(ma.yyyymmdd) <= GETDATE();
Here are two queries that do NOT throw an error:
SELECT DATE(ma.yyyymmdd) AS tracked_date,
DATE(ma.updated_date) AS updated_date
FROM import_genesis.member_activities ma
WHERE DATE(ma.yyyymmdd) > DATE(DATEADD(DAY, -14, GETDATE()))
AND DATE(ma.yyyymmdd) <= GETDATE();
SELECT m.member_id,
t.tracker_id,
DATE(ma.yyyymmdd) AS tracked_date,
DATE(ma.updated_date) AS updated_date
FROM import_genesis.member_activities ma
INNER JOIN master."member" m
ON ma.memberid = m.member_legacy_id
INNER JOIN master.action_activity aa
ON ma.activity_type = aa.activity_type
INNER JOIN master.tracker t
ON t.action_id = aa.action_id;
Any ideas as to what might be wrong or any potential workarounds?

Related

SQL syntax error near rank() and any other window function

I wrote a simple SQL query and it seems no matter what window function I use I get a syntax error for the () after any function. anyone have any ideas?
select emp_ssn, emp_status, appt_date,
rate_eff_dt, class_eff_dt
rank() OVER (
partition by emp_ssn
order by rate_eff_dt desc)
from pds_emp e
inner join pds_rate_new_vw r on e.emp_ssn = r.rate_emp_ssn
inner join pds_class_new c on e.emp_ssn = c.class_emp_ssn
where rate_eff_dt > '0/00/0000' and
class_eff_dt > '0/00/0000'
limit 1000

Postgres Aggregate function in where clause

I am trying to get the count of leads that enrolled in the last 180 days. I have the subquery for leads as
left outer join (select nl.zip, count(distinct lsu.lead_id) as leadCount
from lead_status_updates lsu
join normalized_lead_locations nl on nl.lead_id = lsu.lead_id
where category in ('PropertySell','PropertySearch')
AND lower(status) not like ('invite%')
and DATE_PART('day', current_date - min(lsu.created)) < 181
group by nl.zip) lc on lc.Zip = cbsa.zip
but I get the error message that I can't have aggregate functions in the where clause. Anyone know how to fix this?

syntax error at end of input in PostgreSQL when use INNER JOIN LATERAL

I tried to merge a quarterly table and a monthly table by using INNER JOIN LATERAL, but encountered error.
state_smm table, monthly
unemp table, quarterly
my code is:
SELECT st.state,
st.monthly_reporting_period AS month,
st.CPR,
up.unemp_rate AS Most_Recent_Quarterly_unemp
FROM state_smm AS st
INNER JOIN LATERAL (
-- Find the value for latest quarter that ended on or before the monthly date
SELECT unemp.unemp_rate
FROM unemp
WHERE unemp.state = st.property_state
AND unemp.quarter <= st.monthly_reporting_period
ORDER BY unemp.quarter DESC
limit 1
)AS up
and I got error:
ERROR: syntax error at end of input
LINE 14: )AS up
^
SQL state: 42601
Character: 434
How can I edit my code? Thanks a lot.

Postgres SQL Cursor

I'm a SQL Server guy and I have a need to write some dynamic SQL in Postgres. Here's what I need. The dynamic SQL would be dependent upon integers produced by this query:
SELECT local_channel_id
FROM d_channels dc
INNER JOIN channel c ON c.id = dc.channel_id
AND c.name LIKE '%__Achv'
Using this, I need to build and execute a select and subsequent union select on the below query substituting the the values produced by the above query where indicated below by {X} (4 places):
SELECT
dmc.message_id,
dmm.received_date,
dmm.server_id,
dc.channel_id,
dmcm."SOURCE",
dmcm."TYPE",
dmm.status,
dmc.content
FROM
d_mc{X} dmc
INNER JOIN
d_mm{X} dmm ON dmc.message_id = dmm.message_id
INNER JOIN
d_channels dc ON dc.local_channel_id = {X}
INNER JOIN
d_mcm{X} dmcm ON dmcm.message_id = dmc.message_id
AND dmcm.metadata_id = 0
WHERE
dmm.connector_name = 'Source'
AND dmc.content_type = 1 --Raw
AND date(dmm.received_date) + interval '7' < now()
Can anybody help with this? I'm truly clueless when it comes to Postgres.

sql, group a query with no aggregations and multiple tables

I need to group the query below by dda.LA and need to display all the columns listed in the select but almost none of them are aggregated. i don't know what the syntax to get around this is and i can not find a post that shows this syntax (most examples only have one table, two at tops).
Select dda.a,
dda.b,
dda.c,
dda.d,
dda.e,
dda.f,
dda.g,
dda.h,
dda.i,
dda.j,
dda.k,
dda.l,
dda.m,
dda.n,
dda.o,
dda.p,
dda.r,
dda.u,
dda.LA,
dd.aa,
coalesce(apn.apn,Pt.z) as abc,
coalesce(apn.v,Pt.y) as def,
'RFN' RowFocusIndicator ,
'SRI' SelectRowIndicator ,
'Y' Expanded ,
Convert(Int, Null) SortColumn
From dda (NoLock)
Inner Join dd (NoLock) On dda.d = dd.q and dda.e = dd.e
Left Outer Join apn (nolock) on dda.r = apn.r
Left Outer Join Pt (nolock) on dda.s = Pt.t
Where 1 = 1
And dda.u = (Select Min(c.w)
From c (NoLock)
Where c.x = dda.s)
Thanks!
Just add this to any column that you want to aggregate by:
AggregatedColumnName = Aggregation(fieldToAggregate) Over (Partition By dda.LA)
ex.
aCount = Count(dda.a) Over (Partition By dda.LA)