Solved

MSSQL 2008 Query use query result in new query

Posted on 2012-03-13
8
173 Views
Last Modified: 2012-04-27
Hi. i try to figure this out what kind of syntax to use but cant get this to work.
I want. Search in a database after most freqent ID

then make a new query and search in diffrent database whit the result from the first search as a parameter...  please have a look belowe..  I verry new in MSSQL so be nice:D



SELECT DB01.ID, Count(DB01.ID) AS Antal
FROM DB01
where Timestamp BETWEEN '2010-01-13 04:22:53.390' AND '2012-02-13 05:22:53.390'
GROUP BY DB01.ID
HAVING Count(DB01.ID)>1
order by Antal desc

Results  I get..
ID            Antal
09366      936
33593      368
12118      349
11306      303
10424      233
36535      153
38255      131
35700      130

Then I would like to use

the first result "09366"
and search in a nother db.
ex.



Select *
from DB199
where ID=09366
order by Timestamp asc
0
Comment
Question by:kavvis
8 Comments
 
LVL 12

Expert Comment

by:Sharon Seth
ID: 37713878
Select *
from DB199
where ID=(Your first SQL statment here)
order by Timestamp asc
The first SQL stmt must be modified to return the first row . I would do the following on sybase , not sure abt SQLServer

select top 1 COLUMN_NAME from TABLE_NAME order by COLUMN_NAME desc
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 37713886
try

declare @MaxID int

SELECT TOP 1 @MaxID = ID
from
(
	SELECT DB01.ID, Count(DB01.ID) AS Antal
	FROM DB01
	where Timestamp BETWEEN '2010-01-13 04:22:53.390' AND '2012-02-13 05:22:53.390'
	GROUP BY DB01.ID
	HAVING Count(DB01.ID)>1 	
) A
order by A.Antal desc

Select *
from DB199
where ID=@MaxID 
order by Timestamp asc

Open in new window

0
 

Author Comment

by:kavvis
ID: 37713914
get some problem

Msg 206, Level 16, State 2, Line 13
Operand type clash: uniqueidentifier is incompatible with int

in the second databse the Id is  declared like a  uniqueidentifier

I can´t change this :(  can I fix this whit your querys?
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 37713990
Try declaring it as uniqueidentifier

declare @MaxID uniqueidentifier

SELECT TOP 1 @MaxID = ID
from
(
	SELECT DB01.ID, Count(DB01.ID) AS Antal
	FROM DB01
	where Timestamp BETWEEN '2010-01-13 04:22:53.390' AND '2012-02-13 05:22:53.390'
	GROUP BY DB01.ID
	HAVING Count(DB01.ID)>1 	
) A
order by A.Antal desc

Select *
from DB199
where ID=@MaxID 
order by Timestamp asc

Open in new window

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:kavvis
ID: 37714141
The problem is in the first DB it´s an int
and in my second database the id is declared like uniqueidentifier

so thaat´s the problem
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 37714204
Can you check the values of ID in your second table. You do understand that they are not integers but GUID
You will not be able to do any meaningful comparison between the two fields. Your second table ID should also have been an integer, or create a new integer field used for joining the two tables.
0
 
LVL 25

Accepted Solution

by:
jogos earned 500 total points
ID: 37714213
<<The problem is in the first DB it´s an int
and in my second database the id is declared like uniqueidentifier<<
Then I think the Id's don't match in any way.  Just take a select top 10 ID from both tables .... and tell us how those 2 ever will match.

 Probably you should loock for a name or a description that can replace the id for a match in your sql
0
 

Author Closing Comment

by:kavvis
ID: 37901109
thank you all for your help.. need to change my question to get this done...
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now