-2

次のテーブルがあります

Materials Groups
-------------------------------
|GrpID | GrpName | GrpParentID |
-------------------------------

Materials
--------------------------------------
|MatID | MatName | MatGroupID |Price |
--------------------------------------

Stores
-----------------------------
|StID | StName | StParentID |
-----------------------------

BillType
--------------------
|TypeID | TypeName |
--------------------

それらはタイプです

Store Out
Store In/Price
Sales Return
First Period Inventory
Sales
End Period Inventory
Store Out/Price
Purchase
Store In
Ready Product In
Purchase Return
Sales Return1
Raw Materials Out

Bills
----------------------------------
|BillID | BillType| Client..|....|
----------------------------------

BillItems
---------------------------------
|ItemID| MatID| quantity|BillID |
---------------------------------

必要なのは、このフォームでレポートを表示することです レポートのデザイン

私はそれが大きなミスだと思う、私は何をすべきかわからない

結合、ピボットを試しましたが、不完全なレポートが得られました。非常に複雑だと思います

これは私の最近の試みです

declare @table1 table
(
    MatGUID uniqueidentifier,
    TypeGUID uniqueidentifier,
    Qty int
)
declare @table2 table
(
    MatGroup varchar(250),
    MatName varchar(250),
    BillTypeName varchar(250),
    Qty int
)
insert into @table1
select fatoraitem.MatGUID, fatora.TypeGUID, SUM(fatoraitem.Qty) as Qty
from bi000 fatoraitem
left join bu000 as fatora on fatora.GUID = fatoraitem.ParentGUID
left join bt000 as fatoratype on fatoratype.GUID = fatora.TypeGUID
group by fatoraitem.MatGUID, fatora.TypeGUID


insert into @table2
select gr000.Name as MatGroup, mt000.Name MatName, bt000.Name as BillTypeName, t1.Qty from @table1 t1
left join mt000 on mt000.GUID = t1.MatGUID
left join bt000 on bt000.GUID = t1.TypeGUID
left join gr000 on mt000.GroupGUID = gr000.GUID

declare
    @StoreCols nvarchar(max),
    @BillTypeCols nvarchar(max),
    @stmt nvarchar(max)

select @BillTypeCols = isnull(@BillTypeCols + ', ', '') + '[' + T.BillTypeName + ']' from (select distinct BillTypeName from @table2) as T

IF OBJECT_ID('tempdb..#Results') IS NOT NULL
    DROP TABLE #Results
select * into #Results from @table2
select @stmt = 'select * from #results as T
        pivot 
        (
            sum(T.Qty)
            for T.BillTypeName in (' + @BillTypeCols + ')
        ) as P'

exec sp_executesql  @stmt
4

1 に答える 1

0

人々が反対票を投じる理由がわかりません。質問を編集したり、コメントでアドバイスをしたり、質問に答えたりする代わりに、人々は反対票を投じます。

やっと解決しました 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 ++では、テーブル内の列名を反復処理して分割し、テーブル本体のヘッダーを処理しますサーバーから来るように配置します

于 2013-04-23T11:12:09.843 に答える