Solved

MSSQL 2008 Query use query result in new query

Posted on 2012-03-13
8
175 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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
 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 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