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


Combining two query result sets.


Combining two query result sets.

Author
Message
William Gary Wright
William Gary Wright
SSC Rookie
SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)

Group: General Forum Members
Points: 26 Visits: 112
Well I tried this in the reporting services forum got 1 reply and it seems to have fallen into the crack. I have not presented my request here the same as I did in the forum.
Anyway, I am trying to write a report. The primary query looks like this.
Select
*
from streets
where
calltime >= 'Sometime'
and
street = 'Some street' (streetname)
and
streetnumber >= somenumber (streetfrom)
and
streetnumber <= somenumber (streetto)
The values for the where clause will come form this query and I need to loop these values through the first query.
select
streetname,streetfrom,streetto
from streetranges
where
streetcategory = 'CFMH'
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26305 Visits: 17553
The reason you did not get any responses in your original thread (http://www.sqlservercentral.com/Forums/Topic1451201-150-1.aspx) will be the same reason you don't get any here. You didn't post enough information for anybody to help.

We can't see your screen, we don't your business requirements, we are not intimate with your data structures and we have no concrete idea of what you are trying to do.

I have a feeling the sql for this is pretty easy but without details we are shooting in the dark.

Please take a few minutes and read the first article in my signature for best practices when posting questions. Then come back and post ddl, sample data and desired output. You will find lots of people willing and able to help joining in very quickly.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
William Gary Wright
William Gary Wright
SSC Rookie
SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)

Group: General Forum Members
Points: 26 Visits: 112
I work at a 911 center. Our data is sensitive and proprietary. I also have a contract with our software vendor to not disclose any information about their software. With that in mind this is why I posted the way I did, most of the names I used are not even used in the actual database. I tried to explain what I need without getting into to much detail about the data and I thougt what I sent might be enouigh.
I will try again.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26305 Visits: 17553
William Gary Wright (5/13/2013)
I work at a 911 center. Our data is sensitive and proprietary. I also have a contract with our software vendor to not disclose any information about their software. With that in mind this is why I posted the way I did, most of the names I used are not even used in the actual database. I tried to explain what I need without getting into to much detail about the data and I thougt what I sent might be enouigh.
I will try again.


I certainly understand sensitive data. Many of us around here work with sensitive data as a normal part of our day. For these types of things you just need obfuscate the data but keep it representative of the issue. Certainly nothing wrong with changing column names and only including columns relevant to the issue at hand. That makes it an example of how to do whatever it is you are doing and does reveal any real actual information.

I was pretty sure that all you needed was to join those two tables until I ran into this:


The values for the where clause will come form this query and I need to loop these values through the first query.
select


I have no idea what that means.

I was thinking initially that you wanted something like you were trying to use streetranges columns as some way to validate what is there.

Completely blind due to no information but here are a couple of shots in the dark.


Select
*
from streets s
join streetranges sr on sr.streetnumber = s.streetnumber
join streetranges sr2 on sr.streetname = s.streetname



OR MAYBE...


select *
from streets s
where
calltime >= 'Sometime'
and
street in (select streetname from streetranges)
and
streetnumber >= (select min(streetfrom) from streetranges)
and
streetnumber <= (select min(streetto) from streetranges)



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39764 Visits: 38563
Does this give you what you are looking for?



Select
*
from
streets s
inner join streetranges sr
on (s.street = sr.streetname and
s.streetnumber >= sr.streetfrom and
s.streetnumber <= s.streetto)
where
calltime >= 'Sometime' and
sr.streetcategory = 'CFMH';




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
P Jones
P Jones
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1245 Visits: 1517
If streetranges is actually the result of the first query, you could try a CTE (common table expression - look it up in Books Online) to create and then use it . Or a temp table if you're on an earlier version of sql server.
William Gary Wright
William Gary Wright
SSC Rookie
SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)

Group: General Forum Members
Points: 26 Visits: 112
I left the office last night being half way done writing a script to create a database with some data in it for you all to look at. I came in this morning and read the new replies to my post. I had never considered a join. All of the examples of joins I have seen there was always an = involved and I had not seen >= used in a join. This worked great and it makes sense to me.

Thanks Lynn,
Bill

PS
How does this points thing work?
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39764 Visits: 38563
William Gary Wright (5/14/2013)
I left the office last night being half way done writing a script to create a database with some data in it for you all to look at. I came in this morning and read the new replies to my post. I had never considered a join. All of the examples of joins I have seen there was always an = involved and I had not seen >= used in a join. This worked great and it makes sense to me.

Thanks Lynn,
Bill

PS
How does this points thing work?


How you view the points is up to you actually. Some view it as a show of experience on the site, of a supposed level of knowledge. Others just view it as a level of activity in this particular community.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Eugene Elutin
Eugene Elutin
SSCertifiable
SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)

Group: General Forum Members
Points: 5028 Visits: 5478
William Gary Wright (5/14/2013)
I left the office last night being half way done writing a script to create a database with some data in it for you all to look at. I came in this morning and read the new replies to my post. I had never considered a join. All of the examples of joins I have seen there was always an = involved and I had not seen >= used in a join. This worked great and it makes sense to me.

Thanks Lynn,
Bill

PS
How does this points thing work?


This article is worth to read (about hidden "RBAR" in triangular joins):

http://www.sqlservercentral.com/articles/T-SQL/61539/

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39764 Visits: 38563
Eugene Elutin (5/14/2013)
William Gary Wright (5/14/2013)
I left the office last night being half way done writing a script to create a database with some data in it for you all to look at. I came in this morning and read the new replies to my post. I had never considered a join. All of the examples of joins I have seen there was always an = involved and I had not seen >= used in a join. This worked great and it makes sense to me.

Thanks Lynn,
Bill

PS
How does this points thing work?


This article is worth to read (about hidden "RBAR" in triangular joins):

http://www.sqlservercentral.com/articles/T-SQL/61539/


Eugene,

Yes, it is a good article on triangular joins, I just hope you don't think that the code I provided is a triangular join. It is actually a bound join with a lower an upper bound.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
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