简体   繁体   English

如何从另一个表中查找字段名的表中选择一个字段?

[英]How do I select a field from a table where I am looking up the fieldname from another table?

Doing some ETL and I have a wide file with lots of cols, for each different value. 做一些ETL,我有一个包含大量cols的宽文件,每个不同的值。 I am trying to transform this into a vertical text file with one row per value item. 我试图将其转换为垂直文本文件,每个值项有一行。 So my data table looks like; 所以我的数据表看起来像;

Store  Date  Col1 Col2 Col3 ...
Store1 1/2/12 12   23   34  ...
Store2 1/2/12 23   34   35  ...

My Col lookup table looks like; 我的Col查找表看起来像;

GLCode GLDesc TaxRate XLSColName
00.1234 Cash  0.00    Col2 

So in my output file line I need; 所以在我的输出文件行中我需要;
Store1,1/2/34,00.1234,0.00,23
where 23 is the value from the Data table in the Col2 field which was specificed in teh GL Lookup field. 其中23是Col2字段中Data表中的值,该字段在GL Lookup字段中指定。

I have been trying to do this is SQL as part of an SSIS package, as I could not see how to do it with the Lookup task, but cannot seem to do it in SQL either !! 我一直在尝试这样做是SQL作为SSIS包的一部分,因为我无法看到如何使用Lookup任务执行它,但似乎无法在SQL中执行它!

Hope that makes sense and thanks for your help ! 希望有意义并感谢您的帮助! JK JK

From your what I understand of your question, you're looking for the SQL Server UNPIVOT function , which takes columns and pivots them into individual rows/values. 根据我对您的问题的理解,您正在寻找SQL Server UNPIVOT函数 ,该函数接受列并将它们转换为单独的行/值。 Combine that with a join to get your required information from the Lookup table. 将其与联接组合以从查找表中获取所需信息。

To do this in SSIS, you could use the UNPIVOT Transformation 要在SSIS中执行此操作,可以使用UNPIVOT Transformation

You will need to look at pivoting the data table from: 您将需要查看从以下位置转动数据表:

Store  Date  Col1 Col2 Col3 ...
Store1 1/2/12 12   23   34  ...
Store2 1/2/12 23   34   35  ...

Store  Date  Col Value
Store1 1/2/12 Col1 12
Store1 1/2/12 Col2 23
Store1 1/2/12 Col3 34
Store2 1/2/12 Col1 23
Store2 1/2/12 Col2 34
Store2 1/2/12 Col3 35

So you can treat the column name as data to be joined/looked up. 因此,您可以将列名称视为要加入/查找的数据。

Your question is a little confusing. 你的问题有点令人困惑。 I understand what you are trying to do but not 100% sure where the data is stored. 我明白你要做什么,但不能100%确定数据的存储位置。 Is this correct?: You have 2 tables, 1 table has the data you want? 这是正确的吗?:你有2个表,1个表有你想要的数据吗? 1 table has the value to search the first table for? 1表有搜索第一个表的值吗?

If so you want something like: 如果是这样你想要的东西:

SELECT t1.col1, t1.col2, ... 
FROM table_1 t1 
    LEFT JOIN table_2 t2 ON t1.common_field = t2.common_field 
WHERE t2.search_by = 'CRITERIA'

暂无
暂无

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

相关问题 如何从另一个表中不存在值行的表中选择一行? - How do I select a row from one table where the value row does not exist in another table? 如何从另一个表列中选择一个表? - How do I select a table from another table column? 如何从一个表中选择与表中的列值相匹配的数据? - How do I select data from one table where column values from that table match the conatenated column values of another table? 在SQL中,如何从表中选择*,但是当多行具有相同的字段时,只选择另一个字段B为MAX的那些? - In SQL, how do I select * from a table, but when multiple rows have the same field, select only the ones where another field B is MAX? 如何使用SQL在两个表中选择数据,其中每个表中的字段匹配? - How do i select data across two tables using SQL where a field from each table matches? 我如何从另一个表中选择 - How do I do select into from another table 如何从一个表中选择实际为文本的字段中的文本,并将相应的数字插入另一个表中? - How do I select text from one field that is actually text from one table, and insert the corresponding number into another table? 如何从表中选择数据,在该表中我需要返回在一个字段中具有重复值而在另一个字段中具有指定值的行? - How do I select data from a table where I need to return rows which have a repeat value in one field and a specified value in another? 如何制作这个复杂的 select 语句,其中 WHERE 条件查看另一个表并连接第三个表中的字段 - How to make this complex select statement with WHERE condition looking into another table and with joined fields from third table 如何根据另一个选择的结果制作一个SQL表? - How do I make a SQL table from the result of another select?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM