Posts

Showing posts with the label CTE

SQL Server, the "Distinct clause" competition. Oops i did it again! ...from 24 seconds to 6 milliseconds!

Image
Hi guys, Thank you all mates, the last post was very successful and therefore I propose its continuation. I recommend you read it before reading this post. We were left to the point of using the feature called grouped aggregate pushdown applied to a columnstore index. The shortest run time obtained was 56 milliseconds (I remember that we started from 30 seconds ..omg)   Introduction In the last post we "played" with the "Group By". With a "Group By" clause we can use aggregate functions like COUNT, MIN,MAX,etc and perform calculation on a group of rows to return unique value: SELECT Classifiers, COUNT (Classifiers) FROM Movements GROUP BY Classifiers But if you need only to remove duplicate values and so extract distinct values we can use a DISTINCT operator we can simply write this T-SQL command: SELECT DISTINCT Classifiers FROM Movements Today we apply the same concepts we saw in the last post to a query that contains a distinct ...

SQL Server, generate a number of rows

Image
Hi Guys, Today, after the last tips  SQL Server, Concatenates text from multiple rows into a single string! I want to show you another tips! I suggest you bookmark this page. This way, when you need it, a copy and paste will suffice! How many times did it take you to have a table with a fixed number of rows at your fingertips? Here it is!   Generate a number of rows Suppose you want to generate 10 rows. Just write (or copy and paste) this command: ; WITH progr(x) AS ( SELECT 1 UNION ALL SELECT x+1 FROM progr WHERE x < 10 ) SELECT x FROM progr OPTION (MAXRECURSION 0); And here is the result: What we have done? simple: we used a CTE in order to trigger recursion. That's all for today! Stay tuned mate! Luca Luca Biondi @ SQLServerPerformance blog! Next post: Previous post: SQL Server 2019 and the Approx_Count_Distinct function

Le Common Table Expression (CTE) che cosa sono?

Image
Buongiorno a tutti! Ben ritrovati! Oggi parliamo di Common Table Expression a cui molto più spesso ci si riferisce con l'acronimo CTE . Cosa sono? Cercherò di spiegarlo nel modo più semplice possibile! Pronti? Via!   Le Common Table Expression Quando mi fanno questa domanda rispondo utilizzando il modo più semplice in cui riesco a definirle: Una CTE è un Result set temporaneo a cui associamo un nome ed al quale possiamo riferirci nei vari statements. Le CTE sono state introdotte ormai un po' di anni fa con il rilascio della versione 2005 di SQL Server ma s e non sapete quale versione è installata potete vedere qui: identificare la versione di sql server installata L'obbiettivo era quello di prevedere una sintassi estesa che permettesse di scrivere le Query in modo più agevole. Il loro utilizzo potrebbe inoltre portare ad un incremento delle prestazioni ma questo aspetto lo vedremo in futuro. La sintassi base per l'utilizzo delle CTE è davvero semplice ...