我得到了一个项目清单,每次都可以检查和通过不同的项目。选中的项目将被移动到包含这些选中项目的字符串列表中。
我想学习一种方法来帮助我创建按所选列分组所需的查询,例如,如果我检查“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 列,我将无法从会话查询中对其进行分组,但是让我们从基础开始,如何在没有多个案例手动创建每个查询的情况下让它工作?