- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
查询返回工作日站点和时间的平均账单。 以下查询使用多个联合我想在单个查询中组合所有 我该怎么做。
select
a.Month,
'Weekday' as Type,
'Lunch' as 'Sale',
sum(case
when a.siteId = '102' then a.AvgBill
else '--'
end) as '102',
sum(case
when a.siteId = '103' then a.AvgBill
else '--'
end) as '103',
sum(case
when a.siteId = '104' then a.AvgBill
else '--'
end) as '104',
sum(case
when a.siteId = '105' then a.AvgBill
else '--'
end) as '105',
sum(case
when a.siteId = '106' then a.AvgBill
else '--'
end) as '106'
from
(select
date_format(o.dayStatus, '%M') as Month,
c.companyId as CompanyId,
o.companyId as siteId,
o.bill,
round((sum(o.bill) / count(orderId)), 2) as AvgBill,
date_format(DTTM, '%a') as Day
from
orders o, mdm_sites s, mdm_company c
where
s.siteId = o.companyId
and o.isBilled = 1
and billMode = 0
and s.companyid = c.CompanyId
and time(DTTM) <= '15:00'
and date_format(DTTM, '%a') not in ('Sat' , 'Sun')
and year(o.dayStatus) = (2014)
and case
when 0 in (1) then c.companyId in (c.companyid)
else c.companyId in (1)
end
and case
when 0 in (0) then o.companyId in (o.companyId)
else o.companyId in (0)
end
and case
when 0 in (12) then month(o.dayStatus) in (month(o.dayStatus))
else month(o.dayStatus) in (12)
end
group by c.companyId , o.companyId) as a
group by a.CompanyId
union (select
a.Month,
'Weekday' as Type,
'Evening' as 'Sale',
sum(case
when a.siteId = '102' then a.AvgBill
else '--'
end) as '102',
sum(case
when a.siteId = '103' then a.AvgBill
else '--'
end) as '103',
sum(case
when a.siteId = '104' then a.AvgBill
else '--'
end) as '104',
sum(case
when a.siteId = '105' then a.AvgBill
else '--'
end) as '105',
sum(case
when a.siteId = '106' then a.AvgBill
else '--'
end) as '106'
from
(select
date_format(o.daystatus, '%M') as Month,
c.companyId as CompanyId,
o.companyId as siteId,
o.bill,
round((sum(o.bill) / count(orderId)), 2) as AvgBill,
date_format(DTTM, '%a') as Day
from
orders o, mdm_sites s, mdm_company c
where
s.siteId = o.companyId
and o.isBilled = 1
and billMode = 0
and s.companyid = c.CompanyId
and time(DTTM) > '15:00'
and time(DTTM) < '19:00'
and date_format(DTTM, '%a') not in ('Sat' , 'Sun')
and year(o.dayStatus) = (2014)
and case
when 0 in (1) then c.companyId in (c.companyid)
else c.companyId in (1)
end
and case
when 0 in (0) then o.companyId in (o.companyId)
else o.companyId in (0)
end
and case
when 0 in (12) then month(o.dayStatus) in (month(o.dayStatus))
else month(o.dayStatus) in (12)
end
group by c.companyId , o.companyId) as a
group by a.CompanyId) union (select
a.Month,
'Weekday' as Type,
'Dinner' as 'Sale',
sum(case
when a.siteId = '102' then a.AvgBill
else '--'
end) as '102',
sum(case
when a.siteId = '103' then a.AvgBill
else '--'
end) as '103',
sum(case
when a.siteId = '104' then a.AvgBill
else '--'
end) as '104',
sum(case
when a.siteId = '105' then a.AvgBill
else '--'
end) as '105',
sum(case
when a.siteId = '106' then a.AvgBill
else '--'
end) as '106'
from
(select
date_format(o.dayStatus, '%M') as Month,
c.companyId as CompanyId,
o.companyId as siteId,
o.bill,
round((sum(o.bill) / count(orderId)), 2) as AvgBill,
date_format(DTTM, '%a') as Day
from
orders o, mdm_sites s, mdm_company c
where
s.siteId = o.companyId
and o.isBilled = 1
and billMode = 0
and s.companyid = c.CompanyId
and time(DTTM) >= '19:00'
and date_format(DTTM, '%a') not in ('Fri' , 'Sat', 'Sun')
and year(o.dayStatus) = (2014)
and case
when 0 in (1) then c.companyId in (c.companyid)
else c.companyId in (1)
end
and case
when 0 in (0) then o.companyId in (o.companyId)
else o.companyId in (0)
end
and case
when 0 in (12) then month(o.dayStatus) in (month(o.dayStatus))
else month(o.dayStatus) in (12)
end
group by c.companyId , o.companyId) as a
group by a.CompanyId) union (select
a.Month,
'Weekend' as Type,
'Lunch' as 'Sale',
sum(case
when a.siteId = '102' then a.AvgBill
else '--'
end) as '102',
sum(case
when a.siteId = '103' then a.AvgBill
else '--'
end) as '103',
sum(case
when a.siteId = '104' then a.AvgBill
else '--'
end) as '104',
sum(case
when a.siteId = '105' then a.AvgBill
else '--'
end) as '105',
sum(case
when a.siteId = '106' then a.AvgBill
else '--'
end) as '106'
from
(select
date_format(o.daystatus, '%M') as Month,
c.companyId as CompanyId,
o.companyId as siteId,
o.bill,
round((sum(o.bill) / count(orderId)), 2) as AvgBill,
date_format(DTTM, '%a') as Day
from
orders o, mdm_sites s, mdm_company c
where
s.siteId = o.companyId
and o.isBilled = 1
and billMode = 0
and s.companyid = c.CompanyId
and time(DTTM) <= '15:00'
and date_format(DTTM, '%a') in ('Sat' , 'Sun')
and year(o.dayStatus) = (2014)
and case
when 0 in (1) then c.companyId in (c.companyid)
else c.companyId in (1)
end
and case
when 0 in (0) then o.companyId in (o.companyId)
else o.companyId in (0)
end
and case
when 0 in (12) then month(o.dayStatus) in (month(o.dayStatus))
else month(o.dayStatus) in (12)
end
group by c.companyId , o.companyId) as a
group by a.CompanyId) union (select
a.Month,
'Weekend' as Type,
'Evening' as 'Sale',
sum(case
when a.siteId = '102' then a.AvgBill
else '--'
end) as '102',
sum(case
when a.siteId = '103' then a.AvgBill
else '--'
end) as '103',
sum(case
when a.siteId = '104' then a.AvgBill
else '--'
end) as '104',
sum(case
when a.siteId = '105' then a.AvgBill
else '--'
end) as '105',
sum(case
when a.siteId = '106' then a.AvgBill
else '--'
end) as '106'
from
(select
date_format(o.dayStatus, '%M') as Month,
c.companyId as CompanyId,
o.companyId as siteId,
o.bill,
round((sum(o.bill) / count(orderId)), 2) as AvgBill,
date_format(DTTM, '%a') as Day
from
orders o, mdm_sites s, mdm_company c
where
s.siteId = o.companyId
and o.isBilled = 1
and billMode = 0
and s.companyid = c.CompanyId
and time(DTTM) > '15:00'
and time(DTTM) < '19:00'
and date_format(DTTM, '%a') in ('Sat' , 'Sun')
and year(o.dayStatus) = (2014)
and case
when 0 in (1) then c.companyId in (c.companyid)
else c.companyId in (1)
end
and case
when 0 in (0) then o.companyId in (o.companyId)
else o.companyId in (0)
end
and case
when 0 in (12) then month(o.dayStatus) in (month(o.dayStatus))
else month(o.dayStatus) in (12)
end
group by c.companyId , o.companyId) as a
group by a.CompanyId) union (select
a.Month,
'Weekend' as Type,
'Dinner' as 'Sale',
sum(case
when a.siteId = '102' then a.AvgBill
else '--'
end) as '102',
sum(case
when a.siteId = '103' then a.AvgBill
else '--'
end) as '103',
sum(case
when a.siteId = '104' then a.AvgBill
else '--'
end) as '104',
sum(case
when a.siteId = '105' then a.AvgBill
else '--'
end) as '105',
sum(case
when a.siteId = '106' then a.AvgBill
else '--'
end) as '106'
from
(select
date_format(o.dayStatus, '%M') as Month,
c.companyId as CompanyId,
o.companyId as siteId,
o.bill,
round((sum(o.bill) / count(orderId)), 2) as AvgBill,
date_format(DTTM, '%a') as Day
from
orders o, mdm_sites s, mdm_company c
where
s.siteId = o.companyId
and o.isBilled = 1
and billMode = 0
and s.companyid = c.CompanyId
and time(DTTM) >= '19:00'
and date_format(DTTM, '%a') in ('Fri' , 'Sat', 'Sun')
and year(o.dayStatus) = (2014)
and case
when 0 in (1) then c.companyId in (c.companyid)
else c.companyId in (1)
end
and case
when 0 in (0) then o.companyId in (o.companyId)
else o.companyId in (0)
end
and case
when 0 in (12) then month(o.dayStatus) in (month(o.dayStatus))
else month(o.dayStatus) in (12)
end
group by c.companyId , o.companyId) as a
group by a.CompanyId);
the output of above query
month Type Sale 102 103 104 105 106
------------------------------------------------------------------
December Weekday Lunch 566.63 530.19 644.9 556.9 467.65
December Weekday Evening 433.89 404.69 457.18 547.22 396.6
December Weekday Dinner 427.68 354.54 371.42 386.21 366.03
December Weekend Lunch 410.57 381.36 383.86 365.94 394.14
December Weekend Evening 418.45 305.31 429.12 464.81 301.9
December Weekend Dinner 415.91 374.95 375.28 376.93 360.45
我如何在单个查询中执行此操作,谢谢
最佳答案
枢轴将是理想的解决方案,并节省执行计划。您的数据透视组将是月份、类型、销售额。您的计算组将为 102,103,104,105,106。
数据透视也将在单个查询中完成。
枢轴的一般指南: https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx
一个与您要完成的非常相似的示例: http://blogs.msdn.com/b/spike/archive/2009/03/03/pivot-tables-in-sql-server-a-simple-sample.aspx
关于mysql - 我如何在单个查询中组合多个联合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28048522/
我正在通过修改我为处理 slice 而创建的库来玩转泛型。我有一个 Difference接受 slice 并返回仅在其中一个 slice 中找到的唯一元素列表的函数。 我修改了函数以使用泛型,并且我正
Typescript 编译器 在我尝试使用联合或多个类型/接口(interface)时不断抛出错误。 My requirement 我从服务器收到一个对象作为响应,其中一个键 ('errorMessa
我需要在 SQLAlchemy 中执行 2 选择。例如: select1 = Session.query(col1, col2, col3, col4).filter(...) select2 = S
我建立了一个数据库来输入我所有的头痛和偏头痛跟踪数据。我正在提取一些查询,这些查询显示某一年中按月计算的不同头痛严重程度的计数。我有一个查询按月得到所有头痛,另一个在一定严重程度下得到头痛,最后一个在
我有三个表,一个是默认值表。 我需要做的是选择 TableA 和 TableB 的值,并从默认值的选择中回填任何缺失的值。 每个表都有一个键和值列。 数据的一个例子可能是这样的: DefaultTab
我正在尝试构建一个 单个 JSONPath 查询 ,它将测试 是否存在两个或多个路径 。 让我们考虑以下示例文档: { "firstName": "John",
我正在尝试基于对象中的嵌套属性创建联合类型。请参见下面的示例: type Foo = { abilities: { canManage: boolean } } typ
我有以下查询: SELECT result.globalId AS id, result.date, p1.playerName AS player, p2.playerName AS targe
我有两张 table 。第一个每天刷新。(该表有超过 10 列,但其中 2 列是相关的)我想根据 vid (这是一个唯一的 id )和人口进行每日统计。新的视频 ID 每天都会出现和消失。例如: 第一
这个问题已经有答案了: How to know what table a result came from when using UNION in MySQL (1 个回答) 已关闭 6 年前。 让我
我有 2 个表,一个列出人员及其与其属性的关系,另一个表列出属性(名字、姓氏等)。 人员表中的每个人可能不具有属性表中列出的所有属性。我想要的是每个人都为每个属性返回一行,无论他们是否有链接。 举个例
假设我们有 MySQL 服务器 A,我们需要在其中创建位于服务器 B 上的表的“副本”。 我们没有启用联合。重置服务器 A 会造成很多麻烦,我相信,我们不能在不重置的情况下启用联合。我也认为在B服务器
我有一个 Java 类 A。A 的构造函数调用了几个方法 m1、m2。 class A{ public A(){ m1(); m2(); ......
我正在开发一种编程语言,我想为其提供一个Range 数据类型,目前它不是通常的int 对列表。值 (x,y)约束条件是 x < y .我说不像通常那样,因为通常一个范围只是一对,但在我的例子中,它超过
我正在寻找加速一段合并两个 SortedLists 的代码。 C# 4.0 通用 SortedList:http://msdn.microsoft.com/en-us/library/ms132319
如果我有以下包含函数及其参数的联合,我该如何调用它? type Wrapper = { fn: (a: string) => void arg: string } | { fn: (a:
我正在尝试移植一个内部有一个联合的 C 结构。 Winapi.Winsock2.pas 中的默认结构记录中缺少某些字段。 但这是正确的方法吗?谢谢。 typedef struct _WSACOMPLE
我希望通过“版本”编号的前 8 个字符的子字符串对以下查询的结果进行排序。我理解 SUBSTRING(),所以不要用这个来打扰我。我的问题是尝试实际放置关于 UNION 的 ORDER BY。 更新:
我需要创建一个带有联合的 QueryBuilder,这可能吗? $qb = $this->em->createQueryBuilder() ->select('table1.numObject
我正在为 Magic the Gathering Cards 创建库存系统,需要使用主要卡片信息更新价格。 我有两个表,卡片和价格 卡片有以下列:ID、姓名、Ed、价格 价格有以下列:姓名、Ed、价格
我是一名优秀的程序员,十分优秀!