Getting column names of a query in Teradata - sql

I'm trying to get only columns names of a query result without the need of running all the query.
for example, if table a columns are: id, price, txn_date,city_id and table b columns are: city_id,city_name, country
I want a query that will output only column names of the following query:
Select a.*,b.* from a left join b on a.city_id=b.city_id
without using CPU to compute all the results.
desired output:
id, price, txn_date, city_id, city_name, country

try add condiniion ever false and use an inner join
Select a.*,b.* from a inner join b on a.city_id=b.city_id and 1= 2
this should return a result without rows
or dorectly
Select a.*,b.* from a inner join b on 1= 2

Related

Selecting common items from two tables

Given two Sqlite databases:
db1 and db2 having "functions" table with almost 10k entries
There is a column "fcn_name" that will be conditioned
Both tables have 90% similar entries.
I need to SELECT name and column2 from both tables HAVING same name and expected output is 90% of 10k but I get more than 10k entries as a result. In brief, I need an intersection for name column only but select name and column2 as a result.
I have tried the following sql statement:
select count(*) from main.functions f, other.functions d where f.name=d.name
select distinct f.name from main.functions f, other.functions d where f.name=d.name
Both do not work. Although with "distinct" keyword it returns fewer entries but still more than 10k. Why are there more entries?
Your problem could be caused by duplicates in both tables. Join both tables using DISTINCT names.
For example:
SELECT COUNT(*)
FROM (SELECT DISTINCT name FROM main.functions) f1
INNER JOIN (SELECT DISTINCT name FROM other.functions) f2
ON f1.name = f2.name
Maybe that's the solution.
Referring to your comment:
If your tables have a primary key (e.g. an ID). So the following should deliver the desired result under sqllite:
SELECT f1.name, f1.yourField1, f1.yourField2, f1.yourField3
FROM (SELECT name, yourField1, yourField2, yourField3
FROM main.functions
GROUP BY name
HAVING id = MIN(id)
ORDER BY id) f1
INNER JOIN (SELECT DISTINCT name FROM other.functions) f2
ON f1.name = f2.name
To select rows that exist in both tables based on certain matching criteria, we'd typically use an INNER JOIN, e.g
SELECT COUNT(*)
FROM main.functions f1
INNER JOIN other.functions f1
ON f1.name = f2.name
I'm not familiar with Sqlite, but would often be the approach with most DB's.
I think you want all rows from one table where the name is in the other table. For that, I would suggest:
select f.*
from main.functions f
where exists (select 1
from other.functions o
where o.name = f.name
);
This does not check that the rows are the same, only that the name exists in both tables. It also won't duplicate rows if there are multiple rows with the same name in both tables.

I need help joining these two datasets to produce the results on this exercise

I'm trying to join table 1 and table 2 to create table 3 with using SQL. How would I be able to do this?
you need a join, count and group by
select a.region, b.partnerType, count(*)
from table1 a
inner join table2 b on a.DistrectID = b.DistrectID
group by a.region, b.partnerType

Multiple table joins with aggregate (mssql / sql server)

Very simple question. What I want to do is select all columns from one table and sum of one column (which could have multiple matching rows) from another table.
Example:
table ta (eid, uid, name, year, etc, etc, etc)
table tb (eid, uid, name, year, amount, etc, etc)
eid - will not match between both table
uid, name, year - will match across both tables
So I want to pull out all columns from table ta, simple:
select * from ta where eid='value';
I want to join amount column from table tb to my resultset, simple:
select a.*, b.amount
from ta a
inner join tb b on a.year=b.year
where a.eid='value';
Great, this works fine. But what if I have multiple rows in table tb?
Executing:
select a.*, sum(b.amount)
from ta a inner join tb b on a.uid=b.uid
where a.year='value';
gives me the following error:
Column 'ta.eid' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
So I add:
select a.*, sum(b.amount)
from ta a inner join tb b on a.uid=b.uid
where a.year='value' group by ta.uid;
And I get the same error!
However, if I change my query to:
select a.uid, a.year, sum(b.amount)
from ta a inner join tb b on a.uid=b.uid
where a.year='value'
group by ta.uid, ta.year;
It works, but now I have three columns instead of all columns that I wanted.
So, at this point my question becomes: Is there a better, cleaner way of structuring this query other than me manually typing out all columns I want to pull from two tables with GROUP BY clause?
You can preaggregate in a subquery:
select a.*, b.sumb
from ta a left join
(select b.uid, sum(b.amount) as sumb
from tb b
group by b.uid
) b
on a.uid=b.uid
where a.year = 'value';

Select returns the same row multiple times

I have the following problem:
In DB, I have two tables. The value from one column in the first table can appear in two different columns in the second one.
So, the configuration is as follows:
TABLE_A: Column Print_group
TABLE _B: Columns Print_digital and Print_offset
The value from the different rows and Print_group column of the Table_A can appear in one row of the Table_B but in different column.
I have the following query:
SELECT DISTINCT * FROM Table_A
INNER JOIN B ON (Table_A. Print_digital = Table_B.Print_group OR
Table_A.Print_offset = Table_B.Print_group)
The problem is that this query returns the same row from the Table_A two times.
What I am doing wrong? What is the right query?
Thank you for your help
If I'm understanding your question correctly, you just need to clarify your fields to come from Table_A:
SELECT DISTINCT A.*
FROM Table_A A
INNER JOIN B ON A.Print_digital = B.Print_group
OR A.Print_offset = B.Print_group
EDIT:
Given your comments, looks like you just need SELECT DISTINCT B.*
SELECT DISTINCT B.*
FROM Table_A A
INNER JOIN B ON A.Print_digital = B.Print_group
OR A.Print_offset = B.Print_group
I've still another question... first,to be clear, the right query version is
SELECT DISTINCT A.*
FROM Table_A A
INNER JOIN B ON A.Print_digital = B.Print_group
OR A.Print_offset = B.Print_group.
If I want it returns also one column from the B table it again returns duplicate rows. My query (the bad one) is the following one:
SELECT DISTINCT A.*, B.Id
FROM Table_A A
INNER JOIN B ON A.Print_digital = B.Print_group
OR A.Print_offset = B.Print_group

multi-table query when there is no record in one table

What should I do if I want to:
For now, there are table A and table B,
A:
id, name, address //the id is unique
B
id, contact, email
Since one person may have more than one contact and email, or have no contact and email(which means no record in table B)
Now I want to count how many records for each id, even 0:
And the result will look like:
id, name, contact_email_total_count
How can I do that(for now the only place I can not figure out is how to count 0 record since there is no record in table B)?
For that case you will want to use a LEFT JOIN, then add an aggregate and a GROUP BY:
select a.id,
a.name,
count(b.id) as contact_email_total_count
from tablea a
left join tableb b
on a.id = b.id
group by a.id, a.name
See SQL Fiddle with Demo
If you need help learning join syntax here is a great visual explanation of joins.
Based on your comment the typical order of execution is as follows:
FROM
ON
JOIN
WHERE
GROUP BY
HAVING
SELECT
ORDER BY
Need to do a left join to maintain the records in table A regardless of B:
PostgreSQL: left outer join syntax
Need to aggregate the count of records in B:
PostgreSQL GROUP BY different from MySQL?