grouping rows

  • dear friends, I need to group records. the sample data below is result of collected data from different tables. If you interest also I mentioned which data came from which table.

    [font="Courier New"]

    TRANSACTION 242012-12-09

    TRANSACTION 252012-12-09

    TRANSACTION 262012-14-09

    TRANSACTION 272012-15-09

    TRANSACTIONFEE242012-12-09

    TRANSACTIONFEE252012-12-09

    TRANSACTIONFEE262012-14-09

    TRANSACTIONFEE272012-15-09

    COMISSION 242012-12-09

    COMISSION 252012-12-09

    COMISSION 262012-14-09

    COMISSION 272012-15-09

    MONEYREQUEST - 2012-15-12

    MONEYREQUEST - 2012-17-12

    MONEYREQUEST - 2012-12-09

    CHARGEBACK 24 2012-19-12

    CHARGEBACK 24 2012-14-09 [/font]

    chargebacks -> tblChargebacks

    monetrequests -> tblMonetrequests

    transactions -> tblTransactions, Field : Amount

    transactionfees -> tblTransactions, Field : trnFee

    comissions -> tblTransactions, Field : Comission

    as you see some data come from its own tabe, but transaction related data came from one table. I merge data with this view:

    SELECT 'TRANSACTION' AS type, OrderID, OperationDate FROM tblTransactions

    UNION ALL

    SELECT 'TRANSACTIONFEE' AS type, OrderID, OperationDate FROM tblTransactions

    UNION ALL

    SELECT 'COMISSION' AS type, OrderID, OperationDate FROM tblTransactions

    UNION ALL

    SELECT 'CHARGEBACK' AS type, OrderNo AS OrderID, OperationDate FROM tblChargeBacks

    UNION ALL

    SELECT 'MONEYREQUEST' AS type, '-' AS OrderID, OperationDate FROM tblMoneyRequests

    problem is, as this is an accounting software I need to keep transaction records together. but also I need to obey date sorting rules. nobody wants to see records belong to 2 days ago, in today's report. here's the sample I need:

    [font="Courier New"]TRANSACTION 242012-12-09

    TRANSACTIONFEE242012-12-09

    COMISSION 242012-12-09

    MONEYREQUEST - 2012-12-09

    TRANSACTION 252012-12-09

    TRANSACTIONFEE252012-12-09

    COMISSION 252012-12-09

    TRANSACTION 262012-14-09

    TRANSACTIONFEE262012-14-09

    COMISSION 262012-14-09

    CHARGEBACK 24 2012-14-09

    TRANSACTION 272012-15-09

    TRANSACTIONFEE272012-15-09

    COMISSION 272012-15-09

    MONEYREQUEST - 2012-15-12

    MONEYREQUEST - 2012-17-12

    CHARGEBACK 24 2012-19-12 [/font]

    as you see, basically result must be sorted by date. but meanwhile it should keep TRANSACTION, COMISSION and MONEYREQUEST records together.

    any idea please ?

    edit: I attched sample sql to test. of courde I'll add amount fields to my result set but to simplify I just didnt included to text above.

  • Can't you just order by Order ID?

  • Stupeo (11/14/2012)


    Can't you just order by Order ID?

    no my friend, because transactions, transaction fees and commissions came from one table, others come from different tables. lets say, transaction's ID is at 6000. chargeback's ID is at 2000. then they wont follow process date order. I can achive this on client side. this looks like detail report:

    select amount from tbltrasctions

    -- select fee from tbltansactions where ID= (above ID)

    -- select comissionfrom tbltansactions where ID= (above ID)

    select amount from tblchargebacks

    select amount from tblmoneyrequests

    then I need to sort this according to process date BUT: I need to keep firs 3 record set "together"

  • If you could follow this:

    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    ...

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • sorry. sql statements for creating table and records are in attached file because they seemed too long to me. the sample data in post actually is not data. I used to explain what kind of sorting I need. I think by this way it would be more clear. but if you say so, sure I can remove these explanations, but this time how would I explain reader what kind of sorting do I need.

    please direct me to right way my friend, thanks.

  • Many work places do not allow file downloads from internet...

    So, it will be much more helpful if you include your DDL scripts into post. Just wrap them with appropriate "code" tags from the "IFCode Shortcuts" they are on the left side from text posting box.

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • I've managed to get a script out and alter it to use temp tables

    CREATE TABLE #tblChargeBacks(

    [ID] [int] IDENTITY(1,1) NOT NULL,

    [amount] [float] NULL,

    [OrderID] [nvarchar](50) NULL,

    [OperationDate] [datetime] NULL,

    [type] [nvarchar](50) NULL,

    CONSTRAINT [PK_tblChargeBacks] PRIMARY KEY CLUSTERED

    (

    [ID] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    ) ON [PRIMARY]

    CREATE TABLE #tblMoneyRequests(

    [ID] [int] IDENTITY(1,1) NOT NULL,

    [amount] [float] NULL,

    [OrderID] [nvarchar](50) NULL,

    [OperationDate] [datetime] NULL,

    [type] [nvarchar](50) NULL,

    CONSTRAINT [PK_tblMoneyRequests] PRIMARY KEY CLUSTERED

    (

    [ID] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    ) ON [PRIMARY]

    CREATE TABLE #tblTransactions(

    [ID] [int] IDENTITY(1,1) NOT NULL,

    [OrderID] [nvarchar](50) NULL,

    [amount] [float] NULL,

    [Comission] [float] NULL,

    [fee] [float] NULL,

    [OperationDate] [datetime] NULL,

    [type] [nvarchar](50) NULL,

    CONSTRAINT [PK_Transactions] PRIMARY KEY CLUSTERED

    (

    [ID] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    ) ON [PRIMARY]

    GO

    SET IDENTITY_INSERT #tblChargeBacks ON

    GO

    INSERT #tblChargeBacks ([ID], [amount], [OrderID], [OperationDate], [type]) VALUES (1, 7, N'24', CAST(0x0000A12C00000000 AS DateTime), N'CHARGEBACK')

    GO

    INSERT #tblChargeBacks ([ID], [amount], [OrderID], [OperationDate], [type]) VALUES (2, 7, N'24', CAST(0x0000A0CC00000000 AS DateTime), N'CHARGEBACK')

    GO

    SET IDENTITY_INSERT #tblChargeBacks OFF

    GO

    SET IDENTITY_INSERT #tblMoneyRequests ON

    GO

    INSERT #tblMoneyRequests ([ID], [amount], [OrderID], [OperationDate], [type]) VALUES (1, 9, N'-', CAST(0x0000A12A00000000 AS DateTime), N'MONEYREQUEST')

    GO

    INSERT #tblMoneyRequests ([ID], [amount], [OrderID], [OperationDate], [type]) VALUES (2, 9, N'-', CAST(0x0000A12800000000 AS DateTime), N'MONEYREQUEST')

    GO

    INSERT #tblMoneyRequests ([ID], [amount], [OrderID], [OperationDate], [type]) VALUES (3, 9, N'-', CAST(0x0000A12200000000 AS DateTime), N'MONEYREQUEST')

    GO

    SET IDENTITY_INSERT #tblMoneyRequests OFF

    GO

    SET IDENTITY_INSERT #tblTransactions ON

    GO

    INSERT #tblTransactions ([ID], [OrderID], [amount], [Comission], [fee], [OperationDate], [type]) VALUES (1, N'24', 1, 4, 5, CAST(0x0000A0CA00000000 AS DateTime), N'TRANSACTION')

    GO

    INSERT #tblTransactions ([ID], [OrderID], [amount], [Comission], [fee], [OperationDate], [type]) VALUES (2, N'25', 2, 4, 5, CAST(0x0000A0CA00000000 AS DateTime), N'TRANSACTION')

    GO

    INSERT #tblTransactions ([ID], [OrderID], [amount], [Comission], [fee], [OperationDate], [type]) VALUES (3, N'26', 3, 4, 5, CAST(0x0000A0CC00000000 AS DateTime), N'TRANSACTION')

    GO

    INSERT #tblTransactions ([ID], [OrderID], [amount], [Comission], [fee], [OperationDate], [type]) VALUES (4, N'27', 4, 4, 5, CAST(0x0000A0CD00000000 AS DateTime), N'TRANSACTION')

    GO

    SET IDENTITY_INSERT #tblTransactions OFF

    On tblMoneyRequests as OrderId on the table is blank (or rather '-'), so I cant see a way to join these rows to the other tables.

    _________________________________________________________________________
    SSC Guide to Posting and Best Practices

  • OK, I'm guessing here, since I can't work out why you group the MONEYREQUESTs where you do in your expected results, nor what the Comission and fee columns signify.

    WITH Transactions AS (

    SELECT type, OrderId, OperationDate

    FROM #tblTransactions

    WHERE type = 'TRANSACTION'

    UNION ALL

    SELECT 'TRANSACTIONFEE', OrderID, OperationDate

    FROM #tblTransactions

    WHERE fee = 5

    UNION ALL

    SELECT 'COMISSION', OrderID, OperationDate

    FROM #tblTransactions

    WHERE Comission = 4

    UNION ALL

    SELECT type, OrderID, OperationDate

    FROM #tblMoneyRequests

    WHERE type = 'MONEYREQUEST'

    UNION ALL

    SELECT type, OrderID, OperationDate

    FROM #tblChargeBacks

    WHERE type = 'CHARGEBACK'

    )

    SELECT type, OrderID, OperationDate

    FROM Transactions

    ORDER BY OperationDate, OrderID

  • I found filling a dataset programmatically more easy. My concern was sql performance vs aspnet performance but this way is more easy.

    I thank you everyone for their ideas.

    to Eugene Elutin: I apologize for my long sample post.

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply