Showing posts with label base. Show all posts
Showing posts with label base. Show all posts

Sunday, March 25, 2012

(how frequent) in SQL Server?!

I have used Base SAS for analysis for a while and it was really great.. everything is easy just with a simple command.. I am sure it's not the same in SQL Server but I need some help on how to start with the following:

I have a field called call_country and another field called call_minute. Each call will be saved with the destination country and the total number of minutes..

and I want to run a query to see what are the TOP frequent destinations in this format:

United States - Count: 420 - Total Minues: 12,345

It should be easy in SQL too.

SELECT call_country, COUNT(call_minutes) AS tCount, SUM(call_minutes) AS TotalMinutes

FROM calltable

GROUP BY call_country

|||

If you want use the top, you can do this:

--1.based on total minutes

SELECT TOP (1) call_country, COUNT(call_minutes) AS tCount, SUM(call_minutes) AS TotalMinutes

FROM calltable

GROUP BY call_country

ORDER BY TotalMinutes DESC

--2. based on total count

SELECT TOP (1) call_country, COUNT(call_minutes) AS tCount, SUM(call_minutes) AS TotalMinutes

FROM calltable

GROUP BY call_country

ORDER BY tCount DESC

--3.based on total count and use total minutes as tie break.

SELECT TOP (1) call_country, COUNT(call_minutes) AS tCount, SUM(call_minutes) AS TotalMinutes

FROM calltable

GROUP BY call_country

ORDER BY tCount DESC, TotalMinutes DESC

--4.based on total minutes and use total count as tie break.

SELECT TOP (1) call_country, COUNT(call_minutes) AS tCount, SUM(call_minutes) AS TotalMinutes

FROM calltable

GROUP BY call_country

ORDER BY TotalMinutes DESC, tCount DESC

Monday, February 13, 2012

ê, ç, á, ã, ó,á

Anybody, by chance, does have or did it already elaborate some script that
substitutes all the special characters of a base of Customers for instance
(ê, ç, á, ã, ó,á...), and does it can me to help as doing that?You can create a table with these special characters and with which
character you want it replaced.
For example:
CREATE TABLE CharacterReplacements
(OriginalChar nchar(1) not null primary key
,ReplacementChar nchar(1) not null
)
INSERT INTO CharacterReplacements VALUES (N'ê',N'e')
INSERT INTO CharacterReplacements VALUES (N'ç',N'c')
Declare @.mytext nvarchar(100)
Set @.mytext='Comment ça va?'
SELECT @.mytext = Replace(@.mytext, OriginalChar, ReplacementChar)
FROM CharacterReplacements
SELECT @.mytext
Hope this helps,
Gert-Jan
Frank Dulk wrote:
> Anybody, by chance, does have or did it already elaborate some script that
> substitutes all the special characters of a base of Customers for instance
> (ê, ç, á, ã, ó,á...), and does it can me to help as doing that?