Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with complex query

Posted on 2014-04-10
11
Medium Priority
?
166 Views
Last Modified: 2014-05-02
Hi,

I’m having trouble working how to finish my SQL statement

Basically my SQL gets me this:

sequence      UniqueItemId      reports_to
0      18898-0      18898
1      18899-1      18898
2      18900-2      18899
3      14928-3      18900
4      19624-4      14928
5      20071-5      19624
6      19625-6      19624
7      237-7      19624
8      12389-8      19624
9      12390-9      12389
10      14100-10      19624
11      19623-11      19624
12      19841-12      19624
13      19842-13      19841
14      12222-14      14928
15      12225-15      14928
16      14580-16      14928
17      12232-17      14928
18      12385-18      12232
19      12396-19      14928

You have the making of data that I hopefully will be able to get into a format that allows me to build a chart.
This would be easy if the item_id value was unique, however it’s not, to try and address this I added a -# to the end.
What I now need to do which I can’t figure out is get to state where the data looks like this

sequence      UniqueItemId      reports_to      new_reports_to
0      18898-0      18898      
1      18899-1      18898      18898-0
2      18900-2      18899      18899-1
3      14928-3      18900      18900-2
4      19624-4      14928      14928-3
5      20071-5      19624      19624-4
6      19625-6      19624      19624-4
7      237-7      19624      19624-4
8      12389-8      19624      19624-4
9      12390-9      12389      12389-8
10      14100-10      19624      19624-4
11      19623-11      19624      19624-4
12      19841-12      19624      19624-4
13      19842-13      19841      19841-12
14      12222-14      14928      14928-3
15      12225-15      14928      14928-3
16      14580-16      14928      14928-3
17      12232-17      14928      14928-3
18      12385-18      12232      12232-17
19      12396-19      14928      14928-3

Basically updating the reports to link with the correct identifier.

Any suggestions as to how I can accomplish this is most appreciated as I’m fresh out of ideas

The actual data has 1000s of rows.

TIA.
0
Comment
Question by:andyw27
[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
  • 4
  • 2
  • 2
  • +2
11 Comments
 
LVL 18

Expert Comment

by:Jerry Miller
ID: 39991563
What does your SQL query and tables structure look like?

So you are saying that there can be multiple 19624 items? How will you ever know that you need 19624-4 and not 19624-1, 19624-2, or 19624-3?

You really need a way to have unique values associated with each one even if you build a bridge table and use it in the joins of your query.
0
 

Author Comment

by:andyw27
ID: 39991584
Maybe I’ve been staring at the problem too long, could you set something up that basically says

Search the rows and return uniqueitemid where the first 5 digits = report to and the number after the dash is less than the current row value of column Sequence, encapsulate this into an update statements that runs on temp table which current holds this data?

Unfortunately the SQL statement is on a system that has no connectivity so I can’t really post it.
0
 
LVL 32

Expert Comment

by:awking00
ID: 39991728
Are you looking for a SQL Server solution or an Oracle solution? Please show the version of whichever it is (or both are).
0
CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

 

Author Comment

by:andyw27
ID: 39991784
SQL please.
0
 
LVL 32

Expert Comment

by:awking00
ID: 39991846
And what version? SQL Server 2012 has lead, lag, and first_values functions that would be most helpful in finding a solution for your requirement.
0
 

Author Comment

by:andyw27
ID: 39991861
2008 10.50.400
0
 
LVL 15

Expert Comment

by:Devinder Singh Virdi
ID: 39992037
To me it looks like same example of who is manager of employee.
Please let us know if you are looking something like this
http://docs.oracle.com/cd/B28359_01/server.111/b28286/queries003.htm
0
 
LVL 18

Expert Comment

by:Jerry Miller
ID: 39992749
So you are saying that there can be multiple 19624 items? How will you ever know that you need 19624-4 and not 19624-1, 19624-2, or 19624-3?
0
 
LVL 41

Accepted Solution

by:
Sharath earned 2000 total points
ID: 39992816
try this.
;WITH CTE 
     AS (SELECT *, 
                CONVERT(INT, SUBSTRING(UniqueItemId, 1, CHARINDEX('-', UniqueItemId) - 1))                 UniqueItemId1, 
                CONVERT(INT, SUBSTRING(UniqueItemId, CHARINDEX('-', UniqueItemId) + 1, LEN(UniqueItemId))) UniqueItemId2 
           FROM test) 
SELECT [sequence],UniqueItemId,reports_to,
       CONVERT(VARCHAR, reports_to) + '-' 
       + CONVERT(VARCHAR, (SELECT MAX(t2.UniqueItemId2) FROM CTE t2 WHERE t1.reports_to = t2.UniqueItemId1 AND t1.UniqueItemId2 > t2.UniqueItemId2)) AS new_reports_to
  FROM CTE t1 

Open in new window

http://sqlfiddle.com/#!3/28344/9
0
 

Author Comment

by:andyw27
ID: 40036672
great answer thanks, better than my solution.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

730 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