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


cross tab


cross tab

Author
Message
tran008
tran008
SSC-Enthusiastic
SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)

Group: General Forum Members
Points: 186 Visits: 417

IF OBJECT_ID('TempDB..#tblInv','U') IS NOT NULL
DROP TABLE #tblInv

CREATE TABLE #tblInv
(
ID INT ,
PartNo VARCHAR(10) NOT NULL,
TranType VARCHAR(7) NOT NULL,
Quantity INT NOT NULL
)

insert into #tblInv(id,partno, TranType,quantity)

select 1, 'A', 'buy', 50 union all
select 2,'A', 'buy', 50 union all
select 3,'A', 'buy', 50 union all
select 4,'A', 'buy', 50 union all
select 5,'A', 'buy', 100 union all
select 6,'A', 'sell', 150 union all
select 7,'A', 'sell', 100 union all
select 8,'A', 'buy', 500 union all
select 9,'A', 'sell', 450 union all
select 10,'A' ,'buy', 450 union all
select 11,'A', 'sell', 550 union all
select 12,'B', 'buy', 300 union all
select 13,'B', 'sell', 200 union all
select 14,'B' ,'buy', 200 union all
select 15,'B', 'sell', 250 union all
select 16, 'B', 'sell', 250

select * from #tblInv



Base from this...how can I get something like this:

id bID PartNo TranType Quantity sID sQuantity
1 1 A buy 50 6 150
2 2 A buy 50 7 100
3 3 A buy 50 9 450
4 4 A buy 50 11 550
5 5 A buy 100 0 0
6 8 A buy 500 0 0
7 10 A buy 450 0 0
8 12 B buy 300 13 200
9 14 B buy 200 15 250
10 0 B - 0 16 250



thanks



Ramesh Saive
Ramesh Saive
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3458 Visits: 2643
Actually you don't need a cross tab/pivot....


IF OBJECT_ID('TempDB..#tblInv','U') IS NOT NULL
DROP TABLE #tblInv

CREATE TABLE #tblInv
(
ID INT ,
PartNo VARCHAR(10) NOT NULL,
TranType VARCHAR(7) NOT NULL,
Quantity INT NOT NULL
)

insert into #tblInv(id,partno, TranType,quantity)

select 1, 'A', 'buy', 50 union all
select 2,'A', 'buy', 50 union all
select 3,'A', 'buy', 50 union all
select 4,'A', 'buy', 50 union all
select 5,'A', 'buy', 100 union all
select 6,'A', 'sell', 150 union all
select 7,'A', 'sell', 100 union all
select 8,'A', 'buy', 500 union all
select 9,'A', 'sell', 450 union all
select 10,'A' ,'buy', 450 union all
select 11,'A', 'sell', 550 union all
select 12,'B', 'buy', 300 union all
select 13,'B', 'sell', 200 union all
select 14,'B' ,'buy', 200 union all
select 15,'B', 'sell', 250 union all
select 16, 'B', 'sell', 250

select * from #tblInv

SELECT COALESCE(b.id,s.id) AS id, COALESCE(b.id,0) AS bid, COALESCE(b.partno,s.partno) AS partno,
COALESCE(b.TranType,s.TranType) AS TranType, COALESCE(b.quantity,0) AS bquantity,
COALESCE(s.id,0) AS [sid], COALESCE(s.quantity,0) AS squantity
FROM (
SELECT ROW_NUMBER() OVER( PARTITION BY partno ORDER BY id ) AS rowid, *
FROM #tblInv
WHERE TranType = 'buy'
) b
FULL OUTER JOIN
(
SELECT ROW_NUMBER() OVER( PARTITION BY partno ORDER BY id ) AS rowid, *
FROM #tblInv
WHERE TranType = 'sell'
) s ON b.partno = s.partno AND b.rowid = s.rowid





Edit:
Aaaah, I think I had too much coffee in the dayw00t, posting 2K5 solutions in 2K forums....

Just wait for few minutes, I'll be back with 2K solution...

--Ramesh


tran008
tran008
SSC-Enthusiastic
SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)

Group: General Forum Members
Points: 186 Visits: 417
Ramesh,

thank you very much for looking into this...It would be easy if the database is sql2005, but unfortunately, I'm dealing with sql2k...which make a bit more difficult. I had try the using Case statement, but not getting the anywhere.

thanks



Greg Snidow
Greg Snidow
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2113 Visits: 2486
Try this

http://www.sqlservercentral.com/articles/T-SQL/63681/

It should answer most of your questions.

Greg
_________________________________________________________________________________________________
The glass is at one half capacity: nothing more, nothing less.
Garadin
Garadin
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2703 Visits: 4107
First of all, a disclaimer:

The report you asked for is something that is likely best handled in the front end. This really is not a cross tab report (at least not the way I define it.) This is combining 2 separate data sets into one. The data you are trying to display in the same row has no true relation other than the fact that they're transactions on the same product ordered by their date.

That said, you can get the output you're requesting like this:

IF OBJECT_ID('TempDB..#tblInv','U') IS NOT NULL
DROP TABLE #tblInv

CREATE TABLE #tblInv
(
ID         INT,
PartNo         VARCHAR(10) NOT NULL,
TranType   VARCHAR(7) NOT NULL,
Quantity   INT NOT NULL,
PRIMARY KEY CLUSTERED(PartNo, TranType, ID),
RN     INT,
BSOrder        INT
)

INSERT INTO #tblInv(id,partno, TranType,quantity)

SELECT 1, 'A', 'buy',  50 UNION ALL
SELECT 2,'A', 'buy',  50 UNION ALL
SELECT 3,'A', 'buy',  50 UNION ALL
SELECT 4,'A', 'buy',  50 UNION ALL
SELECT 5,'A', 'buy',  100 UNION ALL
SELECT 6,'A', 'sell',  150 UNION ALL
SELECT 7,'A', 'sell',  100 UNION ALL
SELECT 8,'A', 'buy',  500 UNION ALL
SELECT 9,'A', 'sell',  450 UNION ALL
SELECT 10,'A' ,'buy',  450 UNION ALL
SELECT 11,'A', 'sell',  550 UNION ALL
SELECT 12,'B', 'buy',  300 UNION ALL
SELECT 13,'B', 'sell',  200 UNION ALL
SELECT 14,'B' ,'buy',  200 UNION ALL
SELECT 15,'B', 'sell',  250 UNION ALL
SELECT 16, 'B', 'sell',  250

DECLARE @RN         INT,
  
@BSOrder    INT,
  
@ID         INT,
  
@LastPart   VARCHAR(10),
  
@LastType   VARCHAR(7)
SET @RN = 0
SET @BSOrder = 1

UPDATE #tblInv
SET    @RN = RN = @RN + 1,
  
@BSOrder = BSOrder = CASE WHEN @LastPart = PartNo AND @LastType = TranType THEN @BSOrder + 1 ELSE 1 END,
  
@LastPart = PartNo,
  
@LastType = TranType,
  
@ID = [ID] -- Anchor
FROM #tblInv WITH (INDEX(0))

SELECT     COALESCE(B.RN, S.RN) RN,
  
ISNULL(B.[ID],0) BID,
  
COALESCE(B.PartNo,S.PartNo) PartNo,
  
ISNULL(B.TranType,'-') TranType,
  
ISNULL(B.Quantity,0) Quantity,
  
ISNULL(S.[ID],0) SID,
  
ISNULL(S.Quantity,0) sQuantity
FROM   (SELECT * FROM #tblInv WHERE TranType = 'buy') B
  
FULL OUTER JOIN
  
(SELECT * FROM #tblInv WHERE TranType = 'sell') S
  
ON B.PartNo = S.PartNo AND B.BSOrder = S.BSOrder
ORDER BY COALESCE(B.RN, S.RN)




Seth Phelabaum
Consistency is only a virtue if you're not a screwup. ;-)

Links: How to Post Sample Data :: Running Totals :: Tally Table :: Cross Tabs/Pivots :: String Concatenation
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)

Group: General Forum Members
Points: 85214 Visits: 41078
I don't get it... why does the SQuantity drop to 0 for rows 5 and 8? Not enough quantity has been sold off for that to happen on what appears to be a simple running total problem. I know the problem has been solved, but can someone explain why? Thanks.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)

Group: General Forum Members
Points: 85214 Visits: 41078
Never mind... not enough coffee... I see it. And, I agree with Seth... unless you have some additional information, this is definitely not the way I'd output from an inventory system.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Ramesh Saive
Ramesh Saive
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3458 Visits: 2643
Jeff Moden (1/14/2009)
Never mind... not enough coffee... I see it. And, I agree with Seth... unless you have some additional information, this is definitely not the way I'd output from an inventory system.

Aaaah...Wink, That's what I've done in our payroll system....SmileSmile

--Ramesh


Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)

Group: General Forum Members
Points: 85214 Visits: 41078
Ramesh (1/14/2009)
Jeff Moden (1/14/2009)
Never mind... not enough coffee... I see it. And, I agree with Seth... unless you have some additional information, this is definitely not the way I'd output from an inventory system.

Aaaah...Wink, That's what I've done in our payroll system....SmileSmile


Heh... payroll, inventory... doesn't matter. I don't understand the business rules for matching two seemingly unrelated rows.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Ramesh Saive
Ramesh Saive
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3458 Visits: 2643
Jeff Moden (1/14/2009)
Ramesh (1/14/2009)
Jeff Moden (1/14/2009)
Never mind... not enough coffee... I see it. And, I agree with Seth... unless you have some additional information, this is definitely not the way I'd output from an inventory system.

Aaaah...Wink, That's what I've done in our payroll system....SmileSmile


Heh... payroll, inventory... doesn't matter. I don't understand the business rules for matching two seemingly unrelated rows.


I guess it just like showing data in columnar manner, that you see in payslips. And also there are some not so good reporting tools (which i use) that doesn't do what it should be doing...

--Ramesh


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