Is it possible to do this in one SQL query?
Hi there.
Please forgive me, I only have rudimentary knowledge of databases - I would just like to know if it is possible to do something. That's the most important thing I need to know. Anything additional anyone is nice enough to add regarding how to go about it would be a welcome bonus =)
As it happens, I'm sure this must have been necessary in some capacity before.
Anyway, I have two tables with a very similar attribute 'name' and 'owner name'. They aren't linked as foreign keys. I need to find examples where 'name' and 'owner' name are not the same. Unfortunately, both 'name' and 'owner name' can contain titles and/or first name and/or surname and/or any other nonsense the person completing the form cares to enter. I need a query that will not return results along the lines of:
Name Owner Name
Mr Bob Monkhouse & B Monkhouse
I need to query to classify this as the same thing, and therefore not return this result.
What I am looking to do, is retrieve entries that have the same surname. Obviously there is the disadvantage of the following being classed as the same (when, for the purpose of the query, they are not) :
Name Owner Name
Mrs Dorris Monkhouse & B Monkhouse
I just can't see anyway around that though - to change the database so that it has a First Name attribute, and Surname attribute just isn't possible.
So basically, if anyone could comment on whether it is possible to 'match' a string of characters (i.e. the surname element of 'Name' and 'Owner Name'), and write a query that will return entries with a high probability of the person in each not being the same person, I would be delighted to hear from you!
Thank you for reading
If you assume that the surname is always in the last part of the field (so you will always have "B Monkhouse", never "Monkhouse, B" and it is a varchar(2) field, then thiscan be done using a combination of INSTR (to find the ' ' (space)) and SUBSTR to clip text backwards until you find the space
I'm sure someone knows how to do this off the top of their head - I'll look it up and if no-one replies in the meantime, I'll let you know.
I'm working off a version 7 database - there may be a nattier way to do it in newer versions...
Similar Messages
-
Lookup creation and using this lookup in SQL query
I Have two tables one table called T_KEY_VALUES (KEY_ID , VALUE) and other is my transition table T_TRANSACTIONS (VERSION_ID , COL_VENDOR , COL_PREFIX, COL_RECIPTID , COL_STATE , COL_COUNTRY ..)
The data looks like below:
T_KEY_VALUES:
KEY_ID , VALUE,
10, CA
11, NY
13, NJ
20, USA
21, CANADA
101 , AMC
102, REGAL
1001, MOVIES
1002, MALLS
T_TRANSACTIONS:
VERSION_ID , COL_VENDOR , COL_PREFIX , COL_RECIPTID , COL_SATE , COL_COUNTRY
1, 101 , 1001 , 100001 , 10 , 20
2, 102 , 1002 , 100002 , 11 ,20
Generally, COL_VENDOR, COL_PREFIX , COL_STATE , COL_COUTRY field values exist in the T_KEY_VALUES table.
So How can I use T_KEY_VALUES as Lookup and write the one SQL query to get the data like below:
1, AMC , MOVIES , 100001 , CA ,USA
2, REGAL , MALLS , 100002 , NY , USAHi,
One way is to join t_transactions to 4 copies of t_key_values:
SELECT t.version_id
, v.value AS vendor
, p.value AS prefix
, t.col_reciptid
, s.value AS state
, c.value AS country
FROM t_transactions t
JOIN t_key_values v ON v.key_id = t.col_vendor
JOIN t_key_values p ON p.key_id = t.col_prefix
JOIN t_key_values s ON s.key_id = t.col_state -- or col_sate
JOIN t_key_values c ON c.key_id = t.col_country
If you'd care to post CREATE TABLE and INSERT statements for the sample data, then I could test this.
The query above assumes all 4 coded columns in t_transactions have matching values in t_key_values, as they do in the sample data. If that assumption is wrong, then use outer joins in some (or all) of the places where I used inner joins above.
Another approach is to UNPIVOT t_transactions into 4 times as many rows, do a single join to t_key_values, and then PIVOT those results back to the original number of rows. -
How to get this output using sql query?
Hi,
How to get this output using sql query?
Sno Name Age ADD Result
1 Anil 23 delhi Pass
2 Shruti 25 bangalor Pass
3 Arun 21 delhi fail
4 Sonu 23 pune Pass
5 Roji 26 hydrabad fail
6 Anil 28 delhi pass
Output
Sno Name Age ADD Result
1 Anil 23 delhi pass
28 delhi passHi Vamshi,
Your query is not pretty clear.
write the select query using Name = 'ANIL' in where condition and display the ouput using Control-break statements.
Regards,
Kannan -
How to create a Matrix table using this data in SQL Query Analyzer
Hello all,
I have a problem while I am trying to represent my Sql Table namely table1 in Matrix form
my table Format is
city1 city2 Distance--------------------------------------------------------
Mumbai Delhi 100
Delhi Banaras 50
Mumbai Rajasthan 70
Banaras haryana 40
Mumbai Mumbai 0
784 entries
there are 784 cities each having link to other
Now i want my output as
Mumbai Delhi Banaras haryana
Mumbai 0 100 -- --
Delhi 100 0 50 --
Banaras
haryana
respective distance from one city to other should be shown
final Matrix would be 784*784
I am using SQL Query Analyser for this
Please help me in this regardI'm pretty much certain that you don't want to do this in pure SQL. So that means that you want to do it with a reporting tool. I'm not familiar with SQL Query Analyzer, but if it is in fact a reporting tool you'll want to consult its documentation looking for the terms "pivot" or perhaps "cross tab."
-
How to update two different tables by ony one sql query???
Hi All,
i need to update two different talbes in a single sql query..
i m using the following query
UPDATE FT_User_Alert SET Subscription = 'W' where product_key=1 and measure_key = 12
AND
UPDATE LU_Monthly_Alert_Budget_Sheet SET Min_Red_Range ='16.0' AND Max_Green_Range ='24.0'AND Max_Red_Range ='27.0'AND Min_Green_Range ='16.0' where product_key='1' and measure_key = 12
i m getting the following error:
Odbc driver returned an error (SQLExecDirectW).
Error Details
Error Codes: OPR4ONWY:U9IM8TAC:OI2DL65P
State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 43093] An error occurred while processing the EXECUTE PHYSICAL statement. [nQSError: 17001] Oracle Error code: 936, message: ORA-00936: missing expression at OCI call OCIStmtExecute: UPDATE FT_User_Alert SET Subscription = 'W' where product_key=1 and measure_key = 12 AND UPDATE LU_Monthly_Alert_Budget_Sheet SET Min_Red_Range ='16.0' AND Max_Green_Range ='24.0'AND Max_Red_Range ='27.0'AND Min_Green_Range ='16.0' where product_key='1' and measure_key = 12 . [nQSError: 17011] SQL statement execution failed. (HY000)
SQL Issued: EXECUTE PHYSICAL CONNECTION POOL writeback UPDATE FT_User_Alert SET Subscription = 'W' where product_key=1 and measure_key = 12 AND UPDATE LU_Monthly_Alert_Budget_Sheet SET Min_Red_Range ='16.0' AND Max_Green_Range ='24.0'AND Max_Red_Range ='27.0'AND Min_Green_Range ='16.0' where product_key='1' and measure_key = 12
but when i m ushin the same query in Microsoft SQL Server it executes properly:
please help me out...There's no valid syntax for this, but there are some tricks you could do to achieve it.
i) You could place an update trigger on TABLE1 to update TABLE2 automatically.
ii) You could define a view across both tables and add an INSTEAD OF UPDATE trigger to it to maintain them.
If I had to do this I'd choose option2, but frankly I'd just be running two updates if it really was me. -
How to achieve this result using sql query?
hello gurus,
i have a table like this
id name
1 a
2 b
3 c
4 d
5 e
6 f
7 g
8 h
9 i
10 j
11 k
12 l
13 m now my result should be like this
id name id name id name
1 a 6 f 11 k
2 b 7 g 12 l
3 c 8 h 13 m
4 d 9 i
5 e 10 jhow to achieve it by sql query ?
thanks and regards,
friend
Edited by: most wanted!!!! on Feb 22, 2012 5:55 AMhi,
Did you mean this:
with a as
(select 1 id ,'a' name from dual
union all select 2 id ,'b' name from dual
union all select 3 id ,'c' name from dual
union all select 4 id ,'d' name from dual
union all select 5 id ,'e' name from dual
union all select 6 id ,'f' name from dual
union all select 7 id ,'g' name from dual
union all select 8 id ,'h' name from dual
union all select 9 id ,'i' name from dual
union all select 10 id ,'j' name from dual
union all select 11 id ,'k' name from dual
union all select 12 id ,'l' name from dual
union all select 13 id ,'m' name from dual
select
id_1
,name_1
,id_2
,name_2
,id_3
,name_3
from
select
id id_1
,name name_1
,lead(id,5) over (order by id) id_2
,lead(name,5) over (order by id) name_2
,lead(id,10) over (order by id) id_3
,lead(name,10) over (order by id) name_3
,rownum r
from
a
where
r <=5
D_1 NAME_1 ID_2 NAME_2 ID_3 NAME_3
1 a 6 f 11 k
2 b 7 g 12 l
3 c 8 h 13 m
4 d 9 i
5 e 10 j Regards,
Peter -
How to create this CSV in Sql query?
I have a order_detail table with the primary key order_detail_id and the foregin keys order_id and product_id. Also I have three other fields, A, B and C, to describe the nature of this order detail. A product_id can be existed on different order_id
but not in a single order_id (ie no duplicate product_id on a order). What I want the dataset to return is when passing the order_id, it could be single or multiple order_id, the query will give me the order_detail_id in comma seperate string if two or more
of the same product_id found on the multiple order_ids and the same value on the other three fields, A, B and C, ie I would like to group by all the order_detail_id in a single row if those conditions are met. Thanks a million.
KahluaPlease post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Learn how to follow ISO-11179 data element naming conventions and formatting rules. Temporal data should
use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect.
This is minimal polite behavior on SQL forums.
>> I have a order_detail table with the PRIMARY KEY order_detail_id and the foreign keys order_id and product_id. <<
WRONG. Order details are called weak entities in data modeling. They need a strong entity (the order) to exist. Therefore, the key has to include the order id as part of the PRIMARY KEY. There is no separate “order_detail_id” in a correct model.
>> Also I have three other fields [sic], A, B and C, to describe the nature of this order detail. A product_id can existed on different order_id but not in a single order_id (i.e. no duplicate product_id on a order). <<
Columns are not anything like fields. You need to learn basic terms. Here is what a polite person might have posted:
CREATE TABLE Orders
(order_nbr CHAR(15) NOT NULL PRIMARY KEY,
CREATE TABLE Order_Details
(order_nbr CHAR(15) NOT NULL
REFERENCES Orders(order_nbr)
ON DELETE CASCADE,
product_id CHAR(15) NOT NULL,
PRIMARY KEY (order_nbr, product_id),
order_qty INTEGER DEFAULT 1 NOT NULL
CHECK (order_qty > 0),
-- special attributes
alpha CHAR(5) NOT NULL,
beta CHAR(5) NOT NULL,
gamma CHAR(5) NOT NULL,
>> What I want the data set to return is when passing the order_id, it could be single or multiple order_id, the query will give me the order_detail_id in comma separate string if two or more of the same product_id found on the multiple order_ids and
the same value on the other three fields [sic], A, B and C, i.e. I would like to group by all the order_detail_id in a single row if those conditions are met. <<
WRONG! You want to violate First Normal Form (1NF) and do display formatting in the database! You have not ever read a book or had a class on RDBMS. CSV? No!
Now, where is the sample data? More bad Netiquette! I have done this before; you can read the details at:
http://www.tdan.com/view-perspectives/5343
It is a special case of a relational division. Since you did not post DDL, I will not post DML.
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL -
Custom Many-Many-One SQL Query
I have multiple tables having Employee data in the following format. A few of these tables have multiple records for same employee. As such I need a way to output all these records for each EMPLOYEE horizontally in Excel as mentioned below. All outer joins automatically to a cartisian product join not providing the required output. Is this possible through a query?
Table1:
EMPID EMP_NAME COUNTRY
X1 XYZ USA
X2 IUB UK
Table2:
EMPID SEQ DEPT_NAME DEPT_Location
X1 1 OJD JAPAN
X1 2 PQR US
X1 3 IJN UK
X1 4 PQR AUS
X2 1 OJG US
Table3:
EMPID PSEQ PNAME
X1 1 lskdjf
X1 2 lskdfjsl
Required Output(In EXcel):
EMPID EMP_NAME COUNTRY SEQ DEPT_NAME DEPT_Location PSEQ PNAME
X1 XYZ USA 1 OJD JAPAN 1 lskdjf
X1 XYZ USA 2 PQR US 2 lskdfjsl
X1 XYZ USA 3 IJN UK
X1 XYZ USA 4 PQR AUS
X2 IUB UK 1 OJG USCould you not simply have had a go yourself?
SQL> WITH table1 as (select 'X1' as EMPID, 'XYZ' as EMP_NAME, 'USA' as COUNTRY from dual union all
2 select 'X2','IUB', 'UK' from dual)
3 ,table2 as (select 'X1' as EMPID, 1 as SEQ, 'OJD' as DEPT_NAME, 'JAPAN' as DEPT_LOCATION from dual union all
4 select 'X1', 2, 'PQR', 'US' from dual union all
5 select 'X1', 3, 'IJN', 'UK' from dual union all
6 select 'X1', 4, 'PQR', 'AUS' from dual union all
7 select 'X2', 1, 'OJG', 'US' from dual)
8 ,table3 as (select 'X1' as EMPID, 1 as PSEQ, 'lskdjf' as PNAME FROM DUAL UNION ALL
9 select 'X1', 2, 'lskdfjsl' from dual)
10 -- end of test data
11 select '"'||t1.empid||'","'||t1.emp_name||'","'||t1.country||'",'||t2.seq||',"'||t2.dept_name||'",'||t3.pseq||',"'||t3.pname||'"'
12 from table1 t1 JOIN table2 t2 ON (t1.empid = t2.empid)
13 LEFT OUTER JOIN table3 t3 ON (t2.empid = t3.empid AND t2.seq = t3.pseq)
14 order by t1.empid, t2.seq, t3.pseq
15 /
'"'||T1.EMPID||'","'||T1.EMP_NAME||'","'||T1.COUNTRY||'",'||T2.SEQ||',"'||T2.DEPT_NAME||'",'||T3.PSEQ||',"'||T3.PNA
"X1","XYZ","USA",1,"OJD",1,"lskdjf"
"X1","XYZ","USA",2,"PQR",2,"lskdfjsl"
"X1","XYZ","USA",3,"IJN",,""
"X1","XYZ","USA",4,"PQR",,""
"X2","IUB","UK",1,"OJG",,""
SQL>
{code}
I'll leave you to put that into PL/SQL code and output it to a file using UTL_FILE. -
Want output in this way with SQL Query
I have string "GOOD".
I am in need of output Like
G
O
O
D
Thanksselect substr('abcdefghijk' ,a,1)
from (select rownum a
from dual
connect by level <= length('abcdefghijk')); -
HOW TO EXECUTE TWO "INSERT INTO" IN ONE SQL QUERY?????
i have try the following:
INSERT INTO tab VALUES('a','b');
INSERT INTO tab VALUES('c','d');
and
INSERT INTO tab VALUES('a','b');
INSERT INTO tab VALUES('c','d');
but does not work in ORACLE 10G XE
please help!!It is working fine in Oracle 10g.
SQL> desc emp_t;
Name Null? Type
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)And the script is --
insert into emp_t values(7799,'Ani','CLERK');
insert into emp_t values(2233,'Rupak','SW'); --File Name is a_a.sql
commit;And, now from command prompt --
SQL> set serveroutput on
SQL>
SQL>
SQL> set lin 1000
SQL>
SQL> select * from emp_t;
EMPNO ENAME JOB
7777 Avik CLERK
6666 prithwi CLERK
7639 Roni CLERK
7499 ALLEN SALESMAN
7521 WARD SALESMAN
7566 JONES MANAGER
7654 MARTIN SALESMAN
7698 BLAKE MANAGER
7782 CLARK MANAGER
7788 SCOTT ANALYST
7839 KING PRESIDENT
EMPNO ENAME JOB
7844 TURNER SALESMAN
7876 ADAMS CLERK
7900 BARRY CLERK
7902 FORD ANALYST
7934 MILLER CLERK
5639 Atul CLERK
17 rows selected.
SQL> @C:\Personal\RND\Oracle\Misc\a_a.sql;
1 row created.
1 row created.
Commit complete.
SQL>
SQL>
SQL> select * from emp_t;
EMPNO ENAME JOB
7799 Ani CLERK
2233 Rupak SW
7777 Avik CLERK
6666 prithwi CLERK
7639 Roni CLERK
7499 ALLEN SALESMAN
7521 WARD SALESMAN
7566 JONES MANAGER
7654 MARTIN SALESMAN
EMPNO ENAME JOB
7698 BLAKE MANAGER
7782 CLARK MANAGER
7788 SCOTT ANALYST
7839 KING PRESIDENT
7844 TURNER SALESMAN
7876 ADAMS CLERK
7900 BARRY CLERK
7902 FORD ANALYST
7934 MILLER CLERK
5639 Atul CLERK
19 rows selected.Regards.
Satyaki De. -
Getting A Count In One SQL Statement
Since I don't know how to do what I'm about to ask, I would normally create a table (tbl1) with the main dataset I want and then create a second table (tbl2) with additional data I want, join the two and update tbl1 data with tbl2 data.
However, I would like to know how to do this in one SQL statement. I somehow need to link the count sql with the main body..?
So..I have to pull data by group id. Each group id can have a certain number of active members in it. I would like to use one SQL statement to pull the group id's and also, at the same time, give me a count of each group's active membership. Both the main and count statments in the below SQL include the emp_grps table which can obviously be linked so the right groups are updated..
Not sure how to begin to reflect this in an SQL statement, so I'll just give you the wrong version to show what data elements I have. The following gives the total count by all group's in each groups individual record. Of course, as mentioned, all I want is that group's share of the membership in their individual record:
select group_id,
(select count(*)
from elig elg
join emp_grps eg
on elg.group_id = eg.group_id
where eg.rmc_code in ('CR')
and elg.elig_start_date < sysdate
and elg.elig_end_date > sysdate) mbr_count
from odw.emp_grps eg
where eg.rmc_code in ('1M')
Output from this query:
GROUP_ID MBR_COUNT
A 10,000
B 10,000
C 10,000
D 10,000
What I want to see:
GROUP_ID MBR_COUNT
A 7,000
B 1,000
C 1,500
D 500
Thanks for any assistance..Hi,
So you want one row for every distinct value of group_id, with a count that reflects just that group_id.
That sound like a job for GROUP BY group_id and the aggregate COUNT function, like this:
SELECT eg.group_id
, COUNT ( CASE
WHEN eg.rmc_code IN ('CR')
THEN 1
END
) AS mbr_count
FROM elig elg
JOIN emp_grps eg ON elg.group_id = eg.group_id
WHERE eg.rmc_code IN ('CR', '1M')
AND elg.elig_start_date < SYSDATE
AND elg.elig_end_date > SYSDATE
GROUP BY eg.group_id
HAVING COUNT ( CASE
WHEN eg.rmc_code IN ('1M')
THEN 1
END
) > 1
ORDER BY eg.group_id
;It's unclear what you're trying to do with eg.rmc_code. I'm guessing you want to count the rows where it's one value ('CR'), but only display groups that also have another value ('1M').
Of course, I can't say for sure without seeing your actual tables, or at least small test versions of them.
Post CREATE TABLE and INSERT statements (relevant columns only) for all tables involved, and the resutls you want from that sample data (if it's not the results you already posted).
Always say which version of Oracle you're using. -
How to use this sql query in oracle?
Hi all,
i am using one sql query that is
SELECT @ToDate = '2012-10-03 00:00:00.000'
select @BetweenDate = DATEADD(MM,-1,@ToDate)
select @FromDate = DATEADD(m,DATEDIFF(m,0,@BetweenDate),0)
SELECT @ToDate = DATEADD(month, ((YEAR(@BetweenDate) - 1900) * 12) + MONTH(@BetweenDate), -1)
so @todate value is = '2012-10-03 00:00:00.000'
so in @betweendate value will come 1 month before like '2012-09-03 00:00:00.000'
again in @fromdate value will come like that '2012-09-01 00:00:00.000' means first date of @betweendate
and again @todate value will come like that '2012-09-30 00:00:00.000' means last date of @betweendate
it's happening in sql and i have to use same logic in oracle also.
how to use it??
thanksdeclare
todate date:= to_date('2012-10-03 00:00:00','yyyy-mm-dd hh:mi:ss');
betwendate date := add_months(todate,-1);
for datediff / additions you can direct subtract/add two different date variables
like
datediff = betweendate -todate
dateadd := todate+1; -
Frustrated with my Mac book pro. It's 4 yrs old and I've taken it in 3 times this year for different issues. Just last week they said my Cd Rom has packed it in. As of this week, I can't connect to wifi or get any of my applications to respond. Also won't hold a charge :(. Is it possible I need a new one? I paid 2200.00 for thus one. Seems like it should last longer than that. Any help or advice would be greatly appreciated.
You would do better to ask in the Mac Forum - this is the iPad forum.
-
i am looking for a simple app to enable me to crop a face from one pic to another(fun). is it possible to do this via iPhoto or aperture?
No, you will need something more advanced, like Photoshop.
-
I have a mac mini and just purchased a moshi hdmi connector for my LED TV. The problem is, I wanted to be able to take my mouse from one screen to another, not just as a mirror image! Is it possible to do this some how?
The Apple Support Communities are an international user to user technical support forum. As a man from Mexico, Spanish is my native tongue. I do not speak English very well, however, I do write in English with the aid of the Mac OS X spelling and grammar checks. I also live in a culture perhaps very very different from your own. When offering advice in the ASC, my comments are not meant to be anything more than helpful and certainly not to be taken as insults.
If you have two displays attached, then the function to change from mirrored to extended Desktop will appear in System Prefs/Displays.
Maybe you are looking for
-
Seriously, more and more people are leaving Mozilla because of constant crashes after opening few tabs. I don't like google chrome as I am using this browser for years and the only thing I hate here is crashing. If chrome doesn't crash, why can't you
-
RS-232 speed selection in PXI-8432/4
I would like to know if it is possible to select the communication speed of the RS-232 protocol signals when using the PXI-8432/4 from a Labwindows/CVI application in order to establish a non-standard speed as, for example, 50 kbps. Thank you!
-
Regarding the Report's Default Value
Now the default value of the CrystalDecisions.CrystalReports.Engine is empty which is a problem clarified by both Luke and Don Williams. Don provided a good tips for code, referring Don's reply at Jan 10, 2014 and Jul 31, 2012, I've tested this and i
-
Mass Creation of Appraisal Documents
Hello HR Experts, I want to create appraisal documents in bulk. After an investigation I come to know that, It can be achieved using BAdi HRHAP00_DOC_PREPARE2 and just I need to create a copy of the standard program RHXHAP_APP_DOC_PREPARE_ORG. Is am
-
Disappeared application from App Store
Hi all, I recently installed an application (2do) from the App Store onto my iPhone 3GS. After synching over USB link (and answering 'yes' to the question to transfer apps from the iPhone to iTunes) the app has now disappeared from the iPhone. It doe