T-SQL split table and insert Id - sql

I'm currently working on a stored procedure in T-SQL on SQL Server 2012. I need to merge 2 tables without an Id. The Id will be created on the insert into the first table. My problem is kind of tricky, thus maybe you can help me :)
In the stored procedure, the import table looks the following way:
CREATE TYPE [MySchema].[Target] AS TABLE
(
IsPrivate BIT,
IsPublic BIT,
CountryId VARCHAR(100)
);
GO
#TARGETS MySchema.Target READONLY
Some possible values for the import #Targets:
IsPrivate | IsPublic | CountryId |
----------+----------+-------------+
1 | 0 | CA,FR |
0 | 1 | US,GB |
Desired output: these #Targets I need to split up into 2 tables, Target and Country:
Create new entries in Target :
TargetId | IsPrivate | IsPublic |
---------+-----------+----------+
23 | 1 | 1 |
24 | 0 | 0 |
Split up the CountryId into it's own table, Country and add the TargetId:
Id | TargetId | CountryId |
---+----------+-----------+
1 | 23 | CA |
2 | 23 | FR |
3 | 24 | US |
4 | 24 | GB |
My current query looks like this:
CREATE TABLE #tmpTarget (TargetId INT, CountryId VARCHAR(100));
INSERT INTO [MySchema].[Target]([IsPrivate], [IsPublic])
OUTPUT inserted.TargetId, CountryId INTO #tmpTarget
SELECT IsPrivate, IsPublic
FROM #TARGETS
Of course this query doesn't work. I'm currently thinking of how to solve this issue. Do you have some ideas or useful tips for me on how to solve this problem?
Thanks a lot! :)

Related

Filling information from same and other tables in SQL

For my further work I need to create a lookup table where all the different IDs my data has (because of different sources) are noted.
It has to look like this:
Lookup_Table:
| Name | ID_source1 | ID_source2 | ID_source3 |
-----------------------------------------------
| John | EMP_992 | AKK81239K | inv1000003 |
Note, that Name and ID_Source1 are coming from the same table. The other IDs are coming from different tables. They share the same name value, so e.g. source 2 looks like this:
Source2 Table:
| Name | ID |
--------------------
| John | AKK81239K |
What is the SQL code to accomplish this? Im using Access and it doesnt seem to work with this code for source 2:
INSERT INTO Lookup_Table ([ID_Source2])
SELECT [Source2].[ID]
FROM Lookup_Table LEFT JOIN [Source2]
ON [Lookup_Table].[Name] = [Source2].[Name]
It just adds the ID from Source2 in a new row:
| Name | ID_source1 | ID_source2 | ID_source3 |
-----------------------------------------------
| John | EMP_992 | | |
| | | AKK81239K | |
Hope you guys can help me.
You're looking for an UPDATE query, not an INSERT query.
An UPDATE query updates existing records. An INSERT query inserts new records into a table.
UPDATE Lookup_Table
INNER JOIN [Source2] ON [Lookup_Table].[Name] = [Source2].[Name]
SET [ID_Source2] = [Source2].[ID]

How to create a table from different query results SQL

I want to create a new table using the results from some queries. I might be looking at this the wrong way so please feel free to let me know. Because of this I will try to make this question simple without putting my code to match each employee number with each manager level column from table2
I have two tables, one has employee names and employee numbers example
table 1
+-------------+-----------+-------------+-------------+
| emplpyeenum | firstname | last name | location |
+-------------+-----------+-------------+-------------+
| 11 | joe | free | JE |
| 22 | jill | yoyo | XX |
| 33 | yoda | null | 9U |
+-------------+-----------+-------------+-------------+
and another table with employee numbers under each manager level so basically a hierarchy example
Table 2
+---------+----------+----------+
| manager | manager2 | manager3 |
+---------+----------+----------+
| 11 | 22 | 33 |
+---------+----------+----------+
I want to make a new table that will have the names besides the numbers, so for example but with employee number beside the names
+---------+--------+----------+
| level 1 | level2 | level3 |
+---------+--------+----------+
| jill | joe | yoda |
+---------+--------+----------+
How can I do this?
edit sorry guys I don't have permission to create a new table or view
Why not change your table2 to this?
+------------+----------+
| EmployeeId | ManagerId|
+------------+----------+
| 11 | NULL |
+------------+----------+
| 22 | 11 |
+------------+----------+
| 33 | 22 |
+------------+----------+
Then you can do what you want with the data. At least your data will be properly normalized. In your table2. What happen if employee 33 hire another employee below him? You will add another column?
Based on your available table, this should give you the result you want.
SELECT m1.firstname, m2.firstname, m3.firstname
FROM table2 t
LEFT JOIN table1 m1 ON m1.employeenum = t.manager
LEFT JOIN table1 m2 ON m2.employeenum = t.manager2
LEFT JOIN table1 m3 ON m3.employeenum = t.manager3
You can just do a basic create table, then do a insert select to that will fill the table the way you need it. All you have to do is replace the select statement that I provided with the one you used to create the levels table output.
create table Levels
(
level1 varchar(25),
level2 varchar(25),
level3 varchar(25)
)
insert into Levels(level1, level2, level3)
select * from tables --here you would put the select statement that you used to create the information. If you dont have this script then let me know

Pivot SSRS Dataset

I have a dataset which looks like so
ID | PName | Node | Val |
1 | Tag | Name | XBA |
2 | Tag | Desc | Dec1 |
3 | Tag | unit | Int |
6 | Tag | tids | 100 |
7 | Tag | post | AAA |
1 | Tag | Name | XBB |
2 | Tag | Desc | Des9 |
3 | Tag | unit | Float |
7 | Tag | post | BBB |
6 | Tag | tids | 150 |
I would like the result in my report to be
Name | Desc | Unit | Tids | Post |
XBA | Dec1 | int | 100 | AAA |
XBB | Des9 | Float | 150 | BBB |
I have tried using a SSRS Matrix with
Row: PName
Data: Node
Value: Val
The results were simply one row with Name and next row with desc and next with unit etc. Its not all in the same rows and also the second row was missing. This is possibly because there is no grouping on the dataset.
What is a good way of achieving the expected results?
I would not recommend this for a production scenario but if you need to knock out a report quickly or something you can try this. I would just not feel comfortable that the order of the records you get will always be what you expect.
You COULD try to insert the results of the SP into a table (regular table, temp table, table variable...doesn't matter really as long as you can get an identity column added). Assuming that the rows always come out in the correct order (which is probably not a valid assumption 100% of the time) then add an identity column on the table to get a unique row number for each row. From there you should be able to write some math logic to "group" your values together and then pivot out what you want.
create table #temp (ID int, PName varchar(100), Node varhar(100), Val varchar(100))
insert #temp exec (your stored proc)
alter table #temp add UniqueID int identity
then use UniqueID (modulo on 5 perhaps?) to group records together and then pivot

How to join table with dynamic identifier in postgres?

I have a table name table containing two columns foreign_table_name, and foreign_key.
Is it possible to write a SELECT query that would JOIN values of this table and the table which name is specified in the column foreign_table_name ?
For instance, if we know that all possible targetted foreign tables have a name field, I would like to know if I could write something that would:
SELECT table.foo, table.bar, foreign_table.name
FROM table
JOIN $foreign_table AS foreign_table
ON (foreign_table.id = table.foreign_key
$foreign_table = table.foreign_table);
Any solution using PlpgSQL is of course accepted.
Here's a simple content:
Table ``table``
------------------------------------------------
| foo | bar | foreign_table_name | foreign_key |
------------------------------------------------
| A | 1 | fruits | 8 |
| B | 2 | vegetable | 5 |
------------------------------------------------
Table ``fruit``
---------------
| id | name |
---------------
| 8 | apple |
---------------
Table ``vegetable``
----------------
| id | name |
----------------
| 5 | carrot |
----------------
The expected result table would be:
----------------------
| foo | bar | name |
----------------------
| A | 1 | apple |
| B | 2 | carrot |
----------------------
EDIT: I added the full table example in an attempt to be clearer.
It's usually way easier to do this sort of thing on the client side, but if you want it's possible with PL/PgSQL, e.g.
CREATE OR REPLACE FUNCTION dynamic_call(tblname text)
RETURNS TABLE (foo int, bar text, fname text)
AS $$
BEGIN
RETURN QUERY EXECUTE format('
SELECT t.foo, table.bar, f."name"
FROM mytable t
JOIN %I AS f ON (f.id = t.foreign_key);', tblname);
END;
$$ LANGUAGE plpgsql;
For more information, see the PL/PgSQL documentation.

Multiple Selection in T-SQL and Transpose one cells to another's rows

I have a project that has dynamic field details for an asp page. When the page is executed, the database is queried for the list of dynamic fields and then all the controls are created at runtime on the asp page. Now all these dynamic fields have corresponding data in another (or say more than one) tables.
The data in the Dynamic Control Detail table is like this:
FieldID | FieldName | MappingTableName | MappingColumnName
-------------------------------------------------------------------------
92 | txtPrsnFirstName | Table1 | FirstName
93 | txtPrsnLastName | Table1 | LastName
94 | ddlPrsnGender | Table2 | Gender
95 | txtCompany | Table2 | Company
96 | txtDesignation | Table1 | Designation
The corresponding datatable's data is as follows:
Table1:
PersonID | FirstName | LastName | Designation
-----------------------------------------
1 | Person1 | SomeName | Manager
2 | Person2 | MoreName | Executive
Table2:
PersonID | Gender | Company
--------------------------------------
1 | Male | ABC Cons
2 | Female | XYZ PVT.LTD
Now, currently what I have done is, created a stored proc, that returns all three tables data at once and I would fetch each one of them in a DataSet then I would iterate through the FieldTable data first and then fetch the corresponding data from Table1 and Table2
I need to write a query that fetches the data in a way that the data from Table1 and Table2 will get transposed in form of multiple rows (Fetches only one record at a time) something like this.
FieldID | FieldName | MappingTableName | MappingColumnName | Data
---------------------------------------------------------------------------------------
92 | txtPrsnFirstName | Table1 | FirstName | Person1
93 | txtPrsnLastName | Table1 | LastName | SomeName
94 | ddlPrsnGender | Table2 | Gender | Male
95 | txtCompany | Table2 | Company | ABC Cons
96 | txtDesignation | Table1 | Designation | Manager
I would add a hidden column (I will call it PersonMap) in the Field-Table which indicates a mapping between FieldID and PersonID (meaning: Field with ID XY shows data from Person Z).
Now create a function like (my TSQL is a little rusty - therefore pseudo-SQL):
create function returnPersonData(in tablename varchar, in columnname varchar, in personID varchar) RETURNS VARCHAR
BEGIN
declare res varchar;
set res = EXEC('SELECT %columnname% FROM %tablename% WHERE PersonID = %personID%');
return res;
END
Now use the created function for your Data_column. The FROM statement is your FieldTable + the PersonMap-column:
SELECT FieldID, FieldName, MappingTableName, MappingColumnName, (returnPersonData(MappingTableName, MappingColumnName, PersonMap) AS Data)
FROM (SELECT FieldID, FieldName, MappingTableName, MappingColumnName, (SELECT PersonID from somewhere) AS PersonMap FROM FieldTable ... )
I hope I covered you question... greetings.