SQL Query Schema and Data into One Row from 2 Databases - sql

We have data that was merged by accident in our production site, but we still have the data separated in our test site/database. I'd like to be able to query customer data from both databases to compare based on a customer's uniqueidentifer. What I'd like to see in the query results is the schema tables, columns, and primary uniqueidentifier keys of tables where the table has a matching foreign column containing the customer's key.
For instance, if the customer has an invoice the customer cst_key would be in the inv_cst_key of the ac_invoice. I need the primary key of that table, which would come from the inv_key column of that row. So if the customer had two invoices, two inv_key's would list as separate rows.
I installed ApexSQL Search to search the database uniqueidentifier columns for the customer key and it provided the table and foreign columns where the customer's cst_key existed (e.g.: inv_cst_key), but I still need the primary key (e.g.: inv_key) of that table row which the customer key resides. I tried using those search results to build something with excel (piecing tables, operators, columns, etc.) and copy/paste it to SSMS, but the query pulls millions of results the way it's setup...
DECLARE #cstkey uniqueidentifier
SET #cstkey = 'xxxxxxxxxxxxxxxxxx'
SELECT cst_key,
inv_key,
-- many more columns
FROM co_customer
LEFT JOIN ac_invoice ON cst_key = inv_cst_key
-- more LEFT JOINS
WHERE cst_key = #cstkey
Also, I know how to query data from 2 databases, but I don't know how to query so that I can see table and column names in a row next to the column's data.
DECLARE #cstkey uniqueidentifier
SET #cstkey = 'xxxxxxxxxxxxxxxxxx'
SELECT cst1.cst_key AS cst_key_1, inv1.inv_key AS inv_key_1,
cst2.cst_key AS cst_key_2, inv2.inv_key AS inv_key_2
FROM db1name.dbo.co_customer cst1
LEFT JOIN db1name.dbo.ac_invoice inv1 (NOLOCK) ON inv1.inv_cst_key = cst1.cst_key
INNER JOIN db2name.dbo.co_customer cst2 (NOLOCK) ON cst1.cst_key = cst2.cst_key
INNER JOIN db2name.dbo.ac_invoice inv2 (NOLOCK) ON inv2.inv_cst_key = cst2.cst_key
WHERE cst1.cst_key = #cstkey
AND cst2.cst_key = #cstkey
I'd like the results to look something like this...
DB1 | T1 | PC1 | PC1 Data Key | FC1 | FC1 Data Key || DB2 | T2 | ...
--------------------------------------------------------------------
DB = Database, T= Table, PC = Primary Column, FC = Foreign Column
Btw, the FC1 Data Key would also be the cst_key as mentioned above.
Thanks in advance for any assistance.

Related

populating null rows in table column based on matching IDs via join or otherwise

Just to level set: i'm working within a Vertica database using SQL.
Let's say i have two tables: Table A and Table B. Let's also say that Table A is my final/master table used for data vis within Tableau (or something akin), and that Table B feeds certain columns into Table A based on matches within a tertiary table, Table C (which is not relevant to this conversation).
As is, Table A has columns:
ProgramName [varchar(50)]
CustomerName [varchar(50)]
Total_Cost [numeric(18,4)]
As is, Table B has columns:
CustomerCode [varchar(10)]
Total_Cost [numeric(18,4)]
What I would like to do is update Table A's CustomerName column to equal CustomerCode in Table B where the columns of total_cost_dollars equal each other across tables.
I've run this left join query to ensure that, when I do update Table A's CustomerName to equal CustomerCode, the total cost columns are exact/true matches for my entire data set.
SELECT
A.ProgramName,
A.CustomerName,
A.total_cost_dollars,
B.CustomerCode,
B.total_cost_dollars
FROM
TableA A
LEFT JOIN
TableB B
ON
B.total_cost_dollars = A.total_cost_dollars
WHERE
A.CustomerName IS NULL;
Any idea on how to solve this problem?
Since Vertica supports merge query, you can use merge statement:
merge into TableA A
using TableB B
ON (B.total_cost_dollars = A.total_cost_dollars)
when matched then
update
set
A.CustomerName = B.CustomerCode
where
A.CustomerName IS NULL;

How can I merge 2 access tables, keeping all data from table a and updated data from table b

Table A has a population of names and unique ids. Table B has the same unique ids and names. The majority of the names in table B are null, but some have an updated name. I want to merge the two tables so I get the old names from table A and new names from table B if they exist. Basically layer table B on top of table A to capture changes to the names.
I've done something like this in sas, but am having a problem in Access. merging via sas is no longer an option. can this be done in access?
You can do this in SQL using theIIFandISNULLfunctions to select the name from the correct table (from TableA if TableB is null, otherwise from TableB). If your tables has two fields:(id,the_name)a query could look like this:
SELECT a.id, IIF(ISNULL(b.the_name), a.the_name, b.the_name) AS the_name
INTO TableC
FROM TableA a
INNER JOIN TableB b ON a.id = b.id

Update a single table based on data from multiple tables SQL Server 2005,2008

I need to update table one using data from table two. Table one and two are not related by any common column(s). Table three is related to table two.
Ex : table one(reg_det table)
reg_det_id | reg_id | results
101 | 11 | 344
table two :(temp table)
venue | results
Anheim convention center | 355
Table three (regmaster-tbl)
reg_id| venue
11 | Anaheim convention center
I need to update results column in table one using data from table two. But table one and two are not related. Table two and three and table one and three are related as you can see above. Can anyone please suggest any ideas! I need the results value to be 355 in table one and this data is coming from table 2, but these two are unrelated, and they can be related using table three. Sorry if it is confusing!
Fairly straight forward:
UPDATE T1
SET result = t2.results
FROM [table one] T1
INNER JOIN [table three] t3
on t1.reg_id = t3.reg_id
INNER JOIN [table two] T2
on t2.venue = t3.venue
Almost a question instead of an answer. :)
Couldn't you use an implied inner join?
UPDATE rd
SET rd.results = tt.results
FROM reg_det rd, regmaster rm, temptable tt
WHERE rm.reg_id = rd.reg_id
AND rm.venue = tt.venue;
I find it easier to read, and this syntax works in a SELECT statement, with the same meaning as an explicit inner join.
Try this:
UPDATE rd
SET rd.results = t.results
FROM reg_det rd
JOIN regmaster rm ON rm.reg_id = rd.reg_id
JOIN temptable t ON t.venue = rm.venue
WHERE t.results = 355
I added a WHERE clause because otherwise it will update all reg_det records that have matches in regmaster and temptable.

SQL Server : show foreign key constraints tied to a single record

This probably is a bit complicated but is there anyway or already a script out there that could show you all foreign key constraints tied to a single table row.
What I mean by this is say you have the following DB structure:
TABLE 1
column a
column b
TABLE 2
column c
column d (foreign key constraint to 1.a)
TABLE 3
column e
column f (foreign key constraint to 2.c)
TABLE 4
column g (foreign key constraint to 3.e)
column h
Then, you have 2 rows in Table 1. One of the rows is constrained through table 2, then further to table 3, BUT not further to table 4 (IDs tied throughout tables 1-3).
I would like to simply query one of the rows in Table 1 and have it tell me that for that row there are ties that go to Table 2, and then those rows have ties to Table 3. Using this 'query' on the second row in Table 1 would simply just return nothing as there are no foreign keys that are tying that row down.
Something like this would be immensely useful when it comes to tracking down what tables/rows are currently using a particular starting row.
Thanks!
I think what you're looking for can be accomplished by:
SELECT a, t2=COUNT(d), t3 = COUNT(f), t4 = COUNT(g)
FROM [1] LEFT JOIN [2] ON 1.a=2.d
LEFT JOIN [3] ON 2.c = 3.f
LEFT JOIN [4] ON 4.g = 3.e

Advanced sql query

I have two tables in a sql database, I am trying to update a column from my committed table (committedtbl) with values from my vendor table (vendortbl) based on a common column from both tables.
There is a column with the vendor identification number (vendorno) in both tables, I tried adding the vendor description (vendorname) column from the vendortbl to the committedtbl but there are no values in it.
I need to insert values into the vendorname based on the corresponding numbers from vendorno... How do I accomplish this?
The vendorname column already exists in my committedtbl.
I have tried this, but got an error:
update v_vendorname
set v_vendorname = v_vendorno
from vendortbl vt
where v_vendorno = vt.v_venkey
update committedtbl
set c.vendorname = v.vendorname
from committedtbl c
inner join vendortbl v on v.vendorno = c.vendorno