w3resource

AdventureWorks Database: Get a newdate by adding two days with current date

SQL Query - AdventureWorks: Exercise-114 with Solution

114. From the following table write a query in SQL to obtain a newdate by adding two days with current date for each salespersons. Filter the result set for those salespersons whose sales value is more than zero.

Sample table: Sales.SalesPerson


Click to view Full table

Sample table: Person.Person


Click to view Full table

Sample table: Person.Address


Click to view Full table

Sample Solution:

SELECT p.FirstName, p.LastName  
 	,(now() + INTERVAL '2 day') AS "New Date" 
FROM Sales.SalesPerson AS s   
    INNER JOIN Person.Person AS p   
        ON s.BusinessEntityID = p.BusinessEntityID  
    INNER JOIN Person.Address AS a   
        ON a.AddressID = p.BusinessEntityID  
WHERE TerritoryID IS NOT NULL   
    AND SalesYTD <> 0;

Sample Output:

firstname|lastname         |New Date                     |
---------+-----------------+-----------------------------+
Michael  |Blythe           |2022-11-02 14:28:09.304 +0530|
Linda    |Mitchell         |2022-11-02 14:28:09.304 +0530|
Jillian  |Carson           |2022-11-02 14:28:09.304 +0530|
Garrett  |Vargas           |2022-11-02 14:28:09.304 +0530|
Tsvi     |Reiter           |2022-11-02 14:28:09.304 +0530|
Pamela   |Ansman-Wolfe     |2022-11-02 14:28:09.304 +0530|
Shu      |Ito              |2022-11-02 14:28:09.304 +0530|
José     |Saraiva          |2022-11-02 14:28:09.304 +0530|
David    |Campbell         |2022-11-02 14:28:09.304 +0530|
Tete     |Mensa-Annan      |2022-11-02 14:28:09.304 +0530|
Lynn     |Tsoflias         |2022-11-02 14:28:09.304 +0530|
Rachel   |Valdez           |2022-11-02 14:28:09.304 +0530|
Jae      |Pak              |2022-11-02 14:28:09.304 +0530|
Ranjit   |Varkey Chudukatil|2022-11-02 14:28:09.304 +0530|

SQL AdventureWorks Editor:

Practice Online


Contribute your code and comments through Disqus.

Previous: Add two days to each value in the OrderDate column.
Next: Get the differences between the maximum and minimum orderdate.


What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

SQL: Tips of the Day

Concatenate strings of a string field in a PostgreSQL 'group by' query:

Input:

ID   COMPANY_ID   EMPLOYEE
1    1            Anna
2    1            Bill
3    2            Carol
4    2            Dave
SELECT company_id, string_agg(employee, ', ')
FROM mytable
GROUP BY company_id;

Output:

COMPANY_ID   EMPLOYEE
1            Anna, Bill
2            Carol, Dave

Database: PostgreSQL

Ref: https://bit.ly/2XTiRjq

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook