SQL Inner Join to compare the result - sql

I am new to SQL and want to compare two tables using inner join and then use the result to compare it with the third table and so on. Also the third table does not share the same column name as compared in the first two.
Below is the SQL statement to compare two tables.
SELECT WORKLOAD_ITEM_ID wid
FROM OUTPUT o
INNER JOIN workload_item wi ON O.WORKLOAD_ITEM_ID = wi.WORKLOAD_ITEM_ID
Now whatever is returned I want to use that and do an inner join with a third table which does not have a column WORKLOAD_ITEM_ID. So the question is how to save and compare the result?
Please Help!

You can follow this to hold records in temp table
INSERT INTO #WORKLOAD_ITEM_TEMP
SELECT WORKLOAD_ITEM_ID wid
FROM OUTPUT o
INNER JOIN workload_item wi ON O.WORKLOAD_ITEM_ID = wi.WORKLOAD_ITEM_ID
Then with your third table made a join with this #WORKLOAD_ITEM_TEMP table.
Now you have to decide on which column need to put inner join. Either add a common column between output result set and third table or use just cross join.
I think you need to reevaluate your Database structure and make efficient tables relationship.

Related

I have a stored procedure that is not working for all parameters #AnID

The parameter is an ID that is the primary key for the table1. The other 3 tables contain details for reference against table1. Obviously if I cut out all the joins, I get only data from Table1, with any ID. I would really like to understand what is going on. Thanks.
SELECT
Detail14, Detail15, Detail16, //etc, etc
FROM
Table1
INNER JOIN
dbo.Table2 ON dbo.Table1.FK_AnID = dbo.Table2.PK_AnotherID
INNER JOIN
dbo.tblTable3 ON dbo.Table2.FK_Group = dbo.tblTable3.PK_RowID
LEFT JOIN
dbo.tblTable4 ON dbo.tblTable4.Name = dbo.Table1.Named
WHERE
PK_RefNumber = #AnID
Break this down by doing in steps. Use select * until you have the joins right, then go to the specific fields you want.
So get records for the first table with the where clause (if it is from the first table). Add the second table. If you get records,then that is not the source of the problem and add the third, etc.
Likely you are either joining on the wrong fields or you have an inner join where you need a left join.

SQL Server View tables not joining

I am incredibly new to SQL and am trying to create a view for a pizza store database. The sides ordered table and the sides names table have to be separate but need a view that combines them.
This is the code I have entered,
CREATE VIEW ordered_sides_view
AS
SELECT
ordered_side_id, side.side_id, side_name, number_ordered,
SUM(number_ordered * price) AS 'total_cost'
FROM
ordered_side
FULL JOIN
side ON ordered_side.side_id = side.side_id
GROUP BY
ordered_side_id, side.side_id, side_name, number_ordered;
The problem is that this is the resulting table.
Screenshot of view table:
How do I get the names to match the ordered sides?
You fail to understand what a FULL JOIN and an INNER JOIN operation does.
FULL JOIN returns at least every row from each table (plus any extra values from the ON clause).
INNER JOIN returns only matching row sets based on the ON clause.
OUTER JOIN returns every matching row set PLUS the side of the join that the OUTER JOIN is on (LEFT OUTER JOIN vs RIGHT OUTER JOIN).
In your picture, you can clearly see that there are no rows that match from the tables ordered_side and side...
That is why switching to an INNER JOIN returns zero rows...there are no matches on the COLUMNS YOU CHOSE TO USE.
Why in your SELECT operator do you have this:
SELECT ordered_side_id, side.side_id, side_name, number_ordered,
while your ON clause has this:
side ON ordered_side.side_id = side.side_id
ordered_side_id !=ordered_side.side_id
Investigate your columns and fix your JOIN clause to match the correct columns.
P.S. I like how you structure your queries. Very nice and what an
expert does! It makes reading MUCH, MUCH easier. :)
One suggestion I might add is structure your columns in the SELECT statement in its own row:
SELECT ordered_side_id
, side.side_id
, side_name
, number_ordered
, SUM(number_ordered * price) AS Total_Cost --or written [Total_Cost]/'Total_Cost'
FROM ordered_side
FULL JOIN side ON ordered_side.ordered_side_id = side.side_id
GROUP BY ordered_side_id
, side.side_id
, side_name
, number_ordered;

SQL joining two tables via large id number

I have two tables which look like this
Value EntryID
0200 43300008191907010611241000917561326051
Value EntryID
test 43300008191907010611241000917561326051
I want to join them via INNER JOIN over the EntryID number, but even though it is a nvarchar, the join does not work (I get nothing as result, my new table is empty). Why?
SELECT * FROM #T_TableA AS A INNER JOIN #T_TableB AS B ON A.EntryID = B.EntryID
Maybe one field contains extra spaces? Use the TRIM function to remove spaces:
It should be like:
INNER JOIN #T_TableB AS B ON TRIM(A.EntryID) =TRIM(B.EntryID)
Or:
INNER JOIN #T_TableB AS B ON RTRIM(LTRIM(A.EntryID)) =RTRIM(LTRIM(B.EntryID))
Those cannot be combined using an inner join. Try using a FULL JOIN. If you are trying to yield one single line row try a left or right join. If you are trying to just put the rows in a new table as two rows that look the same use a UNION

Why we should specify column name in where clause at inner join when have same column name?

I have the query
select gltree.*,tsacc.confirm,tsacc.acc_no,commacc.* from tsacc
inner join commacc on tsacc.acc_no = commacc.acc_no and tsacc.glcode = commacc.glcode
inner join gltree on tsacc.glcode = gltree.glcode
where gltree.glcode = 12738
in this query two specified tables have 'glcode' column name , so
why I should specify table name in where clause eg. gltree.glcode and can't use only glcode without table name ? As we have just one glcode in executed query ?
You actually have threes columns with that name (tsacc.glcode, commacc.glcode and gltree.glcode), so you need to tell the database which one you mean.
The list of columns in the select list is evaluated as the last step when processing the statement. So when the DB processes the where clause it does not "know" which of them you are actually using (you could use all of them).
Plus: with an inner join it does indeed not matter, but if you were using an outer join it would make a big difference which of those three columns is used in the where clause.

Possible to combine two tables without losing all data by using JOINS

I have a table as below and I would like to know if I can still join them together, without losing existing data from both tables when they are combined by referencing JOIN methods.
Table details - VIEW Table
SELECT
r.domainid,
r.DomainStart,
r.Domain_End,
r.ddid,
r.confid,
r.pdbcode,
r.chainid,
d.pdbcode AS "CATH_PDBCODE",
d.cathbegin AS "CATH_BEGIN",
d.cathend AS "CATH_END"
FROM dyndom_domain_table r
JOIN cath_domains d ON d.pdbcode::character(4) = r.pdbcode
ORDER BY confid ASC;
As you can see, dyndom_domain_table is a VIEW Table that I have created to make it easier for me to use JOIN clauses with the other table that has the same pdbcode.
So far it just returns all of the data that matches with the PDB Code. What I would like to do is return all of the data that both matches and doesn't match each other's PDB Code.
Is there a rule in which I can apply it to? Or is it not possible?
I believe you want a FULL OUTER JOIN rather than just a JOIN (which is, by default, an INNER JOIN). In a FULL OUTER JOIN, every row in each table will correspond to some row in the result table; rows from one table that don't match the other will be extended with NULLs to fill the missing column.
So FULL OUTER JOIN instead of just JOIN, and that should do you.
I think you're asking for a left join, but I'm not sure.
SELECT
r.domainid,
r.DomainStart,
r.Domain_End,
r.ddid,
r.confid,
r.pdbcode,
r.chainid,
d.pdbcode AS "CATH_PDBCODE",
d.cathbegin AS "CATH_BEGIN",
d.cathend AS "CATH_END"
FROM dyndom_domain_table r
LEFT JOIN cath_domains d ON d.pdbcode::character(4) = r.pdbcode
ORDER BY confid ASC;