Is anyone familiar with an online service where I can paste some code, and it tells me
If all my brackets are in place - Not missing/adding a close bracket?
According the language syntax - that the code is right?
I agree that most editors color scheme/and or give you an error. This specific one does not :-) It's called: Notepad ^^ - Which I use a lot, but still. If such a service exists - it can help no matter what IDE you use. If you have a piece of code you want to check - and don't feel like opening the IDE
Example (There is a missing bracket on the '3' part):
IF( OR( ISBLANK(BillingCountry), UPPER(BillingCountry) = 'UNITED STATES' , UPPER(BillingCountry) = 'USA', UPPER(BillingCountry) = 'US', UPPER(BillingCountry) = 'U.S.', UPPER(BillingCountry) = 'U.S.A.' ),
IF ( OR( BillingState = 'CA' , BillingState = 'WA' , BillingState = 'OR' , BillingState = 'ID' , BillingState = 'NV' , BillingState = 'MT' , BillingState = 'WY' , BillingState = 'CO' , BillingState = 'UT' , BillingState = 'AZ' , BillingState = 'NM'),
"1 - West"
,
IF ( OR( BillingState = 'IL' , BillingState = 'ND' , BillingState = 'SD' , BillingState = 'MI' , BillingState = 'NE' , BillingState = 'IA' , BillingState = 'KS' , BillingState = 'OK' , BillingState = 'TX' , BillingState = 'IL' , BillingState = 'MN' , BillingState = 'OH' , BillingState = 'WI' , BillingState = 'IN' , BillingState = 'CANADA'),
"2 - MIDWEST"
,
IF( OR( BillingState = 'NY' , BillingState = 'ME' , BillingState = 'NH' , BillingState = 'MA' , BillingState = 'RI' , BillingState = 'CT' , BillingState = 'NJ' , BillingState = 'DE' , BillingState = 'D.C.' , BillingState = 'VA' , BillingState = 'WVA' , BillingState = 'NC' , BillingState = 'SC' , BillingState = 'GA' , BillingState = 'FL' , BillingState = 'VT' , BillingState = 'PA' , BillingState = 'MD' , BillingState = 'KY' , BillingState = 'TN' , BillingState = 'AR' , BillingState = 'AL' , BillingState = 'MS' , BillingState = 'LS'),
"3 - East"
,
"Unknown" ) )
,
IF( LEN(BillingCountry) > 0, "4 - International", "Unknown")
)
This might help ideone.com. But you could just install Notepad++
This website might help to satisfy the 1st point : http://www.balancebraces.com/
This website has options to check Braces: { },Parentheses: ( ), Brackets: [ ], and Tags: < > and color them for easy identification.
Related
I have a query that I am using in a PL-SQL view. The view contains a function called Jde_date that takes a date parameter in Julian format and converts it to Gregorian, it works perfectly.
The query is as follows:
select Cedula
, Nombre
, f_nacimiento
, fec_previa
, round(dia) Dia_faltante
, sexo
, Edad
, Fecha_Ingreso
, Nombre_Empresa
, NombreCargo
, Nom_Oficio
, UniddeNegocio
, DesUniNeg
, NombFte
, TipoContrato
, TipoSueldo
, SalHora
, Salario
, cesantias
, prima
, vacaciones
, Total_Prestaciones
, estado
, grupo_cotizante
, tipo_cotizante
, fondo_pensiones
from
(
SELECT distinct total.Cedula
, total.NOMBRE
, total.f_nacimiento
, total.fec_previa
, to_date(total.fec_previa) - sysdate as dia
, total.sexo
, total.Edad
, total.Fecha_Ingreso
, total.Nombre_Empresa
, total.NombreCargo
, total.Nom_Oficio
, total.UniddeNegocio
, Total.DesUniNeg
, total.NombFte
, total.TipoContrato
, total.TipoSueldo
, total.SalHora
, total.Salario
, total.cesantias
, total.prima
, total.vacaciones
, total.Total_Prestaciones
, total.estado
, total.grupo_cotizante
, total.tipo_cotizante
, total.fondo_pensiones
from
(
SELECT * FROM (
select
"F060116"."YAAN8" as AN8,
TRIM("F060116"."YAALPH") as NOMBRE,
TRIM("F060116"."YASSN") as Cedula,
TIPODOC.NNQ70BRTXT as tipo_documento,
"F0010"."CCNAME" as Nombre_Empresa,
"OFI"."DRDL01" as Nom_Oficio,
to_char(integral.JDE_DATE(YADSI), 'dd/mm/yyyy') as Fecha_Ingreso,
"F0005"."DRDL01" as estado,
TRIM(tsal.drdl01) as TipoSueldo,
"Tcon1".drdl01 as TipoContrato
,round(("F060116".yasal/100)/12) as Salario
,round(("F060116".yasal/100)/12/240) as SalHora
,round((("F060116".yasal/100)/12) * 0.0833) as CESANTIAS
,round((("F060116".yasal/100)/12) * 0.0833) as PRIMA
,round((("F060116".yasal/100)/12) * 0.0416) as VACACIONES
,round(((("F060116".yasal/100)/12) * 0.0833) + round((("F060116".yasal/100)/12) * 0.0833) + round((("F060116".yasal/100)/12) * 0.0416)) AS TOTAL_PRESTACIONES
,yasex as sexo
,to_char(integral.JDE_DATE(yadob), 'dd/mm/yyyy') as f_nacimiento
,case
when yasex = 'F' then to_char(add_months(add_months(to_char(integral.JDE_DATE(yadob), 'dd/mm/yyyy'),12*54),-4),'dd/mm/yyyy')
when yasex = 'M' THEN to_char(add_months(add_months(to_char(integral.JDE_DATE(yadob), 'dd/mm/yyyy'),12*59),-4),'dd/mm/yyyy')
END as fec_previa
,TRIM("Fpen"."DRDL01") as fondo_pensiones
,TRIM("gcoti"."DRDL01") as grupo_cotizante
,TRIM("tcoti"."DRDL01") as tipo_cotizante
,to_char(round((sysdate-integral.JDE_DATE(yadob))/365,2)) Edad
,"fte"."DRDL01" as NombFte
,to_char( integral.JDE_DATE(YADT) , 'dd/mm/yyyy') as FechaRetiro
,"F060116"."YAMCU" as UniddeNegocio
,"F0006"."MCDL01" as DesUniNeg
,"F5"."DRDL01" as NombreCargo
from "PRODDTA"."F060116" "F060116"
left join PRODCTL.F0005 TSAL on trim(TSAL.DRKY) = trim(YAP011) --tipo sueldo
and TSAL.DRSY = '06'
and TSAL.DRRT = '11'
left join "PRODCTL"."F0005" "fte" on rtrim(ltrim("fte"."DRKY")) = rtrim("F060116"."YAP005") --Frente trabajo
and "fte"."DRSY" = '06'
AND "fte"."DRRT" = '05'
left join "PRODDTA"."F0006" "F0006" on "F060116"."YAMCU"="F0006"."MCMCU"
left join "PRODCTL"."F0005" "F0005" on rtrim("F060116"."YAPAST")= ltrim("F0005"."DRKY") --Estado Contrato
and "F0005"."DRSY" = '06'
AND "F0005"."DRRT" = 'PS'
left join "PRODDTA"."F0010" "F0010" on "F060116"."YAHMCO" = "F0010"."CCCO"
left join "PRODCTL"."F0005" "F5" on rtrim("F060116"."YAJBCD") = ltrim("F5"."DRKY") --cargo
and "F5"."DRSY" = '06'
AND "F5"."DRRT" = 'G'
left join "PRODCTL"."F0005" "GPO" on rtrim(ltrim("GPO"."DRKY")) = rtrim("F060116"."YAP001") --Grupo de trabajo
and "GPO"."DRSY" = '06'
AND "GPO"."DRRT" = '01'
left join "PRODCTL"."F0005" "OFI" on rtrim(ltrim("OFI"."DRKY")) = rtrim("F060116"."YAP002") --Oficio
and "OFI"."DRSY" = '06'
AND "OFI"."DRRT" = '02'
left join "PRODCTL"."F0005" "fte" on rtrim(ltrim("fte"."DRKY")) = rtrim("F060116"."YAP005") --Ubicacion
and "fte"."DRSY" = '06'
AND "fte"."DRRT" = '05'
left join "PRODCTL"."F0005" "Tcon" on rtrim(ltrim("Tcon"."DRKY")) = rtrim("F060116"."YAP004") --Tipo jornada trabajo
and "Tcon"."DRSY" = '06'
AND "Tcon"."DRRT" = '04'
left join "PRODCTL"."F0005" "Tcon1" on rtrim(ltrim("Tcon1"."DRKY")) = rtrim("F060116"."YAP013") --Tipo de contrato
and "Tcon1"."DRSY" = '06'
AND "Tcon1"."DRRT" = '13'
left join "PRODCTL"."F0005" "Fpen" on rtrim(ltrim("Fpen"."DRKY")) = rtrim("F060116"."YAP017") --Fondo pensiones
and "Fpen"."DRSY" = '06'
AND "Fpen"."DRRT" = '17'
left join "PRODCTL"."F0005" "gcoti" on rtrim(ltrim("gcoti"."DRKY")) = rtrim("F060116"."YAP019") --Grupo cotizante
and "gcoti"."DRSY" = '06'
AND "gcoti"."DRRT" = '19'
left join "PRODCTL"."F0005" "tcoti" on rtrim(ltrim("tcoti"."DRKY")) = rtrim("F060116"."YAP020") --Tipo cotizante
and "tcoti"."DRSY" = '06'
AND "tcoti"."DRRT" = '21'
left join integral.REP_RENOVACION_CONTRATOS rct on trim(rct.CEDULA) = trim("F060116".YASSN)
left join PRODDTA.f01151 correoe on trim(correoe.eaan8) = trim(F060116.yaan8)
and trim(EAIDLN) = '0'
LEFT JOIN proddta.FQ70C014 TIPODOC ON trim(TIPODOC.NNAN8) = trim(F060116.yaan8)
AND NNPN = 0
) me
left join (
select ALADD1 DIRECCION, alan8 from proddta.F0116
where (ALUPMJ,alan8) in
(select max(ALUPMJ),alan8
from proddta.F0116
group by alan8)
group by ALADD1 , alan8 ) direcci
on trim(ME.an8) = trim(direcci.alan8)
left join (
select alan8,drdl01 departamento, ALCTY1 ciudad from proddta.F0116
LEFT JOIN PRODCTL.F0005
ON TRIM(DRKY) = trim(ALADDS)
and DRSY = '00'
AND DRRT = 'S'
where (ALEFTB,alan8) in
(select max(ALEFTB),alan8
from proddta.F0116
group by alan8)
group by alan8 , drdl01,ALCTY1
) ciu
on trim(ciu.alan8) = trim(ME.an8)
left join (
select tel.wpph1 telefono, tel.wpan8
from proddta.F0115 tel
where (tel.WPRCK7,tel.wpan8) in
(select max(tel.WPRCK7),tel.wpan8
from proddta.F0115 tel
where trim(tel.WPPHTP) = 'TEL'
group by tel.wpan8)
group by tel.wpph1 , tel.wpan8
) telt
on trim(ME.an8) = trim(telt.wpan8)
left join (
select celu.wpph1 celular, celu.wpan8
from proddta.F0115 celu
where (celu.WPRCK7,celu.wpan8) in
(select max(celu.WPRCK7),celu.wpan8
from proddta.F0115 celu
where trim(celu.WPPHTP) = 'MOV'
group by celu.wpan8)
group by celu.wpph1 , celu.wpan8
) celut
on trim(ME.an8) = trim(celut.wpan8) ) total
where
((total.estado = 'Active' and total.grupo_cotizante = 'Dependiente' and total.tipo_cotizante ='N/A' and total.sexo ='M'
or (total.estado = 'Active' and total.grupo_cotizante = 'Dependiente' and total.tipo_cotizante ='Depend. pens por vejez activo' and total.sexo ='M')
or (total.estado = 'Active' and total.grupo_cotizante = 'Dependiente' and total.tipo_cotizante ='Cotizante con requisitos cumpl' and total.sexo ='M' )
or (total.estado = 'Active' and total.grupo_cotizante = 'Dependiente' and total.tipo_cotizante ='Cotizante a quién reconoció In' and total.sexo ='M' )
and total.sexo = 'M') AND total.sexo = 'M' and total.edad >=57 and total.edad <=59)
OR
((total.estado = 'Active' and total.grupo_cotizante = 'Dependiente' and total.tipo_cotizante ='N/A' and total.sexo ='F'
or (total.estado = 'Active' and total.grupo_cotizante = 'Dependiente' and total.tipo_cotizante ='Depend. pens por vejez activo' and total.sexo ='F')
or (total.estado = 'Active' and total.grupo_cotizante = 'Dependiente' and total.tipo_cotizante ='Cotizante con requisitos cumpl' and total.sexo ='F' )
or (total.estado = 'Active' and total.grupo_cotizante = 'Dependiente' and total.tipo_cotizante ='Cotizante a quién reconoció In' and total.sexo ='F' )
and total.sexo = 'F') and total.sexo = 'F' and total.edad >=52 and total.edad <=54)
ORDER BY 7 desc)
In the sqldeveolper it works correctly, it returns the information without problems, in the preview of reporting services the same.
But when I publish the report I get the error ORA-01843: not a valid month.
How can I solve that?
to_date(total.fec_previa) - sysdate as dia
You have not specified the format model you are using in the conversion and are relying on the NLS_DATE_FORMAT session parameter to be correct; instead, you should be explicit about the format model you are using:
to_date(total.fec_previa, 'dd/mm/yyyy') - sysdate as dia
I have a stored procedures with three different queries, that querys return values depending of received variable.
They basically return same data, just change WHERE clauses:
Query:
IF #DesignType = 'BDCD'
BEGIN
SELECT
[DT].[DesignTypeGuid]
, [DT].[Name]
, [DT].[Abbreviation]
, CONVERT(BIT , IIF([DT].[DesignTypeGuid] = #ContractedDocument , 1 , 0)) AS [EnforceBaseOnPrevious]
, [DT].[Order]
FROM [DesignType] AS [DT]
WHERE [DT].[IsActive] = 1
AND ([DT].[DesignTypeGuid] != IIF(#DesignTypeGuid = #ContractedDocument , #BidDocument , #ContractedDocument))
AND [DT].[DesignTypeGuid] != #ChangeOrder
AND [DT].[DesignTypeGuid] != #AddedPart
ORDER BY
[DT].[Order]
END
ELSE
BEGIN
IF #DesignType = 'CO'
BEGIN
SELECT
[DT].[DesignTypeGuid]
, [DT].[Name]
, [DT].[Abbreviation]
, 0 AS [EnforceBaseOnPrevious]
, [DT].[Order]
FROM [DesignType] AS [DT]
WHERE [DT].[IsActive] = 1
AND [DT].[DesignTypeGuid] = #ChangeOrder
ORDER BY
[DT].[Order]
END
ELSE
BEGIN
IF #DesignType = 'AP'
BEGIN
SELECT
[DT].[DesignTypeGuid]
, [DT].[Name]
, [DT].[Abbreviation]
, 0 AS [EnforceBaseOnPrevious]
, [DT].[Order]
FROM [DesignType] AS [DT]
WHERE [DT].[IsActive] = 1
AND [DT].[DesignTypeGuid] = #AddedPart
ORDER BY
[DT].[Order]
END
Is there any way to optimize this and do it in a simple SELECT instead do 3 differents depending of DesignType variable? Regards
Not sure about the performance , but the query could definitely be written as
SELECT
[DT].[DesignTypeGuid]
, [DT].[Name]
, [DT].[Abbreviation]
, CASE #DesignType WHEN 'BDCD' THEN CONVERT(BIT , IIF([DT].[DesignTypeGuid] = #ContractedDocument , 1 , 0)) ELSE 0 END AS [EnforceBaseOnPrevious]
, [DT].[Order]
FROM [DesignType] AS [DT]
WHERE [DT].[IsActive] = 1
AND (
(
#DesignType = 'BDCD' AND [DT].[DesignTypeGuid] NOT IN ( IIF(#DesignTypeGuid = #ContractedDocument , #BidDocument , #ContractedDocument),#ChangeOrder,#AddedPart)
)
OR
(
#DesignType = 'CO' AND [DT].[DesignTypeGuid] = #ChangeOrder
)
OR
(
#DesignType = 'AP' AND [DT].[DesignTypeGuid] = #AddedPart
)
)
ORDER BY
[DT].[Order]
While running the below SQL I'm getting an error:
Missing parenthesis
SELECT DISTINCT
RPH.transport_mode, PCP.air_export_ind, PCP.air_import_ind
FROM
RATE_PROFILE_HEADER RPH, PARTNER_CHARGE_PROFILE PCP
WHERE
PCP.charge_code = RPH.Charge_code
AND PCP.charge_calculation_method = 'R'
AND RPH.RP_RECORD_ID = PCP.CHARGE_RATEPROF_RECORD_ID
AND CASE '&psTransport_mode'
WHEN 'A'
THEN (DECODE(RPH.transport_mode, 'ALL', 'A', RPH.transport_mode) = 'A') AND (PCP.air_export_ind = 'Y' or PCP.air_import_ind = 'Y')
WHEN 'M'
THEN (DECODE(RPH.transport_mode, 'ALL', 'M', RPH.transport_mode) = 'M') AND (PCP.air_export_ind = 'Y' or PCP.air_import_ind = 'Y')
ELSE NULL
END
I want to pass multiple conditions in THEN for WHERE clause.
Should I have to use an IF statement?
How about not using a CASE statement at all. It seems like you just need a nested conditional:
Select
distinct
RPH.transport_mode,
PCP.air_export_ind,
PCP.air_import_ind
from
RATE_PROFILE_HEADER RPH,
PARTNER_CHARGE_PROFILE PCP
where
PCP.charge_code = RPH.Charge_code
AND PCP.charge_calculation_method = 'R'
AND RPH.RP_RECORD_ID = PCP.CHARGE_RATEPROF_RECORD_ID
AND
(
'&psTransport_mode' = 'A' AND ( Decode(RPH.transport_mode,'ALL','A',RPH.transport_mode) = 'A') AND (PCP.air_export_ind = 'Y' or PCP.air_import_ind = 'Y')
OR
'&psTransport_mode' = 'M' AND ( Decode(RPH.transport_mode,'ALL','M',RPH.transport_mode) = 'M') AND (PCP.air_export_ind = 'Y' or PCP.air_import_ind = 'Y')
)
Let me preface this by saying I'm working in SSMS2016, and that my SQL is not the greatest.
I have a query with quite a few joins, which runs in slow-but-bearable 20-odd seconds.
I'd like to add a column which is basically the max value of the row_number in the CCR subquery (the 2 lines commented out in the code), however it massively impacts query time (60+ seconds for 1000 records). Is there any way I can get the required column without the huge jump in run time, and any way I can clean up the query in general?
SELECT DISTINCT AWI.*
FROM
(SELECT
4 'SORT'
, CCR.URN
, CCR.SPECIAL_COMMENT
, SL.SCHEDULED_SHIP_DATE
, CASE WHEN SL.LOAD_CLOSED = 'Y' THEN SL.ACTUAL_SHIP_DATE_TIME ELSE NULL END AS 'ACTUAL_SHIP_DATE'
, SL.LOAD_CLOSED
, SH.SHIPPING_LOAD_NUM 'SHIPPING_LOAD'
-- , MAX(CCR.RNK) OVER(PARTITION BY CCR.SHIPPING_LOAD_NUM) 'CTNS_ON_LOAD'
, SH.CARRIER
, SH.CARRIER_SERVICE 'SERVICE'
, IWI.PARENT_INSTR 'WORK_UNIT'
, IWI.CONDITION 'WORK_STATUS'
, IWI.WORK_TYPE
, sl.LOCATION 'DOCK_DOOR'
, CCR.CONTAINER_ID
, SC.LOCATION 'FROM_LOC'
, IWI.TO_LOC
, SH.SHIP_TO
, G.DESCRIPTION 'COUNTRY'
, case when SL.LEADING_STS = '201' then '[201] In Wave'
when SL.LEADING_STS = '300' then '[300] Picking Pending'
when SL.LEADING_STS = '301' then '[301] In Picking'
when SL.LEADING_STS = '400' then '[400] Packing Pending'
when SL.LEADING_STS = '401' then '[401] In Packing'
when SL.LEADING_STS = '500' then '[500] Pack and Hold Pending'
when SL.LEADING_STS = '600' then '[600] Staging Pending'
when SL.LEADING_STS = '650' then '[650] Loading Pending'
when SL.LEADING_STS = '700' then '[700] Ship Confirm Pending'
when SL.LEADING_STS = '800' then '[800] Load Confirm Pending'
when SL.LEADING_STS = '900' then '[900] Closed'
END AS 'LEADING_STS'
, case when SL.TRAILING_STS = '201' then '[201] In Wave'
when SL.TRAILING_STS = '300' then '[300] Picking Pending'
when SL.TRAILING_STS = '301' then '[301] In Picking'
when SL.TRAILING_STS = '400' then '[400] Packing Pending'
when SL.TRAILING_STS = '401' then '[401] In Packing'
when SL.TRAILING_STS = '500' then '[500] Pack and Hold Pending'
when SL.TRAILING_STS = '600' then '[600] Staging Pending'
when SL.TRAILING_STS = '650' then '[650] Loading Pending'
when SL.TRAILING_STS = '700' then '[700] Ship Confirm Pending'
when SL.TRAILING_STS = '800' then '[800] Load Confirm Pending'
when SL.TRAILING_STS = '900' then '[900] Closed'
END AS 'TRAILING_STS'
, CCR.CONTAINER_TYPE
, DIMS.WEIGHT
, DIMS.LENGTH
, DIMS.WIDTH
, DIMS.HEIGHT
FROM
IA_WORK_INSTRUCTION IWI
JOIN
(SELECT INTERNAL_SHIPMENT_NUM, SHIP_TO, CARRIER, CARRIER_SERVICE, SHIPPING_LOAD_NUM, SHIP_TO_COUNTRY FROM SHIPMENT_HEADER) SH
ON SH.INTERNAL_SHIPMENT_NUM = IWI.INTERNAL_NUM
JOIN
(SELECT c1.INTERNAL_WORK_INST_NUM, c1.CONTAINER_ID, c1.URN, c1.INTERNAL_CONTAINER_NUM, c1.SPECIAL_COMMENT, sc1.container_type, H1.SHIPPING_LOAD_NUM
-- , ROW_NUMBER() OVER(PARTITION BY H1.SHIPPING_LOAD_NUM ORDER BY C1.CONTAINER_ID DESC) 'RNK'
FROM CARTON_CALLOFF_REQUEST c1
join shipping_container sc1
on sc1.internal_container_num = c1.internal_container_num
JOIN SHIPMENT_HEADER H1
ON H1.INTERNAL_SHIPMENT_NUM = SC1.INTERNAL_SHIPMENT_NUM
) CCR
ON CCR.INTERNAL_WORK_INST_NUM = IWI.PARENT_INSTR
JOIN
(SELECT DISTINCT IDENTIFIER, DESCRIPTION FROM GENERIC_CONFIG_DETAIL WHERE RECORD_TYPE = 'COUNTRY') G
ON G.IDENTIFIER = SH.SHIP_TO_COUNTRY
JOIN
(SELECT INTERNAL_SHIPMENT_NUM, PARENT, LOCATION FROM SHIPPING_CONTAINER) SC
ON SC.PARENT = CCR.INTERNAL_CONTAINER_NUM AND SC.INTERNAL_SHIPMENT_NUM = SH.INTERNAL_SHIPMENT_NUM
JOIN
(SELECT DISTINCT INTERNAL_CONTAINER_NUM, WEIGHT, LENGTH, WIDTH, HEIGHT FROM SHIPPING_CONTAINER WHERE CONTAINER_ID IS NOT NULL) DIMS
ON DIMS.INTERNAL_CONTAINER_NUM = CCR.INTERNAL_CONTAINER_NUM
JOIN
(SELECT s1.INTERNAL_LOAD_NUM, s1.LEADING_STS, s1.TRAILING_STS, s1.LOAD_CLOSED, s1.ACTUAL_SHIP_DATE_TIME, s1.SCHEDULED_SHIP_DATE, s1.DOCK_DOOR, l1.location FROM SHIPPING_LOAD s1 join location l1 on l1.object_id = s1.dock_door WHERE L1.TEMPLATE_FIELD1 = 'DOOR') SL
ON SL.INTERNAL_LOAD_NUM = SH.SHIPPING_LOAD_NUM
) AWI
WHERE AWI.WORK_TYPE = 'CALL OFF PICK'
You might try replacing the Partition and Over() with Group By.
Please let us know if you face any issues even after adding the Group By.
SELECT DISTINCT AWI.*
FROM
(SELECT
4 'SORT'
, CCR.URN
, CCR.SPECIAL_COMMENT
, SL.SCHEDULED_SHIP_DATE
, CASE WHEN SL.LOAD_CLOSED = 'Y' THEN SL.ACTUAL_SHIP_DATE_TIME ELSE NULL END AS 'ACTUAL_SHIP_DATE'
, SL.LOAD_CLOSED
, SH.SHIPPING_LOAD_NUM 'SHIPPING_LOAD'
, MAX(CCR.RNK)
, SH.CARRIER
, SH.CARRIER_SERVICE 'SERVICE'
, IWI.PARENT_INSTR 'WORK_UNIT'
, IWI.CONDITION 'WORK_STATUS'
, IWI.WORK_TYPE
, sl.LOCATION 'DOCK_DOOR'
, CCR.CONTAINER_ID
, SC.LOCATION 'FROM_LOC'
, IWI.TO_LOC
, SH.SHIP_TO
, G.DESCRIPTION 'COUNTRY'
, case when SL.LEADING_STS = '201' then '[201] In Wave'
when SL.LEADING_STS = '300' then '[300] Picking Pending'
when SL.LEADING_STS = '301' then '[301] In Picking'
when SL.LEADING_STS = '400' then '[400] Packing Pending'
when SL.LEADING_STS = '401' then '[401] In Packing'
when SL.LEADING_STS = '500' then '[500] Pack and Hold Pending'
when SL.LEADING_STS = '600' then '[600] Staging Pending'
when SL.LEADING_STS = '650' then '[650] Loading Pending'
when SL.LEADING_STS = '700' then '[700] Ship Confirm Pending'
when SL.LEADING_STS = '800' then '[800] Load Confirm Pending'
when SL.LEADING_STS = '900' then '[900] Closed'
END AS 'LEADING_STS'
, case when SL.TRAILING_STS = '201' then '[201] In Wave'
when SL.TRAILING_STS = '300' then '[300] Picking Pending'
when SL.TRAILING_STS = '301' then '[301] In Picking'
when SL.TRAILING_STS = '400' then '[400] Packing Pending'
when SL.TRAILING_STS = '401' then '[401] In Packing'
when SL.TRAILING_STS = '500' then '[500] Pack and Hold Pending'
when SL.TRAILING_STS = '600' then '[600] Staging Pending'
when SL.TRAILING_STS = '650' then '[650] Loading Pending'
when SL.TRAILING_STS = '700' then '[700] Ship Confirm Pending'
when SL.TRAILING_STS = '800' then '[800] Load Confirm Pending'
when SL.TRAILING_STS = '900' then '[900] Closed'
END AS 'TRAILING_STS'
, CCR.CONTAINER_TYPE
, DIMS.WEIGHT
, DIMS.LENGTH
, DIMS.WIDTH
, DIMS.HEIGHT
FROM
IA_WORK_INSTRUCTION IWI
JOIN
(SELECT INTERNAL_SHIPMENT_NUM, SHIP_TO, CARRIER, CARRIER_SERVICE, SHIPPING_LOAD_NUM, SHIP_TO_COUNTRY FROM SHIPMENT_HEADER) SH
ON SH.INTERNAL_SHIPMENT_NUM = IWI.INTERNAL_NUM
JOIN
(SELECT c1.INTERNAL_WORK_INST_NUM, c1.CONTAINER_ID, c1.URN, c1.INTERNAL_CONTAINER_NUM, c1.SPECIAL_COMMENT, sc1.container_type, H1.SHIPPING_LOAD_NUM
, ROW_NUMBER() OVER(PARTITION BY H1.SHIPPING_LOAD_NUM ORDER BY C1.CONTAINER_ID DESC) 'RNK'
FROM CARTON_CALLOFF_REQUEST c1
join shipping_container sc1
on sc1.internal_container_num = c1.internal_container_num
JOIN SHIPMENT_HEADER H1
ON H1.INTERNAL_SHIPMENT_NUM = SC1.INTERNAL_SHIPMENT_NUM
) CCR
ON CCR.INTERNAL_WORK_INST_NUM = IWI.PARENT_INSTR
JOIN
(SELECT DISTINCT IDENTIFIER, DESCRIPTION FROM GENERIC_CONFIG_DETAIL WHERE RECORD_TYPE = 'COUNTRY') G
ON G.IDENTIFIER = SH.SHIP_TO_COUNTRY
JOIN
(SELECT INTERNAL_SHIPMENT_NUM, PARENT, LOCATION FROM SHIPPING_CONTAINER) SC
ON SC.PARENT = CCR.INTERNAL_CONTAINER_NUM AND SC.INTERNAL_SHIPMENT_NUM = SH.INTERNAL_SHIPMENT_NUM
JOIN
(SELECT DISTINCT INTERNAL_CONTAINER_NUM, WEIGHT, LENGTH, WIDTH, HEIGHT FROM SHIPPING_CONTAINER WHERE CONTAINER_ID IS NOT NULL) DIMS
ON DIMS.INTERNAL_CONTAINER_NUM = CCR.INTERNAL_CONTAINER_NUM
JOIN
(SELECT s1.INTERNAL_LOAD_NUM, s1.LEADING_STS, s1.TRAILING_STS, s1.LOAD_CLOSED, s1.ACTUAL_SHIP_DATE_TIME, s1.SCHEDULED_SHIP_DATE, s1.DOCK_DOOR, l1.location FROM SHIPPING_LOAD s1 join location l1 on l1.object_id = s1.dock_door WHERE L1.TEMPLATE_FIELD1 = 'DOOR') SL
ON SL.INTERNAL_LOAD_NUM = SH.SHIPPING_LOAD_NUM
GROUP BY
CCR.URN
, CCR.SPECIAL_COMMENT
, SL.SCHEDULED_SHIP_DATE
, CASE WHEN SL.LOAD_CLOSED = 'Y' THEN SL.ACTUAL_SHIP_DATE_TIME ELSE
NULL END
, SL.LOAD_CLOSED
, SH.SHIPPING_LOAD_NUM
, SH.CARRIER
, SH.CARRIER_SERVICE
, IWI.PARENT_INSTR
, IWI.CONDITION
, IWI.WORK_TYPE
, sl.LOCATION
, CCR.CONTAINER_ID
, SC.LOCATION
, IWI.TO_LOC
, SH.SHIP_TO
, G.DESCRIPTION
, SL.LEADING_STS
, TRAILING_STS
, CCR.CONTAINER_TYPE
, DIMS.WEIGHT
, DIMS.LENGTH
, DIMS.WIDTH
, DIMS.HEIGHT
) AWI
WHERE AWI.WORK_TYPE = 'CALL OFF PICK'
Could anyone please suggest what is the error in the below query? Actually I want to remove all duplicates from the select query output and fetch only unique rows. Thanks in advance.
select *
from (
select ROW_NUMBER() over (
partition by Dep
, tariffkode
, LinkTariffType
, poliata
, poliatavia
, podiata
, podiatavia
, PreCarriage
, PortTerminalId
, Product
, RoutingOrder
, PrepaidCollect
, isnull(description, '')
, ScaleCalcCode
, isnull(scalefrom, 0)
, isnull(scaleto, 0)
, CurrencyCode
, Base order by LinkTariffType desc
) Record
, *
from (
select tn.LinkTariffType
, tn.Dep
, tn.POLIata
, tn.POLIatavia
, tn.PODIata
, tn.PODIatavia
, tn.CurrencyCode
, tn.LegalEntityID
, tn.Rate
, tn.Base
, tn.Minimum
, tn.NrDescription
, tn.Description
, tn.DateFrom
, tn.DateUntil
, tn.DateCreate
, tn.DateMod
, tn.ModName
, tn.Tariffkode
, tn.ExpiryDate
, tn.PClass
, tn.Maximum
, tn.RoutingOrder
, tn.TariffCompType
, tn.PrePaidCollect
, tn.Product
, tn.IsDeleted
, (
select distinct Location_IATA
from Company
where Called = 'KARL KING'
and LegalEntityID = 1
and IsDeleted = 0
) as PreCarriage
, tn.PortTerminalID
, tn.ScaleFrom
, tn.ScaleTo
, tn.ScaleCalcCode
, tn.Mandatory
, (
select CompanyID
from PlaceOfReceipt
where warehouse = 'KARL KING'
and LegalEntityID = 1
and OfficeID = 13
and IsDeleted = 0
) as WarehouseID
, tn.TariffRelID
, tn.FreeDescription
, 0
, tn.ShipCode
, tn.AgentID
, tn.ContainerTypeID
, tn.CommodityID
, 0 as TempTable
from TariffNew tn
inner join hhInvoiceLines inv
on tn.Tariffkode = inv.NrInvoiceLine
where (
tn.PreCarriage is not null
and tn.PreCarriage != ''
)
and (
tn.POLIata is not null
and tn.POLIata != ''
)
and inv.OfficeID = 13
and inv.IsDeleted = 0
and inv.LegalEntityID = 1
and tn.LegalEntityID = 1
and tn.Dep = 'E'
and tn.IsDeleted = 0
and tn.DateUntil = '2078-12-31 00:00:00'
and tn.Description = 'kgl'
)
) as b
where b.Record = 1
First, you have to define what you want to call "unique rows".
Once you have the set of columns that determines a row is unique, that is the set of columns you use in the partition by part of row_number()
In the code below, uncomment the column set that defines your "unique rows":
select *
from (
select ROW_NUMBER() over (
partition by
Dep
--, tariffkode
--, LinkTariffType
--, poliata
--, poliatavia
--, podiata
--, podiatavia
--, PreCarriage
--, PortTerminalId
--, Product
--, RoutingOrder
--, PrepaidCollect
--, isnull(description, '')
--, ScaleCalcCode
--, isnull(scalefrom, 0)
--, isnull(scaleto, 0)
--, CurrencyCode
--, Base
order by LinkTariffType desc
) Record
, *
from (
select tn.LinkTariffType
, tn.Dep
, tn.POLIata
, tn.POLIatavia
, tn.PODIata
, tn.PODIatavia
, tn.CurrencyCode
, tn.LegalEntityID
, tn.Rate
, tn.Base
, tn.Minimum
, tn.NrDescription
, tn.Description
, tn.DateFrom
, tn.DateUntil
, tn.DateCreate
, tn.DateMod
, tn.ModName
, tn.Tariffkode
, tn.ExpiryDate
, tn.PClass
, tn.Maximum
, tn.RoutingOrder
, tn.TariffCompType
, tn.PrePaidCollect
, tn.Product
, tn.IsDeleted
, (
select distinct Location_IATA
from Company
where Called = 'KARL KING'
and LegalEntityID = 1
and IsDeleted = 0
) as PreCarriage
, tn.PortTerminalID
, tn.ScaleFrom
, tn.ScaleTo
, tn.ScaleCalcCode
, tn.Mandatory
, (
select CompanyID
from PlaceOfReceipt
where warehouse = 'KARL KING'
and LegalEntityID = 1
and OfficeID = 13
and IsDeleted = 0
) as WarehouseID
, tn.TariffRelID
, tn.FreeDescription
, 0 as UnnamedColumn
, tn.ShipCode
, tn.AgentID
, tn.ContainerTypeID
, tn.CommodityID
, 0 as TempTable
from TariffNew tn
inner join hhInvoiceLines inv on tn.Tariffkode = inv.NrInvoiceLine
where tn.PreCarriage is not null
and tn.PreCarriage != ''
and tn.POLIata is not null
and tn.POLIata != ''
and inv.OfficeID = 13
and inv.IsDeleted = 0
and inv.LegalEntityID = 1
and tn.LegalEntityID = 1
and tn.Dep = 'E'
and tn.IsDeleted = 0
and tn.DateUntil = '2078-12-31 00:00:00'
and tn.Description = 'kgl'
) as s
) as b
where b.Record = 1