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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Help with query

Author  Topic 

tota00
Starting Member

10 Posts

Posted - 2009-08-27 : 06:22:57
Hi!

I have an sqlstatement looking like this:

SELECT Moduls.Modul_ID, Links.Link_Name, Links.Link_Url, Links.Sort, Links.Target,
Portals.Portal_ID, Links.Link_ID FROM Portals INNER JOIN Moduls ON
Portals.Portal_ID = Moduls.Portal_ID RIGHT OUTER JOIN Links ON
Moduls.Modul_ID = Links.Modul_ID
WHERE (Portals.Portal_ID = 45808)
ORDER BY Moduls.Sort, Moduls.Modul_Name, Links.Sort, Links.Link_Name

Output is:

Modul_ID Modul_Name Link_Name Sort Target Portal_ID Link_Id
5187 apa http://www.afton.com 50 0 45808 23416
5187 hepp http://www.zzz.com 50 0 45808 23415
5185 Apa http://www.apa.com 50 1 45808 23421
5185 bepa http://www.bepa.com 50 0 45808 23422
5185 link1 http://proof.telia.se 50 1 45808 23418
5184 link2 http://www.dn.se 50 0 45808 23419
5184 metro http://www.metro.se 50 1 45808 23420

What i need is to rewrite the statement so that one more column shows up for example "NrOfModuleId" And that column should in this case have the output like this:

NrOfModuleId
2
2
3
3
3
2

Ie. It´s a counter of how many unique occurrences of Module_Id there is!

Hope someone can help cause im stuck...

Best // Thomas

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-08-27 : 07:18:16
you can add a column to the select list:
count(Modul_ID) over (partition by Portal_ID,Modul_Id) as NrOfModuleId



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

tota00
Starting Member

10 Posts

Posted - 2009-08-27 : 07:37:03
quote:
Originally posted by webfred

you can add a column to the select list:
count(Modul_ID) over (partition by Portal_ID,Modul_Id) as NrOfModuleId



No i get an error:
Incorrect syntax near the keyword 'over'

Best // Thomas
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-08-27 : 08:00:15
You have posted in Transact-SQL (2005).
Which version you're using?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

tota00
Starting Member

10 Posts

Posted - 2009-08-27 : 08:10:37
quote:
Originally posted by webfred

You have posted in Transact-SQL (2005).
Which version you're using?



Sorry for that... Management Studio is 2008 BUT the actual DB i´m working against is 2000...

Best // Thomas
Go to Top of Page
   

- Advertisement -