Get entire record for Max value (date) in one column - date

I am trying to query data for transactions. I want to get multiple columns for the latest dated transaction. PONumber, Vendor, Price for each item, last time purchased. For example:
Data:
PONumber Item Vendor Price DateOrdered
1 ABC Wal-Mart 1.00 10/29/12
2 ABC BestBuy 1.25 10/20/12
3 XYZ Wal-Mart 2.00 10/30/12
4 XYZ HomeDepot 2.50 9/14/12
Desired Result Set:
PONumber Item Vendor Price DateOrdered
1 ABC Wal-Mart 1.00 10/29/12
3 XYZ Wal-Mart 2.00 10/30/12
Trying to use max function on DateOrdered, but when I include the vendor I get the last purchase for each vendor and item (too many rows). I need one record for each item. Any ideas on how to accomplish? Using MS Access 2007 with ODBC to oracle tables. Thanks in advance.

How about:
SELECT
tran.PONumber,
tran.Item,
tran.Vendor,
tran.Price,
tran.DateOrdered
FROM tran
WHERE tran.DateOrdered = (
SELECT Max(DateOrdered)
FROM tran t
WHERE t.item=tran.item)
Where tran is your table.

Related

How to update the cheapest item owned by someone in postgres?

Let's say I have the following table in Postgres:
fruit
fruit_id owner_id fruit_price notes
-------------------------------------------
1 5 15
2 5 30
3 5 20
4 8 10
5 8 80
I am looking for a way to update the cheapest fruit owned by someone.
That is, I am looking for an operation that would allow me to set the notes column for the cheapest fruit owned by an individual. So this should only ever update one row (updating multiple rows is fine if there are several ties for the smallest value).
For example (psuedocode):
UPDATE fruit SET notes = 'wow cheap' WHERE owner_id = 5 AND fruit_price IS cheapest;
And this would update the first row in the above example data, because fruit_id of 1 is the cheapest fruit owned by user 5.
One possible way is simply to use a correlated subquery:
update fruit set
notes = 'some notes'
where owner_id = 5
and fruit_price = (
select min(fruit_price) from fruit f2
where f2.owner_id = fruit.owner_id
);

How to get debit sum and credit sum on different column in azure data factory

I have data which has 2 column Transaction code('D' or 'C') and Transaction Amount. I want the sum of Debit Transaction in one column and Credit Transaction in another column. I want to perform this calculation using Mapping Data Flow.
Input Data:
Transactioncode TransactionAmount
----------------------------------
D 789
C 450
C 89
Output
DebitAmount CreditAmount
------------------------
789 539
How we can achieve this?
Add an Aggregate transformation. Keep the group by empty. For the aggregates, add 2: DebitAmount, CreditAmount. The formulas will be something like this:
DebitAmount: sumIf(Transactioncode == 'D', TransactionAmount)
CreditAmount: sumIf(Transactioncode == 'C', TransactionAmount)

PostgreSQL product table and sales table

I have the following problem.
I need all the products in a table and at the same time put the sum of the quantities sold of each product.
I need to see each product in the product table with the total sum of sales, according to the date range.
If there is no sales record in that range, it must be zero.
My query is as follows, but it doesn't work.
Product Table : sto_producto
Product sales movement table: sto_movdet
SELECT
sto_producto.pro_codprod AS cod_product,
sto_producto.pro_desc AS name_product,
sum(sto_movdet.mvd_cant) AS total_sale,
AVG(sto_movdet.mvd_costo) AS cost_product_sale
FROM sto_producto
INNER JOIN sto_movdet ON (sto_producto.pro_codprod = sto_movdet.mvd_codprod)
WHERE mvd_fecha BETWEEN '2020301' and '20200716'
GROUP BY pro_codprod, pro_desc
I expect a result similar to
cod_product name_product total_sale cost_product_sale
0004 mousered 45 $ 2.355
0071 pc laptop 0 $ 1.000

Aggregate the columns based on a condition in SPSS modeler

I would like to add the values based on a column:
For example: The input table looks like this:
USERS Order_date Number_of_orders
alice 01-01-2014 2
alice 19-01-2014 5
alice 20-05-2014 8
bob 03-01-2014 1
bob 08-04-2014 9
The output should be like:
USERS Order_date Number_of_orders(NEW)
alice 01-01-2014 2
alice 19-01-2014 7
alice 20-05-2014 15
bob 03-01-2014 1
bob 08-04-2014 10
Number_of_orders(NEW) is the sum of total orders in the same day + the total number of previous orders of that user.
Please let me know how to do this with SPSS modeler.
I know this is fairly late but what I would do is:
1) create a conditional derive node if NAME /= #OFFSET(NAME,1) or #NULL(NAME) then Number_of_orders else null
2) create filler node if #NULL(Derive325) then #OFFSET(Derive325,1)+Number_of_orders
Just FYI, if I were to look for this, I would use the word cumulative, not aggregate

Access version 2000 & 2013 SQL pull latest date, MAX doesn't work

I have a table that needs to pull the latest date from different categories and the date might not always be filled out. I have tried to use MAX, MIN etc. it has not worked.
e.g. ID 1st Game Date 2nd Game Date 3rd Game Date
Joe 6/1/16 missing missing
Anna missing 7/2/16 7/6/16
Rita missing 7/31/16 missing
Needs to Return:
ID Date
Joe 6/1/16
Anna 7/6/16
Rita 7/31/16
I do have this sql that works well but it requires that all the dates get filled in other wise it doesn't return the latest date:
ApptDate: Switch([Pt1stApptDate]>=[2ndApptDate] And [Pt1stApptDate]>=
[3rdApptDate],[Pt1stApptDate],[2ndApptDate]>=[Pt1stApptDate] And [2ndApptDate]>=
[3rdApptDate],[2ndApptDate],[3rdApptDate]>=[Pt1stApptDate] And [3rdApptDate]>=
[2ndApptDate],[3rdApptDate])
Much appreciation in advance for all your help
Use the Nz function:
ApptDate: Switch(Nz([Pt1stApptDate],0)>=Nz([2ndApptDate],0) And
Nz([Pt1stApptDate],0)>= Nz([3rdApptDate],0), Nz([Pt1stApptDate],0),
Nz([2ndApptDate],0)>=Nz([Pt1stApptDate],0) And Nz([2ndApptDate],0)>=
Nz([3rdApptDate],0),Nz([2ndApptDate],0),
Nz([3rdApptDate],0)>=Nz([Pt1stApptDate],0) And Nz([3rdApptDate],0)>=
Nz([2ndApptDate],0),Nz([3rdApptDate],0))
Having said that, your table design is incorrect.
You should be storing each ApptDate per ID in a separate row:
ApptID ID ApptDate ApptNr
1 Joe 6/1/2016 1
2 Anna 7/2/2016 2
3 Anna 7/6/2016 3
4 Rita 7/31/2016 2
whereas ApptID is an autonumber and ApptNr is a sequence per ID (what you seem to call a category).
When you are having problems writing what should be simple queries (SQL DML) then you should consider you may have design flaws (in your SQL DDL).
The missing values are causing you to avoid the MAX set function and compels you to handle nulls in queries (note the NZ() function will cause errors outside of the Access UI). Better to model missing data by simply not adding a row to a table. Think about it: you want the smallest amount of data possible in your database, you can infer the remainder e.g. if Joe was not gaming on 1 Jan and 2 Jan and 3 Jan and 4 Jan etc then simply don't add anything to your database for all these dates.
The following SQL DDL requires ANSI-92 Query Mode (but you can create the same tables/views using the Access GUI tools):
CREATE TABLE Attendance
( gamer_name VARCHAR( 35 ) NOT NULL REFERENCES Gamers ( gamer_name ),
game_sequence NOT NULL CHECK ( game_sequence BETWEEN 1 AND 3 )
game_date DATETIME NOT NULL,
UNIQUE ( game_date, game_sequence ) );
INSERT INTO Attendance VALUES ( 'Joe', 1, '2016-06-01' );
INSERT INTO Attendance VALUES ( 'Anna', 2, '2016-07-02' );
INSERT INTO Attendance VALUES ( 'Anna', 3, '2016-07-06' );
INSERT INTO Attendance VALUES ( 'Rita', 1, '2016-07-31' );
CREATE VIEW MostRecentAttendance
AS
SELECT gamer_name, MAX ( game_date ) AS game_date
FROM Attendance
GROUP
BY gamer_name;
SELECT *
FROM Attendance a
WHERE EXISTS ( SELECT *
FROM MostRecentAttendance r
WHERE r.gamer_name = a.gamer_name
AND r.game_date = a.game_date );
To find the missing sequence values for players, create a table of all possible sequence numbers { 1, 2, 3 } to which you can 'anti-join' (e.g. NOT EXISTS).