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.
Author |
Topic |
latiful
Starting Member
10 Posts |
Posted - 2007-03-10 : 19:39:43
|
Any help will be helpful for the following queryOverview:SQL Table with data:ID - Value - IPAddress======================1 - aa - 210.12 - aa - 210.13 - aa - 210.24 - aa - 210.15 - bb - 210.16 - bb - 210.17 - cc - 210.38 - cc - 210.49 - cc - 210.1Output:Value - TotalIPAddress - UniqueIPAddressaa - 4 - 2cc - 3 - 3bb - 2 - 1Query output explanation for aa - 44 - 2:For "aa", IPAddress "210.1" 3 times and "210.2" 1 time, so TotalIPAddress 4 and UniqueIPAddress 2I was trying with the following query but it is not giving me the right output for the UniqueIPAddress column. Can anybody tell me how can I fix it?select distinctr1.[Value],(Select count(IPAddress) from Report1 where Value=r1.Value) as 'TotalIPAddress',(Select count(IPAddress) from Report1 where Value=r1.Value and IPAddress=r1.IPAddress) as 'UniqueIPAddress'from Report1 r1Order by TotalIPAddress DescThanks for any reply. |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-10 : 20:57:07
|
[code]select Value, count(IPAddress) as TotalIPAddress , count(distinct IPAddress) as UniqueIPAddressfrom SQLTablegroup by Value[/code] KH |
 |
|
latiful
Starting Member
10 Posts |
Posted - 2007-03-10 : 22:47:17
|
I just need to extend it, need two more cols: OverallTotalIPAddress = (sum of TotalIPAddress col 4+3+2=9)and OverallTotalUniqueIPAddress = (sum of OverallTotalUniqueIPAddress col 2+3+1=6)Value - TotalIPAddress - OverallTotalIPAddress - UniqueIPAddress - OverallTotalUniqueIPAddressaa - 4 - 9 - 2 - 6cc - 3 - 9 - 3 - 6bb - 2 - 9 - 1 - 6Thanks for any reply. |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-10 : 23:11:44
|
[code]select *from( select Value, count(IPAddress) as TotalIPAddress , count(distinct IPAddress) as UniqueIPAddress from SQLTable group by Value) across join( select OverallTotalIPAddress = count(IPAddress), OverallTotalUniqueIPAddress = count(distinct IPAddress) from SQLTable) b[/code] KH |
 |
|
latiful
Starting Member
10 Posts |
Posted - 2007-03-11 : 03:15:17
|
Actually you given query is not giving right value for the last col(OverallTotalUniqueIPAddress), instead of 6 it's giving 3, i am guessing which is same as (Select count(distinct IPAddress) from Report1), but I want it will show sum of UniqueIPAddress column.Any idea???I have modified your given query(your help) in the following way and it's also giving same ouput as you, but still not the right value for 'OverallTotalUniqueIPAddress' column.Any help will be appreciate. Thanks for your time.select Value, count(IPAddress) as TotalIPAddress , (Select count(IPAddress) from Report1) as 'OverallTotalIPAddress', count(distinct IPAddress) as UniqueIPAddress, (Select count(distinct IPAddress) from Report1) as 'OverallTotalUniqueIPAddress' from Report1 group by Value |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-11 : 05:13:16
|
[code]-- declare table declare @SQLTable table( ID int, Value varchar(10), IPAddress varchar(10))-- Insert sample data provided into tableinsert into @SQLTableselect 1 , 'aa' , '210.1' union allselect 2 , 'aa' , '210.1' union allselect 3 , 'aa' , '210.2' union allselect 4 , 'aa' , '210.1' union allselect 5 , 'bb' , '210.1' union allselect 6 , 'bb' , '210.1' union allselect 7 , 'cc' , '210.3' union allselect 8 , 'cc' , '210.4' union allselect 9 , 'cc' , '210.1'-- Here's the queryselect TotalIPAddress, OverallTotalIPAddress, UniqueIPAddress, OverallTotalUniqueIPAddressfrom( select Value, count(IPAddress) as TotalIPAddress, count(distinct IPAddress) as UniqueIPAddress from @SQLTable group by Value) across join( select OverallTotalIPAddress = count(IPAddress), OverallTotalUniqueIPAddress = count(distinct IPAddress) from @SQLTable) b/* -- RESULTTotalIPAddress OverallTotalIPAddress UniqueIPAddress OverallTotalUniqueIPAddress -------------- --------------------- --------------- --------------------------- 4 9 2 42 9 1 43 9 3 4*/[/code]quote: Actually you given query is not giving right value for the last col(OverallTotalUniqueIPAddress), instead of 6 it's giving 3,
Actually my query counted 4 and not 3. Can you explain how do you counted 6 ? 210.1, 210.2, 210.3, 210.4 KH |
 |
|
latiful
Starting Member
10 Posts |
Posted - 2007-03-11 : 06:37:53
|
Quote: -------------------------------------------/* -- RESULTTotalIPAddress OverallTotalIPAddress UniqueIPAddress OverallTotalUniqueIPAddress -------------- --------------------- --------------- --------------------------- 4 9 2 42 9 1 43 9 3 4*/Can you explain how do you counted 6 ? -------------------------------------------OverallTotalUniqueIPAddress = sum of the column UniqueIPAddress.OverallTotalUniqueIPAddress = 2+1+3 = 6 (see above the result)Thanks again for your help. |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-11 : 06:43:27
|
2 + 1 + 3 is not the total unique IP Address. Total Unique IP = 210.1, 210.2, 210.3, 210.4 = 4What you want is not really total unique IP Address. It is just a sum of total unique IP by Value.So what do you want really ? KH |
 |
|
|
|
|
|
|