我正在记录一个事件表,并且想要一个输出(如本答案底部所示),问题是我正在获取行中的数据,但希望它们按列显示。
两个维度如下:
我想我必须按时间部分(例如按分钟或每小时)对日期时间值进行分组并检索每个服务器的事件列表,但我不知道如何编写查询?我需要一个by子句吗?
我已经写好了create语句,结构如下:
CREATE TABLE [dbo].[events](
[guidEventId] [uniqueidentifier] PRIMARY KEY,
[dtStart] [datetime2](7) NOT NULL,
[nDirection] [int] NOT NULL,
[nServerId] [int] NOT NULL
)
INSERT INTO events
([guidEventId],[dtStart],[nDirection],[nServerId])
VALUES
(newId(),'2013-08-01 00:01:00','0','1'),
(newId(),'2013-08-01 00:02:00','1','1'),
(newId(),'2013-08-01 00:03:00','0','1'),
(newId(),'2013-08-01 00:04:00','1','2'),
(newId(),'2013-08-02 00:01:00','0','1'),
(newId(),'2013-08-02 00:02:00','1','2'),
(newId(),'2013-08-02 00:03:00','0','2'),
(newId(),'2013-08-02 00:04:00','1','2');
Run Code Online (Sandbox Code Playgroud)
http://sqlfiddle.com/#!6/25e74
茶几应该看起来像这样(白天):
DateTime | Server1 | Server2 | Sum |
--------------------------------------------
2013-08-01 | 3 | 1 | 4 |
2013-08-02 | 1 | 3 | 4 |
Run Code Online (Sandbox Code Playgroud) 我面临的基本问题是我需要每个项目的最新记录。
一些设置... MySQL 5.6.14。
我需要创建两个视图(因为 MySQL 不允许我在视图中有子查询)。我的第一个查询设置了这样的数据。
select
`inventoryrecords`.`inventoryrecordid` AS `inventoryrecordid`,
`inventoryrecords`.`logicaldeviceid` AS `logicaldeviceid`,
`inventoryrecords`.`passrfid` AS `passrfid`,
`inventoryrecords`.`tagepc` AS `tagepc`,
`inventoryrecords`.`currentstate` AS `currentstate`,
`inventoryrecords`.`statedateutc` AS `statedateutc`,
`inventoryrecords`.`ownerobjectid` AS `ownerobjectid`,
`inventoryrecords`.`ownerobjecttype` AS `ownerobjecttype`
from
`inventoryrecords`
where
1
order by `inventoryrecords`.`statedateutc` desc
Run Code Online (Sandbox Code Playgroud)
然后我可以使用我的“真实”查询将所有内容限制为每个 TagEPC 的最后一条记录。
select
`lastinventoryrecords_step1`.`inventoryrecordid` AS `inventoryrecordid`,
`lastinventoryrecords_step1`.`logicaldeviceid` AS `logicaldeviceid`,
`lastinventoryrecords_step1`.`passrfid` AS `passrfid`,
`lastinventoryrecords_step1`.`tagepc` AS `tagepc`,
`lastinventoryrecords_step1`.`currentstate` AS `currentstate`,
`lastinventoryrecords_step1`.`statedateutc` AS `statedateutc`,
`lastinventoryrecords_step1`.`ownerobjectid` AS `ownerobjectid`,
`lastinventoryrecords_step1`.`ownerobjecttype` AS `ownerobjecttype`
from
`lastinventoryrecords_step1`
group by `lastinventoryrecords_step1`.`tagepc`
order by `lastinventoryrecords_step1`.`statedateutc` desc
Run Code Online (Sandbox Code Playgroud)
当我尝试从“真实”视图中选择 * 时,我没有得到我期望的数据。但是,当我在窗口中使用子查询运行查询时。 …
我想在一行而不是多行中显示每个人的联系信息和他们的佣金。
ID COMMISSION DATE
N12545233 774 4/1/15
N12545233 361 5/1/14
N12545233 685 6/1/15
N14521746 3384 10/1/14
N14521746 1188 11/1/14
N14521746 1256 12/1/14
N15981070 1188 1/1/15
N15981070 616 2/1/15
N15981070 936 3/1/15
Run Code Online (Sandbox Code Playgroud)
ID LASTNAME FIRSTNAME STREET CITY STATE ZIP
N12545233 Jones Mark 123 Main St Brooklyn NY 11377
N14521746 Greene Phil 345 Broadway Queens NY 11237
N15981070 Stahl Barbara 567 Lexington New York NY 10018
Run Code Online (Sandbox Code Playgroud)
ID LASTNAME FIRSTNAME STREET CITY STATE ZIP COMMISSION DATE COMMISSION DATE COMMISSION DATE …Run Code Online (Sandbox Code Playgroud) 我有一个 500 行的结果集,但这是 100 张发票。我只需要最后一张发票的前 10 个。
我发现了类似的东西:如何从每个类别中选择前 10 条记录
样本数据:
InvNr | DetailLine
111 | 1
111 | 2
112 | 1
112 | 2
112 | 3
113 | 1
113 | 2
114 | 1
115 | 1
... | ...
Run Code Online (Sandbox Code Playgroud)
我希望得到的是例如:
SELECT DISTINCT TOP 2 InvNr, DetailLine FROM tbl_Invoice
Run Code Online (Sandbox Code Playgroud)
有了这个结果:
InvNr | DetailLine
111 | 1
111 | 2
112 | 1
112 | 2
112 | 3
Run Code Online (Sandbox Code Playgroud)
更新:
所以我需要他们创建的最后 10 个(或上例中的前 2 个)发票,但每个发票可以有“x”个明细行,我希望结果中的所有明细行(最后 10 …
我有一个查询问题。我有一个带有汽车及其规格的数据库,每个型号都有与该型号相关的不同汽车及其价格。我想创建一个查询,为我提供每个模型的最低和最高价格。
像这样:
+--------+----------+
| model | price |
+--------+----------+
| golf | 4000 |
| golf | 6000 |
| golf | 10000 |
| panda | 3000 |
| panda | 5000 |
| panda | 7000 |
+--------+----------+
Run Code Online (Sandbox Code Playgroud)
查询给了我这个:
+--------+----------+
| model | price |
+--------+----------+
| golf | 4000 |
| golf | 10000 |
| panda | 3000 |
| panda | 7000 |
+--------+----------+
Run Code Online (Sandbox Code Playgroud)
你能帮助我吗?
此问题在纯 sql 中搜索答案(无 db-server 特定代码)
-edit- 应有的评论/答案:“纯 sql”- SQL92/99 因为这适用于所有服务器。我不仅在 mysql 上使用此代码,这只是我尝试的第一个代码。我必须支持 Microsoft SQL Server (2000!)、h2、PostGRE SQL 和 MySQL Server,但代码也应该适用于其他人(独立于数据库服务器)。如果“纯sql”不够详细,我很抱歉。下次我会指定 SQL 修订的要求 (3/4, 92/99)
我必须向后兼容。这就是我没有指定数据库服务器的原因。这是故意的。-编辑结束-
我尝试从具有给定限制的表中获取所有值。我用于查询的数据是:可能的语言环境和一个 max_priority
这是要使用的表
SELECT * FROM i18n
Run Code Online (Sandbox Code Playgroud)
.
id|featureName|locale |priority|text
--+-----------+-----------+--------+----
1| type | en| 1|father
2| type | de| 2|Vater
3| type | de_AT| 3|Papa
4| type | de_AT_Wien| 4|Oida
5| firstName | en| 1|first name
6| firstName | de| 2|Vorname
7| lastName | en| 1|last name
8| lastName | de| 2|Nachname
9| lastName …Run Code Online (Sandbox Code Playgroud) 我有一个零件表:
ID, Description
ABC-123 ABC
DEF-456 DEF
Run Code Online (Sandbox Code Playgroud)
我有一个用户定义的字段表,将信息存储在一行中。
ID, DOCUMENTID, STRING_VAL, DATE_VAL
UDF-000021, ABC-123, TEXT123, NULL
UDF-000022, ABC-123, NULL, 6/10/2016
UDF-000023, ABC-123, JOHN WORKING ON IT, NULL
UDF-000024, ABC-123, NULL, 7/1/2016
UDF-000025, ABC-123, YES, NULL
UDF-000021, DEF-456, TEXT123, NULL
UDF-000022, DEF-456, NULL, 6/10/2016
Run Code Online (Sandbox Code Playgroud)
我需要信息看起来像这样:
PART ID DESCRIPTION REVISION REVISION DATE NOTES MARKUP DATE IM
-------------------------------------------------------------------------------
ABC-123 ABC TEXT123 6/10/2016 JOHN 7/1/2016 YES
DEF0456 DEF TEXT123 6/10/2016
Run Code Online (Sandbox Code Playgroud)
我看过连接、枢轴等,我想我似乎无法理解它。任何帮助是极大的赞赏。
我在 MySql 中有这个视图查询,它可以工作
SELECT s.id AS id,
s.customer_id AS customer_id,
s.shipment_status_id AS shipment_status_id,
ss.name AS shipment_status_name,
s.ship_from_date AS ship_from_date,
s.ship_to_company AS ship_to_company,
s.ship_from_company AS ship_from_company,
s.ship_to_address1 AS ship_to_address1,
s.ship_to_address2 AS ship_to_address2,
s.ship_to_postal_code AS ship_to_postal_code,
s.ship_to_city AS ship_to_city,
c.country AS country,
p.tracking_numbers AS tracking_numbers,
s.quote_service_name AS quote_service_name,
s.quote_total_charge AS quote_total_charge,
p.weight AS weight,
p.insurance_amount AS insurance_amount,
p.cod_amount AS cod_amount,
z.fedex AS ZONE,
s_p.code AS state_province_code,
s_p.zone AS state_province_zone,
ss.name AS status_name,
i.total AS total,
cust.first_name AS customer_first_name,
cust.last_name AS customer_last_name,
(CASE …Run Code Online (Sandbox Code Playgroud) 我看过一些建议,但想知道在不使用子查询或不必要的联接的情况下从数据表中选择组、组总数、总计的最佳方法。
我最初的想法是这样的:
select product_family,
sum(widgets),
sum(widgets) over ()
from table.widget
group by product_family
Run Code Online (Sandbox Code Playgroud)
或以下内容:
select product_family,
sum(widgets),
sum(widgets) over (partition by all_field)
from table.widget
group by product_family
Run Code Online (Sandbox Code Playgroud)
显然这两种方法都不起作用。我知道一个分区的顺序可能比实际的行/组更高,但除了第一个示例将其留空之外,我不知道如何按“所有”进行分区。但是,它打破了 group by 语句。
我能找到的最好的是这样的:
select product_family,
family_sum,
sum(family_sum) over () as grand_sum
from (
select product_family, sum(widgets)
from table.widget
group by product_family
) as A
Run Code Online (Sandbox Code Playgroud)
不过,这仍然涉及子查询,这没关系。我只是觉得我在这里缺少一个简单的功能。
我正在编写一个查询来检查我是否在某个列中有重复项。
SELECT COUNT(thecol) FROM thetable WHERE thecol IS NOT NULL GROUP BY thecol
HAVING COUNT(*) > 1
Run Code Online (Sandbox Code Playgroud)
这会给我一个输出
3
5
6
Run Code Online (Sandbox Code Playgroud)
这当然是 thecol 不唯一的每个值的记录数。
现在我如何只得到总和 14?我应该将现有查询作为子查询吗?
group-by ×10
sql-server ×5
mysql ×2
aggregate ×1
datetime ×1
duplication ×1
functions ×1
join ×1
postgresql ×1
top ×1
view ×1