sql-metadata icon indicating copy to clipboard operation
sql-metadata copied to clipboard

The table_aliases method takes GROUP BY as an alias from CTE.

Open cmistiloglou opened this issue 1 year ago • 0 comments

Using the method parser.tables_aliases it brings back GROUP BY as a table alias in MSSQL.

Tables Aliases: {'GROUP BY': '[Table1]', 't3': '[Table1]', 't4': '[Table2]', 't1': '[Table3]', 't2': '[Table4]'}

Executed sql query:

WITH [CTE1] AS (
    SELECT 
        [Col1], 
        MAX([Col2]) AS [MaxCol2]
    FROM 
        [Table1]
    GROUP BY 
        [Col1]
)
SELECT 
    t1.[ColA] AS [PortfolioCode], -- Portfolio unique identifier
    t2.[ColB] AS [InstrumentCode],  -- Instrument unique identifier
    SUM(t3.[Qty1]) AS [OutstandingQuantity], 
    SUM(t3.[Qty2]) AS [InstrumentValuation], 
    t4.[Code] AS [ValuationCurrency], 
    t3.[DateCol] AS [RefDate]
FROM 
    [Table1] t3
JOIN [CTE1] t1 ON t3.[Col1] = t1.[Col1] AND t3.[DateCol] = t1.[MaxCol2]
JOIN [Table2] t4 ON t4.[ID] = t3.[Col2]
JOIN [Table3] t1 ON t1.[ID] = t3.[Col3]
JOIN [Table4] t2 ON t2.[ID] = t3.[Col4] AND (t2.[Code] LIKE @Param1) 
WHERE 
    t3.[Qty1] <> 0
	  
GROUP BY
    t1.[ColA],
    t2.[ColB],
    t4.[Code],
    t3.[DateCol];

Version: sql_metadata==2.13.0

cmistiloglou avatar Sep 04 '24 09:09 cmistiloglou