Solved

SQL Query Question

Posted on 2016-07-28
3
23 Views
Last Modified: 2016-07-28
Select custid, (custid % 3+1) as Value
From Sales.Customers


What does the (custid % 3+1) do?

The results of the query is:

Custid     Value
1               2
2               3
3               1
4               2
5               3
6               1
7               2
8               3
9               1
11             3
0
Comment
Question by:isames
3 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
0
 
LVL 16

Accepted Solution

by:
Brian Pringle earned 250 total points
Comment Utility
Think back to elementary school when you had to do division and leave a remainder.  That causes it to take the customer ID, divide by 3 and find the remainder.  Then, it adds 1 to the remainder.

1 / 3 = 0 R1 --> so the result would be 1 + 1 = 2
2 / 3 = 0 R2 --> so the result would be 2 + 1 = 3
11 / 3 = 3 R2 --> so the result would be 2 + 1 = 3
0
 
LVL 6

Assisted Solution

by:Manju
Manju earned 250 total points
Comment Utility
Yes, second Brian.

(custid % 3+1)

Custid     Value
1               2
2               3
3               1
4               2
11            3

if custid is 4, your query will do (4/3), leaving the reminder to add 1, so result is 2.

(11 % 3 + 1) = (11/3) = 3*3 = 9, so remainder is 2 (+1) = 3
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now