June 14, 2012 at 9:55 pm
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.
June 14, 2012 at 11:06 pm
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]
June 14, 2012 at 11:42 pm
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);
June 15, 2012 at 12:04 am
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 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?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
June 15, 2012 at 12:16 am
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.
June 15, 2012 at 12:21 am
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 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?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
June 15, 2012 at 12:35 am
Thanks to all the feedback. It is actually a work problem, not a homework problem π
June 15, 2012 at 3:32 am
June 15, 2012 at 4:37 am
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 π
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
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply