Code Explainer | DAX

SQL code for Weekly Sales Summary Report

This SQL code extract provides a summarized report of total sales per week using PostgreSQL's `DATE_TRUNC` function, `JOIN` operation, and aggregate functions like `SUM`.


Empty image or helper icon

Prompt

SELECT
    DATE_TRUNC('week', soh.orderdate) AS week_start_date,
    SUM(sod.orderqty * sod.unitprice) AS total_sales
FROM
    {{ Table("salesorderheader") }} soh
JOIN
    {{ Table("salesorderdetail") }} sod ON soh.salesorderid = sod.salesorderid
GROUP BY
    week_start_date
ORDER BY
    week_start_date;

Answer

This SQL code extract is designed to provide a summarized report of total sales per week. The code uses PostgreSQL's DATE_TRUNC function, JOIN operation, and aggregate functions like SUM.

Let's break this code down:

'SELECT' Clause

SELECT
    DATE_TRUNC('week', soh.orderdate) AS week_start_date,
    SUM(sod.orderqty * sod.unitprice) AS total_sales

This part of the code is responsible for defining which information will be extracted from the database.

  • DATE_TRUNC('week', soh.orderdate) AS week_start_date: Here, the DATE_TRUNC function is used to get the first day of the week (by default Monday) from the orderdate. The 'week' parameter instructs the function to truncate the orderdate down to the week, effectively standardizing the time to the start of the week.

  • SUM(sod.orderqty * sod.unitprice) AS total_sales: This calculates the total sales by multiplying orderqty and unitprice and then summarizing it with the SUM function.

'FROM' and 'JOIN' Clauses

FROM
    {{ Table("salesorderheader") }} soh
JOIN
    {{ Table("salesorderdetail") }} sod ON soh.salesorderid = sod.salesorderid

This part of the code indicates the source of the data needed for the retrieval operation.

  • FROM {{ Table("salesorderheader") }} soh: This specifies that the data is being retrieved from the salesorderheader table. soh is an alias used for brevity.

  • JOIN {{ Table("salesorderdetail") }} sod ON soh.salesorderid = sod.salesorderid: This statement joins the salesorderheader table with the salesorderdetail table based on the condition soh.salesorderid = sod.salesorderid.

'GROUP BY' and 'ORDER BY' Clauses

GROUP BY
    week_start_date
ORDER BY
    week_start_date;
  • GROUP BY week_start_date: This statement organizes the selected data into groups by each week's start date. This is necessary for aggregate functions such as SUM to operate correctly.

  • ORDER BY week_start_date;: Finally, the ORDER BY clause organizes the output rows by the week start date in ascending order.

Please note that {{ Table("") }} is a placeholder and it should be replaced with the actual table name, if you want to run the query in any SQL-based database management system.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This SQL code extract demonstrates how to generate a weekly sales summary report using data from the salesorderheader and salesorderdetail tables. The code calculates the total sales for each week by multiplying the orderqty and unitprice columns in the salesorderdetail table and then summing them using the SUM function. It uses the DATE_TRUNC function to truncate the orderdate column to the start of the week. The tables are joined based on the salesorderid column. The result is grouped by the week start date and ordered in ascending order.