SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Distinct count help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

masond
Constraint Violating Yak Guru

447 Posts

Posted - 02/19/2014 :  08:42:07  Show Profile  Reply with Quote
Hey all

I need some help,

Aim- Need to count how many [FDMSAccountNo] fall under a unique parentid

This is my query
SELECT [FDMSAccountNo]
,ParentID
FROM [FDMS].[dbo].[Dim_Outlet]
where ParentID = '878028271889'
Which produces the following,
The number of fdmsaccountno under parentid is two. Its two because one of the fdmsaccountno is = to parentid

FDMSAccountNo ParentID
878028271889 878028271889
878028272887 878028271889
878028273885 878028271889

Desired result

Parentid #_of_outlets
878028271889 2

Looking forward to your help

SwePeso
Patron Saint of Lost Yaks

Sweden
30217 Posts

Posted - 02/19/2014 :  09:19:49  Show Profile  Visit SwePeso's Homepage  Reply with Quote
SELECT ParentID, COUNT(DISTINCT NULLIF(FDMSAccountNo, ParentID)) AS [#_of_outlets] FROM dbo.Dim_Outlet GROUP BY ParentID;



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 02/20/2014 :  00:34:15  Show Profile  Reply with Quote

SELECT COUNT([FDMSAccountNo]) AS NoOfOutlets
,ParentID
FROM [FDMS].[dbo].[Dim_Outlet]
where ParentID <>  [FDMSAccountNo]


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

SwePeso
Patron Saint of Lost Yaks

Sweden
30217 Posts

Posted - 02/22/2014 :  09:31:27  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Visakh, that will omit ParentID which has only Accountnumber equal to ParentID, and the ParentID will be missing completely from the list.



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 02/23/2014 :  02:44:26  Show Profile  Reply with Quote
ok. if you want to include that too just tweak it as

SELECT COUNT(CASE WHEN ParentID <>  [FDMSAccountNo] THEN [FDMSAccountNo] ELSE NULL END) AS NoOfOutlets
,ParentID
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

SwePeso
Patron Saint of Lost Yaks

Sweden
30217 Posts

Posted - 02/24/2014 :  12:34:06  Show Profile  Visit SwePeso's Homepage  Reply with Quote
And that is the same as NULLIF, right?


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000