SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


grouping rows


grouping rows

Author
Message
aykut canturk
aykut canturk
SSC-Enthusiastic
SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)

Group: General Forum Members
Points: 138 Visits: 160
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.
Attachments
sql.txt (15 views, 3.00 KB)
Stupeo
Stupeo
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1293 Visits: 778
Can't you just order by Order ID?
aykut canturk
aykut canturk
SSC-Enthusiastic
SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)

Group: General Forum Members
Points: 138 Visits: 160
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"
Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13262 Visits: 5478
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
aykut canturk
aykut canturk
SSC-Enthusiastic
SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)

Group: General Forum Members
Points: 138 Visits: 160
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.
Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13262 Visits: 5478
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
Jason-299789
Jason-299789
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5235 Visits: 3232
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
John Mitchell-245523
John Mitchell-245523
SSC-Dedicated
SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)

Group: General Forum Members
Points: 36402 Visits: 16701
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


aykut canturk
aykut canturk
SSC-Enthusiastic
SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)

Group: General Forum Members
Points: 138 Visits: 160
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search