SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Querying Results From a Query


Querying Results From a Query

Author
Message
waqqas.zia
waqqas.zia
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
Points: 32 Visits: 79
Good Evening Everyone,

Seeing as in the next few weeks my boss is going to be increasing my workload dramatically Crying i need to find a way of making my current work more efficient and I was hoping that some of you experts could help me with a particular issue which requires me to run 2 querys to produce one report.
I'll explain further...

When one of our engineers visits a site it is assigned a Reference (e.g. BS12345) and job type (TV Install, WIFI Install etc).
When this site is revisited for a service the assigned reference changes to BS12345R1 for the first revisit, R2 for the 2nd etc. and the job type will change to RVI (revisit). however the way it has been set up is there is no way to match the original job with the RVI.

Now what i do...
i query the database to extract all RVI's, i put this into excel, write a formula to remove the R1,R2 etc so im just left with the Job reference in the form BS12345,
I then put these references back into another query to bring back the original job type and job date
then i perform a VLookup to match the original Job with RVI to give me an original job type and RVI....

Very long winded...

I was hoping that someone could show me a technique in which i can use the results of the first query to run the 2nd without having to jump in and out of excel

Your help will be forever appreciated
Lowell
Lowell
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29156 Visits: 39985
we'd have to see the actual table structure and sample data (CREATE TABLE ...INSERT INTO)

but you could do something as easy as joining with an "exotic" join...that is not with an equals sign:

CREATE TABLE MainTable(Reference varchar(30),JobType varchar(30) )
INSERT INTO MainTable SELECT 'BS12345','TV Install' UNION ALL SELECT 'BS54321','WIFI Install'
CREATE TABLE RevisitTable(Reference varchar(30),JobType varchar(30))
INSERT INTO RevisitTable SELECT 'BS12345R1','RVI' UNION ALL SELECT 'BS12345R2','RVI'

SELECT * FROM MainTable
LEFT OUTER JOIN RevisitTable
ON CHARINDEX(MainTable.Reference,RevisitTable.Reference ) > 0

/*
Reference JobType Reference JobType
BS12345 TV Install BS12345R1 RVI
BS12345 TV Install BS12345R2 RVI
BS54321 WIFI Install NULL NULL
*/



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

sgmunson
sgmunson
SSCertifiable
SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)

Group: General Forum Members
Points: 7267 Visits: 4382
What happens when the main table contains BS1234 as well as BS12345 ?

As ugly as it might perform, you may need to join like this:


SELECT fld1, fld2, fld3
FROM VISITS AS V
LEFT JOIN REVISITS AS R
ON V.Reference = LEFT(R.Reference, CHARINDEX('R', R.Reference) - 1)



Steve
‌(aka sgmunson)
Smile Smile Smile
Health & Nutrition
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search