Questions tagged [join]

An SQL join clause combines records from two or more tables or views.

Filter by
Sorted by
Tagged with
1 vote
1 answer
72 views

How to allow predicate pushing on a view that uses group by

We have a table Ecom.McProductToVendorProductCodeMap that has a multi-field PK as shown: Then a view wraps that table to compute a metric and is grouped by the first two fields of that PK: ALTER view ...
crokusek's user avatar
  • 2,078
0 votes
2 answers
55 views

Duplication records with INNER JOIN

I’m validating records after a partition’s rotation.When I run the SELECT COUNT for each table I get 114,817,959 records. SELECT COUNT(*) FROM SMS.dbo.MessagesPerf WITH (NOLOCK) WHERE DateEntered &...
Carolina's user avatar
-1 votes
1 answer
54 views

Question on joins with multi tables

I have 4 tables ( 1 to many table relation) as shown Main , Worker , Location and MiddleName Table Any suggestions on how to get the result. I tried joins and it never worked.
sunny's user avatar
  • 1
0 votes
1 answer
25 views

SQL Pattern to get "root/ start" of uneven hierarchy dataset

I feel this is a common problem and I've seen it in some SQL challenges even but for the life of me, cannot think clearly about a solution. Say you have an uneven hierarchy. Elements that belong to ...
user45867's user avatar
  • 1,651
0 votes
1 answer
26 views

UPDATE table vs JOIN performance

I currently have a query with roughly 10 JOIN statements that takes about 10 seconds to run. I have this displayed as a report on SSRS. Would it be better to create a table in SQL server and run ...
Bigbob23's user avatar
  • 121
0 votes
0 answers
42 views

Complex day-to-day difference of historical data in SQL server

Setting: I have a table (let's call it "HistoricalData") with 10 attribute columns, a date (in string format) and a value. The table has a unique clustered index on (date, idcol1, idcol2,.....
101001000100001's user avatar
0 votes
2 answers
102 views

How to apply JOIN only if there are rows available

Given the following database: CREATE TABLE "Activity" ( "id" INTEGER PRIMARY KEY NOT NULL GENERATED ALWAYS AS IDENTITY, "designation" TEXT NOT NULL ); CREATE ...
Exprove's user avatar
  • 109
1 vote
1 answer
70 views

Does SQL implementations (or at least SQL Azure) skip obvious duplicate data with joins when returning data?

Say I have: very wide table A where I need all columns that I need to join with a small table B that has a LOT of rows. Of course when I join these two tables I get returned all the content of A for ...
Dirk Boer's user avatar
  • 355
1 vote
1 answer
75 views

Limit results of complex query to most frequent values in given attribute

Say that you have the following tables for a multi store platform CREATE TABLE orders ( id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY, store_id BIGINT NOT NULL, ordered_at TIMESTAMPTZ NOT ...
nbon's user avatar
  • 113
0 votes
1 answer
20 views

I am getting an infinite fetch duration when adding a similar third left join

I am using MySQL on Windows 10. The query below should result in three groups of data sets that are different only in the date range. It works for any two date ranges but when I try to run all three ...
Mathly's user avatar
  • 1
0 votes
0 answers
22 views

SELECT for VIEW with steped date dependencies

I have two tables on a mariadb 13.0 - values: id date value1 value2 1 2023-10-01 10 100 2 2023-10-02 20 200 3 2023-10-03 30 300 4 2023-10-04 40 400 5 2023-10-05 50 500 and rates: date rate1 ...
Wookiee's user avatar
0 votes
0 answers
5 views

Handling clunky join condition (Newbi)

(Newbie question I think) [Not a mission critical issue just for my understanding]. I have a situation where I have two SharePoint tables as follows: Table 1: UserTable: user, Position, Department, ...
Todd Wantz's user avatar
1 vote
0 answers
53 views

Adjust SQL query to select only one record

I have three tables: An adjacency-list table, territories with fields id, alignment_id, three_left, tree_right, depth and deleted_at (omitted from table for brevity, all values null). id alignent_id ...
ioni's user avatar
  • 61
0 votes
2 answers
98 views

Does the JOIN really duplicate data on the level of response from the database?

As an example, let's take PostgreSQL and say we have two tables with one to many relationship. Making the INNER JOIN produces "duplicates" from the first table. Are those duplicated rows ...
crabvk's user avatar
  • 3
1 vote
1 answer
43 views

How can I SELECT columns from one table that corresponds with a column of another table on JOIN without creating a relation between the tables?

I have 3 tables: CREATE TABLE catalog.shoes ( shoe_id SERIAL PRIMARY KEY, brand VARCHAR(255) NOT NULL, model VARCHAR(255) NOT NULL, price INT NOT NULL, CONSTRAINT shoe_name UNIQUE (...
ntruter42's user avatar
  • 121
0 votes
1 answer
29 views

Get Point from Another Table based on Category and Value Range

I am trying to get a column which specify the points by comparing the value from original table to min and max value from another table. I have a main table, which consists of few columns of values ...
John Marston's user avatar
0 votes
1 answer
33 views

Why use $group / group_by instead of multiple requests?

Let's suppose that there are 100 types of dogs scattered across the documents of my collection. If I need to group by types of dogs and then compute some aggregate statistic about each type of dog, ...
Bear Bile Farming is Torture's user avatar
0 votes
1 answer
30 views

Slow join on geometry fields

My situation is somewhat similar to this earlier question: In PostgreSQL I have one table containing point geometries (P), and another containing polygons (G). I would like to join on the geometry ...
Berend's user avatar
  • 121
0 votes
2 answers
114 views

Is it a good idea to join data from two different databases?

I am building an application that requires a lot of tables in one database and while the joining and aggregation of data is really nice and seamless, I am starting to wonder if I am creating too many ...
jaffer_syed's user avatar
0 votes
1 answer
66 views

UPDATE & SELECT & INNER JOIN combined (SQL92) — How?

I have two tables with the following columns: Table1 id Sort dou and Table2 id_Table1 Sort and I need to read all values of Table1.Sort where Table1.dou = 1 and write them into Table2.Sort, into ...
Gary U.U. Unixuser's user avatar
0 votes
0 answers
34 views

Dynamically selecting a table in a query based on result from joined SELECT

I have seen many examples but they don't match with my special case (I know... everyone's case is a special case :D ) I have to match values from different tables, some of which are named after the id ...
jprealini's user avatar
  • 101
2 votes
2 answers
299 views

Ignore duplicate rows in a join - and take only one of them

In MySQL I have tables, simplified as follows: companies (id, name, rank, trade_sector_id) orders (id, company_id, paid, subtotal) trade_sectors (id, label) companies_notes (id, company_id, notes) I ...
Ade's user avatar
  • 123
0 votes
1 answer
45 views

Join 1 row out of multiple 1:1 tables and return as array of json objects

I have a database structure like so (in Postgres): An entity has a list of actions associated with it. Each Action has one associated row out of all action-specific tables. Each of these action types ...
Gmanicus's user avatar
0 votes
1 answer
57 views

Help with SQL Query: Is there a cost-efficient way to join multiple tables to one main table?

I have a main table set up like this: id table_to_join table_item_id 1 products 123 2 products 577 3 products 344 4 products 1230 5 images 14 6 images 42 7 video 555 8 products 400 9 video 9 ...
peppy's user avatar
  • 33
0 votes
2 answers
51 views

Delete all rows if input parameter is 0 otherwise delete only those entries which match the input param

I have a stored proc which is receiving id as input. If the id which is received is 0 then we need to delete all the rows otherwise we need to delete only those rows which matches the input id. I can ...
Himanshuman's user avatar
3 votes
5 answers
1k views

Optimizing SQL Server Query When Joining Tables on DATETIME column

I am working with SQL Server and I have two tables, table1 and table2. Both tables have a DATETIME column denoted as dt. I need to join these tables based not only on certain conditions but also to ...
Fajela Tajkiya's user avatar
2 votes
2 answers
180 views

Column isn't in GROUP BY error on join query

I'm trying to get best selling products. Tables CREATE TABLE `items` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `price` ...
Mohammad A. Apu's user avatar
-3 votes
1 answer
90 views

subquery vs join which is better [closed]

SELECT * FROM table1 WHERE t2id in ( SELECT id FROM table2 WHERE t3id = (SELECT id FROM table3 WHERE id = '1') ); SELECT * ...
guradio's user avatar
  • 97
1 vote
1 answer
66 views

PostgreSQL: Sum filtered items in a one-to-many relation

I have events and sessions associated with them in a one to many relation. I want to make a query to get the amount of past, happening and upcominng events. Past events have no active sessions ...
Tomás Mendes's user avatar
1 vote
2 answers
59 views

How to optimize SELECT COUNT with LEFT JOIN?

I have a MariaDB 10.5.10 database, with a table activity made of 150,000 rows. it holds: an id field (integer, primary key) a tenant. There are about 85,000 rows with tenant = 12 an activity_type (...
bfredo123's user avatar
  • 113
0 votes
1 answer
51 views

How to join matching Date fields as intervals?

Let say I've a table with a list of id/year/month (two years + 24 months for semplicity here): Code Year Month MAT000000001 2022 1 MAT000000001 2022 2 MAT000000001 2022 ...
markzzz's user avatar
  • 243
0 votes
1 answer
29 views

Joining per group in group by

I have a table with some animal sightings data where each sighting entry has a region field. There can be 0 to many entries per region per day. I need to create a query where there is one row per day (...
Christopher Lang's user avatar
0 votes
1 answer
635 views

Left Join with only one of multiple matches in the right table

I have a database, where every entry may have multiple names, therefore I made a name database. While all names will be displayed otherwhere I need a list of all main entries with one representative ...
Benito's user avatar
  • 1
0 votes
1 answer
42 views

Different index selection and join strategy between QA and Prod environments in PostgreSQL

I have encountered a scenario where the same query on a PostgreSQL database is exhibiting different index selection and join strategies between the QA and Prod environments. I'm trying to understand ...
sujeet's user avatar
  • 213
0 votes
1 answer
37 views

Compositing ordered SQL tables without Joins

I'm currently exploring some weird ways of doing things and was wondering if anyone has any experience with the following methodology. Background I have a handful of NF3 tables, and NF1 source data. ...
Vocoder's user avatar
  • 117
0 votes
2 answers
26 views

SQL multilevel join how to add a where condition

I have the following SQL query SELECT r.id as id, reports.Timestamp as reportTimestamp, reports.Latitude as reportLat, reports.Longitude as reportLng, FROM Requests r LEFT ...
Matt Welander's user avatar
0 votes
0 answers
423 views

MySQL indexes on temp tables

I am trying to perform joins between two temporary tables, creating an index on the join key for each temp table. However, when I EXPLAIN the join, the row estimates always seem to come out vastly ...
im123's user avatar
  • 1
0 votes
1 answer
36 views

Join two tables with similar columns

I hva two tables as is: Table univers: client commercial Axel Tony Mike Jhon Chris Jhon Table transactions frclient toclient commission Axel NA 10 Mike NA 8 NA Mike 15 Axel NA 4 NA Chris 5 ...
COPEGUEST's user avatar
0 votes
0 answers
23 views

How to Join two tables with SUM in SQL server [duplicate]

i have 2 tables 1.registros 2.servicios what I wanna have is this: id,nombre,apellido,direccion,telefono servicios_pedidos of table 1 + sum of table 2 servicion.precios with table 1 registros.[...
rodrigo 's user avatar
1 vote
2 answers
75 views

How to leave only the rows with an timestamp difference with an interval greater than a parameter

I have a Postgres table with timestamps as entries. From this table, I would like to calculate a new one where there are no consecutive entries with a timestamp difference shorter than 400 ...
eddie_jung's user avatar
0 votes
1 answer
13 views

Issue in Retrieving data from Table when it is having more than 1 records for 1 ID

I have a table ABC and it has 5 columns and now I need to find such records in the table for same ID value where the value in columns C is 0 in all the cases. Solution please.
Nitin Punj's user avatar
0 votes
4 answers
150 views

LEFT JOIN 3 tables NULL rows not as intended

Scenario - a sailing event with entrant categories. I am a volunteer and I am struggling to get a result table for each entrant listing their selection (or NULL if none chosen) for each category. ...
Chris H's user avatar
0 votes
1 answer
148 views

Joining Columns with different character set

I am joining two tables on varchar columns having CHARACTER SET utf8 COLLATE utf8_bin and one with CHARACTER SET latin1. Indexing is not working when i am joining these two tables on this column, I am ...
Manish Kumar's user avatar
0 votes
1 answer
23 views

update row with age calculation with DOB in related table

SQLite database with a table "events" with a columns events.eventDate, events.ageAtEvent and events.foreignKey_participants, and a table "participants" with columns participants....
Stam Kapetanakis's user avatar
0 votes
1 answer
34 views

Select tables data with sum of other tables

I am very new on MySQL DBA and i searched but i am not clearly understanding inner join with sum. user table: id, username, locationid history table: id, username, locationid, spent Example user ...
Imran Ahmed's user avatar
0 votes
1 answer
79 views

SQL Server 2014 Cardinality Estimator estimate final number of rows after OUTER JOIN is less than number of rows from initial table

I have a [SQL query] for SQL Server 2019. It works fine with option (USE HINT ('FORCE_LEGACY_CARDINALITY_ESTIMATION')) and very bad without this hint. I found out, that SQL Server 2014 Cardinality ...
Андрей Ерёмин's user avatar
2 votes
2 answers
92 views

Join order and nullability

I am asking this question in the context of using a Postgres database, although I would imagine it would apply to most SQL databases. When working with the results of a database query inside a ...
Daniel Rearden's user avatar
0 votes
1 answer
94 views

ORA-12015 materialized view complex query error with simple join two table

I have 2 simple table which has columns like : table1 : "OBJECTID" NUMBER NOT NULL ENABLE "OLCEK_ID" NUMBER(*,0) table2 : "OBJECTID" NUMBER NOT NULL ENABLE i created ...
CartWheel's user avatar
1 vote
1 answer
77 views

Worst performance multiple left join big tables after restore DB

Problem Database: PostgreSQL 13.8 (Debian 13.8-0+deb11u1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit After migrate from DigitalOcean to strong Dedicated Servers ...
M. Garbarczyk's user avatar
1 vote
1 answer
109 views

What's the fastest way to query multiple tables at once where each table represents one year of data?

I'm developing an web app based around a database with multiple decades of data with multiple schema changes over the years. Each year of data is released as a zip file containing CSVs named table1....
Tyler's user avatar
  • 111

1
2 3 4 5
40