How to use the MAX and Case function over three tables? - asp.net
I have joined three tables using Max and Case functions.
When I used two tables it working fine but when I added the third table I got this error.
Ambiguous column name 'Month'.
SELECT Category ,
KPI ,
TDTargetValue ,
MAX(CASE WHEN Month = 'Jan' THEN Input
END) Jan ,
MAX(CASE WHEN Month = 'Feb' THEN Input
END) Feb ,
MAX(CASE WHEN Month = 'Mar' THEN Input
END) Mar ,
MAX(CASE WHEN Month = 'Apr' THEN Input
END) Apr ,
MAX(CASE WHEN Month = 'May' THEN Input
END) May ,
MAX(CASE WHEN Month = 'Jun' THEN Input
END) Jun ,
MAX(CASE WHEN Month = 'Jul' THEN Input
END) Jul ,
MAX(CASE WHEN Month = 'Aug' THEN Input
END) Aug ,
MAX(CASE WHEN Month = 'Sep' THEN Input
END) Sep ,
MAX(CASE WHEN Month = 'Oct' THEN Input
END) Oct ,
MAX(CASE WHEN Month = 'Nov' THEN Input
END) Nov ,
MAX(CASE WHEN Month = 'Dec' THEN Input
END) Dec
FROM [NEWSEMAKPI].[dbo].[NewCriteria] NC
INNER JOIN ( SELECT *
FROM [NEWSEMAKPI].[dbo].[UpdateData]
WHERE PeriodId = '1'
) UD ON UD.Cid = NC.Id
LEFT JOIN ( SELECT *
FROM [NEWSEMAKPI].[dbo].[TargetData]
) TD ON UD.Cid = TD.CId
WHERE NC.Grade = 'A'
AND IsActived = '0'
GROUP BY Category ,
KPI ,
TDTargetValue
ORDER BY 1
Any help would be appreciated.
Add alias also for the column Month...
Assuming NewCriteria table has the column Month
SELECT Category ,
KPI ,
TDTargetValue ,
MAX(CASE WHEN NC.Month = 'Jan' THEN Input
END) Jan ,
MAX(CASE WHEN NC.Month = 'Feb' THEN Input
END) Feb ,
MAX(CASE WHEN NC.Month = 'Mar' THEN Input
END) Mar ,
MAX(CASE WHEN NC.Month = 'Apr' THEN Input
END) Apr ,
MAX(CASE WHEN NC.Month = 'May' THEN Input
END) May ,
MAX(CASE WHEN NC.Month = 'Jun' THEN Input
END) Jun ,
MAX(CASE WHEN NC.Month = 'Jul' THEN Input
END) Jul ,
MAX(CASE WHEN NC.Month = 'Aug' THEN Input
END) Aug ,
MAX(CASE WHEN NC.Month = 'Sep' THEN Input
END) Sep ,
MAX(CASE WHEN NC.Month = 'Oct' THEN Input
END) Oct ,
MAX(CASE WHEN NC.Month = 'Nov' THEN Input
END) Nov ,
MAX(CASE WHEN NC.Month = 'Dec' THEN Input
END) Dec
FROM [NEWSEMAKPI].[dbo].[NewCriteria] NC
INNER JOIN ( SELECT *
FROM [NEWSEMAKPI].[dbo].[UpdateData]
WHERE PeriodId = '1'
) UD ON UD.Cid = NC.Id
LEFT JOIN ( SELECT *
FROM [NEWSEMAKPI].[dbo].[TargetData]
) TD ON UD.Cid = TD.CId
WHERE NC.Grade = 'A'
AND IsActived = '0'
GROUP BY Category ,
KPI ,
TDTargetValue
ORDER BY 1
Related
How to return all rows in a column when using cross apply and DelimitedSplit8K_LEAD together in SQL Server?
This code works to return the info that I need from the column but IT IS ONLY RETURNING ONE ROW and I need it to return them all. You can leverage the DelimitedSplit8K_LEAD function which you can read more about here. https://www.sqlservercentral.com/articles/reaping-the-benefits-of-the-window-functions-in-t-sql-2 You will also find the code for the function there. select FirstValue = max(case when s.ItemNumber = 12 then s.Item end) , SecondValue = max(case when s.ItemNumber = 45 then try_convert(datetime, stuff(stuff(stuff(s.Item, 9, 0, ' '), 12, 0, ':'), 15, 0, '.')) end) from myDatabase x cross apply DelimitedSplit8K_LEAD(x.myColumn, ',') s where s.ItemNumber = 12 or s.ItemNumber = 45 Here is an example of the data in the column that I'm trying to return. ,505611,XXXXXXX,,,,,,,,,13M2,,,,,,,,,,,03294961,,,,,,,,,,,,,,,,,,,,,XXXXX,20220216183348,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,US,,,0000,,,,,,,,,,,,,,,,,,,, Here is an example of it working, just not with using it on the table column https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=ca74e807853eb1dea445a8ffb7209b70 Okay so here is an example. Create a table in sql server database... CREATE TABLE honda ( user1 nvarchar(max) ); INSERT INTO honda (user1) VALUES (',523869,HXMFG-01,,,,,,,,,11M2,,,,,,,,,,,03311141,,,,,,,,,,,,,,,,,,,,,EAGLE,20220323082041,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,US,,,0000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,'), (',523869,HXMFG-01,,,,,,,,,12M2,,,,,,,,,,,03311148,,,,,,,,,,,,,,,,,,,,,EAGLE,20220323093049,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,US,,,0000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,'), (',523869,HXMFG-01,,,,,,,,,13M2,,,,,,,,,,,03311216,,,,,,,,,,,,,,,,,,,,,EAGLE,20220323100350,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,US,,,0000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,'), (',523869,HXMFG-01,,,,,,,,,14M2,,,,,,,,,,,03311242,,,,,,,,,,,,,,,,,,,,,EAGLE,20220323103854,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,US,,,0000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,'), (',523869,HXMFG-01,,,,,,,,,15M2,,,,,,,,,,,03311267,,,,,,,,,,,,,,,,,,,,,EAGLE,20220323112420,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,US,,,0000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,'), (',527040,HXMFG-01,,,,,,,,,16M2,,,,,,,,,,,03311352,,,,,,,,,,,,,,,,,,,,,EAGLE,20220323122930,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,US,,,0000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,'), (',527040,HXMFG-01,,,,,,,,,17M2,,,,,,,,,,,03311395,,,,,,,,,,,,,,,,,,,,,EAGLE,20220323130347,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,US,,,0000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,'); If using sql server should only have to run this middle block of code once. CREATE FUNCTION [dbo].[DelimitedSplit8K_LEAD] --===== Define I/O parameters (#pString VARCHAR(8000), #pDelimiter CHAR(1)) RETURNS TABLE WITH SCHEMABINDING AS RETURN --===== "Inline" CTE Driven "Tally Tableā produces values from 0 up to 10,000... -- enough to cover VARCHAR(8000) WITH E1(N) AS ( SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 ), --10E+1 or 10 rows E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max cteTally(N) AS (--==== This provides the "zero base" and limits the number of rows right up front -- for both a performance gain and prevention of accidental "overruns" SELECT 0 UNION ALL SELECT TOP (DATALENGTH(ISNULL(#pString,1))) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4 ), cteStart(N1) AS (--==== This returns N+1 (starting position of each "element" just once for each delimiter) SELECT t.N+1 FROM cteTally t WHERE (SUBSTRING(#pString,t.N,1) = #pDelimiter OR t.N = 0) ) --===== Do the actual split. The ISNULL/NULLIF combo handles the length for the final element when no delimiter is found. SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY s.N1), Item = SUBSTRING(#pString,s.N1,ISNULL(NULLIF((LEAD(s.N1,1,1) OVER (ORDER BY s.N1) - 1),0)-s.N1,8000)) FROM cteStart s ; GO If using dbfiddle take the GO off of the end in the above block. select ECI_Level = max(case when s.ItemNumber = 12 then s.Item end) , 'DateTime' = max(case when s.ItemNumber = 45 then try_convert(datetime, stuff(stuff(stuff(s.Item, 9, 0, ' '), 12, 0, ':'), 15, 0, '.')) end) from honda x cross apply DelimitedSplit8K_LEAD(x.user1, ',') s where s.ItemNumber = 12 or s.ItemNumber = 45
I was able to get it to work by adding GROUP BY like this... select ECI_Level = max(case when s.ItemNumber = 12 then s.Item end) , 'DateTime' = max(case when s.ItemNumber = 45 then try_convert(datetime, stuff(stuff(stuff(s.Item, 9, 0, ' '), 12, 0, ':'), 15, 0, '.')) end) from honda x cross apply DelimitedSplit8K_LEAD(x.user1, ',') s where s.ItemNumber = 12 or s.ItemNumber = 45 GROUP BY user1;
The problem is that you are aggregating over the whole honda table, rather than just over the split data. You need to place the aggregation into an APPLY select s.* from honda x CROSS apply ( SELECT ECI_Level = max(case when s.ItemNumber = 12 then s.Item end) , DateTime = max(case when s.ItemNumber = 45 then try_convert(datetime, stuff(stuff(stuff(s.Item, 9, 0, ' '), 12, 0, ':'), 15, 0, '.')) end) FROM DelimitedSplit8K_LEAD(x.user1, ',') s where s.ItemNumber = 12 or s.ItemNumber = 45 ) s; db<>fiddle Personally, for just two values I wouldn't bother with a split function. Instead you can use CHARINDEX, feeding each one into the next to get the next , location. And I guess this just shows why you shouldn't store data in a database like this in the first place.
R generate mysql select statements
I need to generate complex select statements that change based on an input file each time my script is ran. I have a list of names that serves as my filter list, and I have a select statement that has been suggested to me in another thread. I'm not sure how to generate what I need without sitting on google for the next 6 hours piecing it together. I need to create a MAX(CASE... line for each item in the list. list <- df$names list: 1 square 2 ball 3 dog 4 triangle 5 hamster 6 circle 7 yellow 8 cat suggested SELECT format: SELECT data.loc , data.type , MAX(CASE WHEN data.name = 'cat' THEN 1 ELSE 0 END) AS cat , MAX(CASE WHEN data.name = 'hamster' THEN 1 ELSE 0 END) AS hamster FROM data GROUP BY data.loc , data.type I believe the desired output would be: SELECT data.loc , data.type , MAX(CASE WHEN data.name = 'square' THEN 1 ELSE 0 END) AS square , MAX(CASE WHEN data.name = 'ball' THEN 1 ELSE 0 END) AS ball , MAX(CASE WHEN data.name = 'dog' THEN 1 ELSE 0 END) AS dog , MAX(CASE WHEN data.name = 'triangle' THEN 1 ELSE 0 END) AS triangle , MAX(CASE WHEN data.name = 'hamster' THEN 1 ELSE 0 END) AS hamster , MAX(CASE WHEN data.name = 'circle' THEN 1 ELSE 0 END) AS circle , MAX(CASE WHEN data.name = 'yellow' THEN 1 ELSE 0 END) AS yellow , MAX(CASE WHEN data.name = 'cat' THEN 1 ELSE 0 END) AS cat FROM data GROUP BY data.loc , data.type
simplest approach that I could think of is l <- list("Cat", "dog", "bird") sqlqry <- "SELECT data.loc , data.type" for (i in 1:length(l)) { sqlqry <- paste0(sqlqry, sprintf(", MAX(CASE WHEN data.name = '%s' THEN 1 ELSE 0 END) AS %s",l[i], l[i])) print(sqlqry) } sqlqry <- paste0(sqlry, "your where clause")
load row data based on the first filed in the row
i have to develop new website for CAR Wash company , this company own three cleaning car which receive order from my system , max order per day for every car is 5 orders so i should add 5 green places beside every car in the the same car row . (ex) if car have two order so two fields with background yellow and other three fields in green background I have two tables Cars Orders Car table ID CarName 1 Car1 2 Car2 3 Car3 Order table ID ClientName Phone Date CarID 1 Jack 11111 22-7-2017 10:22:00 AM 2 2 Susan 22222 22-7-2017 01:30:00 PM 2 3 Hany 33333 22-7-2017 10:22:00 AM 3 4 Karim 44444 21-7-2017 08:22:00 AM 1 5 Halaa 55555 22-7-2017 05:22:00 PM 3 i want to select all orders for every car in one row per day based on selection of day from DATETIMEPICKER so i will select orders for 22-07 Cars Orders car1 Empty Empty Empty Empty Empty car2 Jack(111111) Susan(22222) Empty Empty Empty car3 Hany(333333) HalaSusan(55555) Empty Empty Empty So how can do it if i use GridView and what is the best query
The following should do the trick... IF OBJECT_ID('tempdb..#Vehicle', 'U') IS NOT NULL DROP TABLE #Vehicle; GO CREATE TABLE #Vehicle ( VehicleID INT NOT NULL PRIMARY KEY CLUSTERED, VehicleName VARCHAR(10) NOT NULL ); GO INSERT #Vehicle (VehicleID, VehicleName) VALUES (1, 'Car1'), (2, 'Car2'), (3, 'Car3'); GO IF OBJECT_ID('tempdb..#Orders', 'U') IS NOT NULL DROP TABLE #Orders; GO CREATE TABLE #Orders ( OrderID INT NOT NULL PRIMARY KEY CLUSTERED, ClientName VARCHAR(15) NOT NULL, PhoneNumber VARCHAR(10) NOT NULL, ServiceDate DATETIME NOT NULL, VehicleID INT NOT NULL -- create FK refference back to Vehicle.VehicleID) ); GO -- create an index to support the row_number() function... -- The sort cost is greater than the difference between a seek & scan... CREATE NONCLUSTERED INDEX ix_Orders_VehicleID_ServiceDate ON #Orders (VehicleID, ServiceDate) INCLUDE (ClientName, PhoneNumber); GO INSERT #Orders (OrderID, ClientName, PhoneNumber, ServiceDate, VehicleID) VALUES (1, ' Jack ', '11111', '07-22-2017 10:22:00 AM', 2), (2, ' Susan', '22222', '07-22-2017 01:30:00 PM', 2), (3, ' Hany ', '33333', '07-22-2017 10:22:00 AM', 3), (4, ' Karim', '44444', '07-21-2017 08:22:00 AM', 1), (5, ' Halaa', '55555', '07-22-2017 05:22:00 PM', 3); GO --SELECT * FROM #Vehicle v; --SELECT * FROM #Orders o; --======================================================================= DECLARE #WorkDate DATE = '2017-07-22'; WITH cte_AddRN AS ( SELECT o.OrderID, o.ClientName, o.PhoneNumber, o.ServiceDate, o.VehicleID, RN = ROW_NUMBER() OVER (PARTITION BY o.VehicleID ORDER BY o.ServiceDate) FROM #Orders o WHERE CAST(o.ServiceDate AS DATE) = #WorkDate ) SELECT VehicleName = MAX(v.VehicleName), Order1 = MAX(CASE WHEN ar.RN = 1 THEN ar.ClientName + ' (' + ar.PhoneNumber + ')' ELSE '' END), Order2 = MAX(CASE WHEN ar.RN = 2 THEN ar.ClientName + ' (' + ar.PhoneNumber + ')' ELSE '' END), Order3 = MAX(CASE WHEN ar.RN = 3 THEN ar.ClientName + ' (' + ar.PhoneNumber + ')' ELSE '' END), Order4 = MAX(CASE WHEN ar.RN = 4 THEN ar.ClientName + ' (' + ar.PhoneNumber + ')' ELSE '' END), Order5 = MAX(CASE WHEN ar.RN = 5 THEN ar.ClientName + ' (' + ar.PhoneNumber + ')' ELSE '' END) FROM #Vehicle v LEFT JOIN cte_AddRN ar ON v.VehicleID = ar.VehicleID GROUP BY v.VehicleID; Results... VehicleName Order1 Order2 Order3 Order4 Order5 ----------- --------------- --------------- --------------- --------------- --------------- Car1 Car2 Jack (11111) Susan (22222) Car3 Hany (33333) Halaa (55555)
SQL Query to find missing numbers between 2 values
Need your expertise in writing an SQL for the below scenario I have a single row in a table "range_num" as follows. start_num end_num 10 14 Is there any way we can write a query to find all the gaps between 10 and 14 i ..e 11,12,13 in the below format col1 col2 col3 col4 col5 10 11 12 13 14 I am using Teradata databases , so any compliant SQL query will be of great help!!
Here is solution for your problem... select max(case when rownum=1 then gap end) as col1,max(case when rownum=2 then gap end) as col2, max(case when rownum=3 then gap end) as col3,max(case when rownum=4 then gap end) as col4, max(case when rownum=5 then gap end) as col5,max(case when rownum=6 then gap end) as col6, max(case when rownum=7 then gap end) as col7,max(case when rownum=8 then gap end) as col8, max(case when rownum=9 then gap end) as col9,max(case when rownum=10 then gap end) as col10, max(case when rownum=11 then gap end) as col11,max(case when rownum=12 then gap end) as col12, max(case when rownum=13 then gap end) as col13,max(case when rownum=14 then gap end) as col14, max(case when rownum=15 then gap end) as col15,max(case when rownum=16 then gap end) as col16, max(case when rownum=17 then gap end) as col17,max(case when rownum=18 then gap end) as col18, max(case when rownum=19 then gap end) as col19,max(case when rownum=20 then gap end) as col20, max(case when rownum=21 then gap end) as col21,max(case when rownum=22 then gap end) as col22, max(case when rownum=23 then gap end) as col23 from ( select a.id_start, b.day_of_calendar as gap, csum(1,1) as rownum from db_sok.testt a, sys_calendar.calendar b where b.day_of_calendar between a.id_start and a.id_end )X do a little tweaking in case you need something else...:)
encountered ")." was expecting one of "as" in teradata
I am getting above mentioned error while executing a query against Teradata Studio express select Union16.Item as Item, Union16.Description as Description, Union16.Nbr_Trans as Nbr_Trans, Union16.Unit_price as Unit_price, Union16.Amount as Amount, MIN(Union16.Amount) as Total_Amount_ from (select AL1.ticket_document_number as ticket_document_number, AL1.transaction_code as transaction_code, AL1.void_status as void_status, AL1.transaction_date as transaction_date, AL1.pnr_record_locator as pnr_record_locator, AL1.settlement_base_fare_amt as settlement_base_fare_amt, AL1.settlement_total_tax_amt as settlement_total_tax_amt, AL1.Multiplier as Multiplier, (AL1.Multiplier * 2) as Service_Incentive_Fee, (AL1.Multiplier * 2) as Transaction_Fee, (case when AL1.TRANSACTION_CODE = 'ET' and AL1.VOID_STATUS = 'N' then 1 when AL1.TRANSACTION_CODE = 'RF' and AL1.VOID_STATUS = 'N' then -1 else 0 end) as NetTrans, 0 as Comm_Amount, SUM(1) as Total_Trans, AL1.period_end_date as period_end_date, SUM(NetTrans * 2) as Service_Fee_due, AL1.airline_code as airline_code, 1 as Item, (('Supplier Link Service Incentive Fee # $' || '2') || '/Tix') as Description, 2 as Unit_price, SUM(NetTrans * 2) as Amount, SUM(NetTrans * 2) as Transaction_Fee_due, SUM((NetTrans * 2) + (NetTrans * 2)) as Total_Due, SUM(1) as Total_Total_Trans_, SUM(NetTrans * 2) as Total_Service_Fee_due_, SUM(NetTrans * 2) as Total_Transaction_Fee_due_, SUM((NetTrans * 2) + (NetTrans* 2)) as Total_Total_Due_, SUM(NetTrans) as Net_Trans, SUM(NetTrans) as Total_Net_Trans_, SUM(NetTrans) as Nbr_Trans from (SELECT LAST_DAY(AL1.period_end_date) AS PEDHEADER, AL1.TICKET_DOCUMENT_NUMBER as TICKET_DOCUMENT_NUMBER , (case when AL1.TRANSACTION_CODE = 'ET' and AL1.VOID_STATUS = 'N' then 1 when AL1.TRANSACTION_CODE = 'RF' and AL1.VOID_STATUS = 'N' then -1 else 0 end) as NetTrans, (case when AL1.VOID_STATUS = 'V' then 0 when AL1.VOID_STATUS <> 'V' and AL1.TRANSACTION_CODE in ('AT','ET') then 1 when AL1.VOID_STATUS <> 'V' and AL1.TRANSACTION_CODE = 'RF' then -1 when AL1.VOID_STATUS <> 'V' and AL1.TRANSACTION_CODE not in ('AT','ET','RF') then 0 end ) as Multiplier, AL1.TRANSACTION_CODE as TRANSACTION_CODE , AL1.VOID_STATUS as VOID_STATUS, cast(AL1.TRANSACTION_DATE as date) as TRANSACTION_DATE , AL1.PNR_RECORD_LOCATOR as PNR_RECORD_LOCATOR , AL1.SETTLEMENT_BASE_FARE_AMT as SETTLEMENT_BASE_FARE_AMT , AL1.SETTLEMENT_TOTAL_TAX_AMT as SETTLEMENT_TOTAL_TAX_AMT, cast(AL1.PERIOD_END_DATE as date) as PERIOD_END_DATE , AL1.AIRLINE_CODE as AIRLINE_CODE, airline_name as airline_name , SUM (1 ) as Total_records, 0 as Total_comm_due, SUM ((Multiplier * 2) ) as total_serv_inc_fee, SUM ((Multiplier * 2) ) as total_trans_fee_due, ((0 + SUM((Multiplier * 2) )) + SUM((Multiplier * 2) )) as Total_amt_due, 0 as Comm_Amount, (Multiplier * 2) as Service_Incentive_Fee, (Multiplier * 2) as Transaction_Fee, 1 as cntr FROM ODS_VIEWS.BOP_ARC_SETTLEMENT AL1 LEFT OUTER JOIN (select tabC.refvalcode, tabC.acct_cd, tabC.airline_name, tabC.refvaltm from (select ref1.refvalcode , max(case when ref1.refvalattribcode='numericCode' then ref1.refvalvalue else null end) as acct_cd, max(case when ref1.refvalattribcode='Codedescription' then ref1.refvalvalue else null end) as airline_name, tabA.refvaltm from bial.refvalue ref1, (select refvalcode, refvalvalue as airline_name, max(refvalueid) as "refvaltm" from bial.refvalue where refvalcodesetcode='Carrier' and refvalattribcode='Codedescription' group by refvalcode, refvalvalue) tabA where tabA.refvalcode = ref1.refvalcode and ref1.refvalcodesetcode='Carrier' group by ref1.refvalcode, tabA.refvaltm ) tabC , (select tabB.acct_cd, max(tabB.refvaltm) as "refvaltm" from (select ref1.refvalcode , max(case when ref1.refvalattribcode='numericCode' then ref1.refvalvalue else null end) as acct_cd, max(case when ref1.refvalattribcode='Codedescription' then ref1.refvalvalue else null end) as airline_name,tabA.refvaltm from bial.refvalue ref1, (select refvalcode, refvalvalue as airline_name, max(refvalueid) as "refvaltm" from bial.refvalue where refvalcodesetcode='Carrier' and refvalattribcode='Codedescription' group by refvalcode, refvalvalue) tabA where tabA.refvalcode = ref1.refvalcode and ref1.refvalcodesetcode='Carrier' group by ref1.refvalcode, tabA.refvaltm ) tabB group by tabB.acct_cd ) tabD where tabC.refvaltm = tabD.refvaltm and tabC.acct_cd = tabD.acct_cd ) AIRDET ON AL1.AIRLINE_CODE = AIRDET.acct_cd WHERE cast(AL1.PERIOD_END_DATE as date) BETWEEN 01-01-2015 AND 31-01-2015 AND AL1.FILE_TYPE='AM' AND AL1.SUPPLIER_LINK_INDICATOR='D' AND AIRLINE_NAME = 'Alaska Airlines' AND AL1.oltp_deleted_timestamp IS NULL group by AL1.period_end_date , AL1.TICKET_DOCUMENT_NUMBER, NetTrans , Multiplier , AL1.TRANSACTION_CODE, AL1.VOID_STATUS, AL1.TRANSACTION_DATE, AL1.PNR_RECORD_LOCATOR, AL1.SETTLEMENT_BASE_FARE_AMT, AL1.SETTLEMENT_TOTAL_TAX_AMT, AL1.AIRLINE_CODE, airline_name, Total_comm_due, Comm_Amount, Service_Incentive_Fee, Transaction_Fee, cntr, AL1.oltp_deleted_timestamp, AL1.SUPPLIER_LINK_INDICATOR, AL1.FILE_TYPE, AL1.AIRLINE_CODE ) ) Union16 group by Union16.Item, Union16.Description, Union16.Nbr_Trans, Union16.Unit_price, Union16.Amount order by Item asc, Description asc;
Simply you have close look at the query editor. You will notice that Teradata Studio marks the position of the error. There's an alias missing for the Derived Table before ) Union16