Solved

MSSQL 2008 Query use query result in new query

Posted on 2012-03-13
8
178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Ephraim Wangoya
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 32

Expert Comment

by:Ephraim Wangoya
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
 

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:Ephraim Wangoya
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

636 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