简体   繁体   English

SQL,Hive选择具有相同值的列并创建新表

[英]SQL, Hive select columns with same values and create new table

I have one table with two columns - Search & Affiliate but some of the values duplicated between both. 我有一个带有两列的表格-搜索和关联,但是在两者之间重复了一些值。 Ex: Search 123, 345, 567, 768, 008 Affiliate 425, 345, 986, 008 例如:搜寻123、345、567、768、008会员425、345、986、008

I want to take the ones present in both (008, 345) + all the other ones in Affiliate and create a separate table, called unique affiliate. 我想同时接受(008,345)和Affiliate中所有其他成员,然后创建一个单独的表,称为唯一会员。 The remaining ones in Search is also what I want to convert into another separate table called unique search. 搜索中其余的内容也是我想要转换为另一个称为唯一搜索的单独表的内容。

I can create two tables separately, join common values and create a table but how do I include the rest as well with the join? 我可以分别创建两个表,联接公共值并创建一个表,但是如何将其余的联接也包括在内? Or maybe pick common values from both and create a new table but then again, what about the rest of the values in each field? 或者也许从这两者中选择共同的值并创建一个新表,但是再一次,每个字段中其余的值又如何呢?

All the affiliates can be found by a simple select distinct from the Affiliate column. 通过与“会员”列不同的简单选择可以找到所有会员。

The search can be found by selecting all the Search items that are NOT in the list of Search items that occur in the Affiliate column. 通过选择“关联公司”列中出现的“搜索项目”列表中没有的所有“搜索”项目,可以找到该搜索。 To get the Search items that occur in the affiliate column use an inner join sub query to select them. 要获取在“附属”列中出现的“搜索”项目,请使用内部联接子查询将其选中。 Then do a SELECT on the Search column where the items are not in the list generated by the sub query 然后在“搜索”列上执行SELECT,其中项目不在子查询生成的列表中

--INSERT INTO your new affiliates_table
SELECT DISTINCT Affiliate 
FROM tbl_SearchAffiliate
WHERE ISNULL(Affiliate,'') <> ''

--INSERT INTO your new search_table
SELECT DISTINCT Search 
FROM tbl_SearchAffiliate
WHERE Search NOT IN 
    ( --select the search values that occur in the affilliates column
        SELECT x.Search
        FROM tbl_SearchAffiliate x
                INNER JOIN tbl_SearchAffiliate y ON x.Search = y.Affiliate
    )
    AND ISNULL(Search,'') <> ''

/********************
below is the data I assumed from your question
CREATE TABLE [dbo].[tbl_SearchAffiliate](
    [Search] [nvarchar](50) NULL,
    [Affiliate] [nvarchar](50) NULL
) ON [PRIMARY]

GO
INSERT [dbo].[tbl_SearchAffiliate] ([Search], [Affiliate]) VALUES (N'123', N'425')
GO
INSERT [dbo].[tbl_SearchAffiliate] ([Search], [Affiliate]) VALUES (N'345', N'345')
GO
INSERT [dbo].[tbl_SearchAffiliate] ([Search], [Affiliate]) VALUES (N'567', N'986')
GO
INSERT [dbo].[tbl_SearchAffiliate] ([Search], [Affiliate]) VALUES (N'768', N'008')
GO
INSERT [dbo].[tbl_SearchAffiliate] ([Search], [Affiliate]) VALUES (N'008', NULL)
GO
*******************/

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

相关问题 在HIVE中创建与其他表具有相同列的表? - Create table in HIVE with same columns as another table? HIVE:使用原始表中特定列的n值创建一个包含n列的新表 - HIVE: create a new table with n columns using the n values of a specific column from the original table SQL SELECT-返回用其他表中的值填充的新列 - SQL SELECT - Return new columns populated with values from another table SQL-从Select +用户定义的列和值创建表 - SQL - Create table from Select + user defined columns and values SQL - 如何创建一个包含值为列和新格式的表? - SQL - How do I create a table with values as columns and a new format? Hive - 匹配同一表中的 2 列并插入新表 - Hive - Match 2 columns in the Same table and insert to a NEW table Hive 查询:select 一列基于条件另一列值匹配某些特定值,然后将匹配结果创建为新列 - Hive query: select a column based on the condition another columns values match some specific values, then create the match result as a new column 无法使用 spark SQL 创建表:CREATE Hive TABLE (AS SELECT) 需要 Hive 支持; - Cannot Create table with spark SQL : Hive support is required to CREATE Hive TABLE (AS SELECT); SQL从同一表中选择列 - SQL select columns from same table 在带有附加列的配置单元中创建表 - create table in hive with additional columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM