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.



Follow us on Facebook and Twitter for latest update.

SQL: Tips of the Day

Is SQL syntax case sensitive?

The SQL Keywords are case-insensitive (SELECT, FROM, WHERE, etc), but are often written in all caps. However in some setups table and column names are case-sensitive. MySQL has a configuration option to enable/disable it. Usually case-sensitive table and column names are the default on Linux MySQL and case-insensitive used to be the default on Windows, but now the installer asked about this during setup. For MSSQL it is a function of the database's collation setting.

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

 





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