Sql Server View show records with no data in certain tables - sql

I have tables Products, Sales and ProductsInStores. ProductsInStores table keeps track of how many Products there are in each store. And I have a SalesProductsInStoresView with columns from tables Sales and ProductsInStores.
But there are also Products that come from a different chain of stores, so there's no ProductsInStores record for these products. So when the sale is made SalesProductsInStoresView doesn't show Sales if a Product doesn't have a ProductsInStores record.
Can I make my View show Sales with no ProductsInStores record, and just show empty cells on ProductsInStores columns, and if this is not possible what maybe other options do I have, I'm using Asp.Net MVC and Sql Server 2008.

Use Left Outer join
Select a.field1,b.field2
from tablea A
left outer join tableb b on b.fieldc = a.fieldc
this allows data to be returned from the inner table whilst maintaining a join to the outer table even if there is no data present.

Related

Outputting the data from several sql tables without having a common value

I have a select query which combined several tables. PRODUCTION_ORDER_RESULTS, PRODUCTION_ORDERS and SERVICE_GUARANTY_NEW have common value however STOCKS table does not.
SELECT PR_ORDERS.ARRIVED_CITY,
PR_ORDERS.MONTAJ_DATE,
PR_ORDER_RESULT.TRANSFER_DATE,
PR_ORDERS.P_ORDER_ID,
PR_ORDER_RESULT.P_ORDER_ID,
SG.SALE_CONSUMER_ID,
SG.IS_SERI_SONU,
S.BRAND_ID,
S.PROPERTY
FROM workcube_test_1.PRODUCTION_ORDER_RESULTS PR_ORDER_RESULT,
workcube_test_1.PRODUCTION_ORDERS PR_ORDERS,
workcube_test_1.SERVICE_GUARANTY_NEW SG,
workcube_test_1.STOCKS S
WHERE PR_ORDER_RESULT.P_ORDER_ID = PR_ORDERS.P_ORDER_ID
AND PR_ORDER_RESULT.PR_ORDER_ID = SG.PROCESS_ID
when I run the query, it shows the output as below.
The problem here is there are four data rows returned from PRODUCTION_ORDER_RESULTS, PRODUCTION_ORDERS, SERVICE_GUARANTY_NEW and once I have added the STOCKS table, arrived_city, montaj_date, transfer_date columns are side by side with STOCKS table's rows, but the columns value should be null, not filled with data.
The way I tried is UNION of STOCKS table, however unioned table values are ignored, can not use them in html blocks.
there needs to be at least one more join condition among tables where there's for STOCKS table, I think there might exist such a column STOCK_ID within a table such as PRODUCTION_ORDER_RESULTS in order to join with STOCKS table. I think this should be the reason for multiple returning rows. If there's no common column, then the returning data will be produced as many as the number of records within STOCK table due to existing CROSS JOIN logic within the current query. So rearrange your query as
SELECT PR_ORDERS.ARRIVED_CITY,
PR_ORDERS.MONTAJ_DATE,
PR_ORDER_RESULT.TRANSFER_DATE,
PR_ORDERS.P_ORDER_ID,
PR_ORDER_RESULT.P_ORDER_ID,
SG.SALE_CONSUMER_ID,
SG.IS_SERI_SONU,
S.BRAND_ID,
S.PROPERTY
FROM workcube_test_1.PRODUCTION_ORDER_RESULTS PR_ORDER_RESULT
JOIN workcube_test_1.PRODUCTION_ORDERS PR_ORDERS
ON PR_ORDER_RESULT.P_ORDER_ID = PR_ORDERS.P_ORDER_ID
JOIN workcube_test_1.SERVICE_GUARANTY_NEW SG
ON PR_ORDER_RESULT.PR_ORDER_ID = SG.PROCESS_ID
JOIN workcube_test_1.STOCKS S
ON PR_ORDER_RESULT.STOCK_ID = S.ID

Join table query returns multiple results from right table

I'm new to access and Have pulled tog ether a query in the GUI.
I have a Join set up so all customers who have complained are in the left table and all work completed is in the right table.
Both table have the customers reference number so I've joined based on that.
Customers may complain before we've carried out any work so the table is a left Join.
The problem I have is a customer may have had multiple work items, so when I create the join each row in the complaints table is being returned multiple times if they have had more than on work item carried out.
I need to add some logic to the joins so it only brings back a single work item for each complaint. Where multiple work items are present are want the query to return the work item with the date closets to the date of the complaint but never after it.
If the complaint has no work items that occur before it I still want the complain to be shown but with data from the work item.
I only really know how to use the designer GUI but the SQL looks like this at the moment:
SELECT DISTINCT [CEAR326 Master Data fo access1].ID,
ReportingCategory.ReportingCategory,
[CEAR326 Master Data fo access1].Category,
[CEAR326 Master Data fo access1].[Sub Category],
[CEAR326 Master Data fo access1].[Contact Partner],
main_Tbl_ServiceOrder.ServiceProduct, SOType.SOType,
Format([Actual Date From],"mmm-yy") AS [Date],
[CEAR326 Master Data fo access1].[Actual Date From],
main_Tbl_ServiceOrder.BasicStartDate,
[CEAR326 Master Data fo access1].Smart_Flag
FROM (([CEAR326 Master Data fo access1]
LEFT JOIN main_Tbl_ServiceOrder ON [CEAR326 Master Data fo access1].[Contact Partner] = main_Tbl_ServiceOrder.BusinessPartnerNumber)
LEFT JOIN ReportingCategory ON [CEAR326 Master Data fo access1].Category = ReportingCategory.Category) LEFT JOIN SOType ON main_Tbl_ServiceOrder.ServiceProduct = SOType.ServiceOrder
WHERE (((main_Tbl_ServiceOrder.BasicStartDate)<="actual Date from" Or (main_Tbl_ServiceOrder.BasicStartDate)="0" Or (main_Tbl_ServiceOrder.BasicStartDate) Is Null))
ORDER BY [CEAR326 Master Data fo access1].[Contact Partner], main_Tbl_ServiceOrder.BasicStartDate;
I believe the Select distinct isn't working as required due to the line
main_Tbl_ServiceOrder.ServiceProduct, SOType.SOType,
This is where I want my where statement to come in and select the relevant service order by looking at the date of the service order in relation to the date of the CEAR [Actual Date From].
I also think there would be an issue with the current WHERE statement returning multiple Service orders if they occur before the CEAR. I'd only want to return the one closest to the CEAR date.
This uses
Complaints table with ComplaintID as the key and fields for Customer and Complaint_Date
WorkOrders table with WorkOrderID as they key and fields for Customer and WorkOrder_date
I assumed there is only one work order per customer per date. If this isn't true, you may need to use an autonumber field for the WorkOrders instead of the date field in the selection criteria
You should be able to customize this with your table names and field names.
SELECT Complaints.ComplaintID, Complaints.Customer, Complaints.Complaint_date, WorkOrders.WorkOrderID, WorkOrders.WorkOrder_date
FROM Complaints LEFT JOIN WorkOrders ON Complaints.Customer = WorkOrders.Customer
WHERE (((WorkOrders.WorkOrder_date) Is Null Or (WorkOrders.WorkOrder_date)=(select max(WorkOrder_Date) from WorkOrders T where T.customer=Complaints.customer and T.WorkOrder_date<Complaint_date)));

SQL INNER JOIN without linked column

I have an UltraGrid displaying customer information in it. The way the database is set up, there are 2 tables. Customers and Customer_Addresses. I need to be able to display all of the columns from Customers as well as Town and Region from Customer_Addresses, but I'm under the impression that I'd need Town and Region columns in the Customer table to be able to do this? I've never used an INNER JOIN before so I'm not sure if this is true or not, so can anybody give me pointers on how to do this, or if I need the matching columns or not?
Does it even require an INNER JOIN, or is there an alternative way to do this?
Below are the design views of both of the tables - Is it possible to display Add4 and Add5 from Customer_Addresses with all of Customers?
As long as you have another key column you can use to link the tables (ex. ID_Column), it is better that you use LEFT JOIN.
Example:
SELECT c.col1, ... , c.colN, a.town, a.region FROM Customers c
LEFT JOIN Customer_Addresses a ON a.ID_Column = c.ID_Column
In order to clarify how JOIN types work, look at this picture:
In our case, using a LEFT JOIN will take all information from the Customers table, along with any found matching (on ID) information from Customer_Addresses table.
First of all you need some column in common in two tables, all what you have to do is:
CREATE TABLE all_things
AS
SELECT * (or columns that you want to have in the new table)
FROM Costumers AS a1
INNER JOIN Customer_Addresses AS a2 ON a1.column_in_common = a2.column_in_common
The point is what kind of join do you want.
If you can continue the process without having information in table Costumers or in table Customer_Addresses maybe you need OUTER JOIN or other kind of JOIN.

Any way to join tables in Access without reducing entries

I have two tables that I need to combine, our Warehouse table has data for all of our products stored in the warehouse and the other, our Purchase Order Table has data for all of our products that are being purchased from our suppliers. We have over 3,000 products in the warehouse but once I join the two tables together it only shows the 300 or so products that are being purchased now. So it excludes all the products that are not in the Purchase Order table.
Is there a way to combine the two tables without limiting the entries to the Purchase Order table? Is it possible that for the products in the warehouse without a purchase order to be blank instead of eliminating them?

report with subreport ms access 2007

I have three table in my database.
Customers, which contains detaills of each client, such as name, phone number ...
Products, containing detaills of each product.
each time a client requests a product, a new line is inserted in the third table Orders.
the table Orders contains the customer id (foreign key), the product id (foreign key) and the quantity desired.
what I'm looking to do is to creat a report based on the Orders table, that shows me for each Client all the Orders that he has made.
I'm working on ms access 2007.
please help me !!!
Create a query based on the orders table joined to the customer table. Use the query design window to build the query. You can then base your report on the query, using grouping to get the customer details at the top of the group and the prder details as lines within the group. Use the report wizards.
Ok I have the solution.
I created a form with this record source:
SELECT
DISTINCT Costumers.Code, Costumers.Name, Costumers.phone
FROM
Costumers INNER JOIN Orders
ON Costumers.ID=Orders.IdCostumer;
then I've created a subreport with this record source:
SELECT
[Costumers].[Code],
[Orders].[Code],
[Products].[Description],
[Orders].[Quantity]
FROM
Products INNER JOIN
(Costumers INNER JOIN Orders ON Costumers.ID=Orders.IdCostumer)
ON Products.ID=Orders.IdOrder;
and that's working the way I want.
thank you for your interest !!!
:-)