SQL Database "Operation must use an updateable query" Workaround - sql

So my basic goal is to create a database for a shopsystem, which is my task to do for my IT course. I tried to create a UPDATE-Query, that collects all the Sale Positions ("tblPosition.PositionAnzahl") ordered with a SELECT-Query and groups it by the products ordered, to have an overview about how often each product has been sold.
I want to do this to keep track of how many items are still left in the inventory.
The Query was supposed to update 1 field ("tblArtikel.ArtikelVerkauft") in my table "tblArtikel", in which all my articles and their information is stored.
However, i just found out that you cannot run UPDATE-Queries, that use SELECT-Query data, as i get a error, that says "Operation must use an updateable query".
This is the code i used for the query:
UPDATE tblArtikel as a JOIN
(SELECT p.PositionArtikelID, Sum(p.PositionAnzahl) AS SumOfPositionAnzahl
FROM tblPositionen as p
GROUP BY p.PositionArtikelID
) p
ON a.ArtikelID = p.PositionArtikelID
SET ArtikelVerkauft = p.SumOfPositionAnzahl;
Is there another way to keep track of all the Items left in my inventory, apart from doing what i did?
Here are screenshots of the 2 tables (the depending fields are circled red):
tblPositionen with field PositionAnzahl
tblArtikel with field ArtikelVerkauft
I have not worked with SQL before and only learned about it during 45 min, so ther emight be an easy way for this, but i would still appreciate every answer from you guys.

Related

Error running MS Access delete query with left join

I am using Microsoft Access, I believe from the Office 16 suite.
I am trying to delete records in a Contacts table that are:
listed as "Out of Business" and
no longer have any logged donations in a Donations table.
(We only keep Donations records for five years to maintain a manageable database size. So once an out-of-business company's Donations have worked their way out of that five-year mark, we can go ahead and delete them.)
I've successfully built a query that returns the results I'm looking for - I can see the results in Datasheet view both when doing a SELECT and when switching to DELETE.
However, when I go to run the Delete Query, I get an error message:
Specify the table containing the records you want to delete.
Here is the SQL code:
DELETE Contacts.*
FROM Contacts
LEFT JOIN Donations ON Contacts.[ContactID] = Donations.[DonorID]
WHERE Contacts.ProcurePreference="Out of Business" AND Donations.DonorID Is Null
I am new to SQL code, so am guessing there's something in my syntax that's wonky (although, again, Datasheet view shows exactly what I would expect). Or maybe there's something in the way I've set up my tables and records and relationships that is preventing the final step.
I've searched and read other similar problems on this forum but could not successfully incorporate any of those answers to address my own problem.
Thanks in advance!
You can do what you want with NOT EXISTS:
DELETE Contacts.*
FROM Contacts
WHERE Contacts.ProcurePreference = 'Out of Business'
AND NOT EXISTS (SELECT 1 FROM Donations WHERE Contacts.[ContactID] = Donations.[DonorID])
try using this SQL:
DELETE FROM Contacts
WHERE ProcurePreference = "Out Of Business"
AND ContactId NOT IN (SELECT DonorID FROM Donations)

A solution for a join to get all the data that is not in the main query based on a field from the main query

So I have the below case in crystal reports
Main Report which has a query that shows the batch details of an invoice
Sub-report which shows the details of the batches included in the main report but are not included in that document. So for example
Invoice A has 10 items all with the same batch (this will show in the main report)
Invoice B, C, an D has 3 more items in the same batch (so total 13 items for that batch in 4 different documents) and the 3 items that are not in document A is what appears in the sub-report.
Now it's easy to achieve that in Crystal Reports because you can link sub-reports with fields to filter directly without linking the queries, however since CR is not a database engine the filtration is super slow and to solve that I need a solution in an SQL query:
I tried converting it to UNION ALL so it's
Main Report
Union ALL
Sub Report
The problem is I am unable to replicate the sub-report case since the grouping needs to be by Document then Batch and the 3 items from the sub-report are not on the same document so they are not grouped by the same dimension now and that ruins the report.
I know it's not logical but after trying all the solutions that came to my mind I thought maybe someone more experienced would have a different opinion, basically for every invoice in the main report I need to show all items that are not in that specific invoice so right now I have no links because the Item is not the same, the document is not the same, and only the batch is the same but that's a many to many link.
EDIT: For simplicity let's say this is the main query
SELECT
T0.DocumentNumber,
T2.ItemCode,
T2.ItemName,
T3.BatchNumber,
T3.Quantity
FROM DocHeader T0
JOIN DocRows T1 ON T0.DocumentEntry = T1.DocumentEntry
JOIN ItemMaster T2 ON T1.ItemCode = T2.ItemCode
JOIN BatchTransaction T3 ON T1.Document Entry = T3.DocumentEntry and
T3.DocumentType = T1.DocumentType and T3.DocumentLine = T1.DocumentLine
The report is grouped by Document Number, so what i need is the same query but for every other document number that is not in the main query joined on the main query. The 2 problems are 1. They can leave the document number parameter blank and select all the documents in the table so this process needs to happen for each document seperately. 2. the document number in the sub query will never be the same so the grouping is an issue because the report needs to be grouped on the main query document number.
It would help if you wrote out your main query and some of the secondary queries you've tried, but assuming I understand your issue correctly, you should be able to just create an additional JOIN on the Batches table. This will have your SQL query list off ALL the Item rows for the Batch, then you can try to use conditional formatting and/or grouping in Crystal to to show/hide the rows as needed.
Alternatively, using the query described above, you may be able to use the linked Invoice Numbers you're now getting from your main report query to drive your Sub-Report, as opposed to going by the Batch # -- this may result in better performance.
EDIT: Based on your comment and update, I think the right approach here is to create a Stored Procedure. Doing a single query is probably not going to work, I took a crack at it and it's not worth the hassle.
Using a Stored Procedure you can create a table variable in memory, add the main invoice records to the table, then add the records for the secondary Invoices linked by the Batch number. Use some kind of column/flag in your table that indicates whether it's a 'Main Invoice' record or not, and use this flag to drive the Crystal display logic for how you want to show the main vs secondary/linked invoices.
It's doable!

SQL Selecting Matching Data from Another Table where matching columns have the same data

So I'm making a database of orders and I want to create a list of options for a dropdown box for an order form, but the list needs to be derived from a list of Services in another table, but it needs to match a preceeding Catagory Dropdown.
I'm not sure where I'm going wrong. The query is below.
SELECT ServiceTypes.ServiceType
FROM ServiceTypes
WHERE Orders.ServiceCatagory = ServiceTypes.ServiceType;
So orders is the db of Orders I want to pull the list into
ServiceTypes has the lists of of ServiceTypes I want to pull. It's composed of ServicType and ServiceCatagory. I need to limit the list of Service Types based on the Service Types in Orders.
So if someone selects the ServiceCatagory in orders to be "InstalL", the only results from ServiceTypes I want are those that have a ServiceCatagory that equals the Orders Service Catagory.
I suspect I need to join but I'm not sure how or what kind.
-update-
I should point out, I'm doing this in Access and just trying to populate a listbox.
My new query looks like this
SELECT ServiceTypes.ServiceType
FROM ServiceTypes
INNER JOIN Orders ON Orders.ServiceCatagory = ServiceTypes.ServiceCatagory;
Still not sure if that's right
So I've tested some of the SQL i've been using to get the information and I think I've actually got the correct code
SELECT ServiceTypes.ServiceType
FROM ServiceTypes
WHERE ServiceTypes.ServiceCatagory = Orders.ServiceCatagory
The real problem was my implementation in Access and where the query was being called.
I didn't need to use a JOIN, I just need to change where the call is being made. I.e. after the Catagory has been selected and not at the Database design level.

Access Database - Most Recent Record - Max Function

I'm in the process of building a database to keep track of loaning equipment. I'm trying to build a query that will display the latest record of each machines location.
Relevant table is:
Movements:
Movement ID (PK)
EntryDate (Automatically generated on record entry)
Serial (FK from a table called stock, with (Make, Model etc)
Location (Where the machine is)
Status (Things like: Available, Testing, Sold etc)
Current query is:
SELECT Movements.Serial, Max(Movements.EntryDateMovements) AS MaxOfEntryDateMovements
FROM Movements
GROUP BY Movements.Serial;
Which spits out the latest date of a record, and the serial associated with it.
What I need is the status to be shown in the results, but it still be grouped by the serial.
My issue is that when I try and add that, it either comes back with an error with about the expression not being part of the aggregate function, or I get more results than expected, as it no longer just keeps the results unique to the serial.
I'm pretty new Access, and have so far been able to muddle through guides, and books, and this site, to get everything else working, but i'm stuck at this hurdle.
Any help would be much appreciated.
Select top 1 *
from Movements
order by EntryDateMovements desc
This will give you everything for the newest record. This is TSQL but I think it carries over to Access.
Try this
Select t.serial,t.EntryDateMovements ,t.location, t.status
From movements as t
Inner join (SELECT Movements.Serial, Max(Movements.EntryDateMovements) AS MaxOfEntryDateMovements
FROM Movements
GROUP BY Movements.Serial) as MaxMovements on t.serial= MaxMovements.serial and t.EntryDateMovements=MaxMovements.MaxOfEntryDateMovements

SQL query / SQL Reporting Services

Been rattling my brain for a while and I could not get pass how to do the SQL query that will show the relationship/connections between my two tables.
I'm working on an IT equipment inventory program. I have two tables;
SELECT serial_number, model, ship_dat, status FROM items_list
SELECT item_serial, connected-to_serial FROM connections
All items like desktops, laptops, monitors, etc are on the items_list table. To track down the relationship/connections of the items, I created the connections table. IE, Monitor with serial_number=Screen#1 is connected to a Desktop with serial_number=Serial#1. It works ok with my Window Form application because I
used a datagridview control to list all devices simple SQL query.
However, when trying to show the relationship/connection on SQL Reports I've ran out of ideas how to do it. I'm aiming to get the report look like below or something along the lines. I just need to show the connections between the items.
Thank you
You should be able to do this with a table in SSRS if that is what you are using. The query you would need to drive the table of all related items would be:
SELECT item_serial, connected-to_serial, mainItem.*, connectedItem.*
FROM connections
INNER JOIN items_list mainItem ON connections.item_serial = items_list.serial_number
INNER JOIN items_list connectedItem ON connections.connected-to_serial = connectedItem.serial_number
You can of course tailor the SELECT statement to your needs, mainItem.* and connectedItem.* will not give you the most descriptive column names. Using column aliases (found under column_alias here) you can give a more descriptive name to each column.
From here you should be able to use a table and create a row group on the main item (either name or serial number) to get the type of look you are looking to achieve here. I believe the Report Wizard actually has most of the functionality you are looking for and should handle the bulk of this. You may have to move some of the cells around to get the look you are going for though.