Get the first workflow for each sales representative from the SQL database
This article explores how to retrieve the first row of data for each sales representative from a database table, focusing on the presence of multiple workflows per representative, with the goal of obtaining the initial workflow for the month and the year.
To do this, we can use the ROW_NUMBER() function as follows:
SELECT *
FROM (
SELECT
workflowid,
salesRepId,
quantityAssigned,
quantityLeft,
month,
year,
ROW_NUMBER() OVER (PARTITION BY salesRepId ORDER BY workflowid) AS rownumber
FROM sm_salesRepWorkflow
) AS RankedWorkflows
WHERE rownumber = 1;
This query first assigns the line number to each workflow record by sales representative ID. Then, it selects only records with line number equal to 1.
Example usage
Consider the following data:
salesRepId | quantityAssigned | quantityLeft | month | year | |
---|---|---|---|---|---|
WF_101 | EMP_101 | 100 | 90 | May | 2013 |
WF_101 | E MP_102 | 100 | 100 | May | |
WF_101 | EMP_103 | 100 | 80 | &&]May | |
WF_102 | EMP_101 | 100 | 70 |
workflowid | salesRepId | quantityAssigned | quantityLeft | month | |
---|---|---|---|---|---|
WF_101 | EMP_101 | 100 | 90 | May | |
WF_101 | EMP_102 | 100 | 100 | ||
2013 | WF_101 | EMP_103 | 100 | 80 |
These lines represent the first workflow for each representative in the specified month and year. Note that the sort of
workflowid
ORDER BY clause.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3