I am working on a conversion script in Access VBA where i need a query to change the column requested quantity to contain the sum of the delivered quantity per order line. Right now it looks like this:
ordernumber orderlinenumber requestedquantity deliveredquantity
123456 1000 1 1
123456 1001 2 2
123456 2000 50 50
123456 3000 10 10
So for order number 123456 line number 1000 & 1001 requested quantity should become 3.
Basically what i need is an update query that goes through order line 1000-1999, calculates the sum of the delivered quantity and sets it for the requested quantity. I tried a few different constructions using the SUM clause but i can not seem to find a way to loop through the order lines.
Hmmm . . . You can use aggregation
select ordernumber, min(orderlinenumber), sum(requestedquantity), sum(deliveredquantity)
from t
group by ordernumber, orderlinenumber \ 1000
The backwards slant is MS-Access-speak for integer division.
Related
I would like select some elements from the last id
Here an example that I have :
id money
1 200
1 150
1 500
3 50
4 40
4 300
5 110
Here what I would like :
1 500
3 50
4 300
5 110
So like you can see, I took last id and the money who corresponds.
I tried to do a group by id order by id descending with limit 1. But limit 1 is not available in proc sql from sas and it doesn't work.
Thanks in advance
Unlike SAS datasets, SQL tables represent unordered sets. In your case, it looks like you want the maximum value in the second column, in which case you can use aggregation:
proc sql;
select id, max(money)
from t
group by id;
If you actually mean the last row per id based on the ordering in the SAS dataset, I would suggest using a data step instead.
I have a query output showing a list of orders. Some orders might occupy more then one record in the query output if those orders consist of sub-orders.Each sub-order occupies a separate line in the output. There is the OrderID column which has the same value for all sub-orders in the output:
OrderID Sub-Order Price
1 1 100
1 2 50
2 1 30
3 1 50
I need to add a column "Discount" to the output and fill it by following rules:
If certain order has one sub-order - the discount is 10% of the Price
If certain order has more than one sub-order, the discount is 20% on all sub-orders'
My query is a UNION of two SELECTs.
I use mssql with ms sql studio
Use CASE and COUNT window function
SELECT OrderID, Sub-Order, Price,
CASE WHEN (count(*) OVER (PARTITION BY OrderID)) > 1
THEN Price * 0.8
ELSE Price * 0.9
END
FROM ( table or <query> )
My database contains a list of products and finishes. I have been able to successfully average the price of each finish option, but I only want to display the lowest priced option.
Here is a sample of my data.
Table 1 - product_t
ProductID ProductLineID ProductDescription ProductFinish ProductStandardPrice ProductOnHand
1 1 "Cherry End Table" Cherry 175.00 0
2 1 "Birch Coffee Tables" Birch 200.00 0
3 1 "Oak Computer Desk" Oak 750.00 0
4 1 "Entertainment Center" Cherry 1650.00 0
This query results in a list of each available product finish and the average price of an item that has the finish.
SELECT ProductFinish, AVG(ProductStandardPrice) as AveragePrice
FROM product_t
WHERE ProductFinish IS NOT NULL
GROUP BY ProductFinish;
However, I only want to display the lowest price that results from this query. I attempted this query, but it will not execute.
SELECT ProductFinish, MIN (AVG(ProductStandardPrice)) as AveragePrice
FROM product_t
WHERE ProductFinish IS NOT NULL
GROUP BY ProductFinish;
Thanks in advance for any help. It is much appreciated.
A typical way to get the lowest value is to use order by and some sort of limit on the number of rows. In ANSI standard SQL, this would be:
SELECT ProductFinish, AVG(ProductStandardPrice) as AveragePrice
FROM product_t
WHERE ProductFinish IS NOT NULL
GROUP BY ProductFinish;
ORDER BY AveragePrice ASC
FETCH FIRST 1 ROW ONLY;
Note that some databases using LIMIT 1 or TOP 1 instead of FETCH FIRST 1 ROW ONLY. Also, this version only fetches one record with the lowest price. If there are duplicate minimums, an arbitrary value is chosen.
I have a a table where I have two columns, one is a Date Time column (Test_Complete) and another is alphanumeric Record ID column (RecordID).
I need to prepare a count of recordIDs which were processed on a monthly basis. I have already created a query for that.
SELECT (Format([Test_Complete],"mmm"" '""yy")) AS Evaluation_Month,
Count(tbl_TestStatus.Record_ID) AS CountOfRecord_ID
FROM tbl_TestStatus
WHERE (((tbl_TestStatus.[Test_Complete]) Is Not Null))
GROUP BY (Format([Test_Complete],"mmm"" '""yy")),
(Year([Test_Complete])*12+Month([Test_Complete])-1);
This query works well and gives me output like this:
Evaluation_Month CountOfRecord_ID
------------------ -----------------
Jan'12 20
Feb'12 90
Mar'12 40
Apr'12 50
Now what I need is to calculate the percentage of CountOfRecord_ID value against each Evaluation_Month and append the percentage with the value in Evaluation_Month data.
In the above resultset, the sum of all the CountOfRecord_ID is 200. so the percentage needs to be calculated considering 200 as 100%, such that my result looks like this:
Evaluation_Month CountOfRecord_ID
------------------ -----------------
Jan'12 (10%) 20
Feb'12 (45%) 90
Mar'12 (20%) 40
Apr'12 (25%) 50
How can I modify my SQL query to achieve this?
You only need to add a "subquery field" in your select statement with the total count of records. Something like this:
SELECT
(Format([Test_Complete],"mmm"" '""yy")) AS Evaluation_Month,
Count(tbl_TestStatus.Record_ID) AS CountOfRecord_ID,
Count(tbl_TestStatus.Record_ID) / (select count(tbl_testStatus.recordId
from tbl_testStatus
where tbl_testStatus.test_complete is not null) as percent
FROM
tbl_TestStatus
WHERE
(((tbl_TestStatus.[Test_Complete]) Is Not Null))
GROUP BY
(Format([Test_Complete],"mmm"" '""yy")),
(Year([Test_Complete])*12+Month([Test_Complete])-1);
I have 4 tables with diagram below
I want to summary query for the Institution table. where I want to get result of only,
InstitutionType ProductName Quantity
For example. sample data of institution table
Id Name Address InstitionTypeId
1 aaa ny132 1001
2 bbb dx23 1001
3 ccc bn33 1002
And the InstitionProduct is like that
Id ProductId Quantity InstitionId
1 1000 120 1
2 1000 100 2
3 1000 50 3
Then I want a query result to output total quantity of a given product by Instition Type wise. The sample output will look like this.
InstitutionTypeId productId quantity
1001 1000 220
1002 1000 50
So I want to group the institution by type and aggregate the product quantity of all institution type group.
I tried to use the group by clause, but with the product quantity not as a grouping element it results in error.
SELECT
Institution.InstitutionTypeID,
InstitutionProduct.ProductID,
SUM(InstitutionProduct.Quantity)
FROM
Institution
LEFT JOIN
InstitutionProduct
ON InstitutionProduct.InstitutionID = Institution.ID
GROUP BY
Institution.InstitutionTypeID,
InstitutionProduct.ProductID
If you are querying with group by you need to use either aggregate functions or group by all included fields. The reason is, that the 'group by' returns exactly one row per 'group by' value, so if you introduce an ungrouped field, this would conflict if the field has more than one value per grouping constraint. Even though this might not be the case for your dataset, the query engine cannot know this, and raises an error.
The solution is to introduce aggregates for all non-grouping field with aggregates being (among others): average (avg), summarize (sum), minimum (min) and maximum (max). This would lead to something like
SELECT i.InstitutionTypeID, i.Institution.ID, SUM(ip.Quantity)
FROM Institution I LEFT JOIN InstitutionProduct IP
ON IP.InstituationID = I.ID
GROUP BY i.InstitutionTypeID, i.Institution.ID