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

Two query results mixed together Expand / Collapse
Author
Message
Posted Thursday, April 30, 2009 7:40 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, October 17, 2013 12:26 AM
Points: 8, Visits: 52
Dear All,

I am facing a very strange problem and i would highly appreciate if some one can help me.

i have 3 query and their results inserting into a temp table but the results of one query gets clubbed with another query.

Ex. 1 Query . insert into temp select top 2 * from table (5 columns)
Query . insert into temp values('','','','','') [extra line break for visibility]

2 Query . insert into temp select top 2 * from table1 (5 columns)
Query . insert into temp values('','','','','') [extra line break for visibility]

3 Query . insert into temp select top 2 * from table (5 columns)

Now few reocrds of query 1 result gets merge with query 3 result.

I am in dilemma why exactly it happens as i had tried delay for five seconds between each query.

Please suggest me to sort out this issue.

Thanks in Advance.

Vizay

Post #707679
Posted Thursday, April 30, 2009 8:19 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, October 21, 2013 12:32 AM
Points: 2,556, Visits: 2,593
I don't think I understood very well, can explain us what do you mean by "the results of one query gets clubbed with another query"? How about adding some sample data with the generated output for the sample data?

--Ramesh

Post #707714
Posted Thursday, April 30, 2009 11:49 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, October 17, 2013 12:26 AM
Points: 8, Visits: 52
Hi ,

It means that when i select the list from temp table i found that few records of 1 query get display with the records of 3 query.

Ex . if 1 query returns 50 rows and and 3 query returns 20 rows then ... while reviewing the result set from temp table ... i see 45 records from 1 query and 25 records from 3 query.

As i have put some spaces between the queries so that it can be easily understood the result set of all 3 different queries...

I hope this time i had explained properly...

Regards,
Vizay
Post #708267
Posted Monday, May 4, 2009 3:45 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, October 21, 2013 12:32 AM
Points: 2,556, Visits: 2,593
If you want the rows from a query to be returned in any specific order then you must specify the ORDER BY clause. For e.g.

SELECT * FROM SomeTempTable ORDER BY SomeKey


--Ramesh

Post #709266
Posted Tuesday, May 5, 2009 9:26 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, December 6, 2012 2:28 PM
Points: 185, Visits: 1,542
Are you trying to get 3 different result sets with the queries?
Or
One table with the results from 3 different queries in it?
Post #710364
Posted Tuesday, May 5, 2009 1:52 PM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Yesterday @ 6:14 PM
Points: 58, Visits: 680
You could add a "sort" column to your temporary table - like an Identity column that simply generates a sequential number, incremented for each row inserted into the table.

You can then use ORDER BY when querying from the temp table, which should give you the result you are after.


Chris
Post #710609
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse