Manipulate matrix in SQL Server - sql-server

It might be a stupid question, but I couldn't find anything relevant on the internet regarding the next thing.
I have the following matrix in an Excel file:
ClientCode | ClientDescription | THBCK | THHSM | THOEP
Cl0001 MyClient YES YES NO
*Where THBCK etc represents ItemCode.
The next steps are: I import the file in a temp table using the import wizard and I would like to create other custom tables based on that temp table in order to manipulate the information much better.
So, I create a table in which I store information about client code and description (I can select the columns header) which works fine. But when I am trying to create the second custom table I get stuck... the wizard is treating THCBK (which for me means the ItemCode) as being column header which is right taking into consideration the wizard purpose, but it does not helping me.
Is there any way I should do in order to create the 2nd custom table based on the ItemCode but with a different column header description for each?
Thank you

cross apply helped me in this situation. I have used cross apply and not UNPIVOT because the UNPIVOT does not include NULL values.

Related

Pulling data only when it matches a column in another table

I'm new to the T-SQL language as my office is now using the Microsoft SQL Server Management Studio.
I am trying to pull all of the info from a large table but only when the item number shows in 1 column in another table. I have a small subset of items and I need to pull all the info from another that is more robust.
The small set table is named Itemmaster (IM) and has the following columns:
IM.item number
IM.description
IM.manuf
IM.item_Code
The second table, named Item_Directory (ID), has all of the info about the items including items with the same item codes. I want to pull all of the data from the Item_Directory where:
ID.item_Code = IM.Item_Code
No matter how I "think" it should be written, I seem to be wrong. I know this will probably be a simple formula but I'm still learning T-SQL.
My previous employer used Oracle and that just seemed easier for me to learn. Of course I am completely self taught so forgive me if I don't seem to know some of the basics.
Thanks for the suggestions.
select IM.item number,
IM.description,
IM.manuf,
IM.item_Code,
ID.*
From itemmaster IM
inner join item_details ID
on ID.item_Code = IM.Item_Code

How can I get one of my foreign key outputs to repeat in a merge transformation in SSIS?

I tried asking this question before and it seemed to have gotten swept under the rug.
First thing first, here are these two pictures to show the table structure and the current output I get in SSIS.
Table Diagram
Current Output
So in table three, there is only one entry. This entry (name) applies to the other foreign keys though. What I want the final output to look like is like my current output, but instead of the NULLS, there should just be ones.
I was able to get this far on my own through researching and learning about the merge transformations but I can't seem to find anything on manipulating the data in the way that I want.
I greatly appreciate any tips or advice you can offer.
EDIT: Since the images can't be seen apparently, I will try and describe them.
The table diagram has four tables, the top one in the waterfall has a primary key formed from the three foreign keys for the three different tables.
Trying to accomplish filling out this table in SSIS, my output has each foreign key id from the first two tables, but only one in the third table. The rest from the third foreign key are all NULLS. I believe this is because there is only one entry in that table for now, but this entry applies to all of the foreign key ids and so it should be repeating.
It should look like this:
ID1 ID2 ID3
1 1 1
2 2 1
3 3 1
But instead, I am only getting nulls in the ID3 field after the first record. How do I make the single id repeat in ID3?
EDIT 2: Some additional screenshots of my data flow and merge transformation as requested.
[![SSIS Dataflow][3]][3]
After working on this for a few weeks, and with a tips from a colleague, a solution to this question was found. Surprisingly, it was quite simple and I'm slightly shocked that no one on here could provide the answer.
The solution was simply this; Using a data source, write the following SQL code in the data access mode (SQL Command):
SELECT a.T1ID,
b.T2ID,
c.T3ID
FROM Table1 AS a join
Table2 AS b
On a.T1ID = b.T2ID,
Table3 AS c
ORDER BY a.[T1ID] ASC
If Table3 will always have just a single row, the simplest solution would be to use an Execute SQL task to save the T3id to a variable (Control Flow), then use a Derived Column task (Data Flow) to add the variable as a new column.
If that won't work for you (or your data), you can take a look here to see how to fudge the Merge Join task to do what you want.

SQL Server Select Query

I have to write a query to get the following data as result.
I have four columns in my database. ID is not null, all others can have null values.
EMP_ID EMP_FIRST_NAME EMP_LAST_NAME EMP_PHONE
1 John Williams +123456789
2 Rodney +124568937
3 Jackson +124578963
4 Joyce Nancy
Now I have to write a query which returns the columns which are not null.
I do not want to specify the column name in my query.
I mean, I want to use SELECT * FROM TABLE WHERE - and add the filter, but I do not want to specify the column name after the WHERE clause.
This question may be foolish but correct me wherever necessary. I'm new to SQL and working on a project with c# and sql.
Why I do not want to use the column name because, I have more than 250 columns and 1500 rows. Now if I select any row, at least one column will have null value. I want to select the row, but the column which has null values for that particular row should not appear in the result.
Please advice. Thank you in advance.
Regards,
Vinay S
Every row returned from a SQL query must contain exactly the same columns as the other rows in the set. There is no way to select only those columns which do not return null unless all of the results in the set have the same null columns and you specify that in your select clause (not your where clause).
To Anders Abels's comment on your question, you could avoid a good deal of the query complexity by separating your data into tables which serve common purposes (called normalizing).
For example, you could put names in one table (Employee_ID, First_Name, Last_Name, Middle_Name, Title), places in another (Address_ID, Address_Name, Street, City, State), relationships in another, then tiny 2-4 column tables which link them all together. Structuring your data this way avoids duplication of individual facts, like, "who is John Williams's supervisor and how do I contact that person."
Your question reads:
I want to get all the columns that don't have a null value.
And at the same time:
But I don't want to specify column names in the WHERE clause.
These are conflicting goals. Your only option is to use the sys.tables and sys.columns DMVs to build a series of dynamic SQL statements. In the end, this is going to be more work that just writing one query by hand the first time.
You can do this with a dynamic PIVOT / UNPIVOT approach, assuming your version of SQL Server supports it (you'll need SQL Server 2005 or better), which would be based on the concepts found in these links:
Dynamic Pivot
PIVOT / UNPIVOT
Effectively, you'll select a row, transform your columns into rows in a pivot table, filter out the NULL entries, and then unpivot it back into a single row. It's going to be ugly and complex code, though.

insert values from one table into another table with different design in sql server

I'm back with another (possibly) silly question. sorry.
I have a pretty complicated query which joins 4 tables and computes the sum of a column based on the other two columns in two tables. the result returned is like this:
Image http://eternalvinay.iocleicester.com/blahblah.png
Now, I want the results to be like the right hand side of the image. the number rows per month/year might change though its 4 for now.
I am creating a temporary table as:
Declare #TmpTable (id int identity, AnsSum float, AnsMonth int, AnsYear int)
to store the values from image --> table1. However, I cant figure out how to convert those rows into the format required by table 2.
So, Any hints on this please?
Thanks so much..
ps: I tried to google and related questions here, no luck.
pss: I am not expecting the exact answer too, i am quite interested to learn new things so if you know where i can learn to do this, a push in the right direction, that would be great too!
You could use cross apply to get all the values in a comma delimted format in a single column. instead of "4" different columns. The problem is this "4" cannot be defined everytime. it may increase or decrease and it is not advisable to have this as columns.
SELECT DISTINCT AnaMonth, anayear, [DerivedColumn] FROM #TmpTable A
CROSS APPLY
(
SELECT AnaSum + ',' FROM #TmpTable B
WHERE A.AnaMonth = B.AnaMonth
AND A.AnaYear = B.AnaYear
FOR XML PATH('')
) AS C (DerivedColumn)
You will get [6.0000, 1.000, 8.0000, 5.0000] in one column for month 5 and year 2010 etc ... You could use this as a table to query for any particular month.
Hope this helps
So you have normalized data and you want to pivot the result set to create repeating groups.
You could use PIVOT but you'd need some other attribute in your base table to define the four columns.
I would recommend do not pivot this query in SQL. Just do the query against the database and get four rows per month/year. Then write code in your application to aggregate the results by month/year.

How to create column names/descriptors programmatically

In SQL Server given a Table/View how can you generate a definition of the Table/View in the form:
C1 int,
C2 varchar(20),
C3 double
The information required to do it is contained in the meta-tables of SQL Server but is there a standard script / IDE faciltity to output the data contained there in the form described above ?.
For the curious I want this as I have to maintain a number of SP's which contain Table objects (that is a form of temporary table used by SQL Server). The Table objects need to match the definition of Tables or Views already in the database - it would make life a lot easier if these definitions could be generated automatically.
Here is an example of listing the names and types of columns in a table:
select
COLUMN_NAME,
COLUMN_DEFAULT,
IS_NULLABLE,
DATA_TYPE,
CHARACTER_MAXIMUM_LENGTH,
NUMERIC_PRECISION,
NUMERIC_SCALE
from
INFORMATION_SCHEMA.COLUMNS
where
TABLE_NAME = 'YOUR_TABLE_NAME_HERE'
order by
Ordinal_Position
Generating DDL from that information is more difficult. There seems to be some suggestions at SQLTeam
If you want to duplpicate a table definition you could use:
select top 0
*
into
newtable
from
mytable
Edit: Sorry, just re-read your question, and realised this might not answer it. Could you be clear on what you are after, do you want an exact duplicate of the table definition, or a table that contains information about the tables definition?
Thanks for your replies. Yes I do want an exact duplicate of the DDL but I've realised I misstated exactly what I needed. It's DDL which will create a temporary table which will match the columns of a view.
I realised this in looking at Duckworths suggestion - which is good but unfortunately doesn't cover the case of a view.
SELECT VIEWDEFINITION FROM
INFORMATIONSCHEMA.VIEWS
... will give you a list of columns in a view and (assuming that all columns in the view are derived directly from a table) it should then be possible to use an amended version of Duckworths suggestion to pull together the relevant DLL.
I'm just amazed it's not easier ! I was expecting someone to tell me that there was a well established routine to do this given the TABLE objects need to have all columns full defined (rather than the way Oracle does it which is to say - "give me something which looks like table X".
Anyway thanks again for help and any further suggestions welcomed.
In this posting to another question I've got a DB reverse engineering script that will do tables, views, PK, UK and index definitions and foreign keys. This one is for SQL Server 2005 and is a port of one I originally wrote for SQL Server 2000. If you need a SQL Server 2000 version add a comment to this post and I'll post it up here.

Resources