简体   繁体   English

SQL Server:按特定日期将数据从一个数据库插入到另一个数据库结构

[英]SQL Server: inserting data from one database to another database structure by specific date

I need some help to pull data from one database into another database with a different schema I am writing a PHP script to achieve this. 我需要一些帮助,以将数据从一个数据库拉到具有不同模式的另一个数据库中。我正在编写一个PHP脚本来实现这一点。

My first query is: 我的第一个查询是:

SELECT orderid, customerid, validation_date, test_type, test_result, amount 
WHERE test_type IN ('ph', 'bh')

and I get the result as following 我得到如下结果

1 101001 10-01-2018 bh 5 20.00
2 101001 10-01-2018 ph 3 25.00
3 101002 11-02-2018 bh 4 20.00
4 101002 11-02-2018 ph 2 25.00
5 101003 15-02-2018 bh 3 20.00
6 101003 15-02-2018 ph 4 25.00
7 101001 25-04-2018 bh 4 20.00
8 101001 25-04-2018 ph 2 25.00

I want to insert this data into another SQL Server table of the structure in one line for each specific date. 我想在每个特定日期的一行中将此数据插入到结构的另一个SQL Server表中。

The table schema of the other database is as follows: 另一个数据库的表模式如下:

itemid, customerid, validation_date, ph_value, bh_value 

So I want the result to go into the database as follows: 所以我希望结果进入数据库,如下所示:

1 101001 10-01-2018 3 5
2 101002 11-02-2018 2 4 
3 101003 15-02-2018 2 3 
4 101001 25-04-2018 2 4 

Please can you advice on how I can achieve this using a SQL query? 请问您如何使用SQL查询实现此建议? I did a select, AND NOW want to insert into the database in the above format 我做了一个选择,现在想以上述格式插入数据库

You can upload your data to SQL Server "as is" and then use the SQL engine to massage it as you want. 您可以将数据“按原样”上传到SQL Server,然后使用SQL引擎根据需要对其进行按摩

If we considering the case when both values (ph, bh) may not be present simultaneously, then you would need a FULL OUTER JOIN . 如果我们考虑两个值(ph,bh)可能不同时出现的情况,那么您将需要FULL OUTER JOIN You would need to create a new table and a sequence to produce the inserts, as in: 您将需要创建一个新表和一个序列来产生插入,如:

create table my_table (
  id int, 
  customer_id int, 
  validation_date date, 
  ph_value real, 
  bh_value real
);

create sequence seq1; -- a sequence is needed to generate IDs.

Then the query that could produce your data can look like: 然后,可能产生您的数据的查询如下所示:

insert into my_table (id, customer_id, validation_date, ph_value, bh_value) 
select
  next value for seq1,
  coalesce(ph.customer_id, bh.customer_id),
  coalesce(ph.validation_date, bh.validation_date),
  ph.amount,
  bh.amount
from (select * from t where test_type = 'ph') ph
full outer join (select * from t where test_type = 'bh') bh 
  on ph.customer_id = bh.customer_id 
 and ph.validation_date = bh.validation_date

You can use aggregation to pivot the data: 您可以使用聚合来透视数据:

select customer_id, validation_date,
       max(case when test_type = 'ph' then ph.test_result end) as ph_value,
       max(case when test_type = 'bh' then ph.test_result end) as bh_value
from t
group by customer_id, validation_date;

It is unclear where itemid comes from. 目前尚不清楚itemid来自何处。 If calculated on the fly, I would suggest an identity column in the target table. 如果即时计算,我建议在目标表中使用一个identity列。

Then you can use either SELECT . . . INTO 然后,您可以使用SELECT . . . INTO SELECT . . . INTO SELECT . . . INTO or INSERT to put the data in another table. SELECT . . . INTOINSERT将数据放在另一个表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将特定表数据从一个数据库复制到SQL Server中的另一个数据库 - Copy specific table data from one database to another in SQL Server 将数据从一台服务器上的一个数据库插入另一台服务器上的另一数据库 - inserting data from one database on one server to another database on other server 有条件地将数据从一个数据库插入另一个数据库 - Inserting data from one database to another conditionally 将已解析的数据从一个数据库插入另一个数据库 - Inserting parsed data from one database into another SQL Server将数据从一个数据库表复制到另一数据库表 - Sql Server copy data from one database table to another 将SQL Server数据从一个数据库导出到另一个数据库 - Export SQL Server data from one database to another SQL 服务器作业,用于将数据从数据库复制到另一个数据库 - SQL Server job for copying data from a database to another one 将数据从一个SQL Server数据库迁移到另一个SQL Server数据库 - Migrating Data from one to another sql server database 连续如何将SQL Server 2012中的数据从一个服务器数据库插入另一个服务器数据库 - Continuously How to insert data in SQL Server 2012 from one server database to another server database 在C#中将SQL查询的结果从一个数据库插入另一个数据库 - Inserting result of a SQL query from one database to another in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM