How to Select the last price of parts
Hi Gents,
I have below table
TranNum | TranDate | PartNum | Price
1 | 1/1/15 | 111 | 5
2 | 1/5/15 | 212 | 7
3 | 1/7/15 | 111 | 4.5
4 | 1/8/15 | 212 | 6
5 | 1/9/15 | 212 | 5
I need to create new view to select last price per part.
TranDate | PartNum | Price
1/7/15 | 111 | 4.5
1/9/15 | 212 | 5
Thanks in advance.
Nothing is impossible
Something like this will work as well...
IF OBJECT_ID('tempdb..#temp') IS NOT NULL
DROP TABLE #temp
GO
CREATE TABLE #temp (
TranNum INT PRIMARY KEY,
TranDate date,
PartNum int,
Price decimal(10,2)
CREATE UNIQUE NONCLUSTERED INDEX ix_Temp_PartNum_TranDate ON #temp (
PartNum,
TranDate
INCLUDE (
Price
WITH (DATA_COMPRESSION = PAGE)
INSERT INTO #temp (TranNum,TranDate,PartNum,Price)
VALUES
(1 ,'1/1/15 ',111,5),
(2 ,'1/5/15 ',212,7),
(3 ,'1/7/15 ',111,4.5),
(4 ,'1/8/15 ',212,6),
(5 ,'1/9/15 ',212,5)
SELECT
t1.TranNum,
t1.TranDate,
t1.PartNum,
t1.Price
FROM
#temp t1
WHERE EXISTS (
SELECT 1
FROM #temp t2
WHERE t1.PartNum = t2.PartNum
GROUP BY t2.PartNum
HAVING t1.TranDate = MAX(t2.TranDate)
Please note... Without any indexing on PartNum & TranDate, my solution will perform about the same as Martin's solution (currently the fasted of the supplied solutions).
Adding the following index, however, will show that the "EXISTS" solution can dramatically out perform the "NOT EXISTS" solution...
IF OBJECT_ID('tempdb..#temp') IS NOT NULL
DROP TABLE #temp
GO
CREATE TABLE #temp (
TranNum INT PRIMARY KEY,
TranDate date,
PartNum int,
Price decimal(10,2)
CREATE UNIQUE NONCLUSTERED INDEX ix_Temp_PartNum_TranDate ON #temp (
PartNum,
TranDate
INCLUDE (
Price
WITH (DATA_COMPRESSION = PAGE)
INSERT INTO #temp (TranNum,TranDate,PartNum,Price)
VALUES
(1 ,'1/1/15 ',111,5),
(2 ,'1/5/15 ',212,7),
(3 ,'1/7/15 ',111,4.5),
(4 ,'1/8/15 ',212,6),
(5 ,'1/9/15 ',212,5)
SELECT
t1.TranNum,
t1.TranDate,
t1.PartNum,
t1.Price
FROM
#temp t1
WHERE EXISTS (
SELECT 1
FROM #temp t2
WHERE t1.PartNum = t2.PartNum
GROUP BY t2.PartNum
HAVING t1.TranDate = MAX(t2.TranDate)
HTH,
Jason
Jason Long
Similar Messages
-
How to SELECT the last autonumber??
In Access, i have an autonumber column...How do i get the last record (last autonumber) out?
ThanksIn Access, i have an autonumber column...How do i get
the last record (last autonumber) out?
ThanksTry this:
while ( ! rsresult_a.EOF){
//do here the select last record
select row_name
.... etc}
//display the result
<%= rsresult_a%>
Good luck! -
How to select the last elements in a string
Hi Guys
I am using UART RS232 protocol and i need to calculate the check sum. My data came in decimal and in the end it must be doubles to manipulate it. So my problem is, for example, the sum of all elemants is in decimal 1742 and in hex is 6CE and i wont only the last two elemants, CE that corresponds to 206 that is the result of my checksum. Can anyone help me?
Thanks
Solved!
Go to Solution.
Attachments:
String last elements.vi 9 KBThanks Syrpimp
My data came from HMI in decimal like the example that i show above. Each cell is a value. You used an array, but i think that i can't use it because it must be done automatically and the data field have variable length.
The UART protocol frame has a start frame field that corresponds to two ‘{‘ characters and an end frame field that corresponds to two ‘}’. Additionally, besides the data field, that has variable length, it has a byte that corresponds to the command type and another that is the checksum. The following table illustrates the protocol.
Bytes considered in checksum
Command Data
Start Frame
Cmd
(hex)
Data0
Data1
Data2
Data3
Data4
Data5
Data6
Data…
CHK SUM
End Frame
1 Byte CHK SUM (sum of all data bytes truncated to 1 byte)
Fig. 1 UART communication protocol template
I receive data like this:
123
123
160
54
111
103
110
108
107
109
108
108
100
100
100
100
0
0
1
4
5
0
0
0
0
0
0
7
96
1
0
0
0
0
0
10
51
1
0
0
51
1
1
23
12
0
206
125
125
I'm trying explain my doubt the best way i can and i thank you all. -
Select the last saved records in a table
I all
I have this struct in a table
create table INT_OBJ
IDOBJ NUMBER(15) not null,
IDERP NUMBER(15),
IDE NUMBER(15),
CPO VARCHAR2(20),
VLR VARCHAR2(2500),
VLRC VARCHAR2(2500)
How to Select the last saved records in a table. I think usea a temp table for put the last row read and begin to read in the next row.
The table is reading each four hours.Theoretically it can be done with ORA_ROWSCN and enabled ROWDEPENDENCIES, but there are so many nuances, so only adding column for ordering is right.
Regards,
Sayan Malakshinov -
How to develop a report with current price & the last price in the pre.yrs
hi ,
guys,
my client want a report to list of material with respective vendor and to make a price comparison betn the current price & the last price in the privious year.
can any one help how to develop the report, shall go for generic extrc.
thanks in advace.
ramnathCR CR 2011 / "Crystal reports For Visual Studio 2010", you are correct.
Re. the database thinggy. You can connect to a database via ODBC, OLE DB or in some instances natively. Once a report is created you an change the datasource. A good sample app on how to do this is csharp_win_dbengine / vb_win_dbengine. A link to the samples is here:
Crystal Reports for .NET SDK Samples - Business Intelligence (BusinessObjects) - SCN Wiki
More info on connecting to dbs and changing them is in the developer help files:
SAP Crystal Reports .NET SDK Developer Guide
SAP Crystal Reports .NET API Guide
More info on CR APIs for .NET (applies to all versions of CR and VS):
Crystal Reports for Visual Studio 2005 Walkthro... | SCN
You can also use ADO .NET Datasets and in this way you handle the database connections in your app. A good sample is csharp_win_adodotnet (also available in VB) - same link as above.
More info on datasets:
Crystal Reports Guide To ADO.NET
Crystal Reports for Visual Studio .NET - Walkthrough - Reporting Off ADO.NET Datasets
For more complicated operations (e.g.; changing a report from ODBC to OLE DB, changing one table, etc., you will want to use the InProc RAS SDK that is also available in CRVS. Developer help files are here:
Report Application Server .NET SDK Developer Guide
Report Application Server .NET API Guide
Sample apps are here:
NET RAS SDK Samples - Business Intelligence (BusinessObjects) - SCN Wiki
and here:
Crystal Reports .NET In Process RAS (Unmanaged) SDK Sample Applications
More info on RAS SDK:
How to Use The RAS SDK .NET With In-Process RAS Server
Lastly, do use the search box in the top right corner. I find simple search strings such as 'crystal net parameter' return best results (KBAs, Blogs, docs, wikis, discussions and more).
- Ludek -
How can I select the last 2 chars of a string
Hello all,
I am trying to select the last 2 characters of a string to match them to another string. This is for a Poem generator that contains 20 or so words and I have to pick 2 words at random, them look at the last 2 characters to see if they match in each string. Say "Plain" and "Rain" would match because they both have "in" at the end. The length of each word could vary. I don't want this doing for me just what Method should I be looking at.
Thanks.hi,
try this:
public class StringTest {
public static void main(String args[]) {
StringTest obj = new StringTest();
obj.same();
void same() {
String rain = new String("Rain");
String plain = new String("Plain");
int rainLength = rain.length();
int plainLength = plain.length();
if(rain.substring(rainLength-2).equals(plain.substring(plainLength-2))) {
System.out.println("same");
else {
System.out.println("different");
}you can use the length method to get the length of a string and then use the substring method, passing it length-2, so get the last 2 chars. -
How to get the last day according to fiscal period input in selection scree
Hello expert
how to get the last day of fiscal period input.
the fiscal period inculdes 1-16
when fiscal period is greater than 12, only calculate the last day of 12nd month
your solution will be apprecaited, FM existing?
thank you
KevinHi,
when you give a particular date in any month
the following fm will give you the last date of that month
here you can give
R_FDATE-HIGH as 01 and month as the period you wnat and year for current year
concatenates '01' month year into r_fdate-high separated by '.'.
then it will give g_ltdt for that month and year which wil be the last date of that month
CALL FUNCTION 'RP_LAST_DAY_OF_MONTHS'
EXPORTING
DAY_IN = R_FDATE-HIGH
IMPORTING
LAST_DAY_OF_MONTH = G_LTDT
EXCEPTIONS
DAY_IN_NO_DATE = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
thanks & regards,
Venkatesh -
User Defined Field that gives the date output of the last price update.
We would like to create a User Defined Field (UDF) with a Formatted Search (FMS) on the Item Master form that gives the date output of the last price update specifically the wholesale price.
Last Price Update
Pricelist 2 = Wholesale
Is this feasible?
If it is feasible what is the recommended approach?
Current working UDF on Item Master.
/* Date output is based on all the latest updates on the Item Master.*/
select max(updatedate)
from aitm
where itemcode = $[oitm.itemcode]
Resources that were helpful.
1.(Note:1165947 - Tracking Item Price Changes.)
2. Case 4-I6: Query on price updates. Mastering SQL Queries for SAP Business One By: Gordon DuThank you Gordon,
We are looking for the date output of the 'Last Price Update' based on 'Pricelist 2 = Wholesale' which is the challenge.
These 2 are the parameters for the date output, I should have mentioned that in my original message.
Last Price Update
Pricelist 2 = Wholesale
Per reading a bunch of other similar Forum Questions it looked like it is might not be possible to do. With there being no date on the AIT1 table and AITM.Updatedate is based on any and all updates not just last 'Last Price Update' and not sure how to use the loginstanc.
Thanks,
Vern -
How to get the last day of the week?
Hii
i can get the calender week number for any given date using
SELECT to_char(to_date('04/04/2011','MM/DD/YYYY'),'WW') FROM dual
can any body tell me, how to get the last day of that week ?
and the answer should be 04/08/2011(8th april )
thanks
San
Edited by: sandeep9 on Apr 4, 2011 3:50 AMHi, San,
Here's one way:
WITH sample_data AS
SELECT DATE '2011-04-04' AS dt
FROM dual
SELECT dt
, TO_CHAR (dt, 'WW') AS week_num
, NEXT_DAY ( dt - 1
, TO_CHAR ( TRUNC (dt, 'YEAR') - 1
, 'Day'
) AS end_o_week
FROM sample_data;Another way is to use date arrithmetic:
WITH sample_data AS
SELECT DATE '2011-04-09' AS dt
FROM dual
SELECT dt
, TO_CHAR (dt, 'WW') AS week_num
, TRUNC (dt, 'YEAR')
+ (7 * CEIL ( (dt - (TRUNC (dt, 'YEAR') - 1))
/ 7
- 1 AS using_date_arithmetic
FROM sample_data; -
got a new wireless connection for my mac and cannot figure out how to delete the last connection service I had. Everytime I shut the computer, the network goes back to the other one. I cannot find the file ANYWHERE
Under Network Preferences, select the WiFi
click on "Advanced..." button
Select the network you want to delete from the list and click on "-"
Click "Ok"
Click "Apply"
Well done ;-)
You may also want to delete the wireless key from Keychain.
Open Keychain, seach for the old wifi network name, slect it and click delete -
HELP! How te retrieve the last row in MYSQL database using Servlet!
Hi ,
I am new servlets. I am trying to retireve the last row id inserted using the servlet.
Could someone show me a working sample code on how to retrieve the last record inserted?
Thanks
MY CODE
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class demo_gr extends HttpServlet {
//***** Servlet access to data base
public void doPost (HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
String url = "jdbc:mysql://sql2.njit.edu/ki3_proj";
String param1 = req.getParameter("param1");
PrintWriter out = resp.getWriter();
resp.setContentType("text/html");
String semail, sfname, slname, rfname, rlname, remail, message;
int cardType;
sfname = req.getParameter("sfname");
slname = req.getParameter("slname");
rfname = req.getParameter("rfname");
rlname = req.getParameter("rlname");
semail = req.getParameter("semail");
remail = req.getParameter("remail");
message = req.getParameter("message");
//cardType = req.getParameter("cardType");
cardType = Integer.parseInt(req.getParameter("cardType"));
out.println(" param1 " + param1 + "\n");
String query = "SELECT * FROM greeting_db "
+ "WHERE id =" + param1 + "";
String query2 ="INSERT INTO greeting_db (sfname, slname ,semail , rfname , rlname , remail , message , cardType ,sentdate ,vieweddate) values('";
query2 = query2 + sfname +"','"+ slname + "','"+ semail + "','"+ rfname + "','"+ rlname + "','"+ remail + "','"+ message + "','"+ cardType + "',NOW(),NOW())";
//out.println(" query2 " + query2 + "\n");
if (semail.equals("") || sfname.equals("") ||
slname.equals("") || rfname.equals("") ||
rlname.equals("") || remail.equals("") ||
message.equals(""))
out.println("<h3> Please Click the back button and fill in <b>all</b> fields</h3>");
out.close();
return;
String title = "Your Card Has Been Sent";
out.println("<BODY>\n" +
"<H1 ALIGN=CENTER>" + title + "</H1>\n" );
out.println("\n" +
"\n" +
" From " + sfname + ", " + slname + "\n <br> To "
+ rfname + ", " + rlname + "\n <br>Receiver Email " + remail + "\n<br> Your Message "
+ message + "\n<br> <br> :");
if (cardType ==1)
out.println("<IMG SRC=/WEB-INF/images/bentley.jpg>");
else if(cardType ==2) {
out.println("<IMG SRC=/WEB-INF/images/Bugatti.jpg>");
else if(cardType ==3) {
out.println(" <IMG SRC=/WEB-INF/images/castle.jpg>");
else if(cardType ==4) {
out.println(" <IMG SRC=/WEB-INF/images/motocross.jpg>");
else if(cardType ==5) {
out.println(" <IMG SRC=/WEB-INF/images/Mustang.jpg>");
else if(cardType ==6) {
out.println("<IMG SRC=/WEB-INF/images/Mustang.jpg>");
out.println("</BODY></HTML>");
try {
Class.forName ("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection
( url, "*****", "******" );
Statement stmt = con.createStatement ();
stmt.execute (query2);
//String query3 = "SELECT LAST_INSERT_ID()";
//ResultSet rs = stmt.executeQuery (query3);
//int questionID = rs.getInt(1);
System.out.println("Total rows:"+questionID);
stmt.close();
con.close();
} // end try
catch (SQLException ex) {
//PrintWriter out = resp.getWriter();
resp.setContentType("text/html");
while (ex != null) {
out.println ("SQL Exception: " + ex.getMessage ());
ex = ex.getNextException ();
} // end while
} // end catch SQLException
catch (java.lang.Exception ex) {
//PrintWriter out = resp.getWriter();
resp.setContentType("text/html");
out.println ("Exception: " + ex.getMessage ());
} // end doGet
private void printResultSet ( HttpServletResponse resp, ResultSet rs )
throws SQLException {
try {
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<head><title>jbs jdbc/mysql servlet</title></head>");
out.println("<body>");
out.println("<center><font color=AA0000>");
out.println("<table border='1'>");
int numCols = rs.getMetaData().getColumnCount ();
while ( rs.next() ) {
out.println("<tr>");
for (int i=1; i<=numCols; i++) {
out.print("<td>" + rs.getString(i) + "</td>" );
} // end for
out.println("</tr>");
} // end while
out.println("</table>");
out.println("</font></center>");
out.println("</body>");
out.println("</html>");
out.close();
} // end try
catch ( IOException except) {
} // end catch
} // end returnHTML
} // end jbsJDBCServletI dont know what table names and fields you have but
say you have a table called XYZ which has a primary
key field called keyID.
So in order to get the last row inserted, you could
do something like
Select *
from XYZ
where keyID = (Select MAX(keyID) from XYZ);
Good Luckwhat gubloo said is correct ...But this is all in MS SQL Server I don't know the syntax and key words in MYSQL
This works fine if the emp_id is incremental and of type integer
Query:
select *
from employee e, (select max(emp_id) as emp_id from employee) z
where e.emp_id = z.emp_id
or
select top 1 * from employee order by emp_id descUday -
How to find the Last modified date and time of a package
Hi,
We need a clarification on how to find the Last modified date and time of a package in Oracle. We used the example below to explain our scenario,
Lets consider the following example
Let A, B be two packages.
Package A calls the package B. So A is dependent on B.
When A is compiled the TIMESTAMP,LAST_DDL_TIME in USER_OBJECTS gets updated.
Now there a modification in package B so it is compiled. There is no modification in package A.
Now when the package A is executed the TIMESTAMP,LAST_DDL_TIME in USER_OBJECTS gets updated but we did not make any change in Package A. Now we need to find last modified date and time of the package A . So we can not rely on the TIMESTAMP,LAST_DDL_TIME in USER_OBJECTS . Can u please tell us any other solution to get last modified date and time of the package A .
Regards,
Vijayanand.CHere is an example:
SQL> SELECT OBJECT_NAME,CREATED,LAST_DDL_TIME,TIMESTAMP,STATUS FROM USER_OBJECTS
2 WHERE OBJECT_NAME = ANY('A','B');
OBJECT_NAM CREATED LAST_DDL_TIME TIMESTAMP STATUS
A 20-MAY-2004 10:57:32 20-MAY-2004 10:57:32 2004-05-20:10:57:32 VALID
B 20-MAY-2004 10:58:22 20-MAY-2004 10:59:04 2004-05-20:10:59:04 VALID
SQL> CREATE OR REPLACE PROCEDURE A AS
2 BEGIN
3 NULL;
4 NULL;
5 END;
6 /
Procedure created.
SQL> SELECT OBJECT_NAME,CREATED,LAST_DDL_TIME,TIMESTAMP,STATUS FROM USER_OBJECTS
2 WHERE OBJECT_NAME = ANY('A','B');
OBJECT_NAM CREATED LAST_DDL_TIME TIMESTAMP STATUS
A 20-MAY-2004 10:57:32 20-MAY-2004 11:01:28 2004-05-20:11:01:28 VALID
B 20-MAY-2004 10:58:22 20-MAY-2004 10:59:04 2004-05-20:10:59:04 INVALID
SQL> EXEC B
PL/SQL procedure successfully completed.
SQL> SELECT OBJECT_NAME,CREATED,LAST_DDL_TIME,TIMESTAMP,STATUS FROM USER_OBJECTS
2 WHERE OBJECT_NAME = ANY('A','B');
OBJECT_NAM CREATED LAST_DDL_TIME TIMESTAMP STATUS
A 20-MAY-2004 10:57:32 20-MAY-2004 11:01:28 2004-05-20:11:01:28 VALID
B 20-MAY-2004 10:58:22 20-MAY-2004 11:01:53 2004-05-20:11:01:53 VALID
Note that the date under the column 'created' only changes when you really create or replace the procedure.
Hence you can use the column 'created' of 'user_objects'. -
How to find the last update date time and user of record field peoplecode
how to find the last update date time record field peoplecode?
Thank you.One can check the last update date time using the following query
SELECT LASTUPDDTTM FROM PSPCMPROG WHERE OBJECTVALUE1 LIKE 'RECNAME' AND OBJECTVALUE2 LIKE 'FIELDNAME' -
How to find the last modified date of a workflow.
How to find the last modified date of a workflow.
thanks.Hi,
There is nothing as standard that does this - you could write some code to determine the latest begin_date from each of the workflow tables and assume that this was the last time that the definition changed.
What you need is something like this:
select max(wfa.begin_date)
from wf_process_activities wpa
, wf_activities wfa
, wf_item_types_tl wit
where wpa.activity_item_type = wfa.item_type
and wpa.activity_name = wfa.name
and wfa.version = (select max(version) from wf_activities wfa1 where wpa.activity_item_type = wfa1.item_type and wpa.activity_name = wfa1.name )
and wpa.process_item_type = 'your item type'
and wpa.process_item_type = wit.namewhich I think gives what you want.
HTH,
Matt
Alpha review chapters from my book "Developing With Oracle Workflow" are available on my website:
http://www.workflowfaq.com
http://forum.workflowfaq.com
NEW! - WorkflowFAQ Blog at http://thoughts.workflowfaq.com -
How to select the first record?
Hi,
Could anyone tell me how to select the first record of every
group?
ex, I want to find out every customer their first purcaseDate.
CustomerID PurcaseDate Region
A00000001 2001/01/01 AA
A00000001 2001/02/02 AA
A00000002 2001/03/01 AC
A00000002 2001/05/07 AC
A00000003 2001/03/05 DD
result:
A00000001 2001/01/01 AA
A00000002 2001/03/01 AC
A00000003 2001/03/05 DD
ThanksVincent,
You could do it as Carl suggested, with a couple of
corrections. You would need to include the CustomNo column in
the order by clause of your cursor. You would also need to add
a where clause to your update statement, otherwise everywhere
Region in the table would be updated with the same value of the
last CustomNo in the cursor, regardless of the CustomNo in the
table. See corrected code below:
DECLARE
CURSOR cust
IS
SELECT DISTINCT CustomNo, Region, Purchase_Date
FROM my_table
ORDER BY CustomNo, Purchase_Date, Region;
c_customer VARCHAR2 (9) := '...';
c_region VARCHAR2 (2) := '..';
cntr NUMBER := 0;
BEGIN
FOR x IN cust
LOOP
IF x.CustomNo != c_customer
THEN
c_customer := x.CustomNo;
c_region := x.Region;
ELSE
UPDATE my_table
SET Region = c_region
WHERE CustomNo = c_customer;
cntr := cntr + 1;
IF cntr = 25
THEN
COMMIT;
cntr := 0;
END IF;
END IF;
END LOOP;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
Another option is that you could just use one update statement,
like this:
UPDATE my_table a
SET Region =
(SELECT DISTINCT Region
FROM my_table b
WHERE (CustomNo, Purchase_Date) IN
(SELECT CustomNo, MIN (Purchase_Date)
FROM my_table
GROUP BY CustomNo)
AND a.CustomNo = b.CustomNo)
Barbara
Maybe you are looking for
-
Dual Monitor Causing System Problems?
Hi, I'm new to this site, so I hope this is where I ask this question! My design group switched to G5/Cinema Display screen from old g4 setups mid 2005. I retained my Mitsubishi Diamond Pro 2020u monitor to use as a secondary display for pallettes, e
-
Time out error in production server for alv grid report
hi. i have developed alv grid report using nested select statments. when i testing in development server it is giving the output but in production server if i give 4 months then it is showing time out error.please help me out..its urgent. Thanks in
-
Can I Use a Dell 2407FPW?
PowerMac Dual 2.3 G5, ATI Radeon 9600 with 128MB VRAM Can I drive a Dell 2407FPW or will I need a new graphics card?
-
i kept my iphone on my table so with a half charged battery. I picked it up and it would not turn on then i charged it about an hour but still is not turning on.is it a battery problem? please help!
-
So I need a new phone for one of my lines, but my the upgrade isn't up for 14 months (Nokia Twist...worst phone ever made). I have to pay retail & I don't know what to get. Which one is better LG Vortex or Motorola Citrus?? I've read reviews for b