September 1, 2008 at 3:04 am
Dear All,
I have the following table .
ITEM_CODE DPL_CODE MAX_AMT
----------------------------------------------------------
1 VP-LISO 3000 1 XP-CMO 5000 1 MP-COM 1500
Now I want to write a query which will get the records in following format.
ITEM_CODE VP-LISO XP-CMO MP-COM
--------------------------------------------------------
1 3000 5000 1500
How can I do this..? Please help..
Regards,
Santhosh.
September 1, 2008 at 3:08 am
Hi there,
Try looking up the PIVOT funtion in BOL.
If you still having problems let us know and we help you with your code.
Thanks
Chris
----------------------------------------------
Try to learn something about everything and everything about something. - Thomas Henry Huxley
:w00t:
Posting Best Practices[/url]
Numbers / Tally Tables[/url]
September 1, 2008 at 3:17 am
Sorry also,
How do you want to Aggregate MAX_AMT?
For Example if you have you Multiple rows with the same DPL_CODE , should the query add the MAX_AMT's together? or take the lowest or highest?
Thanks
Chris
----------------------------------------------
Try to learn something about everything and everything about something. - Thomas Henry Huxley
:w00t:
Posting Best Practices[/url]
Numbers / Tally Tables[/url]
September 1, 2008 at 3:27 am
Amount is DPL_CODE wise. For same code amount will be same. Dont wan't AVG, SUM or MIN/MAX...Just distinct AMOUNT will do....
Thanks,
Santhosh.
September 1, 2008 at 3:30 am
Could you give me an example please, of what the result will look like if you have the same Code?
Thanks
CHris
----------------------------------------------
Try to learn something about everything and everything about something. - Thomas Henry Huxley
:w00t:
Posting Best Practices[/url]
Numbers / Tally Tables[/url]
September 1, 2008 at 3:54 am
ITEM_CODE VP-LISO XP-CMO MP-COM
--------------------------------------------------------
1 3000 5000 1500
2 3000 0 0
3 0 5000 1500
September 1, 2008 at 4:22 am
Hi there,
OK I am assume this is what you want.
DECLARE @myTable TABLE
(ITEM_CODE INT,
DPL_CODE VARCHAR(10),
MAX_AMT INT)
INSERT INTO @myTable
SELECT 1,'VP-LISO',3000 UNION ALL
SELECT 1,'XP-CMO',5000 UNION ALL
SELECT 1,'MP-COM',1500 UNION ALL
SELECT 2,'VP-LISO',3000 UNION ALL
SELECT 3,'XP-CMO',5000 UNION ALL
SELECT 3,'MP-COM',1500
SELECT
ITEM_CODE,
ISNULL([VP-LISO],0) as [VP-LISO],
ISNULL([XP-CMO],0) as [XP-CMO],
ISNULL([MP-COM],0) as [MP-COM]
FROM
(SELECT ITEM_CODE,DPL_CODE,MAX_AMT
FROM @myTable) as p
PIVOT (MIN(MAX_AMT) FOR DPL_CODE IN ([VP-LISO],[XP-CMO],[MP-COM])
) as pvt
----------------------------------------------
Try to learn something about everything and everything about something. - Thomas Henry Huxley
:w00t:
Posting Best Practices[/url]
Numbers / Tally Tables[/url]
September 1, 2008 at 9:00 am
Except one thing.
Number of 'DPL_CODE' will vary..So the name..Client may put as many as they want with wotever the names they like..
So we can't hard code it like following
ISNULL([VP-LISO],0) as [VP-LISO],
ISNULL([XP-CMO],0) as [XP-CMO],
ISNULL([MP-COM],0) as [MP-COM]
Otherwise everything is perfect...exactly wot I want..
Thanks,
Santhosh Nair.
September 1, 2008 at 9:04 am
HI there,
Try and see what you can come up with by using a dynamic pivot query
----------------------------------------------
Try to learn something about everything and everything about something. - Thomas Henry Huxley
:w00t:
Posting Best Practices[/url]
Numbers / Tally Tables[/url]
September 1, 2008 at 11:17 pm
Hi,
yeah am trying that...Thanks a lot.
Santhu.
September 2, 2008 at 4:19 am
hi,
I did it using a dynamic sql.
create table #dplCodes (dplCode Varchar(21))
--------------------------------------------------------------
DECLARE @STR nVARCHAR(1000)
SET @STR = 'SELECTITEM_CODE'
declare curDPLCodes cursor for
select distinct(DPL_CODE) from DESCRITIONARY_MASTER order by DPL_CODE
declare @dplCode varChar(21)
declare @dplCodes varChar(1000)
set @dplCodes = ''
Open curDPLCodes
fetch next from curDPLCodes into @dplCode
while(@@fetch_status=0)
begin
SET @STR = @STR + ', ISNULL(['+@dplCode+'],0) AS ['+@dplCode+']'
-- insert into #dplCodes values(@dplCode)
if ltrim(rtrim(@dplCodes)) <> ''
begin
set @dplCodes = @dplCodes+','
end
set @dplCodes = @dplCodes+'['+@dplCode+']'
fetch next from curDPLCodes into @dplCode
end
close curDPLCodes
deallocate curDPLCodes
------------------------------------
SELECTITEM_CODE, DPL_CODE, MAX_AMOUNT
FROMDESCRITIONARY_POWER_LIST
) AS p
PIVOT(
MIN(MAX_AMOUNT) FOR DPL_CODE IN ('+@dplCodes+')'
print @STR
EXECUTE SP_EXECUTESQL @STR
drop table #dplCodes
thanks...
santhu..
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply