I have a table t01PriceList with fields including. "Date","LatestPrice01","StockNumber01".
Another table t01StockItem with fields including "LatestPrice02", "StockNumber02".
I want an expression in t01StockItem that returns the amount from "LatestPrice01" at the latest date. There are multiple records in t01Pricelist against the same StockItem.
I tried Dlookup("LatestPrice01","t01PriceList",Dmax(Date))
Thank you.
Create a query to retrieve the latest prices:
Select
StockNumber01, LatestPrice01
From
t01PriceList As A,
(Select StockNumber01, Max([Date]) As LatestDate
From t01PriceList
Group By StockNumber01) As T
Where
A.StockNumber01 = T.StockNumber01 And
A.Date = T.LatestDate
Then join this with t01StockItem on the StockNumber.
Related
We have three table
table 1- app ( id , name )
table 2- appPlayer ( id , name )
table 3- appPlayerSession ( id , appId , appPlayerId , version)
my Current query is:
SELECT (select name from app k where k.id= aps.appId) AS appName,version,appId,count(version) FROM appPlayerSession aps GROUP BY appId,version,appName
we need to count the session users for each game with same version, and also woth the object of all users data using single mysql query.
Current Result using my query, but we also need players for each app..
As you havent given your expected result and on basis of your requirement you can do something this.it may be enhanced as per your requirement.
SELECT (select name from app k where k.id= aps.appId) AS appName,version,appId,(select P.name from appPlayer P where P.id=aps.appPlayerid) as appPlayerName, count(version) FROM appPlayerSession aps GROUP BY appId,version,appName,appPlayerName
Also check fiddle as per your requirement created as you havent given any data set and its on my assumption.
http://sqlfiddle.com/#!9/30fe4f/1
New Sql as per your new added requirement-
select X.appname,X.version,X.appid,GROUP_CONCAT(distinct X.appPlayerName order by X.appPlayerName) as Users ,
sum(X.vercount)
from (SELECT (select name from app k where k.id= aps.appId)
AS appName,version,appId,
(select P.name from appPlayer P where P.id=aps.appPlayerid)
as appPlayerName, count(version)as vercount
FROM appPlayerSession aps
GROUP BY appId,version,appName,appPlayerName) X
group by X.appname,X.version,X.appid
New fiddle -http://sqlfiddle.com/#!9/13646c/5
You can use JOIN in sql to connect with multiple tables and fetch result
Below is the format :
SELECT t1.col,
t3.col
FROM table1
JOIN table2
ON table1.primarykey = table2.foreignkey
JOIN table3
ON table2.primarykey = table3.foreignkey
In your case :
SELECT app.col,
appPlayer.col,
appPlayerSession.col
FROM app
JOIN appPlayer
ON app.id = appPlayer.appId
JOIN appPlayerSession
ON appPlayer.id = appPlayerSession.appPlayerId
Hope this is helpful.
One suggestion . It is not a standard to use camelCase for table and column names. snake_case is preferred widely.
I want to create a report with the top 20 customers (based on revenue).
I am using the query:
SELECT dbo.CustTable.AccountNum
,dbo.dirpartytable.NAME
,dbo.hcmworker.PERSONNELNUMBER
,dbo.CustInvoiceJour.SALESBALANCE
,dbo.custinvoicejour.QTY
FROM dbo.CustTable
inner JOIN dbo.HCMWORKER ON dbo.HCMWORKER.RECID = dbo.CustTable.KEV_Worker
inner join dbo.custInvoiceJour on CustInvoiceJour.OrderAccount = CustTable.AccountNum
inner join dbo.dirpartytable on dirpartytable.recid = custtable.PARTY
where CustTable.KEV_Worker = '5633561745'
ORDER BY SalesBalanceMst DESC
I can't find the relation for the customer revenue, after all, that is how I want to sort the report. I am sorting on SalesBalanceMST right now while building the report. Also I am getting multiple records when executing this query.
What am i doing wrong?
EDIT: I now realize I am showing each Invoice Journal, how can I display the Total Revenue of the customer?
A similar search from AX 2012:
CustInvoiceJour CustInvoiceJour;
CustTable CustTable;
DirPartyTable DirPartyTable;
select forceLiterals generateonly sum(SalesBalanceMST), sum(Qty) from CustInvoiceJour
where CustInvoiceJour.OrderAccount == '102372200'
&& CustInvoiceJour.InvoiceDate > today()-365
join TableId from CustTable
group AccountNum
where CustTable.AccountNum == CustInvoiceJour.OrderAccount
join TableId from DirPartyTable
group Name
where DirPartyTable.RecId == CustTable.Party;
info(CustInvoiceJour.getSQLStatement());
This shows the following SQL:
SELECT SUM(T1.SALESBALANCEMST),SUM(T1.QTY),T2.ACCOUNTNUM,T3.NAME
FROM CUSTINVOICEJOUR T1
CROSS JOIN CUSTTABLE T2
CROSS JOIN DIRPARTYTABLE T3
WHERE (((T1.PARTITION=5637144576) AND (T1.DATAAREAID=N'xxx'))
AND ((T1.ORDERACCOUNT=N'102372200')
AND (T1.INVOICEDATE>{ts '2015-11-06 00:00:00.000'})))
AND (((T2.PARTITION=5637144576) AND (T2.DATAAREAID=N'xxx'))
AND (T2.ACCOUNTNUM=T1.ORDERACCOUNT))
AND ((T3.PARTITION=5637144576)
AND (T3.RECID=T2.PARTY))
GROUP BY T2.ACCOUNTNUM,T3.NAME
ORDER BY T2.ACCOUNTNUM,T3.NAME
What is different from your query:
no join on HcmWorker, as I do not have your custom field.
Using sum() to aggregate
selecting on InvoiceDate
selection on OrderAccount
selection on DataAreaId, really important for performance, implicit in AX
selection on Partition, really important for performance, implicit in AX
You cannot directly sort on a sum, but may on a nested SQL query.
I do not know exactly what is wrong in your query but perhaps this information can help you.
Check this standard report CustTopCustomersbyYTDSales, It has some good queries to do that.
https://technet.microsoft.com/en-us/library/hh389751.aspx
I have a schema like this
...
Event(Name, Date, TotalSeats)
Reservation(EventName, Date, NumSeats)
...
I want to extract the total number of seats available for each event, is the following query a legal way to do it?
SELECT R.EventName, (SELECT TotalSeats
FROM Event
WHERE Name = R.EventName) - SUM( NumSeats )
FROM Reservation R
GROUP BY EventName;
The more traditional way to write this query would use join:
SELECT r.EventName, e.TotalSeats - SUM( r.NumSeats )
FROM Reservation r JOIN
Event e
ON e.Name = r.EventName
GROUP BY r.EventName, e.TotalSeats;
Your version should work in MySQL, assuming there are no duplicates in the Event table.
The query which you have written in your sub-query..
SELECT TotalSeats FROM Event WHERE Name = R.EventName
can consist of more than one records.. so in that case this query will return error..
I am trying to build an access query with multiple criteria. The table to be queried is "tblVendor" which has information about vendor shipment data as shown below:
The second table is "tblSchedule" which has the schedule for each Vendor cutoff date. This table has cutoff dates for data analysis.
For each vendor, I need to select records which have the ShipDate >= CutoffDate. Although not shown in the data here, it may be possible that multiple vendors have same CutoffDate.
For small number of records in "tblCutoffdate", I can write a query which looks like:
SELECT tblVendors.ShipmentId, tblVendors.VendorNumber, tblVendors.VendorName,
tblVendors.Units, tblVendors.ShipDate
FROM tblVendors INNER JOIN tblCutoffDate ON tblVendors.VendorNumber =
tblCutoffDate.VendorNumber
WHERE (((tblVendors.VendorNumber) In (SELECT VendorNumber FROM [tblCutoffDate] WHERE
[tblCutoffDate].[CutoffDate] = #2/1/2014#)) AND ((tblVendors.ShipDate)>=#2/1/2014#)) OR
(((tblVendors.VendorNumber) In (SELECT VendorNumber FROM [tblCutoffDate] WHERE
[tblCutoffDate].[CutoffDate] = #4/1/2014#)) AND ((tblVendors.ShipDate)>=#4/1/2014#));
As desired, the query gives me a result which looks like:
What concerns me now is that I have a lot of records being added to the "tblCutoffDate" which makes it difficult for me to hardcode the dates in the query. Is there a better way to write the above SQL statement without any hardcoding?
You might try something like -- this should handle vendors having no past cutoff,
or those having no future cutoff
"today" needs a suitable conversion to just date w/o time
comparison "=" may go on both, or one, or none Max/Min
"null" may be replaced by 1/1/1900 and 12/31/3999 in Max/Min
SELECT tblvendors.shipmentid,
tblvendors.vendornumber,
tblvendors.vendorname,
tblvendors.units,
tblvendors.shipdate
FROM tblvendors
LEFT JOIN
( SELECT vendornum,
Max( iif cutoffdate < today, cutoffdate, null) as PriorCutoff,
Min( iif cutoffdate >= today, cutoffdate, null) as NextCutoff
FROM tblcutoffdate
GROUP BY vendornum
) as VDates
ON vendornumber = vendornum
WHERE tblvendors.shipdate BETWEEN PriorCutoff and NextCutoff
ORDER BY vendornumber, shipdate, shipmentid
A simpler WHERE clause should give you what you want.
SELECT
v.ShipmentId,
v.VendorNumber,
v.VendorName,
v.Units,
v.ShipDate
FROM
tblVendors AS v
INNER JOIN tblCutoffDate AS cd
ON v.VendorNumber = cd.VendorNumber
WHERE v.ShipDate >= cd.CutoffDate;
Ive got a simple query that is used on a search. My problem is with this query is that as the records in mysql are added everytime there is a transaction, the query returns a list of data when there could only be one or a few more rows instead of a lot more.
SQLFliddle
As you can see here - the query returns a lot of rows, where I want it to return
BLSH103 A001A 31 24/01/2014
Can the qty where the product name & pallet space are the same be summed? And then show the largest date?
just use a sum function on t.Quantity (and a group by clause)
SELECT (t.ProductName) as Pname ,(s.PalletSpace) as PSpace, sum(t.Quantity) as Qty,(t.TransactionDate) as Transac
FROM PalletSpaces s
JOIN ProductTrans t
ON s.PalletSpaceID = t.PalletSpace
WHERE t.ProductName LIKE 'BLSH103' OR s.PalletSpace LIKE 'BLSH103'
group by
Pname,
pSpace,
Transac -- if you want to group by date also...
By the way, using LIKE this way (without %) doesn't make much sense...
see SqlFiddle
You just need to use GROUP BY and SUM in this way:
SELECT (t.ProductName) as Pname ,(s.PalletSpace) as PSpace, SUM(t.Quantity) as Qty,(t.TransactionDate) as Transac
FROM PalletSpaces s
JOIN ProductTrans t
ON s.PalletSpaceID = t.PalletSpace
WHERE t.ProductName LIKE 'BLSH103' OR s.PalletSpace LIKE 'BLSH103'
GROUP BY t.ProductName, s.PalletSpace;