在SQL中创建数据多维数据集

时间:2019-12-13 00:22:57

标签: sql olap cube olap-cube

鉴于下面的表和描述,我试图将SQL与GROUP BY,CUBE和ROLLUP一起使用以创建多维数据集。我的查询有问题,因为基数应该返回〜1440行,但是我的多维数据集的基数仅返回59 ...

Tb_Supplier(Supp_ID, Name, City, State)
Tb_Consumer(Con_ID, Name, City, State)
Tb_Product(Prod_ID, Name, Product_Category, Product_Line, Product_Packaging)
Tb_Offers(Supp_ID, Prod_ID, Quantity, Price)
Tb_Requests(Con_ID, Prod_ID, Quantity, Price)
Tb_Transactions(Tran_ID, Supp_ID, Con_ID, Prod_ID, Quantity, Price)

The dimensions of the cube are: Tb_Supplier and Tb_Product.
Measure groups table is: Tb_Offers.
Measure aggregates: SUM(Quantity), SUM(Quantity*Price), 
MAX(Price) , MIN(Price).
Dimension hierarchies:

Tb_Supplier:    State > City > Name
Tb_Product:     Product_Packaging  > Name 
            Product_Category > Product_Line > Name


SELECT DISTINCT S.Name "Supplier Name", S.State "Supplier State", S.City "Supplier City",
P.Name "Product Name", P.Product_Packaging "Product Packaging",
P.Product_Category "Product Category", P.Product_Line "Product Line",
SUM(Quantity) "Total Transactions Quantity", SUM(Quantity*Price) "Total Transaction Price",
MAX(Price) "Maximum Price", MIN(Price) "Minimum Price"
INTO Tb_Offers_Cube
FROM Tb_Supplier S, Tb_Product P, Tb_Offers O
WHERE S.Supp_ID = O.Supp_ID 
AND O.Prod_ID = P.Prod_ID
GROUP BY CUBE((S.State, S.City, S.Name),
(P.Product_Packaging, P.Name),
(P.Product_Category, P.Product_Line, P.Name)),
ROLLUP(S.State, S.City, S.Name),
ROLLUP(P.Product_Packaging, P.Name),
ROLLUP(P.Product_Category, p.Product_Line, P.Name);

1 个答案:

答案 0 :(得分:0)

我认为您正在尝试创建交叉表格报告。使用 private bool IsActive(DirectoryEntry de) { if (de.NativeGuid == null) return false; int flags = (int)de.Properties["userAccountControl"].Value; return !Convert.ToBoolean(flags & 0x0002); } 语句。

        public void SaveUser(SearchRolesViewModel objSearchRolesViewModel, string userID)
        {
            DirectoryEntry userEntry = QueryAD(objSearchRolesViewModel.User_Id);
            if (userEntry == null) 
            {
               //Handle error where No User was Found.
               throw new ApplicationException("User Not Found");
            }

            USERACCOUNT objUserAccount = new USERACCOUNT
            {

                HPID = Convert.ToInt32(objSearchRolesViewModel.NewUserHealthPlans),
                DOMAIN = "Aeth",
                NTUSERID = objSearchRolesViewModel.User_Id,
                ROLEID = Convert.ToInt32(objSearchRolesViewModel.UserRole),
                FIRSTNAME = userEntry.Properties["givenname"].Value.ToString(), 
                LASTNAME = userEntry.Properties["sn"].Value.ToString(),
                EMAIL = userEntry.Properties["mail"].Value.ToString(),
                ACTIVE = IsActive(userEntry),
                DEFAULTPLANID = Convert.ToInt32(objSearchRolesViewModel.NewUserPrimaryHealthPlan),
                CREATEID = userID,
                CREATEDATE = DateTime.Now,
                UPDATEID = userID,
                UPDATEDATE = DateTime.Now
            };
            _context.USERACCOUNTs.Add(objUserAccount);
            _context.SaveChanges();

        }