我不知道为什么人们投票反对?而不是编辑问题,在评论中提供建议或回答人们投票反对的问题。
我终于解决了它不是100%解决它是90%,换句话说商店的积累不起作用[它计算商店中材料的数量,但对于父商店它不和子商店相加]这是sql
create PROCEDURE [dbo].[AdvancedBillMatReport]
@ParamUserGuid AS UNIQUEIDENTIFIER = NULL,
@ParamMaterialguid AS UNIQUEIDENTIFIER = 0X0,
@ParamGroupLevel AS int = 0,
@ParamStoreGuid AS UNIQUEIDENTIFIER = 0X0,
@ParamDisplayType AS int = 1,
@ParamPriceType AS int = 0,
@ParamBillTypesGuidsStr AS varchar(max) = NULL
AS
SET NOCOUNT ON
IF OBJECT_ID('tempdb..#Result') IS NOT NULL
DROP TABLE #Result
IF OBJECT_ID('tempdb..#Results') IS NOT NULL
DROP TABLE #Results
IF OBJECT_ID('tempdb..#SecViol') IS NOT NULL
DROP TABLE #SecViol
IF OBJECT_ID('tempdb..#SecViols') IS NOT NULL
DROP TABLE #SecViols
CREATE TABLE [#SecViol] (Type [INT], Cnt [INT])
CREATE TABLE [#SecViols] (Type [INT], Cnt [INT])
--BILLTYPES
declare @BillTYpesGuids table
(
TypeGUID uniqueidentifier,
TypeName varchar(250)
)
insert into @BillTYpesGuids
select t1.GUID AS TypeGUID, bt000.Name AS TypeName from splitstring ( @ParamBillTypesGuidsStr, ';' ) t1 left join bt000 on bt000.GUID = t1.GUID
--MATRIALS the actual vertical part of thetable
declare @Materials table
(
MatGUID uniqueidentifier,
GroupGuid uniqueidentifier,
Code varchar(250),
Name varchar(250),
LatinName varchar(250),
[Level] int,
[Path] [VARCHAR](8000),
[Type] int
)
insert into @Materials
exec repMattreeReport
--/////////////FILTER MATERIALS///////////////////////////////
DELETE FROM @Materials WHERE (Level < @ParamGroupLevel)
IF @ParamMaterialguid <> 0X0
BEGIN
DELETE FROM @Materials WHERE MatGUID <> @ParamMaterialguid
END
SELECT * INTO #Result FROM @Materials
exec prcCheckSecurity @ParamUserGuid
DELETE FROM @Materials WHERE MatGUID NOT IN (SELECT MatGUID FROM #Result)
DROP TABLE #Result
INSERT INTO #SecViols SELECT * FROM #SecViol
TRUNCATE TABLE #SecViol
--/////////////DONE FILTERING//////////////////////////
--STORES the repeated Horizontal parts of table
declare @stores table
(
StoreGuid uniqueidentifier,
Name varchar(250),
ParentGUID uniqueidentifier,
Level int
)
insert into @stores(StoreGuid, Name, ParentGUID, Level)
SELECT T1.GUID AS StoreGuid, st000.Name, st000.ParentGUID, T1.Level FROM fnGetStoresListTree(@ParamStoreGuid, 1) AS T1 LEFT JOIN st000 ON T1.GUID = st000.GUID
--/////////////FILTER STORES///////////////////////////
SELECT * INTO #Result1 FROM @stores
exec prcCheckSecurity @ParamUserGuid,0,0,'#Result1'
DELETE FROM @stores WHERE StoreGuid NOT IN (SELECT StoreGuid FROM #Result1)
DROP TABLE #Result1
INSERT INTO #SecViols SELECT *FROM #SecViol
TRUNCATE TABLE #SecViol
--/////////////DONE FILTERING//////////////////////////
--the grouped bill items
declare @MatData table
(
MatGUID uniqueidentifier,
StoreGUID uniqueidentifier,
TypeGUID uniqueidentifier,
GroupGuid uniqueidentifier,
Qty int
)
insert into @MatData
select fatoraitem.MatGUID, fatoraitem.StoreGUID, fatora.TypeGUID, mada.GroupGUID, ISNULL(SUM(fatoraitem.Qty),0) as Qty
from bi000 fatoraitem
left join bu000 as fatora on fatora.GUID = fatoraitem.ParentGUID
left join mt000 as mada on mada.GUID = fatoraitem.MatGUID
group by fatoraitem.MatGUID, fatoraitem.StoreGUID, fatora.TypeGUID, mada.GroupGUID
INSERT INTO @Materials([Type], MatGUID, GroupGuid, Name,Path) VALUES(2, 0X0, 0X0, 'المجموع الكلي', (SELECT MAX(PATH) FROM @Materials))--FOR THE TOTAL
SELECT [Type], [Level], MatGUID, GroupGuid, Name INTO #ResultS FROM @Materials ORDER BY Path
DECLARE SelectedBillTypes CURSOR LOCAL FOR
SELECT TypeGUID, TypeName FROM @BillTYpesGuids
DECLARE STORESITERATOR CURSOR LOCAL FOR
SELECT StoreGuid, Name FROM @STORES
DECLARE MatIterator CURSOR LOCAL FOR
SELECT MatGuid, Name, Level, Type FROM @Materials
OPEN SelectedBillTypes--BILLS-TYPES
DECLARE @TypeGuid UNIQUEIDENTIFIER
DECLARE @TypeName varchar(250)
FETCH NEXT FROM SelectedBillTypes into @TypeGuid, @TypeName
WHILE(@@FETCH_STATUS = 0)
BEGIN
OPEN STORESITERATOR--STORES
DECLARE @StoreGuid UNIQUEIDENTIFIER
DECLARE @StoreName varchar(250)
FETCH NEXT FROM STORESITERATOR into @StoreGuid, @StoreName
WHILE(@@FETCH_STATUS = 0)
BEGIN
DECLARE @ColumnQName VARCHAR(MAX)
DECLARE @ColumnPName VARCHAR(MAX)
DECLARE @AlterStatement VARCHAR(MAX)
set @ColumnQName = '[' + @TypeName + '.' + @StoreName + '.Q]'
SET @AlterStatement = 'ALTER TABLE #Results ADD ' + @ColumnQName + ' INT NOT NULL DEFAULT(0)'
Execute(@AlterStatement)
IF(@ParamDisplayType = 1)--EXPANED DISPLAY
BEGIN
set @ColumnPName = '[' + @TypeName + '.' + @StoreName + '.P]'
SET @AlterStatement = 'ALTER TABLE #Results ADD ' + @ColumnPName + ' INT NOT NULL DEFAULT(0)'
Execute(@AlterStatement)
END
OPEN MatIterator--MATS AND GROUPS
DECLARE @MatGuid uniqueidentifier, @MatName varchar(250), @MatLevel int, @MatType int
FETCH NEXT FROM MatIterator INTO @MatGuid, @MatName, @MatLevel, @MatType
WHILE(@@FETCH_STATUS = 0)
BEGIN
DECLARE @Price INT, @Quantity INT
DECLARE @InsertStatement varchar(MAX)
select @Price = CASE WHEN @ParamPriceType = 0X4 THEN Whole
WHEN @ParamPriceType = 0x8 THEN Half
WHEN @ParamPriceType = 0x10 THEN Export
WHEN @ParamPriceType = 0x20 THEN Vendor
WHEN @ParamPriceType = 0x40 THEN Retail
WHEN @ParamPriceType = 0x80 THEN EndUser
END
from mt000 where GUID = @MatGuid
IF @MatType = 1
SELECT @Quantity = ISNULL(SUM(Qty), 0) FROM @MatData AS T1 WHERE (T1.MatGUID = @MatGUID AND T1.StoreGUID = @StoreGuid And T1.TypeGUID = @TypeGuid)
ELSE
SET @Quantity = 0
set @Price = @Price * @Quantity
SET @InsertStatement = 'UPDATE #Results SET ' + @ColumnQName + ' = ' + CAST(@Quantity as VARCHAR(50))
IF(@ParamDisplayType = 1)--EXPANED DISPLAY
BEGIN
SET @InsertStatement += ', ' + @ColumnPName + ' = ' + CAST(@Price AS VARCHAR(50))
END
SET @InsertStatement += ' WHERE MatGUID = ''' + CAST(@MatGuid AS VARCHAR(50)) + ''''
EXECUTE(@InsertStatement)
FETCH NEXT FROM MatIterator INTO @MatGuid, @MatName, @MatLevel, @MatType
END
CLOSE MatIterator
DECLARE @UpdateStatemENT VARCHAR(MAX)
DECLARE @Total VARCHAR(MAX)
DECLARE @MaxLevel INT
SELECT @MaxLevel = ISNULL(MAX(Level),0) FROM #ResultS
SET @Total = 'UPDATE #ResultS SET ' + @ColumnQName + ' = ' +
'(SELECT ISNULL(SUM(' + @ColumnQName + '), 0) FROM #ResultS AS T2 WHERE T2.Type = 1)'
IF(@ParamDisplayType = 1)--EXPANED DISPLAY
BEGIN
SET @Total += ', ' + @ColumnPName + ' = ' + '(SELECT ISNULL(SUM(' + @ColumnPName + '), 0) FROM #ResultS AS T2 WHERE T2.Type = 1) '
END
SET @Total +=' WHERE [TYPE] = 2'
EXECUTE(@Total)
WHILE (@MaxLevel>-1)
BEGIN
SET @UpdateStatemENT = 'UPDATE #ResultS SET ' + @ColumnQName + ' = ' +
'(SELECT ISNULL(SUM(' + @ColumnQName + '), 0) FROM #ResultS AS T2 WHERE T2.GroupGuid = #ResultS.MatGUID)'
IF(@ParamDisplayType = 1)--EXPANED DISPLAY
BEGIN
SET @UpdateStatemENT += ', ' + @ColumnPName + ' = ' + '(SELECT ISNULL(SUM(' + @ColumnPName + '), 0) FROM #ResultS AS T2 WHERE T2.GroupGuid = #ResultS.MatGUID) '
END
SET @UpdateStatemENT += ' WHERE [TYPE] = 0 AND Level = ' + CAST(@MaxLevel AS VARCHAR(50))
EXECUTE(@UpdateStatemENT)
set @MaxLevel -= 1
END
FETCH NEXT FROM STORESITERATOR into @StoreGuid, @StoreName
END
CLOSE STORESITERATOR
FETCH NEXT FROM SelectedBillTypes into @TypeGuid, @TypeName
END
CLOSE SelectedBillTypes
SELECT * FROM #ResultS
这是结果
表头是在 C++ 中构建的,我从列名列名中获取名称 = billtypename.storename.q 为数量或 billtypename.storename.p 为价格。在c ++中,我遍历表中的列名并将它们拆分并处理表主体中的标题我将它们放在来自服务器的位置