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.
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 3 months ago.
The community reviewed whether to reopen this question last month and left it closed:
Original close reason(s) were not resolved
Improve this question
Joining 2 tables (orders, addresses)
Orders contains columns delivery_address_id (contains NULL values) and invoice_address_id (does NOT contain NULL values)
Addresses contains id column (does NOT contain NULL values)
Primarily, the LEFT JOIN must be performed on orders.delivery_address_id. However, in the case when its value is NULL in the row, perform LEFT JOIN on orders.invoice_address_id.
How do I deal with this?
I tried the operator OR but the result was not correct. I was also thinking about a CASE WHEN statement. My expectations are to get the LEFT JOIN working.
You can use the operator OR in the ON clause:
SELECT ....
FROM Orders o LEFT JOIN Addresses a
ON a.id = o.delivery_address_id
OR (o.delivery_address_id IS NULL AND a.id = o.invoice_address_id);
Or, use COALESCE():
SELECT ....
FROM Orders o LEFT JOIN Addresses a
ON a.id = COALESCE(o.delivery_address_id, o.invoice_address_id);
So, you want to join on delivery_address_id, but sometimes it's NULL, so you need invoice_address_id to be a fallback.
These situations are where the COALESCE function really shines. COALESCE(delivery_address_id, invoice_address_id) will resolve to the delivery address ID if it isn't NULL, otherwise it will resolve to the invoice address ID instead.
Thus we can achieve the join you want:
SELECT
orders.some_field,
addresses.address
FROM
orders
LEFT JOIN
addresses
ON
COALESCE(orders.delivery_address_id, orders.invoice_address_id) = addresses.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 3 months ago.
Improve this question
I'm trying to query between two tables where I can get all the active employees from one table and get all the employees from the form if they signed it from another table.
Example:
Table 1 Table 2
select * from tblEmpl select * from tblSign
select tblSign.sigdate,tblSign.sigtime,tblSign.sigact,tblSign.esignature,tblEmpl.fname,tblEmpl.lname,tblEmpl.location,tblEmpl.estatus,tblLocs.unit,tblLocs.descript,TblLocs.addr1,tblLocs.city,tblLocs.state,tblLocs.zip from tblSign left join tblEmpl on tblSign.eight_id = tblEmpl.eight_id left join tblLocs on tblEmpl.location=tblLocs.location where tblSign.formid='9648' and tblSign.sigact <> 'O' and tblEmpl.estatus='A' and tblEmpl.location='013' and tblSign.sigdate>='2022-11-01' order by tblSign.sigdate asc;
I used this query but it only get all the active employees that signed the form not all the active employees and extra column if they signed.
sigdate sigtime sigact fname lname
2022-11-01 00:00:00 3:16:35 PM A EMORY CORTEZ
Your current query is left joining from tblSign to tblEmpl - i.e. get all records from tblSign and matching records from tblEmpl, from the sounds of your question you actually want to invert that (i.e. get all tblEmpl records matching a certain criteria and any matching records from tblSign). You also have filters in your WHERE clause filtering the set on specific criteria from tblSign - given that you won't have matching tblSign records for every result from tblEmpl, those filters will essentially trim the final resultset down to only records with those values in tblSign.
To address, invert your join condition and push the tblSign filters up into your join condition, something like this:
SELECT tblSign.sigdate,
tblSign.sigtime,
tblSign.sigact,
tblSign.esignature,
tblEmpl.fname,
tblEmpl.lname,
tblEmpl.location,
tblEmpl.estatus,
tblLocs.unit,
tblLocs.descript,
TblLocs.addr1,
tblLocs.city,
tblLocs.state,
tblLocs.zip
FROM tblEmpl
LEFT JOIN tblSign
ON tblSign.eight_id = tblEmpl.eight_id
AND tblSign.formid = '9648'
AND tblSign.sigact <> 'O'
AND tblSign.sigdate >= '2022-11-01'
LEFT JOIN tblLocs
ON tblEmpl.location = tblLocs.location
WHERE tblEmpl.estatus = 'A'
AND tblEmpl.location = '013'
ORDER BY
tblSign.sigdate ASC;
Some other things to note:
Assuming you have tblEmpl records that don't have matches in tblSign, your order by clause will sort NULLs together. If that's what you want, you're good - if you want to treat them differently, you'll need to adapt your ORDER BY to do so (i.e. perhaps with a coalesce or similar)
If you have multiple records in tblSign for given employees, you'll get multiple records fot the same tblEmpl record in the resultset
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 does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm trying to get an extract of data using two tables in Sql. I have an AddressBook table and a companies table. The AddressBook table has a foreign key called companyid which is the primary key in the companies table. The companies table has a column called accountno. How do I lookup all the addresses on the AddressBook table and find the accountno in the companies table using the companyId?
Please let me know if you need any more info
Use the JOIN, i think you want left join. With left join you fetch the companies even if they dont have an adress, but i see you have an inner join tag so i will include that.
left join:
SELECT * FROM companies LEFT JOIN adressbook ON adressbook.companyid = companies.id
inner join:
SELECT * FROM companies INNER JOIN adressbook ON adressbook.companyid = companies.id
select *
from companies
inner join adressbook on adressbook.companyid = companies.id
if i read it correctly this is what you are looking for
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.