Need to append pivoted columns - pivot

my one query is --
select * from
(
select a.model_code,a.fiscal_quarter,a.scenario_no,a.gfcid,a.field_id,a.field_value
from ccar_input_data a,ccar_model_feed_map b
where a.model_code = b.model_code (+) and a.field_id = b.field_id (+)
and a.model_code = 'COMREPROJ'
and a.scenario_no = 2 and a.fiscal_quarter = '2014-Q1' and a.gfcid = 112000000
) src
pivot
(
max(field_value) for field_id in (10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61)
)
which gives me a.model_code,a.fiscal_quarter,a.scenario_no,a.gfcid,a.field_id,a.field_value and 10,11,...61 columns
My second query is ----
select * from
(
select d.field_id,d.field_value
from ccar14a_output_view d,ccar_model_feed_map b
where d.model_code = b.model_code and d.field_id = b.field_id
and d.model_code = 'COMREPROJ'
and d.scenario_no = 2 and d.fiscal_quarter = '2014-Q1' and d.gfcid = 112000000
) src
pivot
(
max(field_value) for field_id in (62,63,64,65,66,67,69)
)
which gives me column 62, 63,...69
I want to make the row as
a.model_code,a.fiscal_quarter,a.scenario_no,a.gfcid,a.field_id,a.field_value and 10,11,...61, 62,63...69
Pl help. I am not sure how can I join this two query...HELP NEEDED (I am very new to sql)

Try this:
select * from
(
select a.model_code,a.fiscal_quarter,a.scenario_no,a.gfcid,a.field_id,a.field_value
from ccar_input_data a,ccar_model_feed_map b
where a.model_code = b.model_code (+) and a.field_id = b.field_id (+)
and a.model_code = 'COMREPROJ'
and a.scenario_no = 2 and a.fiscal_quarter = '2014-Q1' and a.gfcid = 112000000
UNION ALL
select null, null,null, null, d.field_id,d.field_value
from ccar14a_output_view d,ccar_model_feed_map b
where d.model_code = b.model_code and d.field_id = b.field_id
and d.model_code = 'COMREPROJ'
and d.scenario_no = 2 and d.fiscal_quarter = '2014-Q1' and d.gfcid = 112000000
) src
pivot
(
max(field_value) for field_id in (10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61, 62,63,64,65,66,67,69)
)

Related

Couchbase - Case with subquery

I am going to write a case which has subquery, something like below, But I cannot get neither out put nor error:
select
case
when f.resGeo.isRural = true
then (select g.ID as geo_id
from bktsample.scpPC.GeoInfo g
where g.PROVINCE_ID = f.resGeo.province.id
and g.CITYES_ID = f.resGeo.countie.id
and g.PART_ID = f.resGeo.part.id
and g.CITYORCOUNTRY_ID = f.resGeo.countie.id
and g.VILLAGE_ID = f.resGeo.village.id)
when f.resGeo.isRural = false
then (select g.ID
from bktsample.scpPC.GeoInfo g
where g.PROVINCE_ID = f.resGeo.province.id
and g.CITYES_ID = f.resGeo.countie.id
and g.PART_ID = f.resGeo.part.id
and g.CITYORCOUNTRY_ID = f.resGeo.countie.id)
end as geo_id
from bktsample.scpPC.Family f;
PS: GEO is my collection, scpPC is my scope and bktsample is my bucket.
Each document in the Family collection should have returned one document of geo_id (either empty array or objects of ID)
Small change to your query:
CREATE INDEX ix1 ON bktsample.scpPC.Family(resGeo.province.id, resGeo.countie.id, resGeo.part.id, resGeo.countie.id, resGeo.village.id, resGeo.isRural);
CREATE INDEX ix2 ON bktsample.scpPC.GeoInfo(PROVINCE_ID, CITYES_ID, PART_ID, CITYORCOUNTRY_ID, VILLAGE_ID, ID);
SELECT
(SELECT g.ID AS geo_id
FROM bktsample.scpPC.GeoInfo AS g
WHERE g.PROVINCE_ID = f.resGeo.province.id
AND g.CITYES_ID = f.resGeo.countie.id
AND g.PART_ID = f.resGeo.part.id
AND g.CITYORCOUNTRY_ID = f.resGeo.countie.id
AND (f.resGeo.isRural == false OR g.VILLAGE_ID = f.resGeo.village.id))
) AS geo_ids
FROM bktsample.scpPC.Family f
WHERE f.resGeo.province.id IS NOT NULL;

User defined Hash partioning in RDD with key

Hello i would like to make my own hash function with the key being a column "arrival delay"
the code that i had at the moment is
# this is for the flights
def import_parse_rdd(data):
# create rdd
rdd = sc.textFile(data)
# remove the header
header = rdd.first()
rdd = rdd.filter(lambda row: row != header) #filter out header
# split by comma
split_rdd = rdd.map(lambda line: line.split(','))
row_rdd = split_rdd.map(lambda line: Row(
YEAR = int(line[0]),MONTH = int(line[1]),DAY = int(line[2]),DAY_OF_WEEK = int(line[3])
,AIRLINE = line[4],FLIGHT_NUMBER = int(line[5]),
TAIL_NUMBER = line[6],ORIGIN_AIRPORT = line[7],DESTINATION_AIRPORT = line[8],
SCHEDULED_DEPARTURE = line[9],DEPARTURE_TIME = line[10],DEPARTURE_DELAY = 0 if "".__eq__(line[11]) else float(line[11]),TAXI_OUT = 0 if "".__eq__(line[12]) else float(line[12]),
WHEELS_OFF = line[13],SCHEDULED_TIME = line[14],ELAPSED_TIME = 0 if "".__eq__(line[15]) else float(line[15]),AIR_TIME = 0 if "".__eq__(line[16]) else float(line[16]),DISTANCE = 0 if "".__eq__(line[17]) else float(line[17]),WHEELS_ON = line[18],TAXI_IN = 0 if "".__eq__(line[19]) else float(line[19]),
SCHEDULED_ARRIVAL = line[20],ARRIVAL_TIME = line[21],ARRIVAL_DELAY = 0 if "".__eq__(line[22]) else float(line[22]),DIVERTED = line[23],CANCELLED = line[24],CANCELLATION_REASON = line[25],AIR_SYSTEM_DELAY = line[26],
SECURITY_DELAY = line[27],AIRLINE_DELAY = line[28],LATE_AIRCRAFT_DELAY = line[29],WEATHER_DELAY = line[30])
)
return row_rdd
if i take flight_rdd.take(1)
[Row(YEAR=2015, MONTH=6, DAY=26, DAY_OF_WEEK=5, AIRLINE='EV', FLIGHT_NUMBER=4951, TAIL_NUMBER='N707EV', ORIGIN_AIRPORT='BHM', DESTINATION_AIRPORT='LGA', SCHEDULED_DEPARTURE='630', DEPARTURE_TIME='629', DEPARTURE_DELAY=-1.0, TAXI_OUT=13.0, WHEELS_OFF='642', SCHEDULED_TIME='155', ELAPSED_TIME=141.0, AIR_TIME=113.0, DISTANCE=866.0, WHEELS_ON='935', TAXI_IN=15.0, SCHEDULED_ARRIVAL='1005', ARRIVAL_TIME='950', ARRIVAL_DELAY=-15.0, DIVERTED='0', CANCELLED='0', CANCELLATION_REASON='', AIR_SYSTEM_DELAY='', SECURITY_DELAY='', AIRLINE_DELAY='', LATE_AIRCRAFT_DELAY='', WEATHER_DELAY='')]
is the output
i would like to make a user defined hash partitioning function with the key being the ARRIVAL_DELAY column.
If i could i would also like Min and Max value in the ARRIVAL_DELAY column to be used as a key to determine the distribution of the partition.
the furthest i have gone is that
flight_rdd.partitionBy(number of parts, key)
is what i understand
YEAR,MONTH,DAY,DAY_OF_WEEK,AIRLINE,FLIGHT_NUMBER,TAIL_NUMBER,ORIGIN_AIRPORT,DESTINATION_AIRPORT,SCHEDULED_DEPARTURE,DEPARTURE_TIME,DEPARTURE_DELAY,TAXI_OUT,WHEELS_OFF,SCHEDULED_TIME,ELAPSED_TIME,AIR_TIME,DISTANCE,WHEELS_ON,TAXI_IN,SCHEDULED_ARRIVAL,ARRIVAL_TIME,ARRIVAL_DELAY,DIVERTED,CANCELLED,CANCELLATION_REASON,AIR_SYSTEM_DELAY,SECURITY_DELAY,AIRLINE_DELAY,LATE_AIRCRAFT_DELAY,WEATHER_DELAY
2015,2,6,5,OO,6271,N937SW,FAR,DEN,1712,1701,-11,15,1716,123,117,95,627,1751,7,1815,1758,-17,0,0,,,,,,
2015,1,19,1,AA,1605,N496AA,DFW,ONT,1740,1744,4,15,1759,193,198,175,1188,1854,8,1853,1902,9,0,0,,,,,,
2015,3,8,7,NK,1068,N519NK,LAS,CLE,2220,2210,-10,12,2222,238,229,208,1824,450,9,518,459,-19,0,0,,,,,,
2015,9,21,1,AA,1094,N3EDAA,DFW,BOS,1155,1155,0,12,1207,223,206,190,1562,1617,4,1638,1621,-17,0,0,,,,,,
this is the unprocessed version of the data set

Custom virtual attribute to obtain a value from another table via PXDBScalar is not working correctly

For the AP Bill screen AP301000 I want to show the Customer for each line item in the 'Document Details' grid. To do this I am creating a customisation project with a custom virtual attribute on the APTran table.
I have tried many permutations of the attribute, the below is the latest.
[PXString]
[PXUIField(DisplayName="Customer", Enabled=false)]
[PXDBScalar(typeof(Search2<BAccountR.legalName,
InnerJoin<PMProject, On<PMProject.contractID, Equal<APTran.projectID>>,
InnerJoin<AR.Customer, On<PMProject.customerID, Equal<AR.Customer.bAccountID>>,
InnerJoin<BAccountR, On<AR.Customer.bAccountID, Equal<BAccountR.bAccountID>>>>>>))]
It 'should' return the customer legal name from the BAccount table through the relevant joins. However it returns a vendor name instead (not in the customer table).
The generated SQL code is below, the system has made a mess of it. For some reason it has an inner join to the vendor table, which is plainly not in my PXDBScalar attribute decoration.
How can I adjust the attribute decoration to obtain the customer name?
(SELECT TOP (1) [BAccount_BAccount].[legalname]
FROM ( [vendor] [BAccount_Vendor]
INNER JOIN [baccount] [BAccount_BAccount]
ON ( [BAccount_BAccount].[companyid] = 2 )
AND
[BAccount_BAccount].[deleteddatabaserecord] = 0
AND [BAccount_Vendor].[baccountid] =
[BAccount_BAccount].[baccountid]
LEFT JOIN [fsxvendor] [BAccount_FSxVendor]
ON ( [BAccount_FSxVendor].[companyid] = 2 )
AND
[BAccount_FSxVendor].[deleteddatabaserecord] = 0
AND [BAccount_Vendor].[baccountid] =
[BAccount_FSxVendor].[baccountid] )
INNER JOIN [contract] [PMProject]
ON ( [PMProject].[companyid] IN ( 1, 2 )
AND 8 = Substring([PMProject].[companymask],
1, 1) & 8 )
AND [PMProject].[deleteddatabaserecord] = 0
AND ( [PMProject].[companyid] IN ( 1, 2 )
AND 8 = Substring(
[PMProject].[companymask], 1, 1)
& 8
)
AND [PMProject].[deleteddatabaserecord] = 0
AND [PMProject].[contractid] =
[APTran_APTran].[projectid]
INNER JOIN ( [customer] [Customer_Customer]
INNER JOIN [baccount] [Customer_BAccount]
ON ( [Customer_BAccount].[companyid]
= 2 )
AND
[Customer_BAccount].[deleteddatabaserecord]
= 0
AND
[Customer_Customer].[baccountid] =
[Customer_BAccount].[baccountid]
LEFT JOIN [fsxcustomer]
[Customer_FSxCustomer]
ON (
[Customer_FSxCustomer].[companyid] = 2 )
AND
[Customer_FSxCustomer].[deleteddatabaserecord] = 0
AND [Customer_Customer].[baccountid] =
[Customer_FSxCustomer].[baccountid] )
ON [PMProject].[customerid] = [Customer_BAccount].[baccountid]
AND ( [Customer_Customer].[companyid] = 2 )
AND [Customer_Customer].[deleteddatabaserecord] = 0
INNER JOIN [baccount] [BAccountR]
ON ( [BAccountR].[companyid] = 2 )
AND [BAccountR].[deleteddatabaserecord] = 0
AND ( [BAccountR].[companyid] = 2 )
AND [BAccountR].[deleteddatabaserecord] = 0
AND [Customer_BAccount].[baccountid] =
[BAccountR].[baccountid]
WHERE ( [BAccount_Vendor].[companyid] = 2 )
AND [BAccount_Vendor].[deleteddatabaserecord] = 0
ORDER BY [BAccount_BAccount].[legalname]) AS [UsrCustomerName],
Try creating a new class for the BAccountR table and using the PXBreakInheritance attribute, and then use this new table to do your lookup.
namespace Custom.Extension
{
[PXBreakInheritance]
public class CustomBAccountR: BAccountR { }
public sealed class APTranExtension : PXCacheExtension<APTran>
{
#region ProjectCustomerID
public abstract class projectCustomerID : BqlInt.Field<projectCustomerID> { }
[PXInt]
[PXUIField(DisplayName="Customer Legal Name", Enabled=false)]
[PXSelector(typeof(SearchFor<CustomBAccountR.bAccountID>),
SubstituteKey = typeof(CustomBAccountR.acctCD),
DescriptionField = typeof(CustomBAccountR.legalName))]
[PXDBScalar(typeof(Search2<CustomBAccountR.bAccountID,
InnerJoin<PMProject,
On<PMProject.contractID.IsEqual<APTran.projectID>>,
InnerJoin<CustomBAccountR,
On<CustomBAccountR.bAccountID.IsEqual<PMProject.customerID>>>>,
Where<CustomBAccountR.bAccountID.IsNotNull>>))]
public int? ProjectCustomerID { get; set; }
#endregion
}
}
You can now use the description field in your ASPX to display out the value.

How to get table names from Presto query using presto-parser?

Not able to extract table names used within with clause, I'm using presto-parser version 0.226.
SqlParser sqlParser = new SqlParser();
String sql = "WITH dataset AS ( SELECT ROW('Bob', 38) AS users from tabb ) SELECT * FROM dataset";
Query query = (Query)sqlParser.createStatement(sql, ParsingOptions.builder().build());
QuerySpecification body = (QuerySpecification)query.getQueryBody();
System.out.println("From = " + body.getFrom().get());
/* Output
From = Table{dataset}
*/
Expected output
From = Table{dataset, tabb}

Could not add table 'SELECT('

Error given when adding query that runs in SQL developer but not in MS Query. Seems to not like my nested query.
Code I am using:
SELECT ORDER_DATE
,SALES_ORDER_NO
,CUSTOMER_PO_NUMBER
,DELIVER_TO
,STATUS
,ITEM_NUMBER
,DESCRIPTION
,ORD_QTY
,SUM(QUANTITY) AS ON_HAND
,PACKAGE_ID
,PACKAGE_STATUS
,MAX(TRAN_DATE) AS LAST_TRANSACTION
,MIN(DAYS) AS DAYS
FROM (
SELECT TRUNC(SH.ORDER_DATE) AS ORDER_DATE
,SH.SALES_ORDER_NO
,SH.CUSTOMER_PO_NUMBER
,SH.SHIP_CODE AS DELIVER_TO
,SH.STATUS
,SB.ITEM_NUMBER
,IM.DESCRIPTION
,SB.ORD_QTY
,BID.QUANTITY
,SPM.PACKAGE_ID
,CASE
WHEN SPM.SHIPPED = 'Y'
THEN 'SHIPPED'
WHEN SPM.STATUS = 'C'
THEN 'PACKED'
WHEN BID.QUANTITY IS NOT NULL
THEN 'AVAILABLE'
WHEN BID.QUANTITY IS NULL
THEN 'UNAVAILABLE'
END AS PACKAGE_STATUS
,CASE
WHEN SPM.SHIPPED = 'Y'
THEN TRUNC(SPM.BILLING_DATE)
WHEN SPM.STATUS = 'C'
THEN TRUNC(SPM.END_TIME)
WHEN BID.QUANTITY IS NOT NULL
THEN TRUNC(BID.ACTIVATION_TIME)
END AS TRAN_DATE
,CASE
WHEN SPM.SHIPPED = 'Y'
THEN ROUND(SYSDATE - SPM.BILLING_DATE, 0)
WHEN SPM.STATUS = 'C'
THEN ROUND(SYSDATE - SPM.END_TIME, 0)
WHEN BID.QUANTITY IS NOT NULL
THEN ROUND(SYSDATE - BID.ACTIVATION_TIME, 0)
END AS DAYS
FROM SO_HEADER SH
LEFT JOIN SO_BODY SB ON SB.SO_HEADER_TAG = SH.SO_HEADER_TAG
LEFT JOIN SO_PACKAGE_MASTER SPM ON SPM.PACKAGE_ID = SB.PACKAGE_ID
LEFT JOIN ITEM_MASTER IM ON IM.ITEM_NUMBER = SB.ITEM_NUMBER
LEFT JOIN V_BIN_ITEM_DETAIL BID ON BID.ITEM_NUMBER = SB.ITEM_NUMBER
WHERE SH.ORDER_TYPE = 'MSR'
AND (
SB.REASON_CODE IS NULL
OR SB.REASON_CODE NOT LIKE 'CANCEL%'
)
AND (
IM.DESCRIPTION NOT LIKE '%CKV%'
OR IM.DESCRIPTION IS NULL
AND IM.ITEM_NUMBER IS NOT NULL
)
)
WHERE PACKAGE_STATUS <> 'SHIPPED'
GROUP BY ORDER_DATE
,SALES_ORDER_NO
,CUSTOMER_PO_NUMBER
,DELIVER_TO
,STATUS
,ITEM_NUMBER
,DESCRIPTION
,ORD_QTY
,PACKAGE_ID
,PACKAGE_STATUS
ORDER BY (
CASE PACKAGE_STATUS
WHEN 'AVAILABLE'
THEN 1
WHEN 'UNAVAILABLE'
THEN 2
WHEN 'PACKED'
THEN 3
END
)
,LAST_TRANSACTION;
Is there any option I can select that will allow me to run this query?

Resources