Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Extracting data from tables

Author  Topic 

Sjefsgjogleren
Starting Member

2 Posts

Posted - 2012-02-09 : 08:43:26
Hello!
I'm working on a master thesis, that requires some simple SQL programming. The problem is, that my skills in SQL programming is close to none existing. I'm really hoping that you guys can help me out on this one!:)

Problem:
I'm working on a relativley big database and need to extract some data. I'm having several tables, and need to use several tags in order to extract the needed data.

In the first table, lets call it Alarm, I'm specifiying a certain AlarmId, such that all alarms with this Id connectet to it appears. This is done with the standard:
Select*from table_name where AlarmId='xxx' statment (that even I can produce!:)
The thing is that relatet to all alarms there is a StopId. You can then check out that certain stopid in a different table, lets call it Stop, in order to see if any other warnings occured together with the logged alarm.

My question is then, can anyone help me with creating a statment that makes me specify the AlarmId, and then automatically counts up which other warnings that occured together with the logged alarm on all the different StopId relatet to the specified AlarmId?:)

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-09 : 08:54:41
What's the Thesis on?


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-09 : 12:46:11
this maybe?

Select t.AlarmId,COUNT(s.StopId) AS StopCnt
from table_name t
inner join othertable s
on s.AlarmId = t.AlarmId
where t.AlarmId='xxx'
group by t.AlarmId


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Sjefsgjogleren
Starting Member

2 Posts

Posted - 2012-02-09 : 17:33:59
Thanks.

Unfortunatley didn't quite give the answer I was hoping for. This querry was summing up the total number of times that AlarmId='xxx' was found within the 'othertable'. The thing is that the AlarmId needs to be connectet to table_name (first table) since it also can be found within the othertable, but then not as the alarm 'trigger'.
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-02-09 : 17:46:32
It's tough to tell how your data relates from your description. Can you please post DDL, DML and Expected output in a consumable format?

Here are two link that can help you compile that information so we can help you better:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-09 : 17:49:07
quote:
Originally posted by Sjefsgjogleren

Thanks.

Unfortunatley didn't quite give the answer I was hoping for. This querry was summing up the total number of times that AlarmId='xxx' was found within the 'othertable'. The thing is that the AlarmId needs to be connectet to table_name (first table) since it also can be found within the othertable, but then not as the alarm 'trigger'.


sorry didnt get that
can you give an example with sample data and required output?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -