I am using SQL Report Builder 2016.
I have 2 tables, named assets and DepreciationInfo,
following is the structure of these tables.
Table Assets:
ID|Name|Cost|Prior Dep|Prior Dep Period|Use Prior|
values would be like
123|Name|10000|4000|06/03/2014|True|
Table DepreciationInfo:
ID|EndDate|CurrentDepreciation|AccumulatedDepreciation|CarryingValue|Monthly|
values would be like
123|2020-04-30 00:00:00.000|2000|5000|5000|0/1|
I want to achieve following;
I want to select id from table assets, and will show all fields mentioned above from table assets along with fileds from table dep info based on "ID" , Column "ID" is same in both tables.
I am successful in getting all values when Id is common in both table using below mentioned query.
SELECT
Assets.ID
,Assets.Name
,Assets.Cost
,Assets.Prior Dep
,Assets.Prior Dep Period
,Assets.Use Prior
,DepreciationInfo.EndDate
,DepreciationInfo.CurrentDepreciation
,DepreciationInfo.AccumulatedDepreciation
,DepreciationInfo.CarryingValue
,DepreciationInfo.DepID
,DepreciationInfo.Monthly
FROM
Assets
INNER JOIN DepreciationInfo
ON Assets.AssetID = DepreciationInfo.AssetID
where DepreciationInfo.EndDate=#EndDate and DepreciationInfo.Monthly=0
What i want is that i want to show all results from table asset whether or not such id existed in table DepreciationInfo.
I tried all outer joins and result is same, it is showing number of records with Inner and Outer join.
Any help would be appreciated.
Change your inner join to a left join and you will see all results from table asset whether or not a corresponding assetid value exists in table DepreciationInfo. The only issue here is that the query will still be limited by your two filters in the where clause. I suggest you change them to filter off of fields in the asset table if possible:
SELECT
Assets.ID
,Assets.Name
,Assets.Cost
,Assets.Prior Dep
,Assets.Prior Dep Period
,Assets.Use Prior
,DepreciationInfo.EndDate
,DepreciationInfo.CurrentDepreciation
,DepreciationInfo.AccumulatedDepreciation
,DepreciationInfo.CarryingValue
,DepreciationInfo.DepID
,DepreciationInfo.Monthly
FROM
Assets
Left JOIN DepreciationInfo
ON Assets.AssetID = DepreciationInfo.AssetID
where Asset.EndDateorOtherCorrespondingDateValue=#EndDate and Asset.MonthlyorOtherCorrespondingValue=0
Move your where into the join and change to left join
SELECT
Assets.ID
,Assets.Name
,Assets.Cost
,Assets.Prior Dep
,Assets.Prior Dep Period
,Assets.Use Prior
,DepreciationInfo.EndDate
,DepreciationInfo.CurrentDepreciation
,DepreciationInfo.AccumulatedDepreciation
,DepreciationInfo.CarryingValue
,DepreciationInfo.DepID
,DepreciationInfo.Monthly
FROM
Assets
left JOIN DepreciationInfo
ON Assets.AssetID = DepreciationInfo.AssetID
and DepreciationInfo.EndDate=#EndDate
and DepreciationInfo.Monthly=0
Returns all assets and the matches to depreciation based on you ON conditions.
You cannot have left joined objects in your where clause or you convert that left join into an inner join (because the nulls are eliminated).
Related
I have four tables where I am trying to left join the 2nd-4th to the one on the left in this picture. From left to right:
1st table (jobs) is a table of jobs
2nd table (applications_jobs) is a bridge table to link jobs and application IDs
3rd table (applications) is applications
4th table (candidates) is candidates based on those applications
I want to get some columns from 1st table (jobs) and 4th table (candidates). I want to get job name (name) and status (status) columns from jobs table. I want to get first name (first_name) and last name (last_name) from candidates table.
Here's what I've tried:
SELECT
name, status, first_name, last_name
FROM
jobs, candidates
left join
applications_jobs aj on jobs.job_id = id
left join
applications a on aj.job_id = a.id
left join
candidates c on a.candidate_id = c.id
but get a error:
ERROR: invalid reference to FROM-clause entry for table "applications_jobs"
HINT: There is an entry for table "applications_jobs", but it cannot be referenced
from this part of the query.
any ideas?
The items you are selecting should be identified by the table they are coming from when you are performing joins, though it is not always necessary if the tables don't share column names. By writing it out, it would help to prevent the confusion you're having with the FROM clause.
The FROM clause can only be from a single table. In this case, it would be your 'jobs' table. Also, to properly reference your columns in your query, the first join should be application_jobs aj ON aj.job_id = jobs.id, and your second join should be applications a ON aj.application_id = a.id.
SELECT
"jobs".name, "jobs".status, "c".first_name, "c".last_name
FROM
jobs
left join
applications_jobs aj on aj.job_id = jobs.id
left join
applications a on aj.application_id = a.id
left join
candidates c on a.candidate_id = c.id
If you are still getting NULLs for the first and last names, Then you don't have candidates that have applications for that specific job. If you want to omit results that would otherwise be NULL, you can do an INNER JOIN on candidates so that it only returns records that exist on both sides of the equation.
I need to join two dummy tables(one for actual amount and one for budgeted amounts) with Account table which have All account codes and Project Table which have all project Codes,
here my query,
TblActual :=select Actual."AcctCode",Actual."Project",sum(Actual."DocTotal")
from Actual
group by Actual."AcctCode",Actual."Project";
TblBuget := Select Budget."AcctCode",Budget."PrjCode",Sum(Budget."DocTotal")
Form Budget
Group by Budget."AcctCode",Budget."PrjCode";
Select
Project."PrjCode",Account."AcctCode",Account."AcctName",
:TblActual."Amount" as "Actual",:TblBuget."Amount" as "Budget"
From Account
Full outer Join :TblActual on Account."AcctCode" = :TblActual."AcctCode"
Full outer Join :TblBuget on Account."AcctCode" = :TblBuget."AcctCode"
Left Join Project on (:TblActual."Project"= Project."PrjCode" and :TblBuget."PrjCode" = Project."PrjCode" )
where Project."PrjCode"= xxx;
If I need to filter by a project I only received Accounts which both tables have the project and account code. But there are some accounts in budget table with this project code but not in the actual table and
Some accounts are in Actual table with this Project code and not in Budget table.
I need to get all this data without duplicating the account code.How to do that?
Here is a basic illustration of the issue
I have two tables I want to join.
One table contains information about a product sale. (product_sales)
The other contains keys for the names of locations. (states_keys)
Both contain the same column 'state_key'.
The states_keys table has an additional column called 'state_names'.
I would like to join the table so that I can view the accurate state name that coordinates to each product sale.
So, I want to join where each table's state_key are = but display the additional column state_names from table states_keys.
SELECT product_sales.*, state_names
FROM product_sales
INNER JOIN states_keys
ON product_sales.state_key = states_keys.state_key
Try with something like this:
Select prod.*, state.state_names
from product_sales prod
inner join states_keys state on state.state_key = prod.state_key
i have a query which is used to generate reports. There are multiple fields to be displayed. One requirement is such that i need to join one table to different tables with different aliases for data. e.g., table 1 employee id with employee table for knowing the full name. similarly table 2 employee id with employee table for table 2 employee id full name. PFB the query:
select * from office o
left join employee e
on e.id=o.id
left join master m
on m.id=o.id
left join student s1
on e.id=s1.id
left join student s2
on m.id=s2.id
Can we optimize this query to use only one join statement of student table instead of multiple table join statement? I need to reduce the number of tables used in the query since i'm getting the error as too many tables in the query maximum allowed is 50. Please help. Appreciate.
Can we optimize this query to use only one join statement of student
table instead of multiple table join statement?
No, I would not call it query optimization. However, for reporting purposes you reduce the joins by creating views.
http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc00801.1510/html/iqrefso/X315714.htm
I have following query, but it doesn't return any results for where clauses, even when there is row with that kind of name what is queried. If I remove where clause, then all records in Company table which have OfficeLocation table are returned. What is wrong in my query?
SELECT c.*
FROM [MyDb].[dbo].[Company] AS c
INNER JOIN [MyDb].[dbo].[CompanyOfficeLocation] AS col ON c.Id = col.CompanyId
INNER JOIN [MyDb].[dbo].[OfficeLocation] AS ol ON ol.Id = col.OfficeLocationId
WHERE ol.Name like '%Actual Name In This Table%';
Table structure :
Company
Id
etc ...
CompanyOfficeLocation
CompanyId
OfficeLocationId
OfficeLocation
Id
etc ...
Two things for a record to show up given your query:
The OfficeLocation you specified (given the ol.Name value) must have an Id value that is used by a record in the CompanyOfficeLocation table in its OfficeLocationId.
The CompanyOfficeLocation record that you got in #1 must have a CompanyId that exists in the Company table.
If any of those two criteria are not met, then no records will show up in your query result. The INNER JOIN is essentially an 'AND' clause. If a record could not be related to at least one INNER JOINed table, then that record will not show up at all.
If you want a record to show up despite not having any related records in the joined tables, you may want to consider using OUTER JOINs. A RIGHT JOIN in your case to be exact.
I do not find any mistake however I'd suggest you switch the columns after ON when joining to maintain standards.
Instead of - INNER JOIN [MyDb].[dbo].[OfficeLocation] AS ol ON ol.Id = col.OfficeLocationId
Do - INNER JOIN [MyDb].[dbo].[OfficeLocation] AS ol ON col.OfficeLocationId = ol.Id