Snowflake -WITH RECURSIVE... is too long and would be truncated in 'CONCAT' - concatenation

Something aparently as easy as the following query is driving me crazy, anyone can help?
SELECT * FROM
(
WITH recursive colores (id_modelo, cod_color, rango) AS
(
SELECT id_modelo,
cod_color,
rango
FROM dwh_vse.dwh_colores_orden_v
WHERE rango = 1
UNION ALL
SELECT a.id_modelo,
c.cod_color
|| '-'
||a.cod_color,
a.rango
FROM dwh_vse.dwh_colores_orden_v a,
colores c
WHERE a.id_modelo = c.id_modelo
AND a.rango = c.rango+1 )
SELECT id_modelo,
Max(cod_color)AS cod_color_agr
FROM colores
GROUP BY id_modelo
)
**ERROR:
String '01-11-23' is too long and would be truncated in 'CONCAT'**
--Trying with: TRY_CAST(cod_color AS varchar(100))
SELECT * FROM
(
WITH recursive colores (id_modelo, cod_color, rango) AS
(
SELECT id_modelo,
TRY_CAST(cod_color AS varchar(100)) AS cod_color,
rango
FROM dwh_vse.dwh_colores_orden_v
WHERE rango = 1
UNION ALL
SELECT a.id_modelo,
c.cod_color
|| '-'
||a.cod_color,
a.rango
FROM dwh_vse.dwh_colores_orden_v a,
colores c
WHERE a.id_modelo = c.id_modelo
AND a.rango = c.rango+1 )
SELECT id_modelo,
Max(cod_color)AS cod_color_agr
FROM colores
GROUP BY id_modelo
)
**SQL execution internal error: Processing aborted due to error 300010:2265280747; incident 3211946.**
--CAST(cod_color as string)
SELECT * FROM
(
WITH recursive colores (id_modelo, cod_color, rango) AS
(
SELECT id_modelo,
CAST(cod_color as string) AS cod_color,
rango
FROM dwh_vse.dwh_colores_orden_v
WHERE rango = 1
UNION ALL
SELECT a.id_modelo,
c.cod_color
|| '-'
||a.cod_color,
a.rango
FROM dwh_vse.dwh_colores_orden_v a,
colores c
WHERE a.id_modelo = c.id_modelo
AND a.rango = c.rango+1 )
SELECT id_modelo,
Max(cod_color)AS cod_color_agr
FROM colores
GROUP BY id_modelo
)
**SQL execution internal error: Processing aborted due to error 300010:2265280747; incident 1742159.**
The recursive on cod_color should give somenthing for instance like '01-11-23'
I have others querys with recursive and with fields on the recursive longer than in this case (VARCHAR(2)) and I donĀ“t get the problem.

I have solved the problem taking into account the size of the field of the source table.
Must be large enough to contain the concatenation.
For this particular case
dwh_vse.dwh_colores_orden
COD_COLOR VARCHAR(250)

Related

This SQL INSERT INTO does not work with a UNION

I have been at this for some time and am frustrated. Below is the complete SQL statement I am trying to execute. Simply put, create a temp table from a UNION Select(s) statements.
I can run the SELECTs with the UNION just fine. I can run the separate SELECTS just fine. I can run separate INSERT INTOs just fine. The moment I add an INSERT INTO SELECT * FROM (...) at the top and close it at the bottom I get a syntax error:
Msg 102, Level 15, State 1, Line 75
Incorrect syntax near ')'.
I was hoping this would be simple, but I tried everything I know to get this to work, and cannot. is there something, perhaps with the UNION that creates the syntax error?
INSERT INTO #Tmp6 SELECT * FROM
(
SELECT
[ItemStatus]
,[Item_Number]
,[Item_Name]
,[Default_Sales_Name]
,[Special_Order_Item]
,[Returnable]
,[Return_Number_Of_Days]
,[Is_Drop_Ship]
,[Is_JIT]
,[Vendor_Name]
,[Vendor_Number]
,[Default_Sales_Price]
,[Cost_Of_Goods]
,[Purchase_Cost]
,[Additional_Cost1_Text]
,[Additional_Cost1_Value]
,[Additional_Cost2_Text]
,[Additional_Cost2_Value]
,[Additional_Cost3_Text]
,[Additional_Cost3_Value]
,[UPC_Value]
,[Weight]
,[DimWeight]
,[Discontinued_Date]
,[Personalization_Template_Number]
,[Harmonized_Code]
,[Base_Category]
,[Sub_Category]
,[End_Category]
,[RowID]
FROM [dbo].[RAIProductsStg] p
WHERE ItemStatus <> 'Discontinued' OR
p.Item_Number IN (SELECT i.Item_Number FROM RAIInventoryStg i WHERE p.Item_Number = i.Item_Number AND Quantity > 0)
UNION
SELECT
[ItemStatus]
,[Item_Number]
,[Item_Name]
,[Default_Sales_Name]
,[Special_Order_Item]
,[Returnable]
,[Return_Number_Of_Days]
,[Is_Drop_Ship]
,[Is_JIT]
,[Vendor_Name]
,[Vendor_Number]
,[Default_Sales_Price]
,[Cost_Of_Goods]
,[Purchase_Cost]
,[Additional_Cost1_Text]
,[Additional_Cost1_Value]
,[Additional_Cost2_Text]
,[Additional_Cost2_Value]
,[Additional_Cost3_Text]
,[Additional_Cost3_Value]
,[UPC_Value]
,[Weight]
,[DimWeight]
,[Discontinued_Date]
,[Personalization_Template_Number]
,[Harmonized_Code]
,[Base_Category]
,[Sub_Category]
,[End_Category]
,[RowID]
FROM [dbo].[RAIProductsStg] p
LEFT JOIN RAIParentChildStg pc ON p.Item_Number = pc.ParentItemNumber
WHERE(LEN(p.Item_Number) = 6 AND p.ItemStatus = 'Discontinued') AND
(pc.ChildItemNumber IN
(SELECT i.Item_Number FROM RAIInventoryStg i
WHERE pc.ChildItemNumber = i.Item_Number AND i.Quantity > 0))
)
Please try this. Put alias after last bracket.
INSERT INTO #Tmp6
SELECT * FROM
(
SELECT
[ItemStatus]
,[Item_Number]
,[Item_Name]
,[Default_Sales_Name]
,[Special_Order_Item]
,[Returnable]
,[Return_Number_Of_Days]
,[Is_Drop_Ship]
,[Is_JIT]
,[Vendor_Name]
,[Vendor_Number]
,[Default_Sales_Price]
,[Cost_Of_Goods]
,[Purchase_Cost]
,[Additional_Cost1_Text]
,[Additional_Cost1_Value]
,[Additional_Cost2_Text]
,[Additional_Cost2_Value]
,[Additional_Cost3_Text]
,[Additional_Cost3_Value]
,[UPC_Value]
,[Weight]
,[DimWeight]
,[Discontinued_Date]
,[Personalization_Template_Number]
,[Harmonized_Code]
,[Base_Category]
,[Sub_Category]
,[End_Category]
,[RowID]
FROM [dbo].[RAIProductsStg] p
WHERE ItemStatus <> 'Discontinued' OR
p.Item_Number IN (SELECT i.Item_Number FROM RAIInventoryStg i WHERE p.Item_Number = i.Item_Number AND Quantity > 0)
UNION
SELECT
[ItemStatus]
,[Item_Number]
,[Item_Name]
,[Default_Sales_Name]
,[Special_Order_Item]
,[Returnable]
,[Return_Number_Of_Days]
,[Is_Drop_Ship]
,[Is_JIT]
,[Vendor_Name]
,[Vendor_Number]
,[Default_Sales_Price]
,[Cost_Of_Goods]
,[Purchase_Cost]
,[Additional_Cost1_Text]
,[Additional_Cost1_Value]
,[Additional_Cost2_Text]
,[Additional_Cost2_Value]
,[Additional_Cost3_Text]
,[Additional_Cost3_Value]
,[UPC_Value]
,[Weight]
,[DimWeight]
,[Discontinued_Date]
,[Personalization_Template_Number]
,[Harmonized_Code]
,[Base_Category]
,[Sub_Category]
,[End_Category]
,[RowID]
FROM [dbo].[RAIProductsStg] p
LEFT JOIN RAIParentChildStg pc ON p.Item_Number = pc.ParentItemNumber
WHERE(LEN(p.Item_Number) = 6 AND p.ItemStatus = 'Discontinued') AND
(pc.ChildItemNumber IN
(SELECT i.Item_Number FROM RAIInventoryStg i
WHERE pc.ChildItemNumber = i.Item_Number AND i.Quantity > 0))
)c
Place the bracket after the second select statement. That should do it.
INSERT INTO TableA
(
SELECT A, B, C
FROM TableB
UNION
SELECT D, E, F
FROM TableC
)

Adding results from two queries

I'm using MS-SQL 2008 R2.
I have 2 Queries which are returning the required results.
But I need to add the two results from each queries to provide a final value [Enterprise Value]. I'm sure this is very straight forward but I'm going round in circles on this, have tried incorporating SUM which I think is the right approach?
Here is the full query as it currently stands:
declare #d1 datetime='2015-12-22'
(select
c.fs_perm_sec_id,
((c.p_price * s.p_com_shs_out)/1000) as [Enterprise Value]
from fp_v1.fp_basic_bd c
left join edm_v1.edm_security_entity_map e
on e.fs_perm_sec_id= c.fs_perm_sec_id
left join fp_v1.fp_basic_sho s
on s.fs_perm_sec_id = c.fs_perm_sec_id
and c.date=#d1
where s."date" =
(
select MAX(s2."date")
from fp_v1.fp_basic_sho s2
where s2.fs_perm_sec_id=c.fs_perm_sec_id
and s2."date" <= c."date"
)
and c."date"=#d1
and e.termination_date is null
and c.fs_perm_sec_id = 'GPHC8W-S-GB')
UNION ALL
select
ff.fs_perm_sec_id,
((FF_debt + ff_pfd_stk + ff_min_int_accum) - FF.ff_cash_st) as [Enterprise Value]
from ff_v2.ff_basic_af_v2 FF
where FF."date" =
( select MAX(FF2."date")
from ff_v2.ff_basic_af_v2 FF2
where FF2.fs_perm_sec_id=FF.fs_perm_sec_id
and FF.date <= FF2.date
)
and FF.fs_perm_sec_id =('GPHC8W-S-GB')
When inserting a "UNION ALL" between the two queries I get the following results:
fs_perm_sec_id Enterprise Value
GPHC8W-S-GB 9270.5204655
GPHC8W-S-GB 835
What I would like to achieve is a sum of the two values brought onto one row, i.e.:
fs_perm_sec_id Enterprise Value
GPHC8W-S-GB 10105.52
Thanks for your help.
Final SQL:
declare #d1 datetime='2015-12-23'
Select fs_perm_sec_id, SUM([Enterprise Value]) AS 'Enterprise Value'
from
(
(select
c.fs_perm_sec_id,
((c.p_price * s.p_com_shs_out)/1000) as [Enterprise Value]
from fp_v1.fp_basic_bd c
left join edm_v1.edm_security_entity_map e
on e.fs_perm_sec_id= c.fs_perm_sec_id
left join fp_v1.fp_basic_sho s
on s.fs_perm_sec_id = c.fs_perm_sec_id
and c.date=#d1
where s."date" =
(
select MAX(s2."date")
from fp_v1.fp_basic_sho s2
where s2.fs_perm_sec_id=c.fs_perm_sec_id
and s2."date" <= c."date"
)
and c."date"=#d1
and e.termination_date is null
and c.fs_perm_sec_id in ('FT9TC5-S-GB','GPHC8W-S-GB','R85KLC-S-US'))
UNION ALL
select
ff.fs_perm_sec_id,
((FF_debt + ff_pfd_stk + ff_min_int_accum) - FF.ff_cash_st) as [Enterprise Value]
from ff_v2.ff_basic_af_v2 FF
where FF."date" =
( select MAX(FF2."date")
from ff_v2.ff_basic_af_v2 FF2
where FF2.fs_perm_sec_id=FF.fs_perm_sec_id
and FF.date <= FF2.date
)
and FF.fs_perm_sec_id in ('FT9TC5-S-GB','GPHC8W-S-GB','R85KLC-S-US')) t
group by t.fs_perm_sec_id
just use the Derived Table and Group by
Select fs_perm_sec_id,
SUM(Enterprise Value) EnterpriseValue
from (**your whole code**)
GROUP BY fs_perm_sec_id
use group by
How to use group by with union in t-sql
SELECT id,sum(*)
FROM ( SELECT id,
time
FROM dbo.a
UNION
SELECT id,
time
FROM dbo.b
)
GROUP BY id
DECLARE
#d1 DATE = '20151222'
, #fs_perm_sec_id VARCHAR(100) = 'GPHC8W-S-GB'
SELECT #fs_perm_sec_id, SUM([Enterprise Value])
FROM (
SELECT [Enterprise Value]
FROM (
SELECT
c.fs_perm_sec_id
, (c.p_price * s.p_com_shs_out) / 1000 AS [Enterprise Value]
, RowNum = ROW_NUMBER() OVER (ORDER BY s.[date] DESC)
from fp_v1.fp_basic_bd c
join fp_v1.fp_basic_sho s on s.fs_perm_sec_id = c.fs_perm_sec_id
left join edm_v1.edm_security_entity_map e on e.fs_perm_sec_id= c.fs_perm_sec_id
where c.[date] = #d1
and e.termination_date is null
and c.fs_perm_sec_id = #fs_perm_sec_id
) t
WHERE t.RowNum = 1
UNION ALL
SELECT FF_debt + ff_pfd_stk + ff_min_int_accum - ff_cash_st
FROM (
SELECT
ff.fs_perm_sec_id
, FF_debt
, ff_pfd_stk
, ff_min_int_accum
, FF.ff_cash_st
, RowNum = ROW_NUMBER() OVER (ORDER BY FF.[date] DESC)
FROM ff_v2.ff_basic_af_v2 FF
WHERE FF.[date] =
AND FF.fs_perm_sec_id = #fs_perm_sec_id
) t
WHERE t.RowNum = 2
) t

Aliasing in SQL Server

I wrote this query in Oracle and want to compile it as well on SQL Server:
SELECT DISTINCT
HOLDER_CODE, CALCULATED_AMOUNT
FROM
(SELECT
DA.HOLDER_CODE,
ROUND(FAB.AMOUNT * FCE.EXCHANGE_RATE,0) AS CALCULATED_AMOUNT,
FAB.BALANCE_DATE_ID as FAB_DATE,
MAX(FAB.BALANCE_DATE_ID) OVER (PARTITION BY DA.HOLDER_CODE) as MAX_DATE_BALANCE
FROM
DIM_ACCOUNT DA
JOIN
FACT_AS_BALANCE FAB ON FAB.ACCOUNT_ID = DA.ID
JOIN
DIM_AS_CHARACTERISTICS DAC ON DAC.ID = FAB.BALANCE_TYPE_ID
LEFT JOIN
FACT_CURRENCY_EXCHANGE FCE ON FCE.FROM_CURRENCY_ID = FAB.CURRENCY_ID
WHERE
DAC.BALANCE_CLOSING_FLAG = 'Y'
AND TO_CURRENCY_ID = (SELECT DC.ID
FROM DIM_CURRENCY DC
WHERE DC.IS_DEFAULT_CURRENCY = 'Y')
AND FAB.AMOUNT > 0)
WHERE
FAB_DATE = MAX_DATE_BALANCE
ORDER BY
CALCULATED_AMOUNT DESC;
But when I run it I get the following exception:
Error: Incorrect syntax near the keyword 'WHERE'. SQLState: S0001
ErrorCode: 156
So I guess, it's the last where clause, which indicates this. What is wrong and what should it be?
You just need to alias the subquery
SELECT DISTINCT HOLDER_CODE, CALCULATED_AMOUNT
FROM (
SELECT DA.HOLDER_CODE,
//// snip subquery
AND FAB.AMOUNT > 0
) alias_name_here //<--- here
WHERE FAB_DATE = MAX_DATE_BALANCE
ORDER BY CALCULATED_AMOUNT DESC;
You can use as alias_name_here or just alias_name_here - SQL Server allows either syntax.
you must give a name at your "table"
i give is with "as T"
SELECT DISTINCT HOLDER_CODE, CALCULATED_AMOUNT
FROM (
SELECT DA.HOLDER_CODE,
ROUND(FAB.AMOUNT * FCE.EXCHANGE_RATE,0) AS CALCULATED_AMOUNT,
FAB.BALANCE_DATE_ID as FAB_DATE,
MAX(FAB.BALANCE_DATE_ID) OVER (PARTITION BY DA.HOLDER_CODE) as MAX_DATE_BALANCE
FROM DIM_ACCOUNT DA
JOIN FACT_AS_BALANCE FAB ON FAB.ACCOUNT_ID = DA.ID
JOIN DIM_AS_CHARACTERISTICS DAC ON DAC.ID = FAB.BALANCE_TYPE_ID
LEFT JOIN FACT_CURRENCY_EXCHANGE FCE ON FCE.FROM_CURRENCY_ID = FAB.CURRENCY_ID
WHERE DAC.BALANCE_CLOSING_FLAG = 'Y'
AND TO_CURRENCY_ID = (
SELECT DC.ID FROM DIM_CURRENCY DC
WHERE DC.IS_DEFAULT_CURRENCY = 'Y')
AND FAB.AMOUNT > 0
) as T
WHERE FAB_DATE = MAX_DATE_BALANCE
ORDER BY CALCULATED_AMOUNT DESC;

Getting the sum of two select count statements

I've got two select queries I need to combine into one result.
/* Count of all classes students have passed */
/* A */
SELECT COUNT(TECH_ID) as Number1, ct.SUBJ, ct.COU_NBR
FROM [ISRS].[ST_COU] st
JOIN [ISRS].[CT_COU_SQL] ct
ON st.COU_ID = ct.COU_ID
WHERE GRADE = 'A' OR Grade = 'B' or Grade = 'C'
GROUP BY ct.SUBJ, ct.COU_NBR
/* Total Count of all students who needed to take a course */
/* B */
SELECT COUNT(TECH_ID) as Number2, ec.SUBJ, ec.COU_NBR
FROM [dbo].[ST_MAJOR_COMMENT] st JOIN [dbo].[Emphasis_Class] ec
ON st.MAJOR = ec.Emphasis
GROUP BY ec.SUBJ, ec.COU_NBR
I need SUBJ, COU_NBR, sum(B - A)
you can have your sql queries in the ctes and then do a join and group by
with cte1
as
(
first query
)
, cte2
as
(
second query
)
select cte1.subj, cte1.cou_nbr, sum(cte2.number2 - cte1.number1) as difference
from cte1
join cte2
on cte1.subj = cte2.subj
and cte1.cou_nbr = cte2.cou_nbr
group by cte1.subj, cte1.cou_nbr
Use union:
select sum(Number1) from (
SELECT COUNT(TECH_ID) as Number1
-- onit all other columns from select then rest of query 1
UNION
-- just the count column selected then rest of query 2
) x
This syntax will work with most databases.

Update field in table

I have the following table PNLReference
PnlId LineTotalisationId Designation TypeTotalisation Totalisation
1 A Gross Fees Formule A01+A02+A03+A04+A05
2 A01 GF1 Comptes imputables 975800|758000|706900|706000|706430|706420|706410|706400|706530|706520|706510|706001|706401|706431|706531|706902
3 A02 GF2 Comptes imputables 706500|709400|706130|706120|706110|706100|706830|706820|706810|706800|706730|706720|706710|706700|706330|706101|706131|706331|706501|706701|706801|706831|709401|706731
I have filled table DimPNL as following
INSERT [dbo].[DimPNL] (
PNLCode
,PNLName
,PNLParentId
,Operator
)
SELECT *
FROM (
SELECT t.c.value('.', 'nvarchar(255)') AS PNLCode
,Ref.Designation AS PNLName
,split.LineTotalisationId AS PNLParentId
,split.Operator AS Operator
FROM (
SELECT tbl.Designation
,tbl.LineTotalisationId
,tbl.TypeTotalisation
,tbl.PnlId
,tbl.Totalisation
,CAST('<t>' + REPLACE(tbl.Totalisation, tbl.Operator, '</t><t>') + '</t>' AS XML) x
,tbl.Operator
FROM ##TTResults AS tbl
) split
CROSS APPLY x.nodes('/t') t(c)
INNER JOIN [dbo].[PNLReference] Ref
ON Ref.LineTotalisationId = t.c.value('.', 'nvarchar(255)')
) Result
table dimpnl contents a filed sign which have to be filled like that : if all numbers in Totalisation in table PNLReference starts with 7 the sign would be -1 else sign will be 1.How to do it ? any idea ?
Using CASE WHEN LEFT(Totalisation,1)='7' then -1 else 1 END [SIGN] will give you a field that you can take the MAX(sign), if it stays -1 then they all started with 7

Resources