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?