Creating a result set of data using data from one table joining on another table containing two key columns - sql

I'm trying to create a summarised result set joining two tables.
The first table (main and multi row) contains say the following columns:
trans_id,
trans_type_id
The second table (one row only) contains:
from_trans_type_id,
to_trans_type_id
I'm trying to join the two tables so that from_trans_type_id = trans_type_id and to_trans_type_id = trans_type_id and get the relevant trans_id values
I've tried self joining and derived joins to no effect.
The end result is that I'm looking to get a result set that looks something like this:
trans_id as from_trans_id, from_trans_type_id, trans_id as to_trans_id, to_trans_type_id
data is:

you can use join with multiple instance of firsttable
select b.trans_id as from_trans_id, from_trans_type_id, c.trans_id as to_trans_id, to_trans_type_id
from secondtable a
inner join firsttable b on a.from_trans_type_id=b.trans_type_id
inner join firsttable c on a.to_trans_type_id=c.trans_type_id

Related

Postgres SQL INNER JOIN AND ARRAY_AGG

I have three simple tables:
Category which stores my list of different types of categoriesCategory Table
Items, which stores my items:
JunctionTable table, as a connection between category and items - relation N:M
item_list_category junction table
So my problem is, I would like to create select which will select data from all mentioned tables. First I tried this:
SELECT item_list_id, ARRAY_AGG(category_id) AS array_category
FROM item_list_category
WHERE item_list_id = 1
GROUP BY item_list_id;
With this result:
Attempt 1
Then I wanted to Join tables:
SELECT item_list_id, ARRAY_AGG(category_id) AS array_category
FROM item_list_category
WHERE item_list_id = 1
GROUP BY item_list_id;
This is result, but this is not obviously what I need.Attempt 2
I expected for instance: {"Dětské", "Misteriozní"} etc.

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;

SQL Selecting and Comparing Data from 2 tables

I have 2 tables with 1 column in common. I want to select all data from table1 and want to restrict it with a condition. The thing I could not do is writing the where condition with the non-common column.
Here is the code:
$sql="SELECT kategoriler.adi as katadi, urunler.* FROM `urunler`
LEFT JOIN `kategoriler`
ON urunler.kategori_id=kategoriler.Id
WHERE urunler.kategori_id=$id
OR
kategoriler.ust_id = $id";
ust_id is a parent column of kategori_id and kategoriler.Id. I want to select the child values of it.
(Btw as you can see the common columns are urunler.kategori_id and kategoriler.Id)

Select one column and display values in two columns

I have the following Table Data
I want to split column a into two columns like this:
Result Data
Person 00AB has given Paper1, Paper2, and Paper4.
What query will show in two columns which person has given the paper with 00AB.
You can try the following code:
select tableAB.a, tableP.a
from table as tableAB
inner join table as tableP on tableAB.b = tableP.b
where tableAB.a = '00AB'
and tableP.a != '00AB'

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