Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

grouping rows Expand / Collapse
Author
Message
Posted Wednesday, November 14, 2012 4:32 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, January 23, 2014 11:22 AM
Points: 52, Visits: 140
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.

TRANSACTION 24 2012-12-09
TRANSACTION 25 2012-12-09
TRANSACTION 26 2012-14-09
TRANSACTION 27 2012-15-09
TRANSACTIONFEE 24 2012-12-09
TRANSACTIONFEE 25 2012-12-09
TRANSACTIONFEE 26 2012-14-09
TRANSACTIONFEE 27 2012-15-09
COMISSION 24 2012-12-09
COMISSION 25 2012-12-09
COMISSION 26 2012-14-09
COMISSION 27 2012-15-09
MONEYREQUEST - 2012-15-12
MONEYREQUEST - 2012-17-12
MONEYREQUEST - 2012-12-09
CHARGEBACK 24 2012-19-12
CHARGEBACK 24 2012-14-09


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:

TRANSACTION 24 2012-12-09
TRANSACTIONFEE 24 2012-12-09
COMISSION 24 2012-12-09
MONEYREQUEST - 2012-12-09

TRANSACTION 25 2012-12-09
TRANSACTIONFEE 25 2012-12-09
COMISSION 25 2012-12-09

TRANSACTION 26 2012-14-09
TRANSACTIONFEE 26 2012-14-09
COMISSION 26 2012-14-09
CHARGEBACK 24 2012-14-09

TRANSACTION 27 2012-15-09
TRANSACTIONFEE 27 2012-15-09
COMISSION 27 2012-15-09
MONEYREQUEST - 2012-15-12

MONEYREQUEST - 2012-17-12
CHARGEBACK 24 2012-19-12


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.


  Post Attachments 
sql.txt (7 views, 3.14 KB)
Post #1384533
Posted Wednesday, November 14, 2012 4:51 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Sunday, April 13, 2014 11:32 AM
Points: 939, Visits: 778
Can't you just order by Order ID?

Post #1384541
Posted Wednesday, November 14, 2012 5:04 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, January 23, 2014 11:22 AM
Points: 52, Visits: 140
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"
Post #1384548
Posted Wednesday, November 14, 2012 5:32 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, July 4, 2014 3:55 AM
Points: 2,836, Visits: 5,062
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1384563
Posted Wednesday, November 14, 2012 6:08 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, January 23, 2014 11:22 AM
Points: 52, Visits: 140
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.
Post #1384582
Posted Wednesday, November 14, 2012 6:36 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, July 4, 2014 3:55 AM
Points: 2,836, Visits: 5,062
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1384593
Posted Wednesday, November 14, 2012 6:47 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, July 18, 2014 9:09 AM
Points: 870, Visits: 2,385
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
Post #1384596
Posted Wednesday, November 14, 2012 7:29 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 5:17 AM
Points: 5,230, Visits: 9,457
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

Post #1384621
Posted Wednesday, November 14, 2012 3:04 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, January 23, 2014 11:22 AM
Points: 52, Visits: 140
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.
Post #1384895
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse