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
 Count Help

Author  Topic 

masond
Constraint Violating Yak Guru

447 Posts

Posted - 2013-07-10 : 10:47:18
HI All

Another question for you guys, (My mind gone blank today)

What i want to do is count how many times the [Omnipay_Account] column is populated with a number per parentid.

In the [Omnipay_Account] column you can either have a null or a 15 digit number
Idea result layout would be three columns these columns would be
Every row, would need to be grouped via the parentid number
ParentId Omnipay MSIP

My query is

SELECT
[ParentID]
,[Omnipay_Account]
FROM [FDMS].[dbo].[Dim_Outlet]

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-10 : 10:56:22
you need to use COUNT

something like


SELECT
[ParentID]
,COUNT([Omnipay_Account]) AS Cnt
FROM [FDMS].[dbo].[Dim_Outlet]
GROUP BY [ParentID]


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2013-07-10 : 10:58:20
Illustration with the help of data would make it easier for all so always appreciated if you could provide sample data.

you can use count function as following

SELECT ParentId,Count(Omnipay_Account)
From [FDMS].[dbo].[Dim_Outlet]
WHERE Omnipay_Account IS NOT NULL --if you're looking for only records where this field is populated
Group by ParentId

Cheers
MIK
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-10 : 11:00:24
Reading again you're asking for 3 columns in output
so it can be even this


SELECT
[ParentID]
,[Omnipay_Account]
,COUNT([Omnipay_Account]) OVER (PARTITION BY [ParentID]) AS Cnt
FROM [FDMS].[dbo].[Dim_Outlet]


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -