首页 \ 问答 \ 将JOIN与DISTINCT一起使用并确定一个表的优先级(Using JOIN with DISTINCT and prioritize one table)

将JOIN与DISTINCT一起使用并确定一个表的优先级(Using JOIN with DISTINCT and prioritize one table)

我试图结合2个表的数据。
这两个表都包含来自同一传感器的数据(比方说,传感器测量二氧化碳,每10分钟输入一次)。

第一个表包含经过验证的数据。 我们称之为station1_validated 。 第二个表包含原始数据。 我们称之为station1_nrt

原始数据表包含实时数据,而经验证的表仅包含至少1个月的数据点。 (它需要一些时间来验证这些数据并在之后手动控制它,这种情况每个月只发生一次)。

我现在要做的是将这两个表的数据结合起来在网站上显示实时数据。 但是,当验证数据可用时,它应该优先考虑原始数据点上的数据点。

相关的列是:

  • timed [bigint(20)]:包含日期时间作为unix时间戳,以毫秒为单位,从1.1.1970开始
  • CO2 [双倍]:包含测量的CO2浓度(ppm)(百万分率)

我写了这个基本的SQL:

SELECT 
    *
FROM
    (SELECT 
        timed, CO2, '2' tab
    FROM
        station1_nrt
    WHERE
        TIMED >= 1386932400000
            AND TIMED <= 1386939600000
            AND TIMED NOT IN (SELECT 
                timed
            FROM
                station1_nrt
            WHERE
                CO2 IS NOT NULL
                    AND TIMED >= 1386932400000
                    AND TIMED <= 1386939600000) UNION SELECT 
        timed, CO2, '1' tab
    FROM
        station1_validated
    WHERE
        CO2 IS NOT NULL
            AND TIMED >= 1386932400000
            AND TIMED <= 1386939600000) a
ORDER BY timed

这不能正常工作,因为它只选择两个表都有条目的数据点。 但是我现在想用JOIN来做这件事,因为它会更快。 但是我不知道如何使用DISTINCT(或类似的东西)来加入表格的优先级。 有人可以帮我解决这个问题(或解释一下吗?)


I am trying to combine data from 2 tables.
Those 2 tables both contain data from the same sensor (lets say a sensor that measures CO2 with 1 entry per 10 minutes).

The first table contains validated data. Let's call it station1_validated. The 2nd table contains raw data. Let's call this one station1_nrt.

While the raw-data table contains live data, the validated table contains only data points that are at least 1 month old. (It needs some time to validate those data and to control it manually afterwards, this happens only once every month).

What I am trying to do now is to combine the data of those 2 tables to display live data on a website. However when validated data is available it should prioritize that data point over the raw data-point.

The relevant columns for this are:

  • timed [bigint(20)]: Contains the datetime as a unix timestamp in milliseconds from 1.1.1970
  • CO2 [double]: Contains the measured concentration of CO2 in ppm (parts per million)

I wrote this basic SQL:

SELECT 
    *
FROM
    (SELECT 
        timed, CO2, '2' tab
    FROM
        station1_nrt
    WHERE
        TIMED >= 1386932400000
            AND TIMED <= 1386939600000
            AND TIMED NOT IN (SELECT 
                timed
            FROM
                station1_nrt
            WHERE
                CO2 IS NOT NULL
                    AND TIMED >= 1386932400000
                    AND TIMED <= 1386939600000) UNION SELECT 
        timed, CO2, '1' tab
    FROM
        station1_validated
    WHERE
        CO2 IS NOT NULL
            AND TIMED >= 1386932400000
            AND TIMED <= 1386939600000) a
ORDER BY timed

This does not work correctly as it selects only those data points where both tables have an entry. However I want to do this with a JOIN now as it would be much faster. However I don't know how to a JOIN with a DISTINCT (or something similar) with prioritizing a table. Could someone help me out with this (or explain it?)


原文:https://stackoverflow.com/questions/20659686
更新时间:2022-07-06 20:07

最满意答案

由于您使用的是PostgreSQL,因此您可以使用hstore在数据库列中存储任意哈希:

此模块实现hstore数据类型,用于在单个PostgreSQL值中存储键/值对的集合。 这在各种场景中都很有用,例如具有许多很少检查的属性的行或半结构化数据。 键和值只是文本字符串。

甚至还有一个用于向ActiveRecord添加hstore支持的gem:

https://github.com/softa/activerecord-postgres-hstore

然后你可以创建一个名为client_specifichstore列,并使用以下内容查看其中:

M.where("client_specific -> 'likes' = 'pancakes'")
M.where("client_specific @> 'likes=>pancakes'")

看看哪些客户注意到他们喜欢煎饼。

您可能希望在客户记录的某处存储客户特定字段的列表,以使UI方面更容易处理,但这很简单。


Since you're using PostgreSQL, you could use hstore to store arbitrary hashes in database columns:

This module implements the hstore data type for storing sets of key/value pairs within a single PostgreSQL value. This can be useful in various scenarios, such as rows with many attributes that are rarely examined, or semi-structured data. Keys and values are simply text strings.

There's even a gem for adding hstore support to ActiveRecord:

https://github.com/softa/activerecord-postgres-hstore

Then you could create an hstore column called, say, client_specific and look inside it with things like:

M.where("client_specific -> 'likes' = 'pancakes'")
M.where("client_specific @> 'likes=>pancakes'")

to see which clients have noted that they like pancakes.

You might want to store a list of customer-specific fields somewhere with the customer record to make the UI side of things easier to deal with but that's pretty simple to do.

相关问答

更多
  • 您可以尝试动态定义模型,然后调用store.setModel() 。 var starkStore = Ext.create('Ext.data.Store', { model: Ext.data.Model, // only here to suppress warning }); var starkModel = Ext.define(Ext.getId(), { extend: 'Ext.data.Model', fields: ['id', 'first_name', 'la ...
  • 由于您使用的是PostgreSQL,因此您可以使用hstore在数据库列中存储任意哈希: 此模块实现hstore数据类型,用于在单个PostgreSQL值中存储键/值对的集合。 这在各种场景中都很有用,例如具有许多很少检查的属性的行或半结构化数据。 键和值只是文本字符串。 甚至还有一个用于向ActiveRecord添加hstore支持的gem: https://github.com/softa/activerecord-postgres-hstore 然后你可以创建一个名为client_specific的h ...
  • 如前所述,postgreql内置了JSON,但如果您使用的是其他任何数据库,则可以使用序列化设置模型,如此 class Event < ApplicationRecord serialize :property, JSON end 然后为您自动完成解析/保存,不需要让您的代码自己混乱 Event.create(name: 'Famous Person Concert Thing', property: { artist: 'Someone famous, most likely', kids_allo ...
  • 这应该在这种情况下工作: legacy_users = Legacy::User.all legacy_users.each do |legacy_user| u = User.new u.attributes.each do |k, v| old_val = legacy_user.send(k) # Get the attr from old user u.send("#{k}=", old_val) # Set it to the new user end end 你不 ...
  • 我最近有这样的问题。 我最终使用基本类型的form_for设置我的表单,在你的情况下是Person。 在表单中,我为基本字段创建了一个字段集,并使用单独的fieldset标记来表示每个子类中可用的额外字段,如下所示: <%= form_for @person do |f| %>
    <%= f.label :name %> <%= f.check_box :name %>
    在这里找到了这个帖子的解决方案。 我需要重新启动服务器,以便在创建Place对象后加载新创建的路由。 不知道我是否做得正确,但它的工作原理.. #config/route.rb Rails.application.routes.draw do Place.all.each do |pl| get "/#{pl.shortlink}" => redirect("/users/sign_up?q=#{pl.id}&t=#{pl.token}") end end ...
  • 这种关系不存在,因为你没有加载它。 它只知道是外键。 如果它为您抓取所有信息,那将是非常低效的,因为它并不总是需要所有这些。 想想单个模型可能有很多关系的实例,这将是无缘无故的许多数据库调用。 如果需要关系,可以使用$post->category 。 由于关系尚未加载,因此当您执行此操作时,它将为您提供。 或者您可以通过使用以下$post->load('category')来急切加载它,尽管这并不会让您受益,因为此时您正在使用单个Post 。 如果您有Post对象的集合,那么您将开始看到使用$posts-> ...
  • 如何创建一个接受Java,C ++或任何OOL中的动态属性的类? 这实际上取决于你想如何使用它。 在许多情况下,您可以重新编写类以包含某种类型的动态增长集合,例如C ++中的std::map或Java中的Map (或Dictionary )。 这允许您使用在运行时选择的键为每个实例创建和添加任意数据。 How to create a Class that accepts dynamic attributes in Java, C++ or in any OOL? It really depends on h ...
  • 您可能需要检查属性以查看它是否具有特定的属性集: public class CapitalizeFirstLetterModelBinder : DefaultModelBinder { protected override void SetProperty( ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel. ...
  • 那么,这取决于你的要求。 如果一个用户只能有一个profile并且你只是想添加一些与user相关的属性,那么没有理由为它创建一个新的类。 但是,即使用户只能拥有一个配置文件,也可能需要执行一些常规操作 。 如果是这样,您可以从users表中删除一些属性以创建表格profiles ,并且您将具有一对一的关系。 class User < ActiveRecord::Base has_one :profile end class Profile < ActiveRecord::Base belongs ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)