I'm having trouble with joining a pivot table with data from two other tables. The pivot table works fine but when I join it it says 'Syntax error in FROM clause'
SELECT r.resourceName AS 'Employee Name',
p.projectNumber AS 'Project Number',
p.projectSystem AS 'Project System',
p.projectManager AS 'Project Manager',
a.projectName AS 'Project Name'
FROM Projects p
LEFT JOIN
(TRANSFORM SUM(a.AllocationValue)
SELECT r.ResourceName, a.ProjectName
FROM Resources r
GROUP BY a.ResourceName, a.ProjectName
PIVOT a.AllocationMonth IN ('June, 2014', 'July, 2014', 'August, 2014') q
ON p.resourceName = q.resourceName
WHERE p.projectName = a.projectName
ORDER BY r.resourceName, a.projectName
Any help would be appreciated, thanks!
I don't think you can have a combination of implicit and explicit joins, and you are also aliasing both the table Projects as p, and the subquery as p.
Instead of FROM Projects p, Resources r, which is an implicit join, you should make that an explicit join FROM Projects p JOIN Resources r ON p.whatever = r.whatever, and then change the alias of your subquery so it isn't also named p.
You also have Resources aliased as r in both the outer query and the subquery, and that's a conflict.
Those are the issues I see right off the bat.
It appears that you are joining an aliased subquery "p" to another aliased as "q". Therefore, your join "ON p.resourceName = r.resourceName" should be "ON p.resourceName = q.resourceName".
Both the WHERE and ORDER BY also appear incorrect.
Have you tried constructing individual queries in the query editor, saving them, then constructing the final query? Examining the resultant sql (query > sql view) should help you.
Related
I have three tables in MS Access database
users,
rooms,
room_history
room_history contains both users,rooms id. when I try to join users with room_history data successfully retrieve same goes with rooms and room_history.
Here is my query for joining all three tables.
"SELECT users.*,rooms.room_id,rooms.room_name,rooms.type,room_history.start_date,room_history.end_date FROM users " +
"LEFT JOIN room_history ON room_history.user_id= users.user_id " +
"LEFT JOIN rooms ON room_history.room_id = rooms.room_id";
Using above query following error occurs.
syntax error (missing operator) in query expression 'room_history.user_id = users.user_id LEFT JOIN rooms on room_history.room_id = rooms.room_i'
I check/change my database tables name, compact the database.
Is it possible to get to fundamentals here - and design this query using the Access query design view tool? ... rather than attempting to resolve this using a text based sql statement...
Once you make the query in Access using the query design feature, and verify that it runs correctly in returning the correct record set - - then use the option to put it into SQL View (rather than Design View) in order to see the text based sql statement.
Try with table alias's instead - the problem appears to be Type is a reserved SQL keyword:
SELECT U.*,R.room_id,R.room_name,R.[type],RH.start_date,RH.end_date FROM users U
LEFT JOIN room_history RH ON RH.user_id= U.user_id
LEFT JOIN room R ON RH.room_id = R.room_id
Your problem is your naming convention.
You used
rooms.type
where type is a reserved word of your database.
the error you receive
syntax error (missing operator) in query expression 'room_history.user_id = users.user_id LEFT JOIN rooms on room_history.room_id = rooms.room_i'
pertains to naming conventions which below link discusses.
https://support.microsoft.com/en-ph/help/932994/you-receive-an-error-message-when-you-run-a-query-in-microsoft-access
If you would insist to use your query "as is" you may simply put brackets ([]) on all of your table names that are reserve words, (e.g [type], [start_date],[user_id])
I'm kind of new to SQL queries so this is probably something easily fixed, but I can't seem to find any answers.
I've got a bunch of related tables. E.g.
SPClients contains iClientID and sName, SPDocTypes contains iDocTypeID and sName, SPCreditor contains iCreditorID and sName
and then
SPDocIndex contains iIndexID, and then foreign keys to iClientID, iDocTypeID and iCreditorID listed above.
If I do a simple SELECT * FROM SPDocIndex I get all results, with just the IDs being displayed, which isn't much use when bound to a datagrid. So I use an inner join so the actual names appear rather than just their IDs, like so:
SELECT * FROM SPDocIndex
INNER JOIN SPClients ON SPDocIndex.iClientID=SPClients.iClientID
INNER JOIN SPDocType ON SPDocIndex.iDocTypeID=SPDocType.iDocTypeID
INNER JOIN SPCreditor ON SPDocIndex.iCreditorID=SPCreditor.iCreditorID
And the query "works" but it only returns rows that have data in all three columns. If the iCreditorID column of SPDocIndex is null, then that row is NOT returned...but I would like all rows to be returned irrespective of whether the columns are null or not.
Benny - some of the others responded in the comments, that you'll need to adjust your join to a left join instead of an inner join; indeed, that is the case here. Please check out this link for a quick tutorial on the differences between SQL joins.
https://www.w3schools.com/sql/sql_join.asp
Inner Join will always return the return the matching records within two table. In order to get the all the records from first table and matching record of second table you must use left join as shown below. Please try below and let me know if you have any further issues.
SELECT * FROM SPDocIndex
LEFT JOIN SPClients ON SPDocIndex.iClientID=SPClients.iClientID
LEFT JOIN SPDocType ON SPDocIndex.iDocTypeID=SPDocType.iDocTypeID
LEFT JOIN SPCreditor ON SPDocIndex.iCreditorID=SPCreditor.iCreditorID
I have two tables, NAttrValues and NAttrTitles. The NAttrTitles have an ID, which is referenced in the NAttrValues table by the column 'TitleID'. I'm trying to return the elements in NAttrTitles that do not have any associated NAttrValues, using LINQ in EntityFramework.
This SQL query returns exactly what I want
SELECT * FROM NAttrValues nav RIGHT JOIN NAttrTitles nat ON nav.TitleID = nat.ID WHERE nav.TitleID IS NULL
How do I write this in EntityFramework? I've tried various different uses of .DefaultIfEmpty() but they all end up returning either the wrong thing, or nothing at all.
Right Outer Join is the same as Left Outer Join with left and right sides swapped. So the LINQ To Entities equivalent of your SQL query is:
var query =
from nat in db.NAttrTitles
join nav in db.NAttrValues on nat.ID equals nav.TitleID into nat_nav
from nav in nat_nav.DefaultIfEmpty()
where nav == null
select nat;
I have a set of (not very well normalised or relational) tables named
PLAN,
GROUP,
PRODUCT
CLIENT
Most have linkage i.e.
PLAN -> CLIENT on clno
GROUP to PRODUCT on PRODCD
However, the linkage between PLAN and GROUP is tricky. A plan has 2 field of interest GRPNO and PRODCD.
What I want to do is if GRPNO != 0 then join GROUP on GRPNO. However if GRPNO = 0 then I want to join GROUP on PRODCD.
The frustrating thing is that the fileds I want to return in my queries are the same across the board I just need to be able to vary the join, or join the same table twice.
The best I can come up with is 2 queries and merge them using datasets, or possibly using a union.
Is there a nifty way to do this in one select?
I should point out I am access Foxpro over ODBC to do this.
Thank you!
You can do:
JOIN GROUP AS G ON
(PL.GRPNO = 0 AND G.PRODCD = PL.PRODCD) OR
(PL.GRPNO !=0 AND G.GRPNO = PL.GRPNO)
However it would surprise me if this is faster than using UNION ALL.
I created an Entity Framework model that contains two tables from the Northwind database to test some of its functionality: Products and CAtegories.
It automatically created an association between Category and Product which is 0..1 to *.
I wrote this simple query:
var beverages = from p in db.Products.Include("Category")
where p.Category.CategoryName == "Beverages"
select p;
var beverageList = beverages.ToList();
I ran SQL Profiler and ran the code so i could see the SQL that it generates and this is what it generated:
SELECT
[Extent1].[ProductID] AS [ProductID],
[Extent1].[ProductName] AS [ProductName],
[Extent1].[SupplierID] AS [SupplierID],
[Extent1].[QuantityPerUnit] AS [QuantityPerUnit],
[Extent1].[UnitPrice] AS [UnitPrice],
[Extent1].[UnitsInStock] AS [UnitsInStock],
[Extent1].[UnitsOnOrder] AS [UnitsOnOrder],
[Extent1].[ReorderLevel] AS [ReorderLevel],
[Extent1].[Discontinued] AS [Discontinued],
[Extent3].[CategoryID] AS [CategoryID],
[Extent3].[CategoryName] AS [CategoryName],
[Extent3].[Description] AS [Description],
[Extent3].[Picture] AS [Picture]
FROM [dbo].[Products] AS [Extent1]
INNER JOIN [dbo].[Categories] AS [Extent2]
ON [Extent1].[CategoryID] = [Extent2].CategoryID]
LEFT OUTER JOIN [dbo].[Categories] AS [Extent3]
ON [Extent1].[CategoryID] = [Extent3].[CategoryID]
WHERE N'Beverages' = [Extent2].[CategoryName]
I am curious why the query inner joins to Categories and then left joins to it. The select statement is using the fields from the left joined table. Can someone help me understand the reason for this? If I remove the left join and change the select list to pull from Extent2 I get the same results for this query. In what situation would this not be true?
[Extent3] is a realization of Include(Category) and Include should not impact on result of selection from "main" table Product, so LEFT JOIN (all records from Product and some records from the right table Category).
[Extent2] is really to filter all records by related table Category with name "Beverages", so in this case it is the strong restriction (INNER JOIN)
Why two? :) Because of parsing expression-by-expression and auto generation for every statement (Include, Where)
You'll notice that the query is pulling all columns in the SELECT list from the copy of the Categories table aliased Extent3, but it's checking the CategoryName against the copy aliased Extent2.
In other words, in this scenario EF's query generation is not realizing that you're Include()ing and restricting the query via the same table, so it's blindly using two copies.
Unfortunately, beyond explaining what's going on, my experience with EF is not advanced enough to suggest a solution...
djacobson and igor explain pretty well why this happens. The way I personally use the Entity Framework, I avoid using Include altogether. Depending on what you're planning to do with the data, you could do something like this:
var beverages = from p in db.Products
select new {p, p.Category} into pc
where pc.Category.CategoryName == "Beverages"
select pc;
return beverages.ToList().Select(pc => pc.p);
... which, at least in EF 4.0, will produce just a single inner join. Entity Framework is smart enough to make it so that the product's Category property is populated with the category that came back from the database with it.
Of course, it's very likely that SQL Server optimizes things away so this won't actually gain you anything.
(Not directly an answer to your question if the queries are the same, but the comment field is too restricting for this)
If you leave out the .Include(), doesn't it load it anyway (because of the where)? Generally it makes more sense to me to use projections instead of Include():
var beverages = from p in db.Products.Include("Category")
where p.Category.CategoryName == "Beverages"
select new { Product = p, Category = p.Category };
var beverageList = beverages.ToList();