0

我得到了一个项目清单,每次都可以检查和通过不同的项目。选中的项目将被移动到包含这些选中项目的字符串列表中。

我想学习一种方法来帮助我创建按所选列分组所需的查询,例如,如果我检查“OperatorTypeName”和“CompanyName”,我将收到相关查询:

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

如果只检查“OperatorTypeName”,将收到针对此条件的适当查询:

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

我不想为每个条件创建每个查询,因为我可以使用大约 30 个可能的列创建一个组。每次有比上一个更大的查询时,我可能想调用 sql,但是如果我加载了 10 列,而我在下一次搜索中只加载了 2 列,我将无法从会话查询中对其进行分组,但是让我们从基础开始,如何在没有多个案例手动创建每个查询的情况下让它工作?

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 Query 构建方法:

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 和构建的查询之外的枚举方法:

        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 回答