English 中文(简体)
外交部
原标题:SQL PIVOT ON Across Multiple Tables

我正试图在多个表格中填写一个PIVOT(管理服务器2008年),没有涉及总的功能。 我在这里的深 to中不得不坦率地讲我,在努力确定问题的时候,我只想跳跃,向你们展示我的 st(oo),首先,我有三张表格:

CHARTER_vessels
===============

vesselID    vesselName
--------    ----------
1           The Titanic
2           The Pinafore
3           The Black Pearl


CHARTER_rateDateRange
=====================

rateDateRangeID     rateDateRangeName
---------------     -----------------
1                   Spring 2012
2                   Summer 2012
3                   Fall 2012


CHARTER_rates
=============

vesselID     rateDateRangeID      rateCost
--------     ---------------      --------
1            1                    434
1            2                    445
1            3                    231
2            1                    675
2            2                    545
2            3                    768
3            1                    543
3            2                    654
3            3                    658

And the output I m trying to achieve is that the rates for each boat appear in the column for each season, like this:

vesselName         Spring 2012     Summer 2012     Fall 2012
----------         -----------     -----------     ---------
The Titanic        434             445             231
The Pinafore       675             545             768
The Black Pearl    543             654             658

显然,我希望能够尽可能区分不同栏目确定的结果!

问题回答

以下假设船只和日期范围的独特性。 如果情况如此,你不想把一片烟.混为一谈,不是你们的事。 配对器(Cost)是使用SQ服务器机块的要求。 需要建立一个SQ服务器机制,以决定如果一艘船舶有同一日期的多个,将如何返回。 如果发生这种情况,总合实际上毫无意义。 另一种选择是一系列自我结合。 让我知道,你们是否需要看到自我加入的解决办法。

SELECT src.vesselName,pvt.[Spring 2012], pvt.[Summer 2012], pvt.[Fall 2012]
FROM
(select vesselName, rateCost, rateDateRangeName 
from CHARTER_rateDateRange crd
inner join CHARTER_rates cr on cr.rateDateRangeID = crd.rateDateRangeID
inner join CHARTER_vessels cv on cv.vesselID   = crd.vesselID) AS src
PIVOT
(
max(rateCost)
FOR rateDateRangeName IN ([Spring 2012], [Summer 2012], [Fall 2012])
) AS pvt;

为什么没有其他人这样做,就是自我加入的解决办法。 审慎态度根本不是最佳的。

with joinMe as (
select vesselName, rateCost, rateDateRangeName 
from CHARTER_rateDateRange crd
inner join CHARTER_rates cr on cr.rateDateRangeID = crd.rateDateRangeID
inner join CHARTER_vessels cv on cv.vesselID   = crd.vesselID
)

select a.vesselName,a.rateCost as  Spring 2012 ,b.rateCost as  Summer 2012 ,c.rateCost as  Fall 2012 
from joinMe a
inner join joinMe b on b.vesselName= a.vesselName
                   and b.rateDateRangeName =  Summer 2012 
inner join joinMe c on c.cesselName = a.vesselName
                    and c.rateDateRangeName =  Fall 2012 
where a.rateDateRangeName =  Spring 2012 

由于篇幅有限,我将在此向各位发出问答。 你们的以下回报是多少?

select vesselName, rateDateRangeName,count(rateCost)
    from CHARTER_rateDateRange crd
    inner join CHARTER_rates cr on cr.rateDateRangeID = crd.rateDateRangeID
    inner join CHARTER_vessels cv on cv.vesselID   = cr.vesselID
group by vesselName,rateDateRangeName 
order by count(rateCost) desc




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签