0

I got a list of items which can be checked and passed each time differently. The items selected will be moved into a string list containing those checked items.

I want to learn a way which will help me create the query required to group by those columns choosen, for example, If i check "OperatorTypeName" and "CompanyName" Ill receive relative query:

SELECT OperatorTypeName AS Group1, Company.CompanyName AS Group2, SUM(Adv.Price) 
AS SumPrice 
FROM Adv  (nolock) 
INNER JOIN Program (nolock) 
    ON Adv.ProgramID = Program.ProgramID 
INNER JOIN Operator (nolock)  
    ON Program.OperatorID = Operator.OperatorID 
INNER JOIN OperatorType (nolock)  
    ON Operator.OperatorTypeID = OperatorType.OperatorTypeID 
INNER JOIN CampaignVersion (nolock) 
    ON Adv.CampaignVersionID = CampaignVersion.CampaignVersionID 
INNER JOIN Campaign ON CampaignVersion.CampaignID = Campaign.CampaignID
INNER JOIN Campaign2Product (nolock) 
    ON Campaign.CampaignID = Campaign2Product.CampaignID 
INNER JOIN Product2Company (nolock) 
    ON Product2Company.ProductID = Campaign2Product.ProductID 
INNER JOIN Product2SubBranch (nolock) 
    ON Product2SubBranch.ProductID = Campaign2Product.ProductID 
INNER JOIN SubBranch (nolock) 
    ON SubBranch.SubBranchID = Product2SubBranch.SubBranchID
INNER JOIN Branch (nolock) 
    ON SubBranch.BranchID = Branch.BranchID
INNER JOIN Company (nolock) 
    ON Product2Company.CompanyID = Company.CompanyID 
INNER JOIN Company Manager (nolock) on Campaign.Manager =Manager.CompanyID 
             WHERE     (Adv.TransmitDate >= getdate()-7)
             GROUP BY  OperatorTypeName , Company.CompanyName

While if only "OperatorTypeName" will be checked , ill receive the appropriate query for this condition:

SELECT OperatorTypeName AS Group1, SUM(Adv.Price) AS SumPrice 
FROM Adv  (nolock) 
INNER JOIN Program (nolock) 
    ON Adv.ProgramID = Program.ProgramID 
INNER JOIN Operator (nolock)  
    ON Program.OperatorID = Operator.OperatorID 
INNER JOIN OperatorType (nolock)  
    ON Operator.OperatorTypeID = OperatorType.OperatorTypeID 
INNER JOIN CampaignVersion (nolock) 
    ON Adv.CampaignVersionID = CampaignVersion.CampaignVersionID 
INNER JOIN Campaign 
    ON CampaignVersion.CampaignID = Campaign.CampaignID
INNER JOIN Campaign2Product (nolock) 
    ON Campaign.CampaignID = Campaign2Product.CampaignID 
INNER JOIN Product2Company (nolock) 
    ON Product2Company.ProductID = Campaign2Product.ProductID 
INNER JOIN Product2SubBranch (nolock) 
    ON Product2SubBranch.ProductID = Campaign2Product.ProductID 
INNER JOIN SubBranch (nolock) 
    ON SubBranch.SubBranchID = Product2SubBranch.SubBranchID
INNER JOIN Branch (nolock) 
    ON SubBranch.BranchID = Branch.BranchID
INNER JOIN Company (nolock) 
    ON Product2Company.CompanyID = Company.CompanyID 
INNER JOIN Company Manager (nolock) on Campaign.Manager =Manager.CompanyID 
WHERE     (Adv.TransmitDate >= getdate()-7)
GROUP BY  OperatorTypeName

I don't want to create every single query for every single condition since I about 30 possible columns i could create a group by with. I might want to call the sql every time there is a bigger query than the last one, but if i've loaded like 10 columns and than i load only 2 of them in next search, ill get to group it from an sessioned query, But lets start with the basics, How can i get it to work without multiple dozens of cases to create every query manually?

4

2 に答える 2

1

このような何かがそれを行います:

string res = SQL.createSQL("OperatorTypeName");
string res2 = SQL.createSQL("OperatorTypeName", "Company.CompanyName");



public static class SQL
{
    public static string createSQL(params string[] columns)
    {
        string top = string.Empty;
        string bottom = string.Empty;
        for (int i = 0; i < columns.Length; i++)
        {
            if (i > 0)
            {
                top += ", ";
                bottom += ", ";
            }
            top += columns[i];
            bottom += columns[i];
            top += string.Format(" AS Group{0}", i + 1);
        }

        string sql = string.Format("SELECT {0}, SUM(Adv.Price) AS SumPrice " +
            "FROM Adv  (nolock) " +
            "INNER JOIN Program (nolock) " +
                "ON Adv.ProgramID = Program.ProgramID " +
            "INNER JOIN Operator (nolock) " +
                "ON Program.OperatorID = Operator.OperatorID " +
            "INNER JOIN OperatorType (nolock) " +
                "ON Operator.OperatorTypeID = OperatorType.OperatorTypeID " +
            "INNER JOIN CampaignVersion (nolock) " +
                "ON Adv.CampaignVersionID = CampaignVersion.CampaignVersionID " +
            "INNER JOIN Campaign " +
                "ON CampaignVersion.CampaignID = Campaign.CampaignID " +
            "INNER JOIN Campaign2Product (nolock) " +
                "ON Campaign.CampaignID = Campaign2Product.CampaignID " +
            "INNER JOIN Product2Company (nolock) " +
                "ON Product2Company.ProductID = Campaign2Product.ProductID " +
            "INNER JOIN Product2SubBranch (nolock) " +
                "ON Product2SubBranch.ProductID = Campaign2Product.ProductID " +
            "INNER JOIN SubBranch (nolock) " +
                "ON SubBranch.SubBranchID = Product2SubBranch.SubBranchID " +
            "INNER JOIN Branch (nolock) " +
                "ON SubBranch.BranchID = Branch.BranchID " +
            "INNER JOIN Company (nolock) " +
                "ON Product2Company.CompanyID = Company.CompanyID " +
            "INNER JOIN Company Manager (nolock) on Campaign.Manager =Manager.CompanyID " +
            "WHERE     (Adv.TransmitDate >= getdate()-7) " +
            "GROUP BY {1}", top, bottom);

        return sql;
    }
}
于 2013-03-05T10:39:38.610 に答える
0

最終的なコードは次のようになります。

変更を加えたSQLクエリ構築メソッド:

public static class SQL
{
    public static string createSQL(params string[] columns)
    {
        string top = string.Empty;
        string bottom = string.Empty;
        for (int i = 0; i < 5; i++)
        {
            if (i > 0)
            {
                top += ", ";
                if (i < columns.Length)
                {
                    bottom += ", ";
                }
            }

            if (i < columns.Length)
            {
                top += columns[i];
                bottom += columns[i];
            }
            else
            {
                top += "null";
            }
            top += string.Format(" AS Group{0}", i + 1);
        }

        string sql = string.Format("SELECT newid() as ID, {0}, SUM(Adv.Price) AS SumPrice " +
            "FROM Adv  (nolock) " +
            "INNER JOIN Program (nolock) " +
                "ON Adv.ProgramID = Program.ProgramID " +
            "INNER JOIN Operator (nolock) " +
                "ON Program.OperatorID = Operator.OperatorID " +
            "INNER JOIN OperatorType (nolock) " +
                "ON Operator.OperatorTypeID = OperatorType.OperatorTypeID " +
            "INNER JOIN CampaignVersion (nolock) " +
                "ON Adv.CampaignVersionID = CampaignVersion.CampaignVersionID " +
            "INNER JOIN Campaign " +
                "ON CampaignVersion.CampaignID = Campaign.CampaignID " +
            "INNER JOIN Campaign2Product (nolock) " +
                "ON Campaign.CampaignID = Campaign2Product.CampaignID " +
            "INNER JOIN Product2Company (nolock) " +
                "ON Product2Company.ProductID = Campaign2Product.ProductID " +
            "INNER JOIN Product2SubBranch (nolock) " +
                "ON Product2SubBranch.ProductID = Campaign2Product.ProductID " +
            "INNER JOIN SubBranch (nolock) " +
                "ON SubBranch.SubBranchID = Product2SubBranch.SubBranchID " +
            "INNER JOIN Branch (nolock) " +
                "ON SubBranch.BranchID = Branch.BranchID " +
            "INNER JOIN Company (nolock) " +
                "ON Product2Company.CompanyID = Company.CompanyID " +
            "INNER JOIN Company Manager (nolock) on Campaign.Manager =Manager.CompanyID " +
            "WHERE     (Adv.TransmitDate >= getdate()-7) " +
            "GROUP BY {1}", top, bottom);

        return sql;
    }
}

dbcontextと構築されたクエリを除くEnumaratingメソッド:

        public IEnumerable<GroupModel> GroupToEnum(DBS dbs, string query)
    {


        IEnumerable<GroupModel> grp = dbs.Groups.SqlQuery(query);


        return grp;
    }

次のように、コントローラーからこれらのメソッドを呼び出します。

            string res = SQL.createSQL(checked_strings);
        oGrp = qData.GroupToEnum(context, res);

さらに設定を行った後、ビューに戻ります。

return View("GroupTest", oGrp.ToPagedList(pageNumber, pageSize));
于 2013-03-06T10:26:57.687 に答える