<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Sumeet Bansal</title>
    <description>The latest articles on Forem by Sumeet Bansal (@sumeet_bansal).</description>
    <link>https://forem.com/sumeet_bansal</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F785677%2F836bfabb-e123-44fb-b554-d6df86eb245f.jpeg</url>
      <title>Forem: Sumeet Bansal</title>
      <link>https://forem.com/sumeet_bansal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sumeet_bansal"/>
    <language>en</language>
    <item>
      <title>Calculate average delta change in KPIs over a period of time.</title>
      <dc:creator>Sumeet Bansal</dc:creator>
      <pubDate>Sun, 21 Aug 2022 20:22:21 +0000</pubDate>
      <link>https://forem.com/sumeet_bansal/calculate-average-delta-change-in-kpis-over-a-period-of-time-3o4g</link>
      <guid>https://forem.com/sumeet_bansal/calculate-average-delta-change-in-kpis-over-a-period-of-time-3o4g</guid>
      <description>&lt;p&gt;It is often that we have to calculate delta change in KPIs over a period of time. It might be on a daily, weekly, monthly or quarterly level. Monitoring changes in a product's KPIs is a very crucial task. Analysing trends will help us improve products.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--s94TYliN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://media.istockphoto.com/vectors/up-and-down-arrow-vector-vector-id650558230%3Fk%3D20%26m%3D650558230%26s%3D612x612%26w%3D0%26h%3DJDqwXZhcVZJCmClPoA9LWLBTyWncVDE_hJsmrSma7xk%3D" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--s94TYliN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://media.istockphoto.com/vectors/up-and-down-arrow-vector-vector-id650558230%3Fk%3D20%26m%3D650558230%26s%3D612x612%26w%3D0%26h%3DJDqwXZhcVZJCmClPoA9LWLBTyWncVDE_hJsmrSma7xk%3D" alt="Arrows" width="612" height="459"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this blog, we will discuss a similar situation where we have to find the average monthly increase/decrease in total visits for an article in the last 6 months.&lt;/p&gt;

&lt;p&gt;(Table contains data for the last 7 months for each article)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;╔════════════════╦═══════════════╦═════════════════╗
║  article_id    ║ year_month    ║ total_visits    ║
╠════════════════╬═══════════════╬═════════════════╣
║ 1              ║ 202201        ║ 700             ║
╠════════════════╬═══════════════╬═════════════════╣
║ 1              ║ 202202        ║ 1100            ║
╠════════════════╬═══════════════╬═════════════════╣
║ 1              ║ 202203        ║ 1200            ║
╠════════════════╬═══════════════╬═════════════════╣
║ 1              ║ 202204        ║ 1440            ║
╠════════════════╬═══════════════╬═════════════════╣
║ 1              ║ 202205        ║ 4300            ║
╠════════════════╬═══════════════╬═════════════════╣
║ 1              ║ 202206        ║ 4500            ║
╠════════════════╬═══════════════╬═════════════════╣
║ 1              ║ 202207        ║ 4100            ║
╠════════════════╬═══════════════╬═════════════════╣
║ 5              ║ 202203        ║ 9800            ║
╠════════════════╬═══════════════╬═════════════════╣
║ 5              ║ 202204        ║ 12000           ║
╠════════════════╬═══════════════╬═════════════════╣
║ 5              ║ 202205        ║ 14000           ║
╚════════════════╩═══════════════╩═════════════════╝
--------------------------------------------------------------
The expected output for the article with ID 1
╔════════════════╦═════════════╗
║  article_id    ║ monthly_avg ║
╠════════════════╬═════════════╣
║ 1              ║ 760         ║
╚════════════════╩═════════════╝
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first step of solving this would be calculating the difference in visits between the current month and last month for every article. Here we can use a special function called LAG.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;LAG&lt;/strong&gt; function provides access to a row at a given physical offset that comes before the current row, which means the function gives us the nth last row value of a particular column. It takes at least 2 arguments in this case, first is the column and second is the offset.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We create a CTE to calculate the difference in visits by subtracting the current month's visits from last month's visits which we are getting by using the LAG function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WITH cte_articles
AS (
    SELECT article_id
        ,year_month
        ,total_visits - (
            LAG(total_visits, 1) OVER (
                PARTITION BY article_id ORDER BY year_month ASC
                )
            ) AS diff_in_visits
    FROM articles
    )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The difficult task is now done. You must be wondering why didn't we add the filter to include just the last 6 months of data. That is because for calculating the monthly difference of the last 6 months we need 7 months of data and when the LAG function gives us the last offset value of row for the first month we get null.&lt;/p&gt;

&lt;p&gt;This null is ignored when we calculate the average difference in visits using the AVERAGE function.&lt;/p&gt;

&lt;p&gt;Calculating the average for every article.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT article_id
    ,avg(diff_in_visits) AS monthly_avg
FROM cte_articles
GROUP BY article_id;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is how easily we can calculate average delta changes in KPIs using just SQL.&lt;/p&gt;

</description>
      <category>sql</category>
      <category>database</category>
      <category>datascience</category>
      <category>analytics</category>
    </item>
    <item>
      <title>Getting started with reddit.</title>
      <dc:creator>Sumeet Bansal</dc:creator>
      <pubDate>Mon, 15 Aug 2022 14:46:00 +0000</pubDate>
      <link>https://forem.com/sumeet_bansal/getting-started-with-reddit-4flg</link>
      <guid>https://forem.com/sumeet_bansal/getting-started-with-reddit-4flg</guid>
      <description>&lt;p&gt;Reddit is a popular social news website that has been gaining more and more viewers over the years. With such a diverse range of topics covered on its pages, there is bound to be something for everyone. In this blog, we'll go over what you need to know about reddit and how it can help you.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to use reddit?
&lt;/h3&gt;

&lt;p&gt;If you're new to reddit, the front page can be overwhelming. There are thousands of subreddits (sections of reddit), and each one is full of posts. How do you find the good stuff?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What are subreddits?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A subreddit is a specific online community, with its own set of rules and regulations, that is dedicated to a particular topic. For example, the reddit community r/AskReddit is a subreddit dedicated to asking and answering questions. &lt;/p&gt;

&lt;p&gt;Subreddits are created and moderated by volunteers who are passionate about their topic. Anyone can create a subreddit, and anyone can subscribe to any number of subreddits. When you subscribe to a subreddit, the content from that subreddit will appear in your feed. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Finding subreddits&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Check out the default subreddits. Default subreddits are chosen by the reddit team and they cover a range of topics. They're a great starting point for finding new content.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use the search function. On the top-right of the screen, you'll see a search bar. You can use this to find specific subreddits or topics that you're interested in.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Browse by topic. If you click on the "Browse" link at the top of the screen, you'll see a list of popular categories. This is a great way to explore different areas of reddit.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you're new to reddit, or if you're just looking to find some new communities to join, browsing through the most popular subreddits is a great way to get started.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Why should you use Reddit for help with programming?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you're a programmer who likes to learn new stuff or is an experienced one, you should definitely check out Reddit. Why? Because Reddit is full of people who are passionate about sharing knowledge related to programming. &lt;/p&gt;

&lt;p&gt;You can find virtually anything on Reddit, from useful tips and tricks to in-depth discussions about your programming language. And since Reddit is organized into communities (or "subreddits"), it's easy to find the information you're looking for. &lt;/p&gt;

&lt;p&gt;Not only that, but Reddit is also a great place to get feedback on your work. If you have built projects and want to improve them based on community reviews reddit is the best place as people will get to know about your project and will provide suggestions to improve it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What are some tips for using reddit more effectively?&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;First, take some time to explore the different subreddits and find ones that fit your interests. There's a subreddit for just about everything, so you're bound to find some that you enjoy.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once you've found some good subreddits, make sure to upvote the content that you like and downvote the content that you don't. This helps ensure that the best content rises to the top.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Feel free to add your own comments and contribute to the discussions taking place. Reddit is a great community and its users are always happy to help out newcomers. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, if you really want to get involved in the reddit community, consider creating your own subreddit. This is a great way to bring people together who share your interests.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Dark side of reddit&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Like every other social media reddit is also prone to toxicity. You might get downvoted for no reason. You might get abused for going against popular opinion. You might also get banned for subreddits for breaking rules that you were never aware of. You will also have to aware of scammers and other stuffs that are common across all the other social media sites.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Some of my favourite programming subbreddits&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;r/programming&lt;/li&gt;
&lt;li&gt;r/LearnProgramming&lt;/li&gt;
&lt;li&gt;r/cscareerquestions&lt;/li&gt;
&lt;li&gt;r/AskProgramming&lt;/li&gt;
&lt;li&gt;r/BadCode&lt;/li&gt;
&lt;li&gt;r/ProgrammerHumor&lt;/li&gt;
&lt;li&gt;r/CodingHelp&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Do let me know of your favourite subreddits!!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>guide</category>
    </item>
    <item>
      <title>Finding mean, median, and mode using SQL.</title>
      <dc:creator>Sumeet Bansal</dc:creator>
      <pubDate>Wed, 10 Aug 2022 22:10:10 +0000</pubDate>
      <link>https://forem.com/sumeet_bansal/finding-mean-median-and-mode-using-sql-46hc</link>
      <guid>https://forem.com/sumeet_bansal/finding-mean-median-and-mode-using-sql-46hc</guid>
      <description>&lt;p&gt;It is very often that we need to calculate averages in our data for various analysis tasks. It quite easy when it come to calculating Mean, Median and Mode in tools such as Excel with the help of inbuilt functions. &lt;/p&gt;

&lt;p&gt;In this this blog we will discuss ways to calculate these values using just SQL.&lt;/p&gt;

&lt;p&gt;Let us consider table below to find mean, median, and mode of salaries department wise for all the departments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;╔═══════╦════════╦════════╗
║ empid ║ deptid ║ salary ║
╠═══════╬════════╬════════╣
║ 1     ║ 1      ║ 12000  ║
╠═══════╬════════╬════════╣
║ 2     ║ 2      ║ 3400   ║
╠═══════╬════════╬════════╣
║ 3     ║ 3      ║ 45000  ║
╠═══════╬════════╬════════╣
║ 4     ║ 4      ║ 1320   ║
╠═══════╬════════╬════════╣
║ 5     ║ 4      ║ 5600   ║
╠═══════╬════════╬════════╣
║ 6     ║ 2      ║ 2700   ║
╠═══════╬════════╬════════╣
║ 7     ║ 1      ║ 12000  ║
╠═══════╬════════╬════════╣
║ 8     ║ 3      ║ 34000  ║
╠═══════╬════════╬════════╣
║ 9     ║ 4      ║ 4600   ║
╠═══════╬════════╬════════╣
║ 10    ║ 1      ║ 9000   ║
╠═══════╬════════╬════════╣
║ 11    ║ 1      ║ 13000  ║
╠═══════╬════════╬════════╣
║ 12    ║ 2      ║ 12000  ║
╠═══════╬════════╬════════╣
║ 13    ║ 4      ║ 1320   ║
╠═══════╬════════╬════════╣
║ 14    ║ 3      ║ 24000  ║
╠═══════╬════════╬════════╣
║ 15    ║ 3      ║ 45000  ║
╠═══════╬════════╬════════╣
║ 16    ║ 2      ║ 3400   ║
╠═══════╬════════╬════════╣
║ 17    ║ 2      ║ 1230   ║
╠═══════╬════════╬════════╣
║ 18    ║ 1      ║ 15000  ║
╚═══════╩════════╩════════╝
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finding mean is quite easy as it is just the average of all the salary for a department and we can use the AVG function for that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT deptid
    ,AVG(SALARY) AS mean_salary
FROM employee
GROUP BY deptid
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Calculating mode of salary for each department will be a bit tricky as we don't have inbuilt SQL functions for that.&lt;/p&gt;

&lt;p&gt;Steps for it are :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;For this we start by calculating salary count for each unique salary in each department.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then we rank the salary by its frequency (salary count) in each department.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;salary_counts AS (
    SELECT deptid,
        salary,
        COUNT(salary) AS salary_counts
    FROM employee 
    GROUP BY deptid, salary 
)
-- ranking salaries by frequency
, mode_salaries AS (
    SELECT deptid,
        salary,
        RANK() OVER( PARTITION BY deptid ORDER BY salary_counts DESC ) AS salary_counts_rank
    FROM salary_counts
)
SELECT deptid,
    salary as mode_salary
FROM mode_salaries
WHERE salary_counts_rank = 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For calculating median of salary for each department &lt;strong&gt;PERCENTILE_DISC&lt;/strong&gt; SQL function along with GROUP can help us calculate that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;median_salaries AS (
    SELECT deptid,
        PERCENTILE_DISC(0.5) WITHIN GROUP(ORDER BY salary) AS median_salary
    FROM employee
    GROUP BY deptid
)
SELECT deptid,
    median_salary 
FROM median_salaries
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that is how we can calculate mean, median, mode in SQL. &lt;/p&gt;

</description>
      <category>sql</category>
      <category>datascience</category>
      <category>beginners</category>
      <category>database</category>
    </item>
  </channel>
</rss>
