quot;Pivotingquot; a Table in SQL (i.e. Cross tabulation / crosstabulation)(“转动SQL中的表(即交叉表/交叉表))
问题描述
我正在尝试从几个数据库表中生成报告.简化版是这样的
I'm working on trying to generate a report from a couple of database tables. The simplified version looks like this
Campaign
----------
CampaignID
Source
-----------------------
Source_ID | Campaign_ID
Content
---------------------------------------------------------
Content_ID | Campaign_ID | Content_Row_ID | Content_Value
报告需要这样写:
CampaignID - SourceID - ContentRowID(Value(A)) - ContentRowID(Value(B))
ContentRowID(Value(A)) 表示查找具有给定 CampaignID 且 ContentRowId 为A"的行,然后获取该行的 ContentValue"
Where ContentRowID(Value(A)) means "Find a row the has a given CampaignID, and a ContentRowId of "A" and then get the ContentValue for that row"
基本上,我必须将行透视"(我认为这是正确的术语)成列...
Essentially, I have to "pivot" (I think that's the correct term) the rows into columns...
这是一个 Oracle 10g 数据库...
It's an Oracle 10g database...
有什么建议吗?
推荐答案
这是我第一次尝试.一旦我对 Content 表的内容有了更多了解,就会进行细化.
This is my first stab at it. Refinement coming once I know more about the contents of the Content table.
首先,你需要一个临时表:
First, you need a temporary table:
CREATE TABLE pivot (count integer);
INSERT INTO pivot VALUES (1);
INSERT INTO pivot VALUES (2);
现在我们可以查询了.
SELECT campaignid, sourceid, a.contentvalue, b.contentvalue
FROM content a, content b, pivot, source
WHERE source.campaignid = content.campaignid
AND pivot = 1 AND a.contentrowid = 'A'
AND pivot = 2 AND b.contentrowid = 'B'
这篇关于“转动"SQL中的表(即交叉表/交叉表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“转动"SQL中的表(即交叉表/交叉表)
基础教程推荐
- 是否可以执行按位分组功能? 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 在 SQL 中连接多个表 2021-01-01
- 无法解决整理冲突 2021-01-01
