Query Join Condition - sql

I Have two tables named account and assignedgroup. In account table there are two columns named ID and sourceaccount and In assignedgroup I have two columns named ID and assignedgroup.
I want to select ID by joining account and assignedgroup tables. I did something like this:
select a.ID
from account a
left outer join assignedgroup b
on a.sourceaccount = b.assignedgorup
But I get Null as output the reason being that The values in sourceaccount are like this
sourceaccount:
sample
sample
and whereas the values in assignedgroup are like this
assignedgroup:
sample-L1
sample-P-L1
Can anyone help me on how to join these two tables?
I want to get all the ID's whenever there is a sample value in both sourceaccount and assignedgroup.
It is comparing sample with sample-L1 since those are not equal it is returning null but I want the ID even if it has values like that. I mean if somepart of the column values matches then also I want those values to be displayed

SELECT a.id
FROM account a
LEFT JOIN
assignedgroup b
ON b.assignedgorup LIKE a.sourceaccount + '-%'

Clearly your database structure is flawed. If you need the first part of the assigned group to join to the source account, it should be stored that way in a separate column. YOu can do some manipulation to get the right values, but changing the datbase structure is the best fix as any data manipulation you do in a join is very expensive (and likely to introduce bugs).

Since the actual data within those two tables does not actually match, you cannot join those rows in those tables unless you start making some rather broad assumptions about the nature of the data in those tables. For example, this might work:
SELECT a.ID
from Account a
left outer join AssignedGroup b
on a.sourceaccount = left(b.assignedgroup, 6)
or, more generically,
SELECT a.ID
from Account a
left outer join AssignedGroup b
on a.sourceaccount = left(b.assignedgroup, len(a.sourceaccount))
However, this is truly horrible code, logic, and database design, and I would only use this while tyring to troubleshoot and/or fix messed-up data.

Related

Determine datatypes of columns - SQL selection

Is it possible to determine the type of data of each column after a SQL selection, based on received results? I know it is possible though information_schema.columns, but the data I receive comes from multiple tables and is joint together and the data is renamed. Besides that, I'm not able to see or use this query or execute other queries myself.
My job is to store this received data in another table, but without knowing beforehand what I will receive. I'm obviously able to check for example if a certain column contains numbers or text, but not if it is originally stored as a TINYINT(1) or a BIGINT(128). How to approach this? To clarify, it is alright if the data-types of the columns of the source and destination aren't entirely the same, but I don't want to reserve too much space beforehand (or too less for that matter).
As I'm typing, I realize I'm formulation the question wrong. What would be the best approach to handle described situation? I thought about altering tables on the run (e.g. increasing size if needed), but that seems a bit, well, wrong and not the proper way.
Thanks
Can you issue the following query about your new table after you create it?
SELECT *
INTO JoinedQueryResults
FROM TableA AS A
INNER JOIN TableB AS B ON A.ID = B.ID
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'JoinedQueryResults'
Is the query too big to run before knowing how big the results will be? Get a idea of how many rows it may return, but the trick with queries with joins is to group on the columns you are joining on, to help your estimate return more quickly. Here's of an example of just returning a row count from the query above which would have created the JoinedQueryResults table above.
SELECT SUM(A.NumRows * B.NumRows)
FROM (SELECT ID, COUNT(*) AS NumRows
FROM TableA
GROUP BY ID) AS A
INNER JOIN (SELECT ID, COUNT(*) AS NumRows
FROM TableB
GROUP BY ID) AS B ON A.ID = B.ID
The query above will run faster if all you need is a record count to help you estimate a size.
Also try instantiating a table for your results with a query like this.
SELECT TOP 0 *
INTO JoinedQueryResults
FROM TableA AS A
INNER JOIN TableB AS B ON A.ID = B.ID

Newbie to SQL I have run the the inner join query but result comes up with columns only

I have run this query in adventureworks but the result is run successfully but i only get the columns instead of the data with columns how so?
select
a.BusinessEntityID,b.bonus,b.SalesLastYear
from
[Sales].[SalesPersonQuotaHistory] a
inner join
[Sales].[SalesPerson] b
on
a.SalesQuota = b.SalesQuota
My best guess is that instead of joining the tables on SalesQuota, you should be joining them on something else - An ID field, typically.
I don't have Adventureworks here, but judging from the names of the tables and the columns that you've provided, I would assume that there's a SalesPersonID field of some sort that actually connects a Salesperson's quota history to the Salesperson him/herself.
I would expect that you're looking for something closer to this:
SELECT
a.BusinessEntityID
,b.bonus
,b.SalesLastYear
FROM [Sales].[SalesPersonQuotaHistory] a
INNER JOIN [Sales].[SalesPerson] b
ON a.SalesPersonID = b.SalesPersonID
General Knowledge:
INNER JOIN means "Show me only entries (rows) that have a matching value on both sides of the condition." (i.e. The value in Table A matches the value in Table B).
So ON a.SalesQuota = b.SalesQuota means "Only where the value of SalesQuota in Table A matches the value of SalesQuota in Table B."
I'm not sure what the purpose of this query could be, since it is entirely possible that two salespeople have the same values in both tables, and then you would get duplicate rows (because the values of SalesQuota would match in both cases), or that the values wouldn't match at all, and then you wouldn't get any rows - I suspect that is what's happening to you.
Consider the conditions of what you're trying to join. Are you really trying to join quota amounts, or are you trying to retrieve quota information for specific salespeople? The answer should help guide your JOIN conditions.

Get overlapped data from two tables with same structure, giving prefrence to other : Oracle

I am completely lost thinking about how do I solve this challenge of data retrieving.
I have this two tables: MY_DATA and MY_DATA_CHANGE in my Oracle database.
I wanted to select data some thing like this:
SELECT ALL COLUMNS
FROM MY_DATA
WHERE ID IN (1,2,4,5) FROM MY_DATA
BUT IF ANY ID IS PRESENT IN (1,2,4,5) IN MY_DATA_CHANGE
THEN USE ROW FROM MY_DATA_CHANGE
So my overall result must look like:
I can only use SQL not stored procedure, as this query is going to be part of another very big query (legacy code written long back) (will be used in Crystal reports tool to create report).
So guys please help. My column data contains CLOB and the usual UNION logic does not work on them.
How do I do it ?
SELECT
m.Id
,COALESCE(c.CLOB1,m.CLOB1) as CLOB1
,COALESCE(c.CLOB2,m.CLOB2) as CLOB2
FROM
MY_DATA m
LEFT JOIN MY_DATA_CHANGE c
ON m.Id = c.Id
WHERE
m.ID IN (1,2,4,5)
The way I would choose to do that is via a LEFT JOIN between the two tables and then use COALESCE().

insert multiple records into multiple columns of a table from many tables

I want to insert multiple records into multiple columns of a table from many tables. Below is my query, but I just get to insert the records into the first column. The other columns populate with nulls. Can you let me know what am I doing wrong?
INSERT INTO [dbo].[dim_one_staging] ([Parent], [Child], [Child_Alias], [Operator])
SELECT
p.[Parent], c.[Child], a.[Child_Alias], o.[Child_Operator]
FROM
[dbo].[Staging_Parent] AS p
INNER JOIN
[dbo].[Staging_Child] AS c ON p.[id] = c.[id]
INNER JOIN
[dbo].[Staging_Child_Alias] AS a ON c.[id] = a.[id]
INNER JOIN
[dbo].[Staging_Operator] AS o ON a.[id] = o.[id]
Your query is syntactically correct. That doesn't mean it does what you want it to do.
It could be that you have no values in
,c.[Child]
,a.[Child_Alias]
,o.[Child_Operator]
for the records that meet the rest of the query conditions and thus null is the correct value.
It could be that you have no valaues in the join tables for those fields but you should have values, in which case there is a bug in the way the data in being entered into these tables.
Or it could be that you are trying to get values froma table where the value is not required and put them into a table where it is and thus need to use coalesce (or default values) to define what should go in there if the value is null.
Yet another possibility is that there is trigger on the table that is nulling the values out.
Only you can detrmine what the problem is from teh data structure you have and the meaning attached to the data. I don't know how to fix your problem because I don't actually understand your datamodel as far as meaning (as opposed structure.)

How to get names present in both views?

I have a very large view containing 5 million records containing repeated names with each row having unique transaction number. Another view of 9000 records containing unique names is also present. Now I want to retrieve records in first view whose names are present in second view
select * from v1 where name in (select name from v2)
But the query is taking very long to run. Is there any short cut method?
Did you try just using a INNER JOIN. This will return all rows that exist in both tables:
select v1.*
from v1
INNER JOIN v2
on v1.name = v2.name
If you need help learning JOIN syntax, here is a great visual explanation.
You can add the DISTINCT keyword which will remove any duplicate values that the query returns.
use JOIN.
The DISTINCT will allow you to return only unique records from the list since you are joining from the other table and there could be possibilities that a record may have more than one matches on the other table.
SELECT DISTINCT a.*
FROM v1 a
INNER JOIN v2 b
ON a.name = b.name
For faster performance, add an index on column NAME on both tables since you are joining through it.
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins