I have a table called TempAllAddresses with the following columns - ID , Address , State . I want to populate a new table Address , State and Count . Count should represent how many records are in the TempAllAddresses table that have an address similar to that address, followed by a wildcard. If that doesn't make sense, here is an example to illustrate - Let's say I have an entry like this:
ID Address State 12345 13 Phoenix NY
What I want to do is insert a new record into a new table called AddressCount , which has 13 Phoenix for Address , NY for State , as well as the number of records in the table that have NY, state and LIKE address '13 Phoenix% 'for Count .
I want to accomplish this with the TempAllAddresses inner join on myself. This is what I tried, but it doesn't seem to fulfill what I'm looking for:
SELECT t1.Address, t1.State, COUNT(t2.address) As NumEntities FROM TempAllAddresses t1 INNER JOIN TempAllAddresses t2 ON t1.state = t2.state AND T2.Address LIKE t1.address + '%' GROUP BY t1.State, t1.Address
The graph is definitely off. It should be equivalent to running " SELECT COUNT(*) FROM TempAllAddresses WHERE State=thisRecordsState and Address LIKE thisRecordsAddress + '%' ". How can i do this? What am I doing wrong?
Edit:
The account seems to be turned off as follows: If I have a record, as I mentioned above, then I have two more records that also have New York State and then have the addresses β13 Phoenix Roadβ and β13 Phoenix Rd ", then I want to get into the final record this entry:
13 Phoenix NY 3
Instead, I seem to get:
13 Phoenix NY 9
I'm not quite sure what's going on here ... some kind of Cartesian product? Permutations ...? Can anyone explain this?
Edit 2: Another edit, as I seem to have misunderstood (and really need a solution: () ... Here is a query with a correlated subquery that fulfills what I'm looking for. I would like to do the same with the inner join table to yourself, not a subtitle.
SELECT Address, State, (SELECT Count(*) FROM TempAllAddresses innerQry WHERE innerQry.address LIKE outerQry.address + '%' AND innerQry.state = outerQry.state) As NumEntities FROM TempAllAddresses outerQry
Basically, for each record I want to get the number of records in the table that have the same state and the address that starts with this address (or equal to ... I want to include this address as part of the number).