- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
第一次在这里发帖,你们都帮了我很多。
我的问题是我正在使用 ODBC 连接到 SQL Server 2008 R2 数据库在 Excel 2010 中构建销售委员会。我有两个行组和两个列组。组按父子顺序排列。
行组是产品等级和产品。列组是日期和地区(东部和西部)。值(value)是按地区划分的产品价格。我正在尝试在每个给出(东部价格 - 西部价格)的区域组之后添加一个小计。有时只报告一个区域,因此如果是西部,则不应存在小计。
我尝试了计算项目的组合,这几乎可以工作,但为每个区域添加了一列。或者在 SQL Server 中进行工作,除了 Product Grade 分组将所有产品分布在每个父组中之外几乎可以工作。出于显而易见的原因,我更喜欢在 SQL Server 中进行这项工作,而且我是在 Excel 中构建数据的新手。我确定这是我所缺少的非常简单的东西。任何帮助将非常感激。
;with t as
(
select
info.protype,
info.product,
case when info.pricecode = 'x' then 'East' else 'West' end as Region,
SUM(isnull(info.price,0)) as Price,
case when r1.product=r2.product then SUM(x.price-y.price) else 0 end as Diff,
info.effectdate
from
(select
protype, product, pricecode, price, effectdate
from
prc_table
where
pricecode in ('x','y')
and protype = 'z') as info
left join
prc_table r1 on info.protype = r1.protype
and info.product = r1.product
and info.effectdate = r1.effectdate
and r1.pricecode = 'x'
left join
prc_table r2 on info.protype = r2.protype
and info.product = r2.product
and info.effectdate = r2.effectdate
and r2.pricecode = 'y'
where
info.effectdate >= DATEADD(MM, -3, GETDATE())
and info.effectdate <= GETDATE()
group by
info.effectdate, info.protype, info.product,
r1.product, r2.product, info.pricecode, r1.price, r2.price
)
select
c.codedesc as [Grade],
r.product as [Product],
r.Region as [Region],
r.Price as [Price],
r.Diff as [E-W],
r.effectdate as [Date]
from
t r
inner join
pro_item i on r.protype = i.protype and r.product = i.product
inner join
pro_duct p on i.protype = p.protype and i.product = p.product and i.1 = p.1
(product grade join)
inner join
xxx_codes c on p.desc3 = c.code and c.prefix = 'xxx'
where
i.protype = 'z'
and i.loc = 'loc'
and p.desc4 = ''
and i.branch = 'm'
order by
r.effectdate desc, codedesc, product
最佳答案
我明白你想要做什么。而且您在计算 sql 中的差异方面处于正确的轨道上,因为不可能根据它是东/西还是仅东来显示/隐藏数据透视表中的小计。所以用sql做是好的。我认为您在查询中存在一些设计缺陷的地方是试图将差异放在单独的列中。
问题是,如何在数据透视表中呈现它?如果您将 Price 和 Diff 作为数据透视表中的值字段,它将完全破坏布局。您只需要一个值字段 - 价格。并且您希望 East、West 和 Diff 作为您的列标题,因此它们都应该是您数据中的区域。
因此,在您的 sql 中,像您一样获取等级、产品、地区、价格和日期,将其放入临时表中,然后将差异记录插入到该临时表中,其中 region='Diff' 和 Price=实际差异值,其中该产品和生效日期同时存在东部和西部记录。
您的输出应如下所示:
Grade Product Region Price Effdt
A P1 East 1.5 31-Oct-14
A P2 East 3 31-Oct-14
B P3 East 5 31-Oct-14
B P4 East 2.44 31-Oct-14
C P5 East 3.67 31-Oct-14
C P6 East 10.3 31-Oct-14
B P3 West 5.5 31-Oct-14
B P4 West 2.7 31-Oct-14
C P5 West 3.8 31-Oct-14
C P6 West 10.7 31-Oct-14
A P1 East 1.5 30-Nov-14
A P2 East 3 30-Nov-14
B P3 East 5 30-Nov-14
B P4 East 2.44 30-Nov-14
C P5 East 3.67 30-Nov-14
C P6 East 10.3 30-Nov-14
B P3 Diff -0.5 31-Oct-14
B P4 Diff -0.26 31-Oct-14
C P5 Diff -0.13 31-Oct-14
C P6 Diff -0.4 31-Oct-14
关于来自 MS 查询计算和分组问题的 Excel 2010 数据透视表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26435230/
我有一台 MySQL 服务器和一台 PostgreSQL 服务器。 需要从多个表中复制或重新插入一组数据 MySQL 流式传输/同步到 PostgreSQL 表。 这种复制可以基于时间(Sync)或事
如果两个表的 id 彼此相等,我尝试从一个表中获取数据。这是我使用的代码: SELECT id_to , email_to , name_to , status_to
我有一个 Excel 工作表。顶行对应于列名称,而连续的行每行代表一个条目。 如何将此 Excel 工作表转换为 SQL 表? 我使用的是 SQL Server 2005。 最佳答案 这取决于您使用哪
我想合并两个 Django 模型并创建一个模型。让我们假设我有第一个表表 A,其中包含一些列和数据。 Table A -------------- col1 col2 col3 col
我有两个表:table1,table2,如下所示 table1: id name 1 tamil 2 english 3 maths 4 science table2: p
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 1 年前。 Improve th
下面两个语句有什么区别? newTable = orginalTable 或 newTable.data(originalTable) 我怀疑 .data() 方法具有性能优势,因为它在标准 AX 中
我有一个表,我没有在其中显式定义主键,它并不是真正需要的功能......但是一位同事建议我添加一个列作为唯一主键以随着数据库的增长提高性能...... 谁能解释一下这是如何提高性能的? 没有使用索引(
如何将表“产品”中的产品记录与其不同表“图像”中的图像相关联? 我正在对产品 ID 使用自动增量。 我觉得不可能进行关联,因为产品 ID 是自动递增的,因此在插入期间不可用! 如何插入新产品,获取产品
我有一个 sql 表,其中包含关键字和出现次数,如下所示(尽管出现次数并不重要): ____________ dog | 3 | ____________ rat | 7 | ____
是否可以使用目标表中的LAST_INSERT_ID更新源表? INSERT INTO `target` SELECT `a`, `b` FROM `source` 目标表有一个自动增量键id,我想将其
我正在重建一个搜索查询,因为它在“我看到的”中变得多余,我想知道什么 (albums_artists, artists) ( ) does in join? is it for boosting pe
以下是我使用 mysqldump 备份数据库的开关: /usr/bin/mysqldump -u **** --password=**** --single-transaction --databas
我试图获取 MySQL 表中的所有行并将它们放入 HTML 表中: Exam ID Status Assigned Examiner
如何查询名为 photos 的表中的所有记录,并知道当前用户使用单个查询将哪些结果照片添加为书签? 这是我的表格: -- -- Table structure for table `photos` -
我的网站都在 InnoDB 表上运行,目前为止运行良好。现在我想知道在我的网站上实时发生了什么,所以我将每个页面浏览量(页面、引荐来源网址、IP、主机名等)存储在 InnoDB 表中。每秒大约有 10
我在想我会为 mysql 准备两个表。一个用于存储登录信息,另一个用于存储送货地址。这是传统方式还是所有内容都存储在一张表中? 对于两个表...有没有办法自动将表 A 的列复制到表 B,以便我可以引用
我不是程序员,我从这个表格中阅读了很多关于如何解决我的问题的内容,但我的搜索效果不好 我有两张 table 表 1:成员 id*| name | surname -------------------
我知道如何在 ASP.NET 中显示真实表,例如 public ActionResult Index() { var s = db.StaffInfoDBSet.ToList(); r
我正在尝试运行以下查询: "insert into visits set source = 'http://google.com' and country = 'en' and ref = '1234
我是一名优秀的程序员,十分优秀!