Retriev Top to columns of table based on latest date
Hi all,
I have the below table
Date
Value
1/1/2012 1000
1/1/2013 2000
1/1/2014 3000
I need a querry which will fetch the latest two dates and values. ie it should retrieve as below
Date
Value
1/1/2013 2000
1/1/2014 3000
Thanks in advance.
SELECT TOP 2 * FROM tbl ORDER BY Date DESC
Best Regards,Uri Dimant SQL Server MVP,
http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting:
Large scale of database and data cleansing
Remote DBA Services:
Improves MS SQL Database Performance
SQL Server Integration Services:
Business Intelligence
Similar Messages
-
Build a table based on XML data set with Spry
Hi there,
I'm new to spry technology therefore forgive any basic question of mine.
I'm trying to fill content in a table based on XML data set values but nothing is shown :-(
here is my code.... any suggestion? pls tell me where I'm wrong.
Thank you in advance
<script src="SpryAssets/xpath.js" type="text/javascript"></script>
<script src="SpryAssets/SpryData.js" type="text/javascript"></script>
<script type="text/javascript">
var uscite = new Spry.Data.XMLDataSet("data/Calendario 2011.xml", "csv_data_records/record", {sortOnLoad: "Date", sortOrderOnLoad: "ascending"});
uscite.setColumnType("Date", "date");
uscite.setColumnType("km", "number");
</script>
<div class="RankContainer" id="UsciteDiv" spry:region="uscite" >
<table width="100%" border="0" class="RankTable">
<tr>
<th width="10%" scope="col" spry:sort="Date">Data</th>
<th width="20%" scope="col">Destinazione</th>
<th width="5%" scope="col">KM</th>
<th width="35%" scope="col">Percorso</th>
<th width="30%" scope="col">Breve</th>
<!-- <th width="15%" scope="col">Mappa</th>-->
</tr>
<tr>
<script type="text/javascript">
var rows = uscite.getData();
for (var i = 0; i < rows.length; i++)
if (rows[i]["Mappa"].startsWith("/"))
rowContent = "<td> si </td>";
else
rowContent = "<td> no </td>";
document.write("<td>{Date}</td>");
document.write("<td>"+rowContent+"</td>");
document.write("<td>{km}</td>");
document.write("<td>{Percorso}</td>");
document.write("<td>{Breve}</td>");
</script>
</tr>
</table>
</div>Sure this is how it should work (except that no anchor tag shall be present for Destinazione whereas Mappa has no real value in)
http://www.gsc-borsano.it/_Calendario%202011.html
and this is the non working page
http://www.gsc-borsano.it/_v2Calendario%202011.html
Thanks -
Is it possible to find the table based on the Date ?
Dear Team ,
Is it possible to find the table based on the Date ?
I have created an table ,But forgot the Table Name .
Is it possible to find the Tables created on particular Date .
Regards ,
Augustineas date is record the time also below query will work.
select * from user_objects
where
object_type = 'TABLE' and
to_date(created,'DD-MON-YYYY') =to_date('<your date value in DD-MON-YYYY format>','DD-MON-YYYY');
Edited by: shaileshM on Feb 24, 2010 9:39 PM -
Remove duplicate rows based on latest date
Hi Julius, There are various ways to achive this but I would think an optimized way of doing it by making use of (Sorter and) an Aggregator transformation(s), see how.. Sort your input records at SQ level if it's a relational source or else use a Sorter tx (that's why I mentioned Sorter tx in braces in abvoe line) to sort the input rows on Stage_ID and Date, both in Ascending order, thus you get the latest date record at the end in the list per common Stage_IDs. Now, connect to Aggregator (must be right next to the sorter in order to not to loose the sorted data), and enable the Group By for Stage_ID port. By nature, aggregator tx will push the last record of the group by port or last record of the complete data set if no group by port mentioned. So, you will get recent row per Stage_ID. Try and get back to us if you have any questions or run into issues,-Rajani
I have a target table I need to split up: stage_id / date / case / blah / blah100 / 01-jan-2015 / 223 / 32 / 323100 / 01-apr-2015 / 224 / 32 / 322 the stage_id column cannot have more than 1 of the same record, so, i need to keep the latest record (01-apr-2015) in this target table, then remove the old one and put it into a dimension table any suggestions for how to accomplish this? please let me know if more info is needed thanks!
-
Question on updating a table based on report data
Hi all,
I am building a new stock request form for our site and I have a report that provides a listing of all requests that have a status of pending based on supv_approve flag being Null.
I want to be able to update the table BGNA_NEW_STOCK_REQUESTS and set SUPV_APPROVE Flag to either Y or N depending on what the user sets as the value in the report. (by changing the standard report column to a text field and setting it to a named LOV with values Y or N, I can change each row in the displayed report to either be Y or N - but have yet to figure out how you then update the table with those values)
Each row of the report has a unique ID - I just havent figured out how you update a table by chaging the values displayed in a report. I get that I can change the feild type from standard report column to text field but havent figured out how to get an update button to actually update the table once I changed the NULL value to Y or N
any help is greatly appreciated!
Edited by: user8607582 on Aug 10, 2009 1:51 PM
Edited by: user8607582 on Aug 10, 2009 1:58 PMHello Danny,
Add a column to the sql that is something like this:
select apex_item.checkbox(1,<keyfieldname> "Approved", (then the rest of your query for your report).
This will put a check-box on your report. Move it to the beginning of the line for a better user-interface.
When the user clicks the "Submit" button, ONLY those lines that have a check in the box are sent back. Create a process on your page that does the following:
for i in 1..apex_application.g_f01.count Loop
<do the processing of Approved transactions using this syntax for your update processing... where <keyfieldname> = apex_application.g_f01(i);
<set all of the remaining as "N" if you wish or leave the supervisor's approval as NULL>
end loop;
This will then allow you process all of those items that were checked.
I hope this helps,
Don.
You can reward this reply by marking it as either Helpful or Correct :) -
Table based on one data service but filtered by a second data service
Hi,
I'm fairly new to VC and have got comfortable with a few simple scenarios. However, I'm trying to apply some fundamental logic that must be possible and I'm guessing straight forward.
I have a data service that contains document type master data that comes from system A. I have a second data service that supplies a list of the document types that a particular user is able to view. This comes from system B. I want to be able to display the document type master data in a table but only the document types the user is allowed to see. Both data services contain a document type code.
If anyone can point me in the right direction I would really appreciate it. We want to use VC on my current project and this is the last piece of functionality I'm trying to prototype.
Many Thanks
GaryHi,
I am not sure whether you have tried to achieve your requirement using intersect operator or no. I haven't tried it before but you can give it a shot.
Regards,
Murtuza -
I'm trying to link 2 tables A and B together.
For every entry in A there is either 0, 1 or many entries in B.
What I am trying to do is find whichever entry in B has the earliest date in it.
eg
A
PKid
1
B
FKid | date
1 | 01/01/2005
1 | 12/12/2006
1 | 31/12/2007
So in this instance I only want the data from the first row, with the date 01/01/2005.
Is this possible in CR XI?Hi,
maybe you can use the join? of two tables?
Look for Linking options within Crystal Reports Help file.
Many thanks
Kind Regards
Jehanzeb -
Choosing unique record based on latest date
I have a column, say Name, in which a person's name appears with multiple transactions. Each transaction has an unique date. I want to return just the latest transaction by date for each person.
If I write my select as:
select Name, Trans_Dt from <Table>;
I of course get a list of all transactions listing person and date.
I've tried this:
select Name, Trans_Dt from <Table>
group by Trans_Dt having greatest(Trans_Dt).
This doesn't work. Any suggestions?select name, trans_dt
from
select
Name,
Trans_Dt,
max(Trans_Dt) over(partition by name) as max_trans_date
from <Table>
where Trans_Dt= max_Trans_trans_date;Would be one way. -
Select distinct rows based on latest date
Hi
I need to produce data from two tables and return results where only data for the latest OriginationDate is returned for each unique client. I have been trying all sorts of combinations but with no success. Any help would be appreciated
e.g
select
c.client_no
,c.forename
,c.surname
,c.tel_no1
,c.tel_no2
,c.tel_no3
,o.ordernumber
,o.propertyidentifier
,o.contractorlongname
,o.OrderStatus
,o.CancelReasonDesc
,o.OriginationDate
,o.Completed
,DATEDIFF(d,o.OriginationDate,GETDATE())
as Total
FROM
hgmclent AS c
LEFT
OUTER
JOIN QLHPM_Order_Summary
AS o
ON
c.client_no
= o.ClientNumber
WHERE
o.Ordernumber
IS
NOT
NULL
GROUP
BY c.client_no
,c.forename
,c.surname
,c.tel_no1
,c.tel_no2
,c.tel_no3
,o.ordernumber
,o.propertyidentifier
,o.contractorlongname
,o.OrderStatus
,o.CancelReasonDesc
,o.OriginationDate
,o.Completed
Currently this produces the following data from which I have hidden sensitive information. What I need to show for client_no 1 is repair order 6353 which was reported on 2015-01-06 00:00:00.000 being the most recent of 12 orders for this client,
Client No2 only has one order so this will be shown
Client No3 should only show order number 6842 reported on 2015-01-19 00:00:00.000
and so on.
Can anyone help with this
Thanks
JohnI think he needs to return the data for the latest OriginationDate, so ORDER BY o.Completed DESC should be changed to
ORDER BY o.OriginationDate DESC
Row_number() Over(Partition
by c.client_no Order
by o.OriginationDate
DESC ) rn
A Fan of SSIS, SSRS and SSAS -
Column hiding /unhiding based on a condition
I have a report in which I want to add "State" Column. But it show up only when "Region" = 'Northeast'. And do not show State for other "Region"
So basically, I want to hide "State" column dynamically based on condition where "Region"='Northeast'.
Would Siebel On Demand let us do Column hiding /unhiding based on a data condition?
Please let me know if this is possible.
Thanks
RakeshI don't think SOD lets hide/unhide a column based on a condition. All you can do is restrict the number of rows using a filter.
-
Update columns in Table A based on columns in Table B for more than 500K rows
Guys,
I need to update 9 columns in table A based on value from table B for for more than 500K rows.
So what is best way to achieve this. I am thinking of writing a Procedure with cursor to update the rows of table A.
When i googled about it, they say cursor will decrease the performance. So i have no clue how to go for this.
Rough code which i though
1) Procedure with no parameter
2) Will declare 9 variable to store value from cursor
3) cursor will fetch row by row based on join condition between table a and table b
4) i will pass column values from table B to variables
5) will make an update statement for table A
Please let me know if above method is correct or is there any other way to do this without using cursor.Guys,
Below is the rough code i wrote as per my requirement. Does it look correct? As of now i dont have any platform to test it so any help with the below code is highly appreciated. As i said i need to update more than 500K rows by matching Table
A and Table B. One more thing which i would like to add in below code, is to get log of all the rows that are in table B but not exist in table A. Table A already has more than million data in it.
Also not sure how the loop in below code willl run when @rowcount is become to zero?
Please let me know if i need to consider performance related impact while running the script.
GO
SET SERVEROUTPUT ON
CREATE PROCEDURE ONETIMEUPDATE
DECLARE @cnt INT;
SET @cnt = 1;
DECLARE @MSG varchar(255);
DECLARE @COUNT_VAR INT;
SET @COUNT_VAR=0;
WHILE @cnt > 0
BEGIN
Update TOP (50000) A
Set A.Col1=B.Col1,
A.COL2=B.COL2,
A.COL3=B.COL3,
A.COL4=B.COL4,
A.COL5=B.COL5,
A.COL6=B.COL6,
A.COL7=B.COL7
From TableA A
Inner Join TableB B
on A.ID = B.ID--ID
WHERE A.Col1 <> B.Col1
OR A.Col2 <> B.Col2;
SET @cnt = @@ROWCOUNT;
IF @@ROWCOUNT=25000
@COUNT_VAR=@COUNT_VAR + @@ROWCOUNT
SELECT @MSG = CONVERT(varchar, @COUNT_VAR) + "Rows Updated" -- I WANT TO DISPLAY UPDATE after EVERY 25000 ROWS
PRINT @MSG
IF @@ROWCOUNT=0
BEGIN
COMMIT
END
WAITFOR DELAY '00:00:01' --wait for a second before the next update
END;
END; -
Update table based on ID reference comparing each column and value
Hi,
Through UI user can update any information and click save. In backend i will be receiving only ID as reference . Based on the ID value i should update multiple tables wherever i have the reference tables. Here i will not get the values or column name updated.
I just get a ID to find which row is updated.
I should do a comparision now.
I have a history table where i get the row before updation. and i get a row after updation from current data i.e. from ssdb_Railroad[This will be updated by java after updation we will get the ID inorder to update remaining tables]. Now i need to compare
both the table column values and know which column is updated and after getting the value i should update reference tables.
Below is the structure for comparision table , History table and Main table.
Create table Comparision_History (
ID int IDENTITY(1,1) PRIMARY KEY,
InsertUser_UpdateUser varchar(50),
Old_ColVal varchar(100),
New_ColVal varchar(100),
TableName varchar(100),
[Action] varchar(50),
InsertDate_UpdateDate Datetime NOT NULL DEFAULT getdate()
CREATE TABLE SSDB_Railroad_History (
ID int IDENTITY PRIMARY KEY,
SCAC varchar(4) ,
Name varchar(50) NOT NULL,
RailroadID int NOT NULL UNIQUE,
RC2Key
varchar(50),
NOTES varchar(1000),
[Action] varchar(50),
InsertDate_UpdateDate Datetime NOT NULL DEFAULT getdate(),
InsertUser_UpdateUser varchar(50),
CREATE TABLE SSDB_Railroad (
ID int IDENTITY(1,1)PRIMARY KEY,
SCAC varchar(4) UNIQUE,
Name varchar(50) NOT NULL,
RailroadID int NOT NULL UNIQUE,
RC2Key
varchar(50),
InsertDate Datetime NOT NULL DEFAULT getdate(),
UpdateDate Datetime,
InsertUser varchar(50),
UpdateUser varchar(50)
Here SSDB_Railroad table and History table needs to be compared and get a updated value and it should be inserted in a comparision table. as well as it should be updated in reference tables.
Please help me with the code.
Thanks in Advance
DeepaHi Deepa_Deepu,
According to your description, personally, the merge function can meet your requirements. Usually, we can synchronize two tables by updating or inserting rows in a target table based on differences found in the source tables. Just as your description, when
the value of ID and InsertDate_UpdateDate in the source table matches a value, update them in the target table. When the values does not match, the source row is inserted into the target table.
For more information, see: using MERGE to perform UPDATE and INSERT operations on a target table by using a derived source table.
http://msdn.microsoft.com/zh-cn/library/bb510625.aspx
Regards,
Sofiya Li
Sofiya Li
TechNet Community Support -
Disable columns in table control based on condition
Hello Gurus,
I am having a table control in my module pool program.Out of 25 columns , I am having 12 columns on months wise like (jan, feb, march....).Now my concern is based on current month I want to enable the column of month rest all months will be in disable mode.So that the user can enter his/her time of work.Could you please tell me any way.I tired to read tc-cols and stored in col with index but its not working I wrote this code for first month without any condition
READ TABLE tC-COLS INTO col INDEX 22.
col-SCREEN-INPUT = 0.
MODIFY tC-COLS FROM col INDEX 22.
but its not working.I wrote this code in PBO as well as i tried in PAI as wellstill my columns is in enable mode.Also when user clicks on button then only the table control required fields should start working like months enable/disable process.for eg. I am having 2 button one is Create and other is Search on both these buttons after click the condition for enable/disable months should work.
Thanks
GauravHi,
In my scenario, I have used months as radio buttons. A month is selected and "enable" button must be clicked so that only the corresponding column of the month turns into an editable mode.
I have attached the output.
The field can be disabled or enabled by using the line type of <table_control_name>-cols.
It contains the screen elements. For disabling / enabling, use active element in screen.
This must be written in PBO event of the corresponding screen containing the table control.
Coding:
* Type-Pools
TYPE-POOLS: abap.
* Types
TYPES: BEGIN OF ty_month_emp,
jan TYPE char30,
feb TYPE char30,
mar TYPE char30,
apr TYPE char30,
may TYPE char30,
jun TYPE char30,
jul TYPE char30,
aug TYPE char30,
sep TYPE char30,
oct TYPE char30,
nov TYPE char30,
dec TYPE char30,
id TYPE char10,
name TYPE char10,
age TYPE char10,
END OF ty_month_emp,
ty_t_month_emp TYPE STANDARD TABLE OF ty_month_emp.
* Data
DATA: gs_month_emp TYPE ty_month_emp,
gt_month_emp TYPE ty_t_month_emp,
ok_code TYPE sy-ucomm,
r1 TYPE char1 VALUE abap_true,
r2 TYPE char1,
r3 TYPE char1,
r4 TYPE char1,
r5 TYPE char1,
r6 TYPE char1,
r7 TYPE char1,
r8 TYPE char1,
r9 TYPE char1,
r10 TYPE char1,
r11 TYPE char1,
r12 TYPE char1.
* Table Controls
CONTROLS: tc TYPE TABLEVIEW USING SCREEN 9000.
* Field-Symbols
FIELD-SYMBOLS: <fs_cols> LIKE LINE OF tc-cols.
* Initialization
INITIALIZATION.
CALL SCREEN 9000.
*& Module ENABLE_COLUMN OUTPUT
* text
MODULE enable_column OUTPUT.
LOOP AT tc-cols ASSIGNING <fs_cols>.
PERFORM enable_col USING: r1 'GS_MONTH_EMP-JAN' CHANGING <fs_cols>,
r2 'GS_MONTH_EMP-FEB' CHANGING <fs_cols>,
r3 'GS_MONTH_EMP-MAR' CHANGING <fs_cols>,
r4 'GS_MONTH_EMP-APR' CHANGING <fs_cols>,
r5 'GS_MONTH_EMP-MAY' CHANGING <fs_cols>,
r6 'GS_MONTH_EMP-JUN' CHANGING <fs_cols>,
r7 'GS_MONTH_EMP-JUL' CHANGING <fs_cols>,
r8 'GS_MONTH_EMP-AUG' CHANGING <fs_cols>,
r9 'GS_MONTH_EMP-SEP' CHANGING <fs_cols>,
r10 'GS_MONTH_EMP-OCT' CHANGING <fs_cols>,
r11 'GS_MONTH_EMP-NOV' CHANGING <fs_cols>,
r12 'GS_MONTH_EMP-DEC' CHANGING <fs_cols>.
ENDLOOP.
* LOOP AT tc-cols INTO ls_cols.
* IF r1 = 'X'.
* ls_cols-screen-active = 1.
* else.
* ls_cols-screen-active = 0.
* ENDIF.
* modify tc-cols FROM ls_cols INDEX sy-tabix.
* ENDLOOP.
ENDMODULE. " ENABLE_COLUMN OUTPUT
*& Form ENABLE_COL
* text
* -->P_0170 text
* -->P_0171 text
* <--P_<FS_COLS> text
FORM enable_col USING p_radio TYPE c
value(p_screen_name) LIKE screen-name
CHANGING pa_cols LIKE LINE OF tc-cols.
IF p_radio = abap_true.
IF pa_cols-screen-name = p_screen_name.
pa_cols-screen-active = 1.
ELSE.
pa_cols-screen-active = 0.
ENDIF.
ENDIF.
ENDFORM. " ENABLE_COL
Thanks & Regards,
T. Prasanna Kumar -
Update Columns in Table from Other Table based on Fund Code
Hello All!!
Hope you are doing good!!!
I like to update 5 columns in X Table based on fund_code. The Fund code and 5 column values come from Y Table. Please let me know how to wirte a sql statement to update all values for 5 columns in X Table. Please post me any template code.
Thanks.I think you are looking for an updatable join,
Please see the syntax below,
UPDATE (SELECT b.col1
FROM table1 b, table2 E
WHERE b.col_pk = E.col_pk) T
SET T.col1 = 0Thanks,
G. -
Import flat file to multiple tables based on identifier column
Hello,
I am trying to setup a package that will import one pipe-delimited flat file (a utility bill) to multiple data tables based on the value of the first column. I have been told it is similar in format to an EDI file, but there are some differences.
The number of columns is consistent where the first columns are the same. Meaning a record that has '00' in the first column will always have 10 columns; a record that has '01' in the first column will always have 9 columns; etc.
Each value in the first column represents a separate destination data table. Meaning a record that has '00' in the first column should be output to table '00'; a record that has '01' in the first column should be output to table '01'; etc. All
destination tables reside on the same SQL Server.
Identifier columns can repeat multiple times throughout the flat file. Meaning a record that starts with '01' may be repeated multiple times in the same.
Sample Data:
00|XXXXXXXX|XXX|XXXXXXXX|XXXXXX|XXXX|X|XXXXXXXXXX|XX|XXXXX
01|XXXXXXXXXXX|XXX|XXXXXXXX|XXXXX|XXXXXXXXXXXXXXXXXXXX|XXXXXXXXXX|XXXXXXX|XXXXXXXXXXXXXX
02|XXXXXXXXXXX|XXXXXXXX|XXXXXXX|XXXXX|XXXXX|XXXXX|XXXXX|XXXXX|XXXXX|XXXXX|XXXXX
04|XXXXXXXXXXX|XXXXXXXXXXXXX|XXX|XXXXXXXX
05|XXXXXXXXXXX|XXXXXXXXXXXXX|XXX|XXXXXXXX|XXXX
07|XXXXXXXXXXXXX|X|XXXXXXXXXXXXXXX|XXX|XXXXXXXX|XXXX|XXXXXXX|XXXXXXXXXXX
07|XXXXXXXXXXXXX|X|XXXXXXXXXXXXXXX|XXX|XXXXXXXX|XXXX|XXXXXXX|XXXXXXXXXXX
07|XXXXXXXXXXXXX|X|XXXXXXXXXXXXXXX|XXX|XXXXXXXX|XXXX|XXXXXXX|XXXXXXXXXXX
07|XXXXXXXXXXXXX|X|XXXXXXXXXXXXXXX|XXX|XXXXXXXX|XXXX|XXXXXXX|XXXXXXXXXXX
01|XXXXXXXXXXX|XXX|XXXXXXXX|XXXXX|XXXXXXXXXXXXXXXXXXXX|XXXXXXXXXX|XXXXXXX|XXXXXXXXXXXXXX
02|XXXXXXXXXXX|XXXXXXXX|XXXXXXX|XXXXX|XXXXX|XXXXX|XXXXX|XXXXX|XXXXX|XXXXX|XXXXX
04|XXXXXXXXXXX|XXXXXXXXXXXXX|XXX|XXXXXXXX
Any help would be appreciated.Hi koldar.308,
If there are few distinct values in the first column, we can use Flat File Source connect to that flat file, then use Conditional Split Transformation to split the first column to multiples, and then load the data to multiple tables with OLE DB Destination
based on the outputs of Conditional Split.
After testing the issue in my environment, please refer to the following steps to achieve this requirement:
Drag a Flat File Source connect to that flat file with Flat File Connection Manager.
Drag a Conditional Split Transformation connects to the Flat File Source.
Double-click the Conditional Split Transformation, add several Output based on the first column values as below:
Drag same number OLE DB Destinations as the outputs of Conditional Split, connect to Conditional Split with one case output:
If there are any other questions, please feel free to ask.
Thanks,
Katherine Xiong
If you have any feedback on our support, please click
here.
Katherine Xiong
TechNet Community Support
Maybe you are looking for
-
From my downloads folder it recognizes the file as a Pages document, I can preview the entire document, but I try to open the file in multiple ways (from right click, to open from preview, from inside pages) and it refuses to open. The "opening" pro
-
When will Aperture support Panasonic Lumix raw files?
-
Problem with Query in supplier in R12
Hi, I have issue with the Supplier sites and Supplier contacts join, can any one please let me know what are the joins in R12 between Sites and contacts? Edited by: 902157 on Apr 30, 2012 1:17 AM
-
Can I transfer Contacts from my Ipod Touch to my new Iphone 4s?
My Iphone is at home waiting for me right now and I cant wait to get off work!!! I lost my phone a week ago and switch to Verizon and orderd the iPhone 4s. I got my contacts from online and put them into my iPod Touch so I wouldnt lose them, but I do
-
Is there any way I can convert my AI CS3 Workspace to CS4?
Is there any way I can convert my AI CS3 Workspace settings files to CS4-compatible files?