Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Combining two query result sets. Expand / Collapse
Author
Message
Posted Monday, May 13, 2013 11:22 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, June 12, 2014 9:26 AM
Points: 22, 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'


Post #1452241
Posted Monday, May 13, 2013 12:15 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:05 PM
Points: 13,320, Visits: 12,804
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 Moden's 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)
Post #1452262
Posted Monday, May 13, 2013 12:59 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, June 12, 2014 9:26 AM
Points: 22, 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.
Post #1452280
Posted Monday, May 13, 2013 1:46 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:05 PM
Points: 13,320, Visits: 12,804
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 Moden's 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)
Post #1452298
Posted Monday, May 13, 2013 3:24 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 12:34 AM
Points: 20,857, Visits: 32,871
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';





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)
Post #1452336
Posted Tuesday, May 14, 2013 1:22 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: 2 days ago @ 1:51 AM
Points: 579, Visits: 1,255
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.
Post #1452416
Posted Tuesday, May 14, 2013 7:00 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, June 12, 2014 9:26 AM
Points: 22, 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?
Post #1452547
Posted Tuesday, May 14, 2013 7:12 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 12:34 AM
Points: 20,857, Visits: 32,871
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.




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)
Post #1452555
Posted Tuesday, May 14, 2013 7:13 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, December 12, 2014 10:09 AM
Points: 2,876, Visits: 5,201
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1452556
Posted Tuesday, May 14, 2013 7:16 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 12:34 AM
Points: 20,857, Visits: 32,871
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.




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)
Post #1452558
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse