I have 4 tables, project, project_status, invoice and purchase.
I have the following query I have created, jut can't get my head around the type of joins with more than 2 tables. The query I have created seems to only be finding one record, but what I would really like is display all the records from the project table between project_status 1 - 4, regardless whether the project has many related invoice/purchase records. The related invoice/purchase data should be summed
Thanks
SELECT project.projectID, project.project_title,
sum(invoice.invoice_net), sum(purchase.total_order)
FROM project
left JOIN invoice
ON project.projectID=invoice.projectID
left JOIN purchase
ON project.projectID=purchase.projectID
LEFT JOIN project_status
ON project.project_statusID=project_status.project_statusID
WHERE project.project_statusID BETWEEN 1 AND 5
Related
I have encountered with a problem while joining table with a view
select count(*) from table A inner join table B
on A.scen_id=B.scen_id
and A.value=10 --(till here records returned are 0)
inner join View C --this view contains millions of records
on B.scen_id=C.scen_id
Since join of two tables A and B returns 0 records but the whole query is takinh ~ 2 Hrs just because of View and gives me 0 records.
Is there any solution to this problem so i can get 0 records with a good performance?.
Can you try following, send explain and lets know if it is working or not:
select count(*)
from table A
inner join table B
on A.scen_id=B.scen_id
inner join View C
on B.scen_id=C.scen_id
where A.value=10
also make sure stats on A.Value have been refreshed.
I need to build a query that I believe needs different types of joins, but I am not sure how to write it.
I have a customers table and an orders table. I need a query that will return 2 columns displaying the following:
1- list of all customer_id that have never had an order
2- list of all customer_ID that have 1 or more record in the orders table
I have this left join query but i feel like its missing something that indicates the customers column should not have a value on the orders table?
SELECT Customers.userid, Orders.userid
FROM Customers
LEFT JOIN ORDERS
ON Customers.userid=orders.USER_ID
I'm working with SQLite and have 3 tables, project, invoice and order. I have a select statement based on the project table which picks off the fields stored in this table, however I would also like the sum value of invoices for a particular project and the sum value of orders for a particular project. I know how to get all the values individually, however I haven't figured out how to get the Sum value of orders/invoices into the project data row. Is it a nested select I need to search for, or another concept?
Select based on project table
SELECT project.projectID, project.project_title, project.end_date, project.project_manager, project_status.project_status
FROM project
LEFT JOIN project_status
ON project.project_statusID=project_status.project_statusID
WHERE project.project_statusID BETWEEN 1 AND 12
Select based on invoice table where x would be the project id from the first select
SELECT sum(invoice_net)
FROM invoice
WHERE projectID= x
Select based on order table where x would be the project id from the first select
SELECT sum(total_order)
FROM order
WHERE projectID = x
You didn't provide sample data or even enough of your DB schema for an answer to provide the exact SQL you need.
You don't describe the invoice or order tables. I have to assume they contain a ProjectID attribute.
You could use a subselect:
http://www.techrepublic.com/article/use-sql-subselects-to-consolidate-queries/1045787/
https://msdn.microsoft.com/en-us/library/ff487138.aspx
You're select might look something like (untested):
SELECT
project.projectID,
project.project_title,
project.end_date,
project.project_manager,
project_status.project_status,
(SELECT sum(invoice.invoice_net) FROM invoice WHERE invoice.projectID = project.projectID),
(SELECT sum(order.total_order) FROM order WHERE order.projectID = project.projectID)
FROM project
LEFT JOIN project_status
ON project.project_statusID=project_status.project_statusID
WHERE project.project_statusID BETWEEN 1 AND 12
Or a Join with a Group By. See:
SQL JOIN, GROUP BY on three tables to get totals
You'll have to group by all the non-aggregated (in general this will be all the fields you select from your project table), then apply an aggregate function (sum) to your detail attributes (invoice_net and total_order).
Like this (untested):
SELECT
project.projectID,
project.project_title,
project.end_date,
project.project_manager,
project_status.project_status,
sum(invoice.invoice_net),
sum(order.total_order)
FROM project
LEFT JOIN project_status
ON project.project_statusID=project_status.project_statusID
LEFT JOIN invoice
ON project.projectID = invoice.projectID
LEFT JOIN order
ON project.projectID = order.projectID
WHERE project.project_statusID BETWEEN 1 AND 12
GROUP BY project.projectID, project.project_title, project.end_date,
project.project_manager, project_status.project_status
Which to choose? Performance should be comparable, but it's possible a weakness in the query optimizer could lead to one being favored over the other. Test performance if this is important.
For a one-off, I'd probably use the subselect, if a view of the joined tables didn't already exist. But in practice a view does or should exist, in which case using that (or creating the view, if needed), with GROUP BY, is pretty natural.
So the real answer becomes (untested):
CREATE VIEW project_order_invoice AS
SELECT
project.projectID,
project.project_title,
project.end_date,
project.project_manager,
project_status.project_status,
invoice.invoice_net,
order.total_order
FROM project
LEFT JOIN project_status
ON project.project_statusID=project_status.project_statusID
LEFT JOIN invoice
ON project.projectID = invoice.projectID
LEFT JOIN order
ON project.projectID = order.projectID
SELECT
projectID,
project_title,
end_date,
project_manager,
project_status,
sum(invoice_net),
sum(total_order)
FROM project_order_invoice
WHERE project_statusID BETWEEN 1 AND 12
GROUP BY projectID, project_title, end_date,
project_manager, project_status
For the view, you'll probably want to include all the attributes from all the tables, except only 1 copy of the ID attributes.
I am using crystal reports XI. I am working with a SQL database that was created before I got here, and I can't make changes to the tables or link structure. There are 4 tables in the database that I need for this report.
Table 1 - Companies || Fields: CompanyIDPK, CompanyName, YearActiveIDFK
Table 2 - ActiveYears || Fields: YearActiveIDPK, YearNameIDFK
Table 3 - YearNames || Fields: YearNameIDPK, YearName
Table 4 - CompanyOrders || Fields: OrderIDPK, CompanyIDFK, YearNameIDFK, OrderNumber, OrderCost
I want to create a report that is grouped by Year and by Company. I want each company to show the number of orders within each year, including showing 0 if there were no orders that year.
I can get the report to show all the companies that were in a given year, but as soon as I try to start showing a count, it only shows companies that had at least one order.
Thanks for any help!!!
My guess is that this is happening because Crystal only adds tables to your SQL query after you've added them to the designer. This happens even if you have linked your tables in the database expert.
I'm assuming you have the default join type of INNER JOIN. What's probably happening is as soon as you add your Count Summary on one of the fields in CompanyOrders, Crystal is adding it to your SQL Query.
The reason this causes a problem is because an inner join only returns records if the linked fields are in both tables. If companies haven't placed an order in the last year, they won't have any records in the CompanyOrders table. This means your SQL Query won't return any records for those companies, because those companies need to be in both tables for records to be returned.
The solution for this is to change the join type from INNER JOIN to LEFT OUTER JOIN. This can be accomplished by going into the Database Expert (Menu > Database > Database Expert), clicking the Links tab, double clicking the line that goes from your Companies to your CompanyOrders table, and selecting the Left Outer Join Radio button.
Now all of the Companies will show up, but since some don't have records in the CompanyOrders table, the count for the orders will be 0.
Let me know if this was your problem.
ZMcK
You didn't say that you couldn't create database objects, so if it is possible I would create a view or stored procedure in the SQL Server database to return the data you require in the format you want and take Crystal Reports out of the equation in terms of linking tables.
Good noon to every one
my query is that i have one table name Purchase&Sales and two different field
Purchase
Sales
the data which will be in Purchase text box will be fetch from Total purchase table
and the data will be in sales table will be fetch from Total Sales Table
means the both Value will come from different table to one table
So please Give me a syntax or some idea
Hoping for your Great and positive response
select sum(Purchase) Result from PurchaseTable
union all
select sum(Sales) Result from SalesTable
Using JOIN or try with ForeignKey concept if any.
SELECT
S.Total, -- selecting from one table
P.Total -- selecting from another table
FROM
Sales S
INNER JOIN -- inner join if you can or similar
Purchase P
ON
S.PurchaseId = P.ID
see here for more info http://www.techrepublic.com/article/sql-basics-query-multiple-tables/