I am joining two tables together using a lag function so that the first table will display no duplicate metric rows.
I am however having trouble referencing the lagged column when pre selecting for a join.
My query is as follows, the mentioned column is marked with !!!
select "Campaign",
"Ad group" ,
"Final URL" ,
"Headline 1" ,
"Headline 2" ,
"Description" ,
"Path 1" ,
"Path 2" ,
"Status" ,
"Labels" ,
case when prev_key is null or prev_key != "Key" then "Clicks" end as
"Clicks",
case when prev_key is null or prev_key != "Key" then "Impressions" end
as
"Impressions",
case when prev_key is null or prev_key != "Key" then "Cost" end as
"Cost",
case when prev_key is null or prev_key != "Key" then "Avg. position" end
as
"Avg. position",
case when prev_key is null or prev_key != "Key" then "Initial Leads" end
as
"Initial Leads",
case when prev_key is null or prev_key != "Key" then "Evaluations" end
as
"Evaluations",
case when prev_key is null or prev_key != "Key" then "Won Leads" end as
"Won Leads",
case when prev_key is null or prev_key != "Key" then "Opportunities" end
as
"Opportunities",
"Language",
"Network",
"Main Keyword",
"Cluster Keyword 1",
"Match Type"
from
(SELECT
"x"."Campaign",
"x"."Ad group",
"x"."Final URL",
"x"."Headline 1",
"x"."Headline 2",
"x"."Description",
"x"."Path 1",
"x"."Path 2",
"x"."Status",
"x"."Labels",
!!!lag ("x"."Key") over () AS prev_Key!!!,
"x"."Clicks",
"x"."Impressions",
"x"."Cost",
"x"."Avg. position",
"x"."Initial Leads",
"x"."Evaluations",
"x"."Won Leads",
"x"."Opportunities",
"x"."Language",
"x"."Network",
"x"."Main Keyword",
"x"."Cluster Keyword 1",
"x"."Match Type"
FROM ad_copies_final_joined_concatenated x join
ad_copies_final_to_join_concatenated
using ("Key")
order by "Campaign" desc, "Key"
) sub ;
The output error message is as follows;
ERROR: column "Key" does not exist
LINE 11: case when prev_key is null or prev_key != "Key" then "Cli...
^
You shouldn't use the same syntax for string, and column names.
I suspect the error is due to the use of double quotations with strings.
Try changing everything that is not a column to a single quote mark .
E.G.
case when prev_key is null or prev_key != 'Key' then "Avg. position" end
as "Avg. position"
Or, you actually want to compare prev_key column with Key column. In this case, you forgot to select Key in your inner query, so add it to the select list.
Related
I am trying to create a query that shows the list of the workers that have at least a shift during the week, but in the query that I created the names of the workers appear multiple times according to the number of shifts they have completed during the period.
WITH time_frame AS
(SELECT date_trunc('week',NOW())-interval '1 week')
select
null as "Employer Ref",
ff.id as "Personnel Ref",
null as "Employment Sequence No",
ff.first_name as Forename,
ff.last_name as Surname,
null as "Second forename",
null as "Third forename ",
case ff.gender
when 'F' then 'Miss'
when 'M' then 'Mr'
else null
end as Title,
null as "Known as",
ff.address1 as "Address line 1",
ff.address2 as "Address line 2",
ff.city as "Address line 3",
null as "Address line 4",
null as "Address line 5",
lp.postcode as Postcode,
'UK' as Country,
null as "Telephone number",
ff.gender as gender,
ff.birth_date as "Date of birth",
ff.mobile as "Mobile Telephone",
au.email as "Email Address",
ff.sort_code as "Bank1 sort code",
ff.account_number as "Bank1 account number",
concat(ff.first_name,' ',ff.last_name) as "Bank1 account name"
from booking_booking bb
join freelancer_freelancer ff on bb.freelancer_id = ff.id
join job_jobrequest jj on bb.jobrequest_id = jj.id
join auth_user au on au.id = ff.user_id
join location_postcode lp on lp.id = jj.postcode_id
WHERE bb.status != 'FC'
AND bb.status != 'BC'
AND bb.status != 'BU'
AND jj.agency_id IN('1')
AND date_trunc('week',jj.date) =
(SELECT *
FROM time_frame)
I need help with the following query code. Currently it is giving me the 3802 query error "Database does not exist". Well I did my "from" selection query as 'oo', so that definitely exists. This worked yesterday with some minor changes to the "from" part of the query. The assistance is appreciated.
UPDATE snd_bqa.open_order_all_test
FROM (
SELECT
CASE WHEN m.order_status_desc = 'Completed' AND NVL(c.supptype, 'NULL') <> 'CAN'
THEN 'YES'
ELSE 'NO'
END AS "Order Complete",
CASE WHEN m.order_status_desc = 'Completed' AND c.supptype = 'CAN'
THEN 'YES'
ELSE 'NO'
END AS "Order Cancelled",
CASE WHEN pih.effective_start_date IS NOT NULL AND pih.product_instance_status_code = 3
THEN 'YES'
ELSE 'NO'
END AS "Service Active in PB",
c.ordernumber,
c1.order_requested_due_date,
c.creationdate AS "Order Creation Date",
c.ordersubmitdate AS "Order Submit Date",
c.lastupdateddate,
c.supptype,
m.order_status_desc,
m.phase_stage,
cus.sub_nasp_id,
cus.gch_id,
b.work_order_no,
b.itemcode,
b.instance_id,
COALESCE(b.specific_date, b.standard_date) AS "Service Req Due Date",
m.milestone,
m.milestone_desc,
cus.custlegalname,
a.account_number,
vle.vle_id,
vle.currency_cd,
cus.duns_number,
a.cle_vle_id,
CURRENT_TIMESTAMP(2) AS "Originally Loaded",
CURRENT_TIMESTAMP(2) AS "Last Upd",
CAST((CASE WHEN m.order_status_desc = 'Completed' AND NVL(c.supptype, 'NULL') <> 'CAN'
THEN c.lastupdateddate
ELSE NULL
END) AS TIMESTAMP(2)) "Order Completed Date",
CAST((CASE WHEN m.order_status_desc = 'Completed' AND c.supptype = 'CAN'
THEN c.lastupdateddate
ELSE NULL
end) AS TIMESTAMP(2)) "Order Cancelled Date",
pih.last_modified AS "Service Last Modified Date",
pih.effective_start_date AS "Service Effective Date"
FROM (
SELECT *
FROM edw_stg_ord_cw_vw.cwpc_basketitem
WHERE itemcode LIKE 'PR%'
AND (instance_id, lastupdateddate) IN (
SELECT instance_id, MAX(lastupdateddate)
FROM edw_stg_ord_cw_vw.cwpc_basketitem
GROUP BY instance_id
)) AS b
INNER JOIN edw_stg_ord_cw_vw.cworderinstance AS c
ON c.basket_id = b.basketid
AND c.isactive = 1
AND c.request_type = 'ORD'
LEFT JOIN (
SELECT c.ordernumber,
MAX(COALESCE(b.specific_date, b.standard_date)) order_requested_due_date
FROM edw_stg_ord_cw_vw.cworderinstance AS c
INNER JOIN edw_stg_ord_cw_vw.cwpc_basketitem AS b
ON b.basketid = c.basket_id
AND c.request_type = 'ORD'
AND c.isactive = 1
GROUP BY c.ordernumber
) AS c1
ON c1.ordernumber = c.ordernumber
LEFT JOIN edw_stg_ord_cw_vw.uno_customer AS cus
ON cus.cworderid = c.cwdocid
AND cus.oi_customer_id = c.ordering_customer_id
LEFT JOIN edw_stg_ord_cw_vw.uno_milestone AS m
ON m.milestone_id = c.milestone_id
LEFT JOIN edw_stg_ord_cw_vw.uno_account AS a
ON a.cworderid = c.cwdocid
AND b.account_id = a.oi_account_id
LEFT JOIN edw_stg_ord_cw_vw.uno_cle_vle AS vle
ON vle.cle_vle_id = a.cle_vle_id
AND c.cwdocid = vle.cworderid
INNER JOIN snd_bqa.open_order_all_test tst
ON tst."Service Instance ID" = b.instance_id
AND tst."Milestone" <> m.milestone
LEFT JOIN edw_pb_stg_vw.product_instance_history AS pih
ON pih.general_5 = tst."Service Instance ID"
WHERE cus.sub_nasp_id NOT IN ('19HNYO', '23MAXA', '19HNYR', '10INTD')
AND COALESCE(b.specific_date, b.standard_date, c1.order_requested_due_date) BETWEEN CURRENT_DATE - 60 AND CURRENT_DATE - 30
) oo
SET "Order Complete" = oo."Order Complete",
"Order Cancelled" = oo."Order Cancelled",
"Service Active in PB" = oo."Service Active in PB",
"Service Order Number" = oo.ordernumber,
"Order Requested Due Date" = oo.order_requested_due_date,
"Order Last Updated" = oo.lastupdateddate,
"Order Creation Date" = oo."Order Creation Date",
"Order Submit Date" = oo."Order Submit Date",
"Order Supp Type" = oo.c.supptype,
"Order Status" = oo.order_status_desc,
"Phase Stage" = oo.phase_stage,
"Milestone" = oo.milestone,
"Milestone Description" = oo.milestone_desc,
"NASP ID" = oo.sub_nasp_id,
"GCH ID" = oo.gch_id,
"Work Order Numbers" = oo.work_order_no,
"Product Code" = oo.itemcode,
"Service Requested Due Date" = oo."Service Req Due Date",
"Customer Name" = oo.custlegalname,
"Account Number" = oo.account_number,
"VLE ID" = oo.vle_id,
"Currency Code" = oo.currency_cd,
"DUNs Number" = oo.duns_number,
"CLE VLE ID" = oo.cle_vle_id,
"Last Record Update" = oo."Last Upd",
"Order Completed Date" = oo."Order Completed Date",
"Order Cancelled Date" = oo."Order Cancelled Date",
"Service Last Modified Date" = oo."Service Last Modified Date",
"Service Effective Date" = oo."Service Effective Date"
WHERE open_order_all_test."Service Instance ID" = oo.instance_id
;
I found the issue. I had oo.c.supptype, when it should have just been oo.supptype
This caused Teradata to think 'oo' was referring to a database. case closed.
I have a question I am doing an Update query and using the IIF function in the process:
Update someTable as tab Set tab.[field1] = "new filed1 value", tab.[field2] = IIF
(
tab[field2] = "some value" , "New value",IIF
(
tab.[field2] = tab.[field2],tab.[field2]))
)) Where tab.[field1] = "Old field 1 value"
With the above query I wanto to change field2 to "New Value" if it equals to "some value" but if it doesn't then I don't want to change it (or put the same value as it was like the above)
So my question is would the below line in the above query work as expected?
IIF
(
tab.[field2] = tab.[field2],tab.[field2]
)
I think the expression is too complicated. This is what you want:
Update someTable as tab
Set tab.[field1] = "new filed1 value",
tab.[field2] = IIF(tab.[field2] = "some value", tab.[field2])
Where tab.[field1] = "Old field 1 value";
The following query returns a single number, representing the n in a 1:N ratio. Instead of just returning a single number, how can I prepend the integer value with the following text 1: to give the result the correct ratio format?
select round((sum (case when unified_rollup_level_1 = "Company A" and person_type = "Employee" and worker_status = "Active" then 1 else 0 end)) /
(sum (case when unified_rollup_level_1 = "Company A" and person_type = "Employee" and worker_status = "Active" and job_level IN ("08", "09") then 1 else 0 end)),0)
Assuming it would be for MySQL:
select
CONCAT("1:", round((sum (case when unified_rollup_level_1 = "Company A"
and person_type = "Employee"
and worker_status = "Active"
then 1
else 0
end)) /
(sum (case when unified_rollup_level_1 = "Company A"
and person_type = "Employee"
and worker_status = "Active"
and job_level IN ("08", "09")
then 1
else 0
end)),0))
I hope it helps.
You can prepend text by just doing this:
SELECT "Foo" + stuff FROM table;
In this case, if stuff was 3, the result would be "Foo3" (without the quotes).
So in your case:
SELECT "1:" + ... -- the rest of it goes here
Declare #Vaccine varchar(300)
Set #Vaccine = CASE
WHEN OHM.ohmuser.IMMTYPE.FLDCODE IN ('105','HBIG','HEPA') THEN 'HEPATITIS'
WHEN OHM.ohmuser.IMMTYPE.FLDCODE IN ('109','109A','109B','109C','HIN1IM','HIN1IS','FLUID','FLUHD','FLULOA','109QIM','109QIS') THEN 'FLU'
WHEN OHM.ohmuser.IMMTYPE.FLDCODE IN ('102','104','107','108','MMRDEC') THEN 'MMR'
--WHEN OHM.ohmuser.IMMTYPE.FLDCODE IN ('105','HBIG','HEPA') THEN 'MISC'
--WHEN OHM.ohmuser.IMMTYPE.FLDCODE IN ('105','HBIG','HEPA') THEN 'TDAP'
--WHEN OHM.ohmuser.IMMTYPE.FLDCODE IN ('105','HBIG','HEPA') THEN 'FIT'
--WHEN OHM.ohmuser.IMMTYPE.FLDCODE IN ('105','HBIG','HEPA') THEN 'SCREEN'
ELSE NULL
END
BEGIN
SELECT
tEmployee.ID as "Employee Number",
tEmployee.LastName,
tEmployee.FirstName,
tEmployee.BirthDate as "Date of Birth",
tEmployee.Sex as "Gender",
tEmployeeWorkDetail.Department
FROM OHM.ohmuser.tEmployee LEFT OUTER JOIN OHM.ohmuser.tEmployeeWorkDetail on tEmployee.EmployeeID = tEmployeeWorkDetail.EmployeeID
--WHERE tEmployeeWorkDetail.Department = '100801000'
SELECT CASE #Vaccine
WHEN 'HEPATITIS' THEN
(SELECT
HEPATITS.FLDDATE1 as "Hepatitis Vaccine Date",
HEPATITS.FLDDATE2 as "Hepatitis Vaccine Date2",
HEPATITS.FLDDATE3 as "Hepatitis Vaccine Date3",
IMMUNE.FLDTYPE as "Vaccine",
IMMTYPE.FLDDESCR as "Vaccine Descr",
HEPATITS.FLDADM1 as "Hepatitis Given By",
HEPATITS.FLDMAN1 as "Manufacturer",
HEPLAB.FLDCOMMENT as "Comments",
HEPATITS.FLDLOGNUM1 as "Lot #1",
HEPATITS.FLDLOTNUM2 as "Lot #2",
HEPATITS.FLDLOTNUM3 as "Lot #3",
HEPLAB.FLDDATE as "Lab Date",
HEPLAB.FLDSANTBVAL as "Surface Antibody"
FROM OHM.ohmuser.tEmployee LEFT OUTER JOIN OHM.ohmuser.HEPATITS ON tEmployee.EmployeeID = HEPATITS.FLDEMPLOYEE LEFT OUTER JOIN
OHM.ohmuser.HEPLAB ON tEmployee.EmployeeID = HEPLAB.FLDEMPLOYEE LEFT OUTER JOIN
OHM.ohmuser.IMMUNE ON tEmployee.EmployeeID = IMMUNE.FLDEMPLOYEE INNER JOIN
OHM.ohmuser.IMMTYPE ON IMMUNE.FLDTYPE = IMMTYPE.FLDCODE)
WHEN 'FLU' THEN
(SELECT
IMMUNE.FLDDATE as "Date",
IMMUNE.FLDTYPE as "Vaccine",
IMMTYPE.FLDDESCR as "Vaccine Descr",
IMMUNE.FLDNOTE1 as "Comments",
IMMUNE.FLDMANUFACT as "Mfr",
IMMUNE.FLDLOTNUM as "Lot #",
IMMUNE.FLDADMIN as "Given By"
FROM OHM.ohmuser.tEmployee LEFT OUTER JOIN
OHM.ohmuser.IMMUNE ON tEmployee.EmployeeID = IMMUNE.FLDEMPLOYEE INNER JOIN
OHM.ohmuser.IMMTYPE ON IMMUNE.FLDTYPE = IMMTYPE.FLDCODE)
WHEN 'MMR' THEN
(SELECT
IMMUNE.FLDDATE as "Date",
IMMUNE.FLDTYPE as "Procedure",
IMMTYPE.FLDDESCR as "Procedure Descr",
IMMUNE.FLDNOTE1 as "Comments",
IMMUNE.FLDMANUFACT as "Mfr",
IMMUNE.FLDLOTNUM as "Lot #",
IMMUNE.FLDADMIN as "Given By",
TITER.FLDDATE as "Titer Date",
TITER.FLDTYPE as "Titer Type",
TITER.FLDRESULT as "Titer Result",
TITER.FLDCLINIC as "Clinic",
TITER.FLDCOMMENT as "Comments"
FROM OHM.ohmuser.tEmployee LEFT OUTER JOIN OHM.ohmuser.TITER ON tEmployee.EmployeeID = TITER.FLDEMPLOYEE LEFT OUTER JOIN
OHM.ohmuser.IMMUNE ON tEmployee.EmployeeID = IMMUNE.FLDEMPLOYEE INNER JOIN
OHM.ohmuser.IMMTYPE ON IMMUNE.FLDTYPE = IMMTYPE.FLDCODE)
End
End
I want the first part of the SQL to print once. If #Vaccine is equal to ‘Hepatitis’ or ‘Flu’ or ‘MMR’ then it should print additional data. However, I am getting an error that says “The multi-part identifier "OHM.ohmuser.IMMTYPE.FLDCODE" could not be bound.” What am I doing wrong?
The message is basically saying OHM.ohmuser.IMMTYPE.FLDCODE doesn't exist either on any of the SERVER (OHM), DATABASE (ohmuser), SCHEMA (IMMTYPE), TABLE (FLDCODE). Have a quick check to ensure it does on all.
What I have noticed is that you have prefixed all your tables with "OHM.ohmuser". I'm assuming that this is a stored procedure and its in the same database "OHM.ohmuser". Hence, you should be able to remove all instances of it making your code more readable and less prone to error.