Your content is what I am following from my initial days of learning sql. If possible please make more practise videos on sql with concepts every week/weekend so that our logic buildng can increase week by week. Thanks for the good content.
Sir one important doubt can we work in company and also post tech videos and tutorials as a freelancer will this be considered dual employment/moonlighting?
Your discord channel is asking me too many questions about my verification? why? verify If I am human, verified. verify my email- verified, verify if I human again - verified, now give my phone number...?? I mean why?
Good video, but for SQL Server i would recommend you look at the GROUPING SETS command. With that you can rewrite your query to below : When using "set statistics time on;" you can see it beeing a lot faster. ;with inner_data as ( select customer_id as Customer ,format(sales_date, 'MMM-yy') as sales_date ,cast(replace(amount, '$', '') as int) as amount from sales_data ) select * from ( select isnull(Customer, 'Total') as Customer, isnull(sales_date, 'Total') as sales_date, case when sign(sum(amount)) = -1 then concat('(', ABS(sum(amount)), ')$') else concat(sum(amount), '$') end as amount from ( SELECT a.Customer, b.sales_date, 0 as amount from inner_data a inner join inner_data b on a.Customer b.Customer and a.sales_date b.sales_date union all select Customer ,sales_date ,amount from inner_data ) a group by GROUPING SETS( (Customer, sales_date), (Customer),(sales_date)) ) as sales pivot ( max(amount) for sales_date in ([Jan-21], [Feb-21], [Mar-21], [Apr-21] ,[May-21], [Jun-21], [Jul-21], [Aug-21] ,[Sep-21], [Oct-21], [Nov-21], [Dec-21],[Total]) ) as pivot_table
What an outstanding video! Ver well done! Thank you for putting it together. I have a question, though. I'm primarily a MySQL users but also use SQL Server. How could we set this up to be more dynamic? For example, let's say your column table covered 24 months and had 5 customers. Instead of writing out 24 lines of code each time (1 for each column) and then 24 lines multiple times, is there a way to write much fewer lines using variables, loops and a WITH clause? Thanks again - your videos have taught me so, so much!
Everytime you upload a new video, it brings abundance of knowledge in our lives. Thanks for this quality content. Maza aa jata hai aapka explanation sunke😁 Please consider making videos on topics such as Indexing(in depth) and database designing if possible.
Although i am not an English speaker, that does not prevent me from following you. It is very well explained with well chosen examples. I learned a lot thanks to you. Believe me you are the best. I copied the exact same query using crosstab and when i added the second argument i got the error : column ("Jan-21") does not exist. I hope you can explain the origin of the problem to me. GREAT THANKS (i replaced ("Jan-21") with (' 'Jan-21' ') and it worked)
this is a great one. thanks. i always wanted to do pivot queries. never really got around to it. now i got to learn it with my current project. very helpful video. thanks
Hello - For some reasons some of the data extracted using crosstab does not align in the columns they are expected to. For example there are 5 columns and one of the records has values for 1, 3 and 5 but when I used crosstab the values for columns 1,3 and 5 goes to columns 1,2 and 3. What should I do to align those data properly? thank you for those who will respond.
@techtfq: Of all your videos this one is the most confusing and messy! Just jumping from here to there! Wish you could do more organized considering how good you are in explaining complex matters in simple ways!
Hi Taufiq, thanks for sharing, all your videos are awesome. is there a way to keep month in row wise and custumer name will show region wise in column? awaiting your answer..i have sent an email to you on this, could you please respond?
PG: passing sql as string argument is just awful solution, can we do better, and not using extensions? Here's the request for your next video :) Thanks!
What will be the solution where columns varies based on customer names instead of month names as column. I want to say that month are fixed to 12 columns but customers are dynamic & any time a new customer can be inserted in our record. I need customer names in columns & Month-Year in rows.
This was a great video! I also loved how well you presented it, explaining every little detail. That way it is easy to remember the syntax. Thank you!! This was super helpful. Earlier I used to do sum() or max() to get a pivot output in postgres. crosstab will make my life a lot easier. One question - Is there a way we can dynamically populate the values for "as" and "values". For instance, instead of months, I have a list of products (which is dynamically populated) and I want to pivot customer against products. Is this possible in postgres?
Thanks for the clarity Taufiq! I'm wondering if Crosstab allows date ranges to be selected dynamically. Instead of hard coding the months, is there a way for it to pick up whatever months the data has?
Following and enjoying your videos. But in these cases, isn't it just much easier and faster to do these transformations in Excel and import the results into sequel tables for further analysis if needed?
Tf...awesome and great work. Although I have been writing SQL for several years, i could learn few new tips and tricks from you. I have a suggestion. Can you come up with a video that shows how to calculate cumulative totals and average totals on a rolling window. for e.g i need to calculate a 9 quarter ( 27months) average balance and cumulative losses. I have seen your window function video but that video lacks a real life problem solving component
Taufiq sir, absolutely clear the "without ORDER BY clause in postgre SQL Crosstab"...what ever customer is visible as per the sequence , CROSSTAB is grouping those visible data. That's why ORDER BY is mandatory otherwise this weird type data will be show. Thank you sir
While useful this video confirms one of my believes: that pivoting on the database has the potential to be messy, and it's best dealt with in the application.
Thanks for the explanation. But instead of writing the names of Columns like JAN-21, FEB-21 inside PIVOT I use the function STUFF which does it dynamically. So, we can avoid writing the column names.
@@muhammedsiraj6247 try this..... 1. SELECT @ColumnNames =STUFF(( SELECT DISTINCT ',' + QUOTENAME(PersonType) FROM databasename FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)'),1,1,'') 2. PRINT @ColumnNames 3. PIVOT ( COUNT(PhoneNumberTypeID) FOR PersonType IN (' + @ColumnNames + ') ) AS PIVOTTABLE' I have above points in a procedure, I can share the entire code with you. Thanks
Thank you Taufiq. This is really helpful. I have one quick question. All period which you have used in example and query are kind of static one (Jan21, Feb21 …). How do we make it more dynamic? Meaning, what if we have updated sales data for year 23 or 24… changing period value in query may not be good idea in long run. Any thoughts on this ?
I haven't tried it yet, but can you try to use a query for the "for column value in ()" select and make some kind of distinct month-year in the same query?
In SQL server dynamic pivots are not supported natively. You would need to generate the SQL as a text string and run sp_execute sql on that. It’s a really big missing feature, stops me using pivots at all just not worth the hassle
Don't use simple query to attract people for like. Use some extraordinary concept ny way write query to show row n times according to column value of same table.
Good video. You can wrap base query in (Double Dollar) $$ SUBQUERY $$ for postgres. Then you don't need to escape single quotes and makes it easy to read and write subquery.
Thanks for the content you put out, Taufiq. One question: can't you put the coalesce/NVL in the base query? Then you wouldn't have to put 12 such statements after the pivot.
@@techTFQ Actually I'm trying to execute it in oracle sql dvevoloper. But I'm getting error....missing right parenthesis in line 11 and 13 position.... Please help
Hey, I have two table one is student table with the field studentID, Name and other details and another is Attendance table with the field studentID and Date. I want to select record for each student for each day, fill record with the latter 'P' as present and 'A' as absent. How can I do??
Hi your videos are very helpful to me and I m begginer bit confused what is the difference between mysql,sql server, oracle,posgresql,plsql can u make a video on it please. Thank you in advance
Hello Sir, Very good example about transpose method in SQL using PIVOT as well as its equivalent in other RDBMS. Kindly clarify whether there is any method to make IN part in pivot dynamic instead of hardcoding. Thank you
honestly thank you for the video , but i learnt sub query in a different way , and you adding alias name in front and few things made me confuse a bit tbh , i understood the concept , thank you
This is the best crosstab explanation I've seen. Thank you!!!
Your content is what I am following from my initial days of learning sql. If possible please make more practise videos on sql with concepts every week/weekend so that our logic buildng can increase week by week. Thanks for the good content.
can you make a video on transactions please. Appreciate your work ,Thank you.
Bro kindly make video on regular expressions from scratch till advanced level
Noted bro will consider it
Great 👍 content as always 👌 thank you
Glad you liked it buddy
One video on data cleaning in sql
Sure bro will do it
Thanks for the explanation. Ur explanations are crystal clear.
I subscribed to ur channel..☺️
Awesome explanation! As always. Great thanks!
Glad you liked it :)
Great work techTFQ
great content as always, just a heads up blog link is broken.
Awesome. Great video! Much helpful
Thank You 👏👏 great content
Glad you think so!
Good Approach, can we have one video on UN Pivot same scenario, and what about if the new month is added it won't be dynamic. Any approach?
good work 👍
thank you, subbed :)
Sir one important doubt can we work in company and also post tech videos and tutorials as a freelancer will this be considered dual employment/moonlighting?
Hi tfq can you please upload the video about data validation and performance tuning
how to provide dynamic values in pivot funcition IN clause instead of constant values
sir can we visualize the data through sql... like can we make graphs by using sql......
Not really,, we generally use visualization tools like power bi or tableau for such things
Where can i download the script
Your discord channel is asking me too many questions about my verification? why? verify If I am human, verified. verify my email- verified, verify if I human again - verified, now give my phone number...?? I mean why?
Forget my discord channel, no idea why few have issues..
You can download the scripts from my blog.. link to my blog in video description
Outstanding. worked perfectly (for MySql). Thanks heaps
Glad to hear that
Hi when will your next batch starts?
Probably in August
Good video, but for SQL Server i would recommend you look at the GROUPING SETS command.
With that you can rewrite your query to below :
When using "set statistics time on;" you can see it beeing a lot faster.
;with inner_data as
(
select
customer_id as Customer
,format(sales_date, 'MMM-yy') as sales_date
,cast(replace(amount, '$', '') as int) as amount
from sales_data
)
select *
from (
select
isnull(Customer, 'Total') as Customer,
isnull(sales_date, 'Total') as sales_date,
case when sign(sum(amount)) = -1 then concat('(', ABS(sum(amount)), ')$') else concat(sum(amount), '$') end as amount
from (
SELECT a.Customer, b.sales_date, 0 as amount
from
inner_data a inner join
inner_data b
on a.Customer b.Customer and a.sales_date b.sales_date
union all
select
Customer
,sales_date
,amount
from inner_data ) a
group by GROUPING SETS( (Customer, sales_date), (Customer),(sales_date))
) as sales
pivot
(
max(amount)
for sales_date in ([Jan-21], [Feb-21], [Mar-21], [Apr-21]
,[May-21], [Jun-21], [Jul-21], [Aug-21]
,[Sep-21], [Oct-21], [Nov-21], [Dec-21],[Total])
) as pivot_table
What an outstanding video! Ver well done! Thank you for putting it together. I have a question, though. I'm primarily a MySQL users but also use SQL Server. How could we set this up to be more dynamic? For example, let's say your column table covered 24 months and had 5 customers. Instead of writing out 24 lines of code each time (1 for each column) and then 24 lines multiple times, is there a way to write much fewer lines using variables, loops and a WITH clause? Thanks again - your videos have taught me so, so much!
Everytime you upload a new video, it brings abundance of knowledge in our lives. Thanks for this quality content.
Maza aa jata hai aapka explanation sunke😁
Please consider making videos on topics such as Indexing(in depth) and database designing if possible.
Thank you Rohit..
Very happy to read your positive feedback ❤️
Although i am not an English speaker, that does not prevent me from following you. It is very well explained with well chosen examples.
I learned a lot thanks to you. Believe me you are the best. I copied the exact same query using crosstab and when i added the second
argument i got the error : column ("Jan-21") does not exist. I hope you can explain the origin of the problem to me. GREAT THANKS
(i replaced ("Jan-21") with (' 'Jan-21' ') and it worked)
this is a great one. thanks. i always wanted to do pivot queries. never really got around to it. now i got to learn it with my current project. very helpful video. thanks
thanks again. I just completed my report. Just have few little things to add but this video was very handy.
Hello - For some reasons some of the data extracted using crosstab does not align in the columns they are expected to. For example there are 5 columns and one of the records has values for 1, 3 and 5 but when I used crosstab the values for columns 1,3 and 5 goes to columns 1,2 and 3. What should I do to align those data properly? thank you for those who will respond.
@techtfq: Of all your videos this one is the most confusing and messy! Just jumping from here to there! Wish you could do more organized considering how good you are in explaining complex matters in simple ways!
Hi bro ,
how to pass dynamic value in start_date instead of hard-code values.. in Oracle how it work
Can you explain
Hi Taufiq, thanks for sharing, all your videos are awesome.
is there a way to keep month in row wise and custumer name will show region wise in column?
awaiting your answer..i have sent an email to you on this, could you please respond?
Plz can you share database file for practice a question
already shared. check description
Good video sir it is very help full and iam alway thankful to you sir ,sir kindly provide every week one imortant video
Thank you bro..
And sure will try doing it bro
PG: passing sql as string argument is just awful solution, can we do better, and not using extensions? Here's the request for your next video :) Thanks!
What will be the solution where columns varies based on customer names instead of month names as column.
I want to say that month are fixed to 12 columns but customers are dynamic & any time a new customer can be inserted in our record.
I need customer names in columns & Month-Year in rows.
This was a great video! I also loved how well you presented it, explaining every little detail. That way it is easy to remember the syntax. Thank you!! This was super helpful. Earlier I used to do sum() or max() to get a pivot output in postgres. crosstab will make my life a lot easier.
One question - Is there a way we can dynamically populate the values for "as" and "values". For instance, instead of months, I have a list of products (which is dynamically populated) and I want to pivot customer against products. Is this possible in postgres?
Thank you 🙏🏼
Instead of values you can provide an sql query as the second argument in crosstab..
But this query should just return 1 column
@@techTFQ Thank you!! I will try this 🙏🏼
Bro you are one of the best trainers I could ever come across. To the point and the crystal clear. Thanks one again.
Thanks for the clarity Taufiq! I'm wondering if Crosstab allows date ranges to be selected dynamically. Instead of hard coding the months, is there a way for it to pick up whatever months the data has?
MMM-YY, Great, some developer in the future will now need to Y3K proof the code. :)
12:00 is there no way to generate those columns with months programatically, (procedurally) instead of hardcoding them
Following and enjoying your videos. But in these cases, isn't it just much easier and faster to do these transformations in Excel and import the results into sequel tables for further analysis if needed?
Your videos are quite incredible, thank you Thoufiq for your patience and knowledge.
Thank you taufiq, i struck in converting columns to rows can you help in doing that please
Tf...awesome and great work. Although I have been writing SQL for several years, i could learn few new tips and tricks from you. I have a suggestion. Can you come up with a video that shows how to calculate cumulative totals and average totals on a rolling window. for e.g i need to calculate a 9 quarter ( 27months) average balance and cumulative losses. I have seen your window function video but that video lacks a real life problem solving component
This is the best explanation! Thank you so much ❤
your video is very helpful for me. thank you. 👍
It was asked in one of my interview. And I was like yaa allah iske aage bhi kuch hota hai kya
Brilliant one Bro. Waiting for Unpivot and Regular Expressions 😅
Thank you and noted bro
Taufiq sir, absolutely clear the "without ORDER BY clause in postgre SQL Crosstab"...what ever customer is visible as per the sequence , CROSSTAB is grouping those visible data. That's why ORDER BY is mandatory otherwise this weird type data will be show. Thank you sir
While useful this video confirms one of my believes: that pivoting on the database has the potential to be messy, and it's best dealt with in the application.
Amazing work please also share raw data of this amazing problem
Data passing to Procedure by using
Type table in postgreSql
till i watched this video, i was scared about pivot query, now i can understand it easily. thank yo so much for the video
Williams Sarah Martinez Scott Williams Susan
🤩 you deserve millions subscribers bro
Thank you so much :)
Hi sir, good work done, please, may we get some videos on TRIGGER in postgresql....? Thanks
Thank you and sure will do
With dynamic columns? we do not know how many colums as a coloumns?
in oracle, coalesce is definitely supported! 👍🏽
Just wanted to say Thanks!!! Your sql joins video helped me crack my first tech interview. Kep up the good work brother.....
thank you, Great to hear Abhijeet!
Your every content are amazing and very well explained.Can you please create playlist around SQL query tuning and performance?
Thank you Amit , glad you like them..
And noted on the suggestion
@@techTFQ thank you
could you please make a video on date related functions
Lewis Michelle Hall Donna Lee Ruth
I m so thankful to you brother....i must say video is very informative which is helping me to increase my skills on SQL...!! 👍
thank you Sandeep :)
Thanks for the explanation. But instead of writing the names of Columns like JAN-21, FEB-21 inside PIVOT I use the function STUFF which does it dynamically. So, we can avoid writing the column names.
Can you please write the syntax ?
@@muhammedsiraj6247 try this.....
1. SELECT @ColumnNames =STUFF(( SELECT DISTINCT ',' + QUOTENAME(PersonType)
FROM databasename FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)'),1,1,'')
2. PRINT @ColumnNames
3. PIVOT (
COUNT(PhoneNumberTypeID)
FOR PersonType
IN (' + @ColumnNames +
')
)
AS PIVOTTABLE'
I have above points in a procedure, I can share the entire code with you. Thanks
@@Beant1980 Hi Beant, can you please share the entire code with us?
Many thanks!
@@yassirnakkach5171 Hi, I have the code written in Microsoft SQL Server for AdventureWorks2016 database. I can email it you the text file?
Hi Beant, can you pls also share the entire code with me pls. Thanks so much
Thank you so much... helping multi tools at same time...
Mannnn you are simply the best ever, thanks so much, it helps a lottttttttttt!!!!
Glad to hear that ☺️
Bro how can we do the same thing in workbench
Great content tauphik sir, can you please create a separate video like this for unpivot...
Sure bro will do it
Sir if you don't mind can you make a video on analytical functions in sql
I have already made it Amulya..
Analytics functions are also called as window function so you can look for window functions videos on my channel
Another thing I want to convey that CROSSTAB is more logical and concise than PIVOT of SQL server and Oracle as per my point of view.
Sir, Please mention queries for oracle too
I have already given Oracle query here. Do check the video
@@techTFQ Please try for weekend classes this time🙏🙏
Thank you Taufiq. This is really helpful.
I have one quick question. All period which you have used in example and query are kind of static one (Jan21, Feb21 …). How do we make it more dynamic? Meaning, what if we have updated sales data for year 23 or 24… changing period value in query may not be good idea in long run. Any thoughts on this ?
I haven't tried it yet, but can you try to use a query for the "for column value in ()" select and make some kind of distinct month-year in the same query?
In SQL server dynamic pivots are not supported natively. You would need to generate the SQL as a text string and run sp_execute sql on that. It’s a really big missing feature, stops me using pivots at all just not worth the hassle
Don't use simple query to attract people for like. Use some extraordinary concept ny way write query to show row n times according to column value of same table.
Good video.
You can wrap base query in (Double Dollar) $$ SUBQUERY $$ for postgres. Then you don't need to escape single quotes and makes it easy to read and write subquery.
Thanks for the in detail explanation. Can you provide WEBAPI for the same
Thanks thoufig.. Is it possible make column names are dynamic.. I felt you are hard coding?
Hello sir, Can you plz show me how to insert the large data sets into SQL from excel. Csv/xls etc.
Thank you
Thanks for the content you put out, Taufiq. One question: can't you put the coalesce/NVL in the base query? Then you wouldn't have to put 12 such statements after the pivot.
TFQ im getting error in the PIVOT line its not turning into Yellow colour in sqldevoloper.....please help
Sorry I don’t understand,
@@techTFQ Actually I'm trying to execute it in oracle sql dvevoloper. But I'm getting error....missing right parenthesis in line 11 and 13 position....
Please help
Are you using the oracle query I gave ?
Please use oracle query which u can download from my blog or discord
@@techTFQ yes I'm using the oracle query form the blog.
I guess pivot isn't supporting in oracle sql devoloper....
@@techTFQ I found that i was using 11g db.....now i have upgraded to 21c and it works...
Hey, I have two table one is student table with the field studentID, Name and other details and another is Attendance table with the field studentID and Date. I want to select record for each student for each day, fill record with the latter 'P' as present and 'A' as absent.
How can I do??
The totals were a bit messy. Could you have added Total values along with the Customers and Months and created that with an initial WITH clause?
hi Sir, one Query. Can we use the pivot command without an aggregate function? Your Suggestions on this please.
Hii can you share the same problem with joins ? I don't exactly have the table but need to get data from different tables. Like 3 or 4 tables
Hi your videos are very helpful to me and I m begginer bit confused what is the difference between mysql,sql server, oracle,posgresql,plsql can u make a video on it please. Thank you in advance
11:42, wish there was a way to do just "select * from sales_date" instead of explicitly specifying these column names
Can't we use distinct customer in select, add both date and customer in group by, and then use sum(amount)??
Hello Sir,
Very good example about transpose method in SQL using PIVOT as well as its equivalent in other RDBMS.
Kindly clarify whether there is any method to make IN part in pivot dynamic instead of hardcoding.
Thank you
can you expalin that last part where you are finding the total respective of each column. Please explain the query you used in mysql
Hey Taufiq
When are you planning to start fresh sql course ? Looking forward to enrol.
Thanks.
honestly thank you for the video , but i learnt sub query in a different way , and you adding alias name in front and few things made me confuse a bit tbh , i understood the concept , thank you
thanks but don't confuse this with subquery. Ive made separate video for subquery.
@@techTFQ but you wont call this a subquery? ohh , i thought the end part as corelated subquery
This is the syntax for pivot operator..
Writing every single column name in both inner and outer queries for sqlserver pivot is totally unpractical in real life application.
mind blowing :)
Do video to PLSQL real- time querys bro
This video you do is help many persons bro
The link of blog is not working
Is it possible to use Cross Apply ?
what is hardcoding in mysql means can you please elaborate it or please make a seperate vedio on it.
Thank you so much! Just a quick note, in oracle we can use coalesce.
please tell me PL/SQL, POSTGRESQL both same or different?
Tqsm sir ❤️❤️