Pergunta

I have a database (MSSQL) with a table as follows:

CREATE TABLE [dbo].[Ciphers](
    [Time] [datetime] NULL,
    [Hostname] [varchar](50) NULL,
    [ClientIP] [varchar](50) NULL,
    [UserAgent] [varchar](550) NULL,
    [CipherName] [varchar](200) NULL,
    [CipherVersion] [varchar](100) NULL,
    [CipherBits] [varchar](50) NULL
) ON [PRIMARY]

A couple of rows can look like this:

enter image description here

Now I want to count the number of occurrences of the combination of CipherName, CipherVersion and CipherBits for each top level domain.

Almost like this:

SELECT CipherName, CipherVersion, CipherBits, COUNT(ClientIP) As "Hits" 
from Ciphers
WHERE Hostname like '%domain.com' 
Group by CipherName, CipherVersion, CipherBits 

The crux is that I only want to count unique instances of each client IP. That is, if a customer has browsed to sub.domain.com and test.domain.com I don't want to count this as two, just one.

Foi útil?

Solução

Given that you aren't grouping by HostName, it looks like all you need is COUNT(DISTINCT x):

SELECT CipherName, CipherVersion, CipherBits, COUNT(DISTINCT ClientIP) As "Hits" 
from Ciphers
WHERE Hostname like '%domain.com' 
Group by CipherName, CipherVersion, CipherBits;

SqlFiddle here

Outras dicas

YOu can try this -

SELECT CipherName, CipherVersion, CipherBits, COUNT(DISTINCT ClientIP) OVER (PARTITION BY CipherName, CipherVersion, CipherBits) As "Hits" 
from Ciphers
WHERE Hostname like '%domain.com';
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top