Solved

SQL Query Question

Posted on 2016-07-28
3
33 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 70

Expert Comment

by:Éric Moreau
ID: 41733124
0
 
LVL 16

Accepted Solution

by:
Brian Pringle earned 250 total points
ID: 41733131
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
ID: 41733138
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

860 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