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 12»»

cross tab Expand / Collapse
Author
Message
Posted Tuesday, January 13, 2009 6:28 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Sunday, January 13, 2013 10:55 AM
Points: 166, 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



Post #635327
Posted Tuesday, January 13, 2009 6:47 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, December 21, 2014 12:05 AM
Points: 2,552, Visits: 2,600
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 day, posting 2K5 solutions in 2K forums....

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


--Ramesh

Post #635351
Posted Tuesday, January 13, 2009 9:26 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Sunday, January 13, 2013 10:55 AM
Points: 166, 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



Post #635560
Posted Tuesday, January 13, 2009 1:14 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, December 19, 2014 11:41 AM
Points: 1,564, Visits: 2,397
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.
Post #635786
Posted Tuesday, January 13, 2009 10:16 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, December 19, 2014 9:18 AM
Points: 1,519, Visits: 4,086
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
Post #636007
Posted Wednesday, January 14, 2009 3:13 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 2:15 PM
Points: 35,849, Visits: 32,519
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #636085
Posted Wednesday, January 14, 2009 3:15 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 2:15 PM
Points: 35,849, Visits: 32,519
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #636086
Posted Wednesday, January 14, 2009 3:24 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, December 21, 2014 12:05 AM
Points: 2,552, Visits: 2,600
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...;), That's what I've done in our payroll system....:):)


--Ramesh

Post #636090
Posted Wednesday, January 14, 2009 3:41 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 2:15 PM
Points: 35,849, Visits: 32,519
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...;), That's what I've done in our payroll system....:):)


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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #636095
Posted Wednesday, January 14, 2009 3:55 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, December 21, 2014 12:05 AM
Points: 2,552, Visits: 2,600
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...;), That's what I've done in our payroll system....:):)


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

Post #636099
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse