Showing posts with label crosstab. Show all posts
Showing posts with label crosstab. Show all posts

Friday, March 30, 2012

Please Help me Conversion CrossTab Access to SQL Server 2000 Query

TRANSFORM IIf(Sum(IIf([blockinyield]=True,[SIZE],0))>0,Sum([Y_TOTAL_ton])/Sum(IIf([blockinyield]=True,[SIZE],0)),0) AS Yield_THA
SELECT OILPALM.NAME, OILPALM.YEAR, formatyear([year]) AS yearDisplay, Count(OILPALM.BLOCK) AS CountOfBLOCK
FROM OILPALM
GROUP BY OILPALM.NAME, OILPALM.YEAR
PIVOT Year([D_PLANTED]);

Please Help me

Thank You

Hi Sam,

Today, my recommendation is the same one that I gave you yesterday. Click here to check it out. Several folks have taken their time and given you suggestions -and you don't seem to be learning from those suggestions.

In fact, I'll post it again:

I suggest that you take the time to work through the suggestions that have been provided to your previous posts until you understand the process.

When you work through the process and begin to understand the process, you will be able to work these out for yourself. (Otherwise, we might think that you are just trying to get folks to do your work for you. And that probably wouldn't be fair to you ...)

|||not understand the process Function TRANSFORM and PIVOT?please help me

thank you

|||

Transform is NOT a Transact-SQL keyword -in this situation, you can probably safely ignore it.

PIVOT is a SQL 2005 keyword that displays rows as columns.

You 'should' have a copy of SQL Server 2005 Books Online. You can get a lot of good information from that source.

A couple of folks indicated to you in your other posts that IIF was not a SQL function (it is an Access function), and they offered suggestions about how to turn IIF functions into CASE structures. Perhaps looking in Books Online and reading about using CASE would be useful.

I, for one, am willing to assist those that seem intent on learning and helping themselves. BUT I get annoyed to repeated be asked to do someone's work for them -UNLESS, of course, I'm getting paid for it.

Just in case others here are not so compelled as I, I shall refrain from additional comments on your posts. Good luck.

|||

This query will help you...

Code Snippet

Declare @.JoinQuery as Varchar(1000);
Declare @.SelectQuery as Varchar(1000);
Declare @.PreparedJoinQuery as Varchar(1000);
Declare @.PreparedSelectQuery as Varchar(1000);
Select @.JoinQuery = '', @.SelectQuery = ''
Select @.PreparedJoinQuery = 'Left Outer Join OILPALM as [?] On [?].Year=? and [?].NAME=[Main].NAME '
Select @.PreparedSelectQuery =',Count([?].BLOCK) as [?]'
Select
@.JoinQuery = @.JoinQuery + Replace(@.PreparedJoinQuery,'?',Cast(year as Varchar))
,@.SelectQuery = @.SelectQuery + Replace(@.PreparedSelectQuery,'?',Cast(year as Varchar)) From OILPALM Group By Year

Exec ('Select [Main].NAME' + @.SelectQuery + ' From (Select Distinct NAME From OILPALM) as [Main]' + @.JoinQuery + ' Group By [Main].NAME')

|||This query help me....Thank you so much...

Monday, February 20, 2012

PIVOT Query Question - SQL 2005

I am trying to create a crosstab query using SQL 2005 and the PIVOT
operator using the following data:
Id MetricId Metric BaseVal PeerVal
012141 1 MarketValueChange 0.08 0.46
012141 2 BarraPredictedBeta NULL 1.44
012141 3 Beta 1.261 1.81
012141 4 PERatio 21.80 75.39
012141 5 EquityDRate 0.12 0.14
and I can create a query that contains just the BaseVal data with the
query below. How can add to the query to include the PeerVal data so
the output would look like this:
Id MarketValueChange-Base MarketValueChange-Peer...
012141 0.08 0.46
Also, do I need to "hardcode" the metricnames in the select statement
when I have them in the data or can I dynamically have them set as my
column names?
Select Id
,"1" AS MarketValueChange1Year
,"2" AS BarraPredictedBeta
,"3" AS Beta
,"4" AS PriceToEarningsRatio
,"5" AS EquityDiscountRate
FROM (
SELECT Id, MetricID, BaseVal
FROM OutputAll WHERE Id= '012141')
p PIVOT (
MIN(baseval)
FOR metricid IN ("1","2","3","4","5")
) pvt
Order by Id
GO
Thanks
*** Sent via Developersdex http://www.examnotes.net ***Hello John .,

> Also, do I need to "hardcode" the metricnames in the select statement
> when I have them in the data or can I dynamically have them set as my
> column names?
I'll give part 1 some more thought, but as to this question, yes, you do
have to hardcode it today. If you want a dynamic pivot, you'll need a dynami
c
query.
Thank you,
Kent Tegels
DevelopMentor
http://staff.develop.com/ktegels/|||PIVOT in 2005 is like taking your wife to McDonald's
on your anniversary:)
It's quite shallow to say the least.
Check out RAC for all dynamic pivoting/xtabs problems.
www.rac4sql.net
"John ." <jrp210@.yahoo.com> wrote in message
news:%230aGTavFGHA.1396@.TK2MSFTNGP11.phx.gbl...
> I am trying to create a crosstab query using SQL 2005 and the PIVOT
> operator using the following data:
> Id MetricId Metric BaseVal PeerVal
> 012141 1 MarketValueChange 0.08 0.46
> 012141 2 BarraPredictedBeta NULL 1.44
> 012141 3 Beta 1.261 1.81
> 012141 4 PERatio 21.80 75.39
> 012141 5 EquityDRate 0.12 0.14
>
> and I can create a query that contains just the BaseVal data with the
> query below. How can add to the query to include the PeerVal data so
> the output would look like this:
> Id MarketValueChange-Base MarketValueChange-Peer...
> 012141 0.08 0.46
> Also, do I need to "hardcode" the metricnames in the select statement
> when I have them in the data or can I dynamically have them set as my
> column names?
> Select Id
> ,"1" AS MarketValueChange1Year
> ,"2" AS BarraPredictedBeta
> ,"3" AS Beta
> ,"4" AS PriceToEarningsRatio
> ,"5" AS EquityDiscountRate
> FROM (
> SELECT Id, MetricID, BaseVal
> FROM OutputAll WHERE Id= '012141')
> p PIVOT (
> MIN(baseval)
> FOR metricid IN ("1","2","3","4","5")
> ) pvt
> Order by Id
> GO
> Thanks
> *** Sent via Developersdex http://www.examnotes.net ***|||> Also, do I need to "hardcode" the metricnames in the select statement
> when I have them in the data or can I dynamically have them set as my
> column names?
>
That would be nice, but not in 2005. PIVOT and UNPIVOT requires hardcoded
values. Use dynamic SQL to get around that if necessary..
Perhaps something like this will work for you. Union the set together
renaming the metric to include the value you want, and then do the pivot on
it:
create table test
(
id char(6),
MetricId tinyint,
Metric varchar(20),
baseval numeric(4,2),
peerval numeric(4,2)
)
go
insert into test
select 012141, 1, 'MarketValueChange', 0.08, 0.46
UNION ALL
select 012141, 2, 'BarraPredictedBeta', NULL, 1.44
UNION ALL
select 012141, 3, 'Beta', 1.261, 1.81
UNION ALL
select 012141, 4, 'PERatio', 21.80, 75.39
UNION ALL
select 012141, 5, 'EquityDRate', 0.12, 0.14
go
--CTE to cover the UNION
With flatter as
(
select id, metric + '-baseVal' as metric, baseVal as value
from test
UNION ALL
select id, metric + '-peerVal', peerVal
from test )
--then hardcoding the names of the columns
SELECT id, [MarketValueChange-baseVal], [MarketValueChange-peerVal]
FROM ( SELECT id, value, metric
FROM flatter) AS expanded
PIVOT ( max(value) FOR metric IN
([MarketValueChange-baseVal], [MarketValueChange-peerVal])
) AS ourPivot
----
Louis Davidson - http://spaces.msn.com/members/drsql/
SQL Server MVP
"Arguments are to be avoided: they are always vulgar and often convincing."
(Oscar Wilde)
"John ." <jrp210@.yahoo.com> wrote in message
news:%230aGTavFGHA.1396@.TK2MSFTNGP11.phx.gbl...
>I am trying to create a crosstab query using SQL 2005 and the PIVOT
> operator using the following data:
> Id MetricId Metric BaseVal PeerVal
> 012141 1 MarketValueChange 0.08 0.46
> 012141 2 BarraPredictedBeta NULL 1.44
> 012141 3 Beta 1.261 1.81
> 012141 4 PERatio 21.80 75.39
> 012141 5 EquityDRate 0.12 0.14
>
> and I can create a query that contains just the BaseVal data with the
> query below. How can add to the query to include the PeerVal data so
> the output would look like this:
> Id MarketValueChange-Base MarketValueChange-Peer...
> 012141 0.08 0.46
> Also, do I need to "hardcode" the metricnames in the select statement
> when I have them in the data or can I dynamically have them set as my
> column names?
> Select Id
> ,"1" AS MarketValueChange1Year
> ,"2" AS BarraPredictedBeta
> ,"3" AS Beta
> ,"4" AS PriceToEarningsRatio
> ,"5" AS EquityDiscountRate
> FROM (
> SELECT Id, MetricID, BaseVal
> FROM OutputAll WHERE Id= '012141')
> p PIVOT (
> MIN(baseval)
> FOR metricid IN ("1","2","3","4","5")
> ) pvt
> Order by Id
> GO
> Thanks
> *** Sent via Developersdex http://www.examnotes.net ***