Basically i'm trying to take an address field that is one column and separate it into three columns, the first having the address number, the second having the street name, and the third having if its an apt # or lot #
Address
-------------------
990 A street Apt 1B
127 B street Lot 3
So far i have this which works for the number and the street.
Select LEFT(MP.[ADDRESS],PATINDEX('%[0-9][^0-9]%', MP.[ADDRESS] )) AS Number,
LTRIM(RIGHT(MP.[ADDRESS], LEN(MP.[ADDRESS]) - PATINDEX('%[0-9][^0-9]%', MP.[ADDRESS] ))) As Street
From MPFILE mp
and it ends up like this:
Number | Street
----------------------------
990 | A street Apt 1B
127 | B Street Lot 3
Trying to get this though:
Number | Street | Apt/Lot
-----------------------------------
990 | A street | Apt 1B
127 | B street | Lot 3
Something like this will work with your example data given, but as Gordon says Addresses are hard to parse and probably should not be parsed in SQL.
WITH locations AS
(
SELECT ADDRESS,
LEN(ADDRESS) AS Len,
PATINDEX('%[0-9][^0-9]%', ADDRESS) AS NumLoc,
PATINDEX('%APT%', ADDRESS) AS APTLoc,
PATINDEX('%Lot%', ADDRESS) AS LotLoc
FROM MPFILE
), numbers AS
(
ADDRESS,
NumLoc,
Len - NumLoc AS StartAddr,
CASE WHEN APTLoc > 0 THEN AptLoc
WHEN LotLoc > 0 THEN LotLoc
ELSE Len AS EndAddr,
CASE WHEN APTLoc > 0 THEN AptLoc
WHEN LotLoc > 0 THEN LotLoc
ELSE Len AS StartApt,
Len AS EndApt
)
Select LEFT(NumLoc, MP.ADDRESS )) AS Number,
SUBSTRING(MP.ADDRESS, StartAddr, EndAddr),
SUBSTRING(MP.ADDRESS, StartApt, EndApt)
From numbers
Declare #t table ( Address varchar(200));
Insert Into #t (Address) values
('990 A street Apt 1B'),
('127 B street Lot 3'),
('127 B street Lot 345'),
('4444 Rice Street Suite 105'),
('715 South King Street #200'),
('101 Aupuni Street Room 342')
Select Left(Address,CHARINDEX(' ',Address)) as Number,
SUBSTRING(Address,CHARINDEX(' ',Address)+1, CHARINDEX('street',SUBSTRING(Address,CHARINDEX(' ',Address)+1,LEN(Address)))+LEN('street')) as Street
,Substring(Address,CHARINDEX('street',Address)+LEN('street'),LEN(Address)) as [Apt/Lot]
From #t
** Result**
Number |Street |Apt/Lot
----------------------------|-------
990 |A street |Apt 1B
127 |B street |Lot 3
127 |B street |Lot 345
4444 |Rice Street |Suite 105
715 |South King Street |#200
101 |Aupuni Street |Room 342
Related
I have the following table
Address Table
Code Company Name Type Address Line 1 Address Line 2 City State ZIP
ABC01 ABC Company B 123 Bill To 1 Bill to Apt 2 Newark NJ 12345
ABC01 ABC Company S 425 Ship To 1 Ship To Line 2 Edison NJ 44445
DEF01 DEF Bill To B 993 Bill To 1 Bill to Apt 2 Newark NJ 12345
DEF01
DEF Ship To S 456 Ship To 1 Ship To Line 2 Edison NJ 44445
Invoice Table
Invoice # Code Bill To Name Ship to Name
12345 ABC01 ABC Company ABC Company
12346 DEF01 DEF Bill To DEF Ship To
I need the following results
Invoice # Code Bill To Name Ship To Name Bill to Address Line 1 Ship to Address Line 1
12345 ABC01 Bill To 1 Ship To 1 123 Bill To 1 425 Ship To 1
12346 DEF01 DEF Bill To DEF Ship To 993 Bill to 1 456 Ship To 1
Basically I need to join the Invoice table to the address table based on the code + Company Name + Type in 2 different joins.
I am stuck with the query and cant figure how to join them together.
for answering your question you can use the following query to get the required result :
SELECT invoice.`Invoice #`,invoice.Code,
ba.`Company Name` as "Bill to Name" ,sa.`Company Name` as "Ship to Name" ,
ba.`Address Line 1` as "Bill to Address Line 1", sa.`Address Line 1`
FROM `invoice`
JOIN address as ba on ba.`Company Name` = invoice.`Bill To Name` and ba.Type = 'B'
JOIN address as sa on sa.`Company Name` = invoice.`Bill To Name` and sa.Type = 'S';
How to read read a comma delimited file in Hive version 0.13 when the data itself contains comma and the fields doesn't have quote character. example
fname,lname,country, city, addr, dob are the column names,
tom, kate, USA,CA,los angeles,34 brad street 5thfloor, Jun/23/1975
russel,smith,USA, Tx, 763, grass street, 5th floor, dallas, Jan/31/1999
first line doesn't have any columns with comma in the data
second line in the address field there are commas in the data
763, grass street, 5th floor, dallas
how to read this in hive 0.13 version
thanks
Mx
Assuming addr is the only field that might contain a comma
create external table mydata
(
fname string
,lname string
,country string
,city string
,addr string
,dob string
)
row format serde 'org.apache.hadoop.hive.serde2.RegexSerDe'
with serdeproperties ("input.regex" = "(.*?),(.*?),(.*?),(.*?),(.*),(.*)")
location '/user/hive/warehouse/mydata'
;
select * from mydata;
+--------------+--------------+----------------+-------------+--------------------------------------+-------------+
| mydata.fname | mydata.lname | mydata.country | mydata.city | mydata.addr | mydata.dob |
+--------------+--------------+----------------+-------------+--------------------------------------+-------------+
| tom | kate | USA | CA | los angeles,34 brad street 5thfloor | Jun/23/1975 |
+--------------+--------------+----------------+-------------+--------------------------------------+-------------+
| russel | smith | USA | Tx | 763, grass street, 5th floor, dallas | Jan/31/1999 |
+--------------+--------------+----------------+-------------+--------------------------------------+-------------+
Let's say there is a table of medical records. Each visit has a unique ID but is made up of several rows corresponding to various codes/services rendered for the visit.
For example, there could be 3 rows with claimID "John" for each unique procedure code "123", "456", and "789"; 15 rows for "Jane" with codes; 6 rows for "David"...
ID Code
John 123
John 456
John 789
Jane 123
Jane 456
Jane 789
Jane 321
Jane 654
David 123
David 456
David 789
David 987
I have a list of 50 unique procedure codes and want to return the entire set of claim lines (i.e. all rows of "John") where any combination of these 50 codes have been billed with another, but not with themselves ("123" with "321", but not "123" with "123"). If "123" is in my list of 50 but "456" and "789" are not, it should not return the set of "John" claims since only one code of my 50 are present. I hope this makes sense.
Positive Result Codes
123
321
987
The query should return all 5 Jane rows (123 and 321) and all 4 David rows (123 & 987).
ID Code
Jane 123
Jane 456
Jane 789
Jane 321
Jane 654
David 123
David 456
David 789
David 987
Try this code:
;WITH Visits as (
SELECT claimID,COUNT(DISTINCT Code) as CNT FROM tbl_Visits
WHERE Code in (123,123,321,987)
GROUP by claimID
HAVING COUNT(DISTINCT Code) > 1
)
SELECT * FROM tbl_Visits
WHERE claimID in (SELECT claimID FROM Visits);
I have a query that returns a list of customers and their addresses.
ID FName LName Address1 City Postcode
--------------------------------------------------------
1 James Smith 1 Bank Street London W1C 1AA
2 Sarah Jones 45 Moor Ave London SW1 1YH
3 Mary Smith 1 Bank Street London W1C 1AA
4 Sean Baker 17 White Blvd London SE3 7TH
5 Bob Patel 58B Canal St London NW2 2TT
6 Seeta Patel 58B Canal St London NW2 2TT
7 David Hound 4 Main St London E11 8AB
I'm trying to produce another query from this data that selects a list of customers who are related/living together.The criteria for this would be the same Address 1 and Postcode fields.
My question is how I can produce a query that only selects records that have at least 1 other record with matching [Address1] and [Postcode]? ie; in the above example return only records 1, 3, 5 and 6.
Select * From
Customers c JOIN
(SELECT Address1, PostCode FROM Customer GROUP BY Address1, PostCode HAVING Count(1) > 1) c2
ON c.Address1 = c2.Address1 AND c.PostCode = c2.PostCode
I have a SQL Server table with the following fields and sample data:
ID Name Address Age
23052-PF Peter Timbuktu 25
23052-D1 Jane Paris 22
23052-D2 David London 24
23050-PF Sam Beijing 22
23051-PF Nancy NYC 26
23051-D1 Carson Cali 22
23056-PF Grace LA 28
23056-D1 Smith Boston 23
23056-D2 Mark Adelaide 26
23056-D3 Hose Mexico 25
23056-D4 Mandy Victoria 24
Each ID with -PF is unique in the table.
Each ID with the -Dx is related to the same ID with the -PF.
Each ID with -PF may have 0 or more IDs with -Dx.
The maximum number of -Dx rows for a given -PF is 9.
i.e. an ID 11111-PF can have 11111-D1, 11111-D2, 11111-D3 up to 11111-D9.
Output expected for above sample data:
ID ID (without suffix) PF_Name PF_Address PF_Age D_Name D_Address D_Age
23052-PF 23052 Peter Timbuktu 25 Jane Paris 22
23052-PF 23052 Peter Timbuktu 25 David London 24
23050-PF 23050 Sam Beijing 22 NULL NULL NULL
23051-PF 23051 Nancy NYC 26 Carson Cali 22
23056-PF 23056 Grace LA 28 Smith Boston 23
23056-PF 23056 Grace LA 28 Mark Adelaide 26
23056-PF 23056 Grace LA 28 Hose Mexico 25
23056-PF 23056 Grace LA 28 Mandy Victoria 24
I need to be able to join the -PF and -Dx as above.
If a -PF has 0 Dx rows, then D_Name, D_Address and D_Age columns in the output should return NULL.
If a -PF has one or more Dx rows, then PF_Name, PF_Address and PF_Age should repeat for each row in the output and D_Name, D_Address and D_Age should contain the values from each related Dx row.
Need to use MSSQL.
Query should not use views or create additional tables.
Thanks for all your help!
select
pf.ID,
pf.IDNum,
pf.Name as PF_Name,
pf.Address as PF_Address,
pf.Age as PF_Age,
dx.Name as D_Name,
dx.Address as D_Address,
dx.Age as D_Age
from
(
select
ID, left(ID, 5) as IDNum, Name, Address, Age
from
mytable
where
right(ID, 3) = '-PF'
) pf
left outer join
(
select
ID, left(ID, 5) as IDNum, Name, Address, Age
from
mytable
where
right(ID, 3) != '-PF'
) dx
on pf.IDNum = dx.IDNum
SqlFiddle demo: http://sqlfiddle.com/#!6/dfdbb/1
SELECT t1.ID, LEFT(t1.ID,5) "ID (without Suffix)",
t1.Name "PF_Name", t1.Address "PF_Address", t1.Age "PF_Age",
t2.Name "D_Name", t2.Address "D_Address", t2.Age "D_Age"
FROM PFTable t1
LEFT JOIN PFTable t2 on LEFT(t1.ID,5) = LEFT(t2.ID,5)
WHERE RIGHT(t1.ID,2) = 'PF'