Author |
Topic |
duhaas
Constraint Violating Yak Guru
310 Posts |
Posted - 2007-09-14 : 10:02:37
|
I have the following:select CLIENT_CODE,count(TICKER) as [# of Securities] from POSgroup by CLIENT_CODEorder by CLIENT_CODE Give me results like the followingCLIENT_CODE # OF SECURITIES110903010 17110909017 1110911013 4110913019 211A204012 111B034012 211C046015 111C048011 111D412018 411D413016 411D414014 511G041012 2311G041020 9111J078011 111J079019 111J080017 211L065015 311M265010 6311M265028 211M266018 611M267016 1411M268014 111M269012 211M270010 311M272016 911ML11111 12711ML22222 134 I would like to get the average number of securites per CLIENT_CODE |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-14 : 10:09:06
|
[code]select CLIENT_CODE, count(TICKER) as [# of Securities]from POSgroup by CLIENT_CODEUNION ALLSELECT 'Total', AVG(TICKER)FROM POSorder by 1[/code] E 12°55'05.25"N 56°04'39.16" |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-14 : 10:12:13
|
[code]DECLARE @Sample TABLE (Code VARCHAR(9), Ticker INT)INSERT @SampleSELECT '110911013', 2 UNION ALLSELECT '110911013', 2 UNION ALLSELECT '110913019', 2 UNION ALLSELECT '11A204012', 1 UNION ALLSELECT '11B034012', 2 UNION ALLSELECT '11C046015', 1 UNION ALLSELECT '11C048011', 1 UNION ALLSELECT '11D412018', 1 UNION ALLSELECT '11D412018', 1 UNION ALLSELECT '11D412018', 1 UNION ALLSELECT '11D412018', 1 UNION ALLSELECT '11D413016', 4SELECT Code, COUNT(*) AS Securities, AVG(Ticker) AS YakFROM @SampleGROUP BY CodeORDER BY Code[/code] E 12°55'05.25"N 56°04'39.16" |
|
|
duhaas
Constraint Violating Yak Guru
310 Posts |
Posted - 2007-09-14 : 10:25:32
|
Trying to be a big boy and figure some things out on my own, but when i run it I get"The average aggregate operation cannot take a char data type as an argument"the column TICKER is a CHAR(9) |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-14 : 10:32:49
|
I see.Did you really think we could expect that behaviour when yuo have given us nothing to know about your table?Please remember, this is the first time we ever see your table!What kind of data do TICKER column hold?And also, how do you expect and string to be calculated as an average? E 12°55'05.25"N 56°04'39.16" |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-14 : 10:33:46
|
Try thisSELECT 'Total', 1.0 * COUNT(TICKER) / COUNT(DISTINCT CLIENT_CODE)FROM POS E 12°55'05.25"N 56°04'39.16" |
|
|
duhaas
Constraint Violating Yak Guru
310 Posts |
Posted - 2007-09-14 : 10:36:07
|
My apologizes, the end result I want is to find out the average number of tickers per account. A sampling of the ticker column is as follows:000752AA3000752AA3000752AA3000886AB700180TFF0001957BD000209TAA300252FAP6002824AJ9002824AK6 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-14 : 10:37:18
|
And what is the AVERAGE for "00180TFF0" and "001957BD0" ?????????????????????????????????????????? E 12°55'05.25"N 56°04'39.16" |
|
|
duhaas
Constraint Violating Yak Guru
310 Posts |
Posted - 2007-09-14 : 10:37:32
|
Thanks, I believe that worked, your hard work is appreciated. In posting more and more on this forum, I am beginning to learn that I need to offer up more data/example and am trying. |
|
|
duhaas
Constraint Violating Yak Guru
310 Posts |
Posted - 2007-09-14 : 10:40:31
|
I should of said average number of total TICKERS / per account. So count of all the tickers per account and than give me an average of the total per account |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-14 : 10:42:44
|
And again, AVERAGE OF WHAT?By default, an average is the sum of something divided by the number of samples producing the sum.Please post some proper and accurate sample data and your expected output.Also read this blog, please!http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx E 12°55'05.25"N 56°04'39.16" |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-14 : 10:43:54
|
[code]select CLIENT_CODE, count(TICKER) as [# of Securities], 1.0 * COUNT(TICKER) / COUNT(DISTINCT TICKER) AS Twigfrom POSgroup by CLIENT_CODEorder by CLIENT_CODE[/code] E 12°55'05.25"N 56°04'39.16" |
|
|
duhaas
Constraint Violating Yak Guru
310 Posts |
Posted - 2007-09-14 : 10:55:20
|
PESO, I appreciate your patience once again, and appreciate the advice offered up via that blog. In doingSELECT 'Total', 1.0 * COUNT(TICKER) / COUNT(DISTINCT CLIENT_CODE)FROM POS It's giving me the number I want. It's totalling up the total number of TICKER'S and dividing it up by the total number of accounts giving me the average number of tickers/account. Once again, thanks |
|
|
|