w3resource

AdventureWorks Database: Find unit price, ID number, and modulus of division

SQL Query - AdventureWorks: Exercise-141 with Solution

141 From the following table write a query in SQL to return the ID number, unit price, and the modulus (remainder) of dividing product prices. Convert the modulo to an integer value.

Sample table: Sales.SalesOrderDetail


Click to view Full table

Sample Solution:

SELECT ProductID, UnitPrice, OrderQty,  
   CAST(UnitPrice AS INT) % OrderQty AS Modulo  
FROM Sales.SalesOrderDetail;

Sample Output:

productid|unitprice|orderqty|modulo|
---------+---------+--------+------+
      776| 2024.994|       1|     0|
      777| 2024.994|       3|     0|
      778| 2024.994|       1|     0|
      771| 2039.994|       1|     0|
      772| 2039.994|       1|     0|
      773| 2039.994|       2|     0|
      774| 2039.994|       1|     0|
      714|  28.8404|       3|     2|
      716|  28.8404|       1|     0|
      709|      5.7|       6|     0|
      712|   5.1865|       2|     1|
      711|  20.1865|       4|     0|
      762| 419.4589|       1|     0|
      758|  874.794|       1|     0|
      745|   809.76|       1|     0|
      743| 714.7043|       1|     0|
      747| 714.7043|       2|     1|
      712|   5.1865|       4|     1|
      715|  28.8404|       4|     1|
...

SQL AdventureWorks Editor:

Practice Online


Contribute your code and comments through Disqus.

Previous: Calculate sales targets per month for salespeople.
Next: Find marketing Assistants with more than 41 vacation hours.


What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

SQL: Tips of the Day

Convert Timestamp to date in MySQL Query:

DATE_FORMAT(FROM_UNIXTIME(`user.registration`), '%e %b %Y') AS 'date_formatted'

Database: MySQL

Ref : https://bit.ly/3EJPnMQ