Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I want to left join multiple tables to one table. The tables are themselves results of subqueries.
A classical example that comes to my mind is, I have a bunch of subqueries:
1. Subquery A gives me details of students - say table 1
2. Subquery B gives me student scores in Math - say table 2
3. Subquery C gives me student scores in English - say table 3
The tables contain scores only if the student has taken that test and the student is to be considered failed if he/she has not taken test at all (or has a score < passing score). I have student IDs (unique per person) in each table to join on.
What do I want from these? I am trying to build a dynamic query (where some parts are populated at runtime by an external mechanism) by performing some joins on these tables to give me:
1. Students who passed in both tests and corresponding scores
2. Students passed in either test, but failed (or did not take) the other test and the corresponding scores (NULL if not taken).
3. All students and their corresponding scores.
What I have on mind is left joining each score table to student profile table. How should I go about this?
Before you go ahead and suggest table 1 left join table 2 left join table 3, this structure will cause problems if, say table 2 contains a null record for a particular student (as per my knowledge). And this basically joins table 3 on table 2 and not on table 1, from my understanding, which is what I want.
PS: Feel free to suggest better ways to get what I need, if you know any.
You can create the appropriate relations by writing carefully your query:
select
t1.*, t2.foo, t3.bar
from
table1 as t1
left join table2 as t2 on t1.id = t2.id
left join table3 as t3 on t1.id = t3.id
As you can see, table3 is related to table1 (not to table2), which is what you want.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Sorry, if question unclear, i had misstakes it first tables. I made some updates:
Database: PostgreSQL
I want to group table based on transition (if a=b & b=c then a=c)
Adding a pair (4,c) will merge 2 groups to one "group1".
i assume u want a.b.c to be group1 and the d as group2..
the groupby will work perfectly fine with aliases..
but the number of group op wants is 3 millions groups so a stored proc with a incremental in the end and group by will work fine..
From your comments, it looks like you want to find out transitive relationship.
You can do that with following query. But if the goal here is just to identify the relationship among different groups with their respective id, i guess you can afford to have groups which are not getting incremented with 1.
According to your given example in OP, i think it won't affect you if end result has group1 and group5 instead of group2.
If mention result is fine then you can do that with following updated query. Giving group names in successive manner will impact on query performance which you don't want as you've 3 million of groups.
Please try following query:
select t1.id, concat('group', min(t2.minId)) groups
from t1
join
(select min(id) minId, groups
from t1
group by groups
) t2
on t1.groups = t2.groups
join (select #cnt := 1)y
group by t1.id;
Demo : Click here
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Hello, Does anuone knows how can I remowe the null values corresponding to the description of the same product key? So, I have one product Key and its description in each language.
I'm trying to use the coalesce function, but it doesnt return me anything.
It looks like you want to combine the rows together for each ProductKey.
So instead of 5 lines, each with a single column populated, you want one line, with all 5 columns populated.
Do it like this:
Select Distinct T.ProductKey, C1.Column1, C2.Column2, C3.Column3 from MyTable T
left join MyTable C1 on C1.ProductKey = T.ProductKey and C1.Column1 is not null
left join MyTable C2 on C2.ProductKey = T.ProductKey and C2.Column2 is not null
left join MyTable C3 on C3.ProductKey = T.ProductKey and C3.Column3 is not null
Just replace "MyTable" above with your table name, and "Column1, Column2, Column3" with the names of the columns your data is in.
Pretend that each column is on its own separate table, and you need to use joins to connect all the tables back to your master set of ProductKeys.
Think about it in sets:
Basically you are going to make one master list of the keys that is distinct/unique (step 1), and then do a new left join for each column you want to attach to the master list (step 2), and as part of the joins, tell it to get the non-NULL values and ignore the NULLs.
you can use GROUP BY as below.
SELECT ProductKey,
MAX(C_ar_description) AS C_ar_description,
MAX(C_en_description) AS C_en_description,
MAX(C_fr_description) AS C_fr_description
FROM YourTable
GROUP BY ProductKey
You should use the results to fix the data so you dont need to do this every time.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
So I've run a SELECT which gave me this result so far:
These two columns are from different tables I've joined together. Both are stored as Numbers. In this case "ID" is the main entity and ID_1 describes a state that "ID" can be in. A main entity can have multiple states at once and multiple entities can be in the same state at the same time.
Right now, my select shows ALL entity-state-pairs on my DB. My goal for the final result is to show the rows for any entity that does not have at least one entry with their state set to 1 or 9. In the above example, I would like the final results to show the rows with the following IDs: 62 and 82
Any idea on how to archive this?
Thanks in advance!
You can use an outer join to accomplish this by doing something akin to
SELECT DISTINCT t1.ID
FROM TABLE_1 ti
LEFT OUTER JOIN (SELECT t2.ID
FROM TABLE_2 t2
WHERE t2.ID_1 IN (1, 9))
WHERE t2.ID IS NULL;
This will get the ID's from the second table where ID_1 is 1 or 9:
SELECT ID
FROM second_table
WHERE ID_1 IN (1, 9)
Using that, you can select from the first table where the ID isn't in the result set above:
SELECT DISTINCT ID
FROM first_table
WHERE ID NOT IN (
SELECT ID
FROM second_table
WHERE ID_1 IN (1, 9)
);
If you have a scenario where, for example, ID 63 has three records in the second table with ID_1 values of 3, 4 and 6, the SELECT DISTINCT will make sure ID 63 only shows once in the results instead of three times.
Addendum
Also see the answer from Bob Jarvis. The query is more involved but returns the same results. More importantly, it will run a lot faster for larger datasets.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have the following query to retrieve bunch of data but somehow it creates duplicate rows for certain records.I tried distinct but no use
What i am doing wrong here
SELECT Distinct dbo.tblAssessmentEcosystemCredit.ManagementZoneID, AssessmentEcosystemCreditID,dbo.tblAssessmentEcosystemCredit.AssessmentVersionID ,
(COALESCE(dbo.tblManagementZone.SiteValueCurrentScore,0)
-COALESCE(dbo.tblManagementZone.SiteValueFutureScore,0)) AS LossinSiteValueScore,
5 AS SaveType, dbo.ufn_varbintohexstr(dbo.tblAssessmentEcosystemCredit.RowTimestamp) AS RowTimestamp,
dbo.tblVegetationZone.EECID,
CASE WHEN dbo.tblVegetationZone.EECID > 0 THEN 3.0
ELSE 1.0
END AS EECOffSetMultiplier
FROM dbo.tblAssessmentEcosystemCredit
INNER JOIN dbo.tblVegetationType
ON dbo.tblAssessmentEcosystemCredit.VegTypeID = dbo.tblVegetationType.VegTypeID
INNER JOIN dbo.tblManagementZone
ON dbo.tblAssessmentEcosystemCredit.ManagementZoneID = dbo.tblManagementZone.ManagementZoneID
INNER JOIN dbo.tblVegetationZone
ON dbo.tblVegetationZone.VegetationZoneID = dbo.tblManagementZone.VegetationZoneID
INNER JOIN dbo.tblAssessmentVersion AV ON
AV.AssessmentVersionID = dbo.tblAssessmentEcosystemCredit.AssessmentVersionID
INNER JOIN tblAssessment TBA ON
TBA.AssessmentID = AV.AssessmentID
WHERE dbo.tblAssessmentEcosystemCredit.AssessmentVersionID= #AssessmentVersionID
Possibly you have duplicate rows in your central base table, dbo.tblAssessmentEcosystemCredit. That should be easy to check, as you know which rows to look at.
More likely, you are obtaining multiple result rows corresponding to a few of the dbo.tblAssessmentEcosystemCredit rows because one of the tables you are joining to it has multiple matches for those rows. That is, one of these columns contains at least one duplicated value:
dbo.tblVegetationType.VegTypeID
dbo.tblManagementZone.ManagementZoneID
dbo.tblVegetationZone.VegetationZoneID
dbo.tblAssessmentVersion.AssessmentVersionID
tblAssessment.AssessmentID
The responsible column must not be subject to a single-column UNIQUE constraint, and must not be a single-column primary key for its table, so that may help narrow it down. Note that the whole row does not need to be duplicated, only the ID.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Edit* I want to get the actual info. Right now I would get something like this:
Bob
21
1
I want to replace 21 with California and 1 with United States
If I have a database such as:
Client
Name
StateId
CountryId
Then what is the best way to get that data? Currently I have:
SELECT * FROM Client
SELECT * FROM Client
LEFT JOIN StateId,
ON Client.StateId=State.Id
SELECT * FROM Client
LEFT JOIN CountryId,
ON Client.CountryId=Country.Id
But this gives me 3 tables. Is this how database information should be read or should I get back one table that with the information in it?
You can do multiple joins in the same query. Something like:
SELECT Client.Name, State.Name, Country.Name
FROM Client
LEFT JOIN State ON Client.StateId=State.Id
LEFT JOIN Country ON Client.CountryId=Country.Id
This assume that your states in State table and countries are in Country table.
SELECT * FROM Client
SELECT * FROM Client
LEFT JOIN StateId,
ON Client.StateId=State.Id
SELECT * FROM Client
LEFT JOIN CountryId,
ON Client.CountryId=Country.Id
You can turn this into 1 query with the following
SELECT c.ClientID, c.Name, s.Name, co.Name from Client c
inner join Country co on c.Countryid = co.ID
left join state s on c.StateID = s.ID
I believe you will want an inner join actually, but this is my interpretation. Left joins will return all values in table a regardless of a value being present in table b, so if you want all people regardless of whether they have a country ID then yes a left join, but if you want all people with a country relationship and a state relationship I believe you will initially want an inner join. the second join may be inner as well, depending on if nulls are acceptable in your end table result.
Also, left joins can return some unwanted results, if a person is in multiple countries or has multiple states you could return more rows than necessary. You should establish the business rules of each table before implementing the contents of two left joins into your result set.
This may be for a class, and has no real purpose for business, but it is best not to assume and give you all possible scenarios of your result set.
What I'm doing is returning all the clients with a corresponding countryID, i don't want to return nulls here because in your next join relationship you will also return a null if country is the link between client and state.