[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

sql help

Posted on 2016-09-20
5
Medium Priority
?
61 Views
Last Modified: 2016-09-23
I am trying to update the table. where I have following columns:
folioNo,pgPos,PgName,

folioNo is the order
pgPos is either right or left (if folioNo is odd then put L (left) if even put R(right)) . Let say I want to add another FolioNo in beteween 11 and 12. I will need to make new itemWith a folio 12 and PgPos R. and the following reordered accordingly with update in folioNo and PgPos
sample data
PDF_Filename	FolioNo	PgPos	
A-1	                             11	 	R	
A-2	                            12      	      L	
A-3	                              13      	R		
A-4	                            14	         L	
A-5	                            15	      R	
A-6	                            16	       L	

Open in new window


With cte As
(
SELECT folioNo,pgPos,
ROW_NUMBER() OVER (ORDER BY folioNo ) AS RN
FROM  tbl_160154dd
)

SELECT * FROM cte

Open in new window

0
Comment
Question by:erikTsomik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 32

Expert Comment

by:Pawan Kumar
ID: 41807864
@Author - Do you need an update query or Select Query?
0
 
LVL 32

Accepted Solution

by:
Pawan Kumar earned 2000 total points
ID: 41807867
Bro try this..

--


;With cte As
(
	SELECT folioNo,pgPos,
	ROW_NUMBER() OVER (ORDER BY folioNo ) AS RN
	FROM  tbl_160154dd
)
UPDATE x
SET x.pgPos = CASE WHEN RN % 2 = 0 THEN 'R' ELSE 'L' END 
FROM cte x


--

Open in new window


--
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 41807885
It would help if you can give us a data mockup of your desired return set, as it's not clear from the description.  Thanks.
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 41807897
I would expect something like this

PDF_Filename	FolioNo	PgPos	
A-1	                             11	 	R	
A-5                               12              L     I added this record
A-2	                             13      	       R	
A-3	                             14      	       L		
A-4	                             15	       R	
A-5	                             16	       L	
A-6	                             17	       R	

Open in new window

0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 41807997
Consider using a "computed column" instead

With this approach as soon as you insert a new row it will calculate the L or R value for you without needing an update.

ALTER TABLE tbl_160154dd
ADD [ComputedColumn] AS CASE WHEN RN % 2 = 0 THEN 'R' ELSE 'L' END

If you want to continue using [pgPos] as the column name you would need to drop that column first, then add it with the calculation

Note: It is possible to "persist" (store) the calculation, refer to the URL above for more details on this.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

656 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