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

How to query results in debit and credit column Expand / Collapse
Author
Message
Posted Thursday, June 14, 2012 9:55 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Today @ 2:22 AM
Points: 79, Visits: 459
Hi SQL Gurus,

I would like to run a query to display the amount in a debit and credit column from a FACT table. The value appearing in the debit or credit column depends on the account sign in the ACCOUNT table.

FACT TABLE
ACCOUNT | ORGANIZATION | YEAR | PERIOD | AMOUNT
-----------------------------------------------------
1001 | aaa | 2012 | 01 | 100
1002 | aaa | 2012 | 01 | 50
1003 | aaa | 2012 | 01 | -100
1004 | aaa | 2012 | 01 | 90


ACCOUNT TABLE
ACCOUNT | DESC | SIGN
------------------------
1001 | 1001 | Dr
1002 | 1002 | Cr
1003 | 1003 | Dr
1004 | 1004 | Cr


How can I transform to this result?
ACCOUNT | ORGANIZATION | YEAR | PERIOD | Dr   | Cr 
-----------------------------------------------------
1001 | aaa | 2012 | 01 | 100 |
1002 | aaa | 2012 | 01 | | 50
1003 | aaa | 2012 | 01 | -100 |
1004 | aaa | 2012 | 01 | | 90


Below is the DDL and sample data for this:
DECLARE @ACCOUNT TABLE
(
ACCOUNT CHAR(4) NOT NULL,
DESC CHAR(4) NOT NULL,
SIGN CHAR(4) NOT NULL
)

DECLARE @FACT TABLE
(
ACCOUNT CHAR(4) NOT NULL,
ORGANIZATION CHAR(3) NOT NULL,
YEAR SMALLINT NOT NULL,
PERIOD CHAR(2) NOT NULL,
AMOUNT int NOT NULL
)

INSERT @ACCOUNT
VALUES ('1001', '1001', 'Dr'),
('1002', '1002', 'Cr'),
('1003', '1003', 'Dr'),
('1004', '1004', 'Cr')

INSERT @FACT
VALUES ('1001', 'aaa', 2012, '01', 100),
('1002', 'aaa', 2012, '01', 50),
('1003', 'aaa', 2012, '01', -100),
('1004', 'aaa', 2012, '01', 90)

Thanks.
Post #1316282
Posted Thursday, June 14, 2012 11:06 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 5:50 AM
Points: 63, Visits: 293
Try this,

SELECT Acc.Account,Fct.ORGANIZATION ,Fct .Year,Period,
CASE WHEN Acc .[SIGN]='Cr' THEN SUM(Fct.AMOUNT ) ELSE 0 END AS Cr,
CASE WHEN Acc.[SIGN]='Dr' THEN SUM(Fct.AMOUNT ) ELSE 0 END AS Dr
FROM #ACCOUNT Acc
INNER JOIN #FACT Fct ON Acc.ACCOUNT=Fct.ACCOUNT
GROUP BY Acc.Account,Fct.ORGANIZATION,Fct.Year,Period,Acc .[SIGN]
Post #1316293
Posted Thursday, June 14, 2012 11:42 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 11:08 PM
Points: 20,696, Visits: 32,330
Not sure why you need the SUM or GROUP BY.


DECLARE @ACCOUNT TABLE
(
ACCOUNT CHAR(4) NOT NULL,
[DESC] CHAR(4) NOT NULL,
[SIGN] CHAR(4) NOT NULL
);

DECLARE @FACT TABLE
(
ACCOUNT CHAR(4) NOT NULL,
ORGANIZATION CHAR(3) NOT NULL,
[YEAR] SMALLINT NOT NULL,
PERIOD CHAR(2) NOT NULL,
AMOUNT int NOT NULL
);

INSERT @ACCOUNT
VALUES ('1001', '1001', 'Dr'),
('1002', '1002', 'Cr'),
('1003', '1003', 'Dr'),
('1004', '1004', 'Cr');

INSERT @FACT
VALUES ('1001', 'aaa', 2012, '01', 100),
('1002', 'aaa', 2012, '01', 50),
('1003', 'aaa', 2012, '01', -100),
('1004', 'aaa', 2012, '01', 90);

select
f.ACCOUNT,
f.ORGANIZATION,
f.[YEAR],
f.PERIOD,
case when [SIGN] = 'Dr' then cast(AMOUNT AS VARCHAR) else '' end Dr,
case when [SIGN] = 'Cr' then cast(AMOUNT AS VARCHAR) else '' end Cr
from
@ACCOUNT a
inner join @FACT f
on (a.ACCOUNT = f.ACCOUNT);





Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1316302
Posted Friday, June 15, 2012 12:04 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 4:09 AM
Points: 3,420, Visits: 5,344
Lynn Pettis (6/14/2012)
Not sure why you need the SUM or GROUP BY.




I was going to say the same thing but I think Sony was assuming there might be duplicated rows (different transaction amounts) for account/org/year/month.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1316310
Posted Friday, June 15, 2012 12:16 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 11:08 PM
Points: 20,696, Visits: 32,330
dwain.c (6/15/2012)
Lynn Pettis (6/14/2012)
Not sure why you need the SUM or GROUP BY.




I was going to say the same thing but I think Sony was assuming there might be duplicated rows (different transaction amounts) for account/org/year/month.


That's okay. Now I am wondering if this was also a homework problem.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1316318
Posted Friday, June 15, 2012 12:21 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 4:09 AM
Points: 3,420, Visits: 5,344
Lynn Pettis (6/15/2012) Now I am wondering if this was also a homework problem.


SELECT SolutionFor
FROM MyHomework
WHERE Solutionby = 'Lynn' OR Solutionby = 'Sony'





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1316322
Posted Friday, June 15, 2012 12:35 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Today @ 2:22 AM
Points: 79, Visits: 459
Thanks to all the feedback. It is actually a work problem, not a homework problem :)
Post #1316328
Posted Friday, June 15, 2012 3:32 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, September 19, 2014 7:13 AM
Points: 1,127, Visits: 1,594
The Sql Server Gurus are really fast here at SSC.

By the time I read the new thread, it was all done and dusted.

Nice work Lynn and Dwain.


Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden
Post #1316424
Posted Friday, June 15, 2012 4:37 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:37 AM
Points: 6,748, Visits: 13,890
yingchai (6/15/2012)
Thanks to all the feedback. It is actually a work problem, not a homework problem :)


It has few of the characteristics of a homework problem. Folks who are too idle to do their homework and post here hoping for a free ride are also, usually, too idle to give a full description of the problem or provide DDL and DML.
You did both


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1316479
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse