[英]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.