Help writing a sql for problem - Mesed up data in my 1st post!
Sorry!!!!!!!!!!! I mess up with my first post!
Data
rec z1 z2 z3 z4 z5
6597 4 5 9 10 23
6596 2 5 7 19 22
6594 9 15 24 30 39
6593 9 13 20 27 39
6592 8 16 25 29 30
6591 2 8 13 29 35
6590 22 28 33 36 37
6589 4 5 11 26 36
6588 10 15 17 20 30
6587 4 9 12 32 39
6586 1 3 5 11 35
6585 3 8 26 30 31
6584 4 11 29 35 37
6583 12 15 33 34 38
6582 2 23 24 37 38
6581 9 14 16 25 33
6580 8 23 29 32 37
6579 14 18 20 24 34
6578 2 32 33 35 38
6577 1 16 18 19 30
6576 4 11 27 32 38
Expected Results
recz1 recz2 recz3 recz4 recz5
6589 6596 6594 6588 6582
6591 6589 6575 6595 6595
6593 6588 6582 6592 6593
6587 6591 6588 6576 6587
6591 6581 6581 6591 6588
I am trying to query the above table to take each column entry, row by row across all columns and search for the next previous occurrence and move the rec number to another column. See expected results. Row 6597 has column entries of 4, 5, 9, 10, 23. The next previous occurrence of 4 was found on row 6589 and moved to column recz1 etc. Can someone help me?
See my reply in your other thread.
Close this thread - Just edit your thread and replace the content with what you have here.
Similar Messages
-
Need help writing a query for following scenario
Hi all, I need some help writing a query for the following case:
One Table : My_Table
Row Count: App 5000
Columns of Interest: AA and BB
Scenario: AA contains some names of which BB contains the corresponding ID. Some
names are appearing more than once with different IDs. For example,
AA BB
Dummy 10
Me 20
Me 30
Me 40
You 70
Me 50
Output needed: I need to write a query that will display only all the repeating names with their corresponding IDs excluding all other records.
I would appreciate any input. ThanksIs it possible to have a records with the same values for AA and BB? Are you interested in these rows or do you only care about rows with the same value of AA and different BB?
With a slight modification of a previous posting you can only select those rows that have distinct values of BB for the same value of AA
WITH t AS (
SELECT 'me' aa, 10 bb FROM dual
UNION ALL
SELECT 'me' aa, 20 bb FROM dual
UNION ALL
SELECT 'you' aa, 30 bb FROM dual
UNION ALL
SELECT 'you' aa, 30 bb FROM dual
SELECT DISTINCT aa, bb
FROM (SELECT aa, bb, COUNT(DISTINCT bb) OVER(PARTITION BY aa) cnt FROM t)
WHERE cnt > 1; -
A little help writing an SQL please? May need some advanced functionality..
Hi All
I have arequirement to write an SQL that, given a total, and some rows from a table, can return the rows where one of the columns adds up to the total
These are transactions, so I'll boil it down to:
TranID, TranAmount
1, 100
2, 200
3, 250
4, 50
Suppose I will know that I need to seek 2 transactions totalling 300.
Possible combinations are ID 1+2, or 3+4. In this case, it is an error that I cannot find a single set of transactions that satisfies the number-of and the sum-of requirements
Suppose I will know that I need to seek 3 transactions totalling 550.
Only combination is ID 1+2+3
The difficulty for me here comes in that the number of transactions hunted will change, possibly without limits but as there will be a factorial element in terms of number of combinations, imposing an upper limit would be sensible.
I considered one solution within my understanding of SQL, that I can take my rows, and (supposing I enforce a limit of 3.. i.e. I cannot look for any more than a combination of 3 transactions) cross join them to themselves on condition that no tran ID is equal to any other tran ID, I will then get a huge block of cartesian joined transactions. I can then use a where clause to pick out row combinations having the correct sum. If I can use some kind of ID > otherID in my join, then I can make the cartesian a triangle shape which hopefully would prevent essentially duplicated rows of 1,2,3 and 1,3,2 and 3,2,1 and 3,1,2 etc
If I was only looking for 2 or 1 possible combinations, I would replace the tran amounts with 0 using a case when (because the number of times I'll cross join is fixed; I dont want to get into executing a dynamic sql)
Lastly I should point out that I'm doing this in PL/SQL or possibly Java so I have the ability to introduce custom logic if needs be
I would love to hear any other input from the wise and wizened members here as to how they might approach this problem.. Maybe oracle has some cool analytical functionality that I do not know of that will help here?
Thanks in advance guys n girls!>
Now, as I'll be looking to update another columns on these transactions so that it can be picked up by an external process, can I modify this query so that it produces a list of TranIDs so I can say:
UPDATE tran SET falg='Y' WHERE tranID IN (<the query>)
>
Well if there's a workaround for the NOCYCLE, then the following will give all the transactions as rows for a particular total...
SQL> ed
Wrote file afiedt.buf
1 with t as (select 1 as TranID, 100 as TranAmount from dual union all
2 select 2, 200 from dual union all
3 select 3, 250 from dual union all
4 select 4, 50 from dual)
5 -- end of test data
6 select distinct substr( trans
7 , decode( level, 1, 1, instr(trans,'+',1,level-1)+1)
8 , decode( instr(trans,'+',1,level), 0, length(trans), instr(trans,'+',1,level) - decode( level, 1, 0, instr(trans,'+',1,level-1))-1)
9 ) the_value
10 from (select trans
11 from (
12 select ltrim(rtrim(comb.trans,'+'),'+') as trans, sum(case when instr(comb.trans,'+'||t.tranid||'+')>0 then tranamount else null end) as sum_tran
13 from (select sys_connect_by_path(tranid, '+')||'+' as trans
14 from t
15 connect by nocycle tranid > prior tranid) comb
16 ,t
17 group by comb.trans
18 )
19 where sum_tran = 550)
20 connect by level < length(replace(translate(trans,'01234567890','00000000000'),'0')) + 2
21* order by 1
SQL> /
THE_VALUE
1
2
3
SQL> ed
Wrote file afiedt.buf
1 with t as (select 1 as TranID, 100 as TranAmount from dual union all
2 select 2, 200 from dual union all
3 select 3, 250 from dual union all
4 select 4, 50 from dual)
5 -- end of test data
6 select distinct substr( trans
7 , decode( level, 1, 1, instr(trans,'+',1,level-1)+1)
8 , decode( instr(trans,'+',1,level), 0, length(trans), instr(trans,'+',1,level) - decode( level, 1, 0, instr(trans,'+',1,level-1))-1)
9 ) the_value
10 from (select trans
11 from (
12 select ltrim(rtrim(comb.trans,'+'),'+') as trans, sum(case when instr(comb.trans,'+'||t.tranid||'+')>0 then tranamount else null end) as sum_tran
13 from (select sys_connect_by_path(tranid, '+')||'+' as trans
14 from t
15 connect by nocycle tranid > prior tranid) comb
16 ,t
17 group by comb.trans
18 )
19 where sum_tran = 300)
20 connect by level < length(replace(translate(trans,'01234567890','00000000000'),'0')) + 2
21* order by 1
SQL> /
THE_VALUE
1
2
3
4
SQL> -
Need help writing startupItem/launchd for dansguardian
Hey there,
i want to write a startupitem for dansguardian (or should i use launchd?).
i am quite new to this stuff, but thinking positive! the problem is i don't really know which file exactly starts dansguardian. there is a unix-executable in usr/local/sbin/dansguardian which i can use to start dansguardian through
sudo /usr/local/sbin/dansguardian
in terminal. without sudo it won't work, i then get a message saying
vierstein$ /opt/local/sbin/dansguardian; exit
Unable to setgid()
logout
i can manage dansguardian through a webinterface called webmin and i can easily start and stop it there through a link.
i did not figure out yet how the dansguardian webmin module starts/stops it and which files/commands it uses to do that..
how can i write a startup script for dansguardian, because doing it manually is a bit laborious. i am a bit desperate here, have been trying so many things during the last two days and i'm really stuck. hopefully somebody here knows a way to do this.
the following code I put in a file called DansGuardian in /Library/StartupItems/Dansguardian - the file is a unix-executable
#! /bin/sh
# DansGuardian Filter
. /etc/rc.common
StartService ()
if [ "${DGSQUID:=-NO-}" = "-YES-" ]; then
CheckForNetwork
if [ "${NETWORKUP}" = "-NO-" ]; then exit; fi
ConsoleMessage "Starting DansGuardian Content Filter"
/usr/local/sbin/dansguardian -s
fi
StopService ()
/usr/local/sbin/dansguardian -q
RestartService ()
/usr/local/sbin/dansguardian -r
RunService "$1"
my StartupParameters.plist file in the same folder says
Description = "DansGuardian Content Filter";
Provides = ("Content Filter");
Requires = ("Web Proxy");
OrderPreference = "None";
after rebooting i get this in console
SystemStarter[55]: The following StartupItems were not attempted due to failure of a required service:
SystemStarter[55]: /Library/StartupItems/DansGuardian
it would be great if somebody here knew how to do this rightin case somebody else is looking for an answer on this, here is the working launchd script. i did not write it myself though..
put the following code in
/Library/LaunchDaemons/org.dansguardian.dansguardian.plist
change the "Program" string to point to your binary, and the "WatchPaths" as well. The way this one works is that it waits until Squid is launched before it starts. That's what the WatchPath does - it looks for Squid's PID file to be created, and then launches DansGuardian.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.dansguardian.dansguardian</string>
<key>Disabled</key>
<false/>
<key>UserName</key>
<string>root</string>
<key>GroupName</key>
<string>nogroup</string>
<key>Program</key>
<string>/opt/dansguardian/sbin/dansguardian</string>
<key>ProgramArguments</key>
<array>
<string>dansguardian</string>
<string>-N</string>
</array>
<key>RunAtLoad</key>
<false/>
<key>OnDemand</key>
<false/>
<key>WatchPaths</key>
<array>
<string>/opt/squid/var/logs/squid.pid</string>
</array>
<key>Umask</key>
<integer>027</integer>
<key>ServiceDescription</key>
<string>The DansGuardian Content Filter Daemon</string>
</dict>
</plist> -
Can anyone help me with sql for this issue?
I need to get the data from Last year starting Date From 01-Jan-2012 to current date i.e. 27th June 2013... Upper limit I can have as SYSDATE. Can some one help me how to get the start date for last year?
SELECT TRUNC ( (TRUNC (SYSDATE, 'YYYY') - 1), 'YYYY') FROM DUAL;
can be used.
Cheers,
Manik. -
Need help writing a code for this program
Design a code that reads a sentence and prints each individual word on a different line
tsith's suggestion is excellent. Run it and see what happens. I just did that myself and doing so I learned something new about the Scanner class.
Before you run it, add a couple of extra System.out.println for (hopefully) illustrating debugging purposes:
import java.lang.String;
import java.util.Scanner;
public class Sentence {
// main line of program
public static void main(String[] args) {
boolean z;
String a;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter sentence: ");
z = keyboard.hasNext();
while (z) {
System.out.println("z = " + z + ". Fetching the next word...");
a = keyboard.next();
System.out.println(a);
System.out.println("Done!");
}Let us know if the program ever prints "Done!" to the console. While you are waiting, read what the javadocs has to say about the Scanner class, and pay extra attention to what it says about the hasNext() and next() methods:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html -
Need help writing correct code for nested button
This is the code on the main timeline.
stop();
garage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler(event:MouseEvent):void {
gotoAndPlay("open");
The movieClip gDoor is nested inside of garage and "open" is a label on that timeline. I know the gotoAndPlay("open"); isn't correct. How would i target the label on the nested timeline.
the link below is to dowload the fla
clienttestsite.x10.mx/beta/garage.flaThe problem is that you have an event listener assigned to the garage, which is blocking/overriding interaction with anything inside of it. When you click on the chest's button, you are still clicking on the garage, which does the gotoAndPlay("open")... where it stops at the stop();
One way to get around this is to assign an instance name to the door and assign the opening code to that....
stop();
garage.door.addEventListener(MouseEvent.CLICK, openHandler);
function openHandler(event:MouseEvent):void{
garage.play();
garage.chest.closeBtn.addEventListener(MouseEvent.CLICK, closeHandler);
function closeHandler(event:MouseEvent):void{
garage.gotoAndPlay("close");
Notes: as shown in the code above...
- use CLICK instead of MOUSE_DOWN.
- in the openHandler use play() instead of gotoAndPlay("open"). You are already in the "open" frame, and it is only because the stop() is used up that it actuallys works.
Doing what I explain above also makes it so that you can click the door to close things as well. -
SQL: Having problems passing a date value to dynamic SQL
SQL Version 2008 (not R2)
Problem: 1) I'm getting the following error message when trying to run the sql code below.
SQL "Messages" Returned when running code:
Msg 206, Level 16,
State 2, Procedure uspxc_WAWP_10_Get_VPData, Line 0
Operand type clash: int is incompatible with date
Loaded VP DATA for Month: 20121201
Problem Code Line:
SET @dynamicSQL = 'EXEC ' + @StoredProcedureName + ' ' + @DEVModeYN + ', ' + CAST(@WIPMonth as varchar);
Any help would be greatly appreciated. I've spent several hours trying to get around this error.
Full Code:
ALTER PROCEDURE [dbo].[uspxi_XIUD_98_DataConversionGET_VP_WIPJobHistory] (@DEVModeYN nchar(1) = 'D', @WIPMonth Date)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @returnCode AS INT ,
@dynamicSQL AS VARCHAR(8000),
@msg as varchar(60),
@DEVModeYN nchar(1) = 'D',
@WIPMonth date = '20121201',
@StoredProcedureName AS VARCHAR(60)
SET @returnCode = 0
SET @StoredProcedureName = 'uspxc_WAWP_10_Get_VPData'
-- Check to see if @StoredProcedureName exists in the database.
IF EXISTS(SELECT name FROM sys.procedures WHERE name = @StoredProcedureName)
BEGIN
-- RUN SP to Import VP Data for each WIPMonth parameter value
SET @dynamicSQL = 'EXEC ' + @StoredProcedureName + ' ' + @DEVModeYN + ', ' + CAST(@WIPMonth as varchar);
SELECT @dynamicSQL;
-- RUN stored procedure for WIP Month
EXEC (@dynamicSQL);
SET @returnCode = 0;
SELECT @returnCode;
SET @msg = 'Loaded VP DATA for Month: ' + @WIPMonth;
PRINT @msg;
GoTo SPEND
END
ELSE
SET @returnCode = 1;
SET @msg = 'NO DATA IMPORTED for Month: ' + CAST(@WIPMonth as varchar(10))
PRINT @msg
SPEND:
END
Bob SutorWhen you work with dynamic SQL, you should never build a full SQL string by concatenating values, because this is more difficult to get right and also has problems with SQL injection and efficient use of the SQL Server cache.
So the correct way to do this in dynamic SQL would be:
SET @dynamicSQL = 'EXEC ' + quotename(@StoredProcedureName) '@DEVModeYN, @WIPMonth';
EXEC sp_executesql @SQL, N'DEVModeYN nchar(1), @WIPMonth date',
@DEVModeYN, @WIPMonth
A lot easier and cleaner! (Note that the variable @SQL must be declared as nvarchar.)
...however, in this particular case, this is still an overkill, and there is no need for dynamic SQL at all. EXEC accepts a variable for the procedure name, so:
EXEC @StoredProcedureName @DEVModeYN, @WIPMonth
...but it does not stop there. You can make all these changes, but you will get the same error. To wit, you get the error on Line 0, which means that it is the call to the procedure that fails. Apparently, you are passing an integer value, when you were
supposed to pass a date. Maybe you forgot to put the dates in quotes?
Erland Sommarskog, SQL Server MVP, [email protected] -
Need help about MS SQL Gate with Mess Code Data
My Database is Oracle9i, and select * from nls_database_parameters with fellow table params
PARAMETER VALUE
1 NLS_CSMIG_SCHEMA_VERSION 3
2 NLS_NCHAR_CHARACTERSET AL16UTF16
3 NLS_LANGUAGE AMERICAN
4 NLS_TERRITORY AMERICA
5 NLS_CURRENCY $
6 NLS_ISO_CURRENCY AMERICA
7 NLS_NUMERIC_CHARACTERS .,
8 NLS_CHARACTERSET UTF8
9 NLS_CALENDAR GREGORIAN
10 NLS_DATE_FORMAT DD-MON-RR
11 NLS_DATE_LANGUAGE AMERICAN
12 NLS_SORT BINARY
13 NLS_TIME_FORMAT HH.MI.SSXFF AM
14 NLS_TIMESTAMP_FORMAT DD-MON-RR HH.MI.SSXFF AM
15 NLS_TIME_TZ_FORMAT HH.MI.SSXFF AM TZH:TZM
16 NLS_TIMESTAMP_TZ_FORMAT DD-MON-RR HH.MI.SSXFF AM TZH:TZM
17 NLS_DUAL_CURRENCY $
18 NLS_COMP BINARY
19 NLS_LENGTH_SEMANTICS BYTE
20 NLS_NCHAR_CONV_EXCP FALSE
21 NLS_RDBMS_VERSION 9.2.0.5.0
22 NLS_SAVED_NCHAR_CS WE8ISO8859P1
and confing access ms sql with oracle gate by ODBC, and the ms sql characterset with Chinese_PRC_CS_AI, Chinese_PRC_CS_AS.
my pc NLS_LANG=AMERICAN_AMERICA.ZHS16GBK.
when i query oracle database chinese data is ok, but when access the chinese data in ms sql will be ????(mess code data)?
How can i confing to access mssql chinese data be normal format, pls ?
Edited by: Rain.zhang on 2008-10-8 下午3:15hi llturro,
Thanks for answering my question. I have tried out what you suggested, but I still can't fix my problem. The following are the messages that display when I try to run my servlet. It seems that the DataSource, Connection and Statement are ok, however when it comes to the ResultSet the error message appear. What's wrong with my ResultSet coding ?
ResultSet rs = stat.executeQuery("SELECT ISBN FROM BOOKSINFO WHERE ISBN='"+primarykey+"'");
setEntityContext Method
Find by primary key
DataSource OK
Connection OK
Primary Key = 013-00-675721-9
Statement OK
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.RemoteException: Unknown Exception/Error thrown by EJB method.; nested exception is:
java.lang.NullPointerException
java.rmi.RemoteException: Unknown Exception/Error thrown by EJB method.; nested exception is:
java.lang.NullPointerException
java.lang.NullPointerException
<<no stack trace available>> -
Connection of jsp with sql for retreval of input data
Sir,
i am a beginner.
i want to know how to connect jsp with the sql, and retrieve the values from the database.
could u please help me.
its urgent..
thank youThis has been asked many times so try using the search bar to the left of this page
If you are using Tomcat, there is a tutorial within the documentation
http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html -
SQL Developer - problems with the Data Modeller part
Hi SQL Developer users,
I can open the Data Modeler pane, but I can't insert any object into the logical model or into the relational model. Any ideas about what I'm doing wrong? Do I need specific privileges to the database, or do I need to install SQL Developer in a different way?
Thanks,
CsiszHi Csisz,
as Ivaylo wrote - it's Data Modeler Viewer into SQL Developer.
I can't insert any object into the logical model or into the relational modelYou can drag tables from SQL Developer browser and drop them on relational model diagram. If you keep Ctrl key pressed then all child tables also will be imported and shown on diagram.
Philip -
Sql loader problem with point data
I am struggling to import bulk point data.
Example line from data file
2 1 Highest mountain on Skye with steep faces and narrow ridges. 4 32 3001 8307 57.206116 -6.223032 992
So its all tab deliminated
3001 is sdo_gtype
8307 is sdo_srid
57.xxx is my X point
-6.xxx is my Y point
992 is my Z point
I can import all of the data apart from the final section, and my loader looks like:
GEOM COLUMN OBJECT
SDO_GTYPE INTEGER EXTERNAL TERMINATED BY X'09' ,
SDO_SRID INTEGER EXTERNAL TERMINATED BY X'09',
SDO_POINT_TYPE(X,Y,Z) INTEGER EXTERNAL TERMINATED BY ','
Any help? I simply need it take the last three numbers and assign them to x,y,z of point_type.
Need any more info?TSEdinburgh,
Try
LOAD DATA INFILE * APPEND INTO TABLE MyTable
FIELDS TERMINATED BY X'09' TRAILING NULLCOLS
MyCol1,
MyCol2,
description,
MyCol3,
MyCol4,
geom COLUMN OBJECT (
sdo_gtype INTEGER EXTERNAL,
sdo_srid INTEGER EXTERNAL,
sdo_point COLUMN OBJECT (
x FLOAT EXTERNAL,
y FLOAT EXTERNAL,
z FLOAT EXTERNAL
)Regards,
Noel -
SQL*Loader Problem while loading date from earlier era
Hi everybody,
I'm trying to load a date with SQL*Loader in Oracle 8.1.6. The value of the date is '11671102004338' with a date format 'YYYYMMDDHH24MISS'. Everytime I try it, I get the error message "ORA-01438: Wert größer als angegebene Stellenzahl für diese Spalte zuläßt" (meens: value is larger than the maximum length of the column). This is a little bit crazy, because the same works in SQL*Plus with the to_date function.
What I've to do?
Thanks in advance
RalfSorry, it's solved (I've to respect to correct type)
-
NEED HELP IN SQL HOMEWORK PROBLEMS
I NEED HELP IN MY SQL HOMEWORK PROBLEMS....
I CAN SEND IT VIA EMAIL ATTACHMENT IN MSWORD....Try this:
SELECT SUBSTR( TN,
DECODE(LEVEL, 1, 1, INSTR(TN, '#', 1, LEVEL-1) + 1),
DECODE( INSTR(TN, '#', 1, LEVEL) , 0 ,
LENGTH(TN) + 1, INSTR(TN, '#', 1, LEVEL) )
- DECODE(LEVEL, 1, 1, INSTR(TN, '#', 1, LEVEL-1 ) + 1)
) xxx
FROM (
SELECT '234123#1254343#909823#908232#12345' TN FROM DUAL
CONNECT BY LEVEL <= LENGTH(TN) - LENGTH(REPLACE(TN,'#')) + 1
XXX
234123
1254343
909823
908232
12345
SELECT regexp_substr(tn, '[^#]+', 1, level) xx
FROM (
SELECT '234123#1254343#909823#908232#12345' TN FROM DUAL
CONNECT BY LEVEL <= LENGTH(TN) - LENGTH(REPLACE(TN,'#')) + 1
XX
234123
1254343
909823
908232
12345 -
Need Sql for terda data database
Can any one help send me the sql for teradata database. for creating the variables. I need to create variables for Last month begin date and last month end date.
I am trying this tera data Sql this is for curent month date.
select cast(current_date as date) - (extract (day from cast(current_date as date)) - 1) + interval '1' month - 1
I need tera data sql for Last month begin date and last month end date. I searched various forums but could not get the answer. Any suggesstions please.
Maybe you are looking for
-
Comparing date with time stamp
Hii, I am trying to write a query where i have to pick all the record which have same date even thought the time stamp is different. '2008-06-23 14:19:23.060941' '2008-06-23 14:30:03.647688' I have to pick records based on the 2008-06-23. i tried sel
-
Email review creates workflows file size that makes Acrobat 9 unbearably slow.
I have been using the email review features in Acrobat pro version 9.3.1. All was going fine until, apparently, my workflows file got to around 1.5mb. If I understand correctly this is the file that Review Tracker uses to track PDF emailed for review
-
Why is the Zen touch so hard to find? None of my local reatilers have it, i.e. Best Buy, Circuit City, CompUSA. CompUSA did have it, but when I went back to go purchase it, they did not stock it any more. Then I tried Amazon, and they didn't have any
-
Hello fellow developers! I am having some problems with my SPRY Accordion. I have ruined the default CSS file for the Spry Accordion (spryAccordion.css) I have changed the settings and now made a mess. The problem seems to be with Mozilla Firefox mos
-
Is anyone aware of the features of SAP LUMIRA 1.18?
I need a help in understanding the upcoming features of SAP LUMIRA 1.18.