LaVOZs

The World’s Largest Online Community for Developers

'; c# - Creating User defined aggregation for SQL - LavOzs.Com

Here’s a sample of what I have in mind. When you query tbl with the aggregation function you should be this Result

Tally Agregation function

Table: tbl  

Tag length              
abc 8               
cde 8               
fgh 10      

SQL:

SELECT aggTally(Tag, Length) FROM tbl   

Result:

2/8   
1/10            

I am very new to C#, so How do I create this, and have the dll for use?

I don't see how Tag is used as mentioned above but the below code returns

2/8 1/10

->

    private static void TotalsByLength()
    {
        List<Tuple<string, int>> tagdata = new List<Tuple<string, int>>
        {
            new Tuple<string, int>("abs", 8),
            new Tuple<string, int>("cde", 8),
            new Tuple<string, int>("fgh", 10)
        };

        var tagcounts = from p in tagdata
            group p.Item2 by p.Item2 into g
            orderby g.Count() descending
            select new { g.Key, TotalOccurrence = g.Count() };

        foreach (var s in tagcounts)
        {
            Console.WriteLine("{0}/{1}", s.TotalOccurrence, s.Key );
        }
    }

T-SQL approach:

CREATE TABLE dbo.TestTable (tag CHAR(3), taglen INT)
GO

INSERT INTO dbo.TestTable VALUES ('abs',8), ('cde', 8), ('fgh',10)
GO

;WITH TagTotal AS (SELECT taglen, COUNT(*) AS totallengthbytag
FROM dbo.TestTable
GROUP BY taglen)
SELECT a.totallengthbytag, a.taglen
FROM TagTotal a
Related
How can I prevent SQL injection in PHP?
How do I perform an IF…THEN in an SQL SELECT?
Add a column with a default value to an existing table in SQL Server
How to return only the Date from a SQL Server DateTime datatype
How to concatenate text from multiple rows into a single text string in SQL server?
Inserting multiple rows in a single SQL query?
How do I UPDATE from a SELECT in SQL Server?
Finding duplicate values in a SQL table
How do I import an SQL file using the command line in MySQL?
Wrong result of user defined aggregate in SQL Server