I'm having some issues with a SQL query. I need to order the results of a select statement by street address.
I started with:
SELECT * FROM addressTable ORDER BY streetAddress ASC
but this would return
1 bob street
10 bob street
2 bob street
To rectify this, I changed my statement to:
SELECT * FROM database ORDER BY LEN(StreetAddress), StreetAddress ASC
I thought this was ideal as it was now returning:
1 bob street
2 bob street
10 bob street
However I hadn't anticipated apartment and flat numbers, which are in the format of 1a 1b 210a 210b etc, so I found my query would return like so:
210 bob street
211 bob street
212 bob street
213 bob street
210a bob street
210b bob street
Instead of:
210 bob street
210a bob street
210b bob street
211 bob street
212 bob street
213 bob street
What is the simplest query I can use to achieve this?
Try this:
ORDER BY CAST(LEFT(StreetAddress, PATINDEX('%[^0-9]%', StreetAddress) - 1) AS INT),
StreetAddress
The idea is to extract the leading number value, cast it to INT, sort by it, then sort by the field itself. It should work provided StreetAddress values are like the ones posted.
Related
I always be impressed on what I found so far on this site. This time it is my turn to have a question...
I have the following tables :
Appt :
ApptId
ResourceId
12345
abcde
12345
bcdef
23456
defgh
34567
cdefg
Resource:
ResourceId
ResourceType
abcde
Person
bcdef
Place
cdefg
Place
defgh
Person
Place :
PlaceId
ResourceId
PlaceName
zzzzz
bcdef
Boston
yyyyy
cdefg
Dallas
Person:
PersonId
ResourceId
PersonName
wwwww
abcde
Smith
xxxxx
defgh
Doe
and I try to have this :
ApptId
ResourceName
PlaceName
12345
Smith
Boston
23456
Doe
34567
Dallas
So for the moment I am able to have this :
ApptId
ResourceName
PlaceName
12345
Smith
12345
Boston
23456
Doe
34567
Dallas
But I failed to aggregate Resource and Place if they are about the same Appointment.
Hope this is clear
You likely want to have SELECT statement with two left outer joins, as follows:
SELECT Appt.ApptId, Person.PersonName, Place.PlaceName
FROM Appt
LEFT OUTER JOIN Person ON Appt.ResourceId = Person.ResourceId
LEFT OUTER JOIN Place ON Appt.ResourceId = Place.ResourceId
I have following data table
id - name - city
- 1 George Seattle
- 2 Sam Boston
- 3 John Los Angeles
- 4 Amy Milwaukee
- 5 Eric Chicago
- 6 Nick New York
here i want to consider chicago and Seattle as same city
so the order i want is
id - name - city
- 1 George Seattle
- 5 Eric Chicago
- 6 Nick New York
- 4 Amy Milwaukee
- 3 John Los Angeles
- 2 Sam Boston
so what condition i should use when using order by city Desc
You can use a case when then condition in the order by clause
select id,name, city from
yourtable
order by
(case when city=N'Chicago' then N'Seattle' else city end) desc
I'd suggest that if you need to do this often then consider using a mapping table
I have this as input data
CustomerId Customer_name PurchaseId Purchase_Date
----------------------------------------------------
1234 Robert Benson ABC123 12/07/2012
1218 Gary Thomas PP122 26/01/2013
1218 Gary Thomas PP122 28/01/2013
1234 Robert Benson ABC123 28/01/2013
1234 Robert Benson ABC123 29/01/2013
1254 Robert Sharma PML563 29/04/2012
1218 Gary Thomas PR124 06/03/2013
1234 Benson Cruiso LML123 14/07/2012
1234 Martha Cruiso FMPL123 15/07/2012
I want to sort the data on multiple columns i.e CustomerId and PurchaseId and add an order of the sorted data as
GroupID CustomerId Customer_name PurchaseId Purchase_Date
-------------------------------------------------------------
1 1218 Gary Thomas PR124 06/03/2013
1 1218 Gary Thomas PP122 26/01/2013
2 1218 Gary Thomas PP122 28/01/2013
1 1234 Robert Benson ABC123 12/07/2012
2 1234 Robert Benson ABC123 28/01/2013
2 1234 Robert Benson PP122 29/01/2013
1 1234 Benson Cruiso LML123 14/07/2012
1 1234 Martha Cruiso FMPL123 15/07/2012
1 1254 Robert Sharma PML563 27/04/2012
If I understand your question correctly, this can be achieved with a simple row_number or rank, depending on how you want your query to handle ties.
Select Row_Number() over
(partition by CustomerID, PurchaseID
order by PurchaseDate) as GroupID, *
from MyTable
This will assign sequential numbers to rows that have the same CustomerID and PurchaseID, assigning 1 to the first PurchaseDate, 2 to the next, and so on, and starting the numbering over again when it reaches a new CustomerID/PurchaseID combination. In the event of two rows with the same CustomerID, PurchaseID, and PurchaseDate, it will arbitrarily assign different numbers. If you want those cases to be treated as ties and assigned the same number, use rank or dense_rank instead of row_number.
I would like to write a trigger so that when a new record comes in, if certain columns of it contains the same content as the previous record, one particular ID column for all existing entries would increase by 1 and the new entry would be 0, and if there is no existing record, the new record is automatically assigned to have ID 0 in that column.
For example, say my existing data is like this:
FirstName LastName InvoiceID Amt
-----------------------------------
John Doe 1 $5
Bill Jane 0 $6
John Doe 0 $3
Now say I want to insert John Doe with $3.5 of invoice, then the record would automatically becomes:
FirstName LastName InvoiceID Amt
-----------------------------------
John Doe 1 $5
Bill Jane 0 $6
John Doe 2 $3
John Doe 0 $3.5
Now if I need to insert someone new, say Jane Smith for $2, it would become:
FirstName LastName InvoiceID Amt
--------------------------------------
John Doe 1 $5
Bill Jane 0 $6
John Doe 2 $3
John Doe 0 $3.5
Jane Smith 0 $2
May I know how should write such trigger in T-SQL?
Thanks!
Here's what I would do, rather than having the trigger:
create table T (TID int IDENTITY(1,1) not null,FirstName varchar(10) not null,LastName varchar(10) not null,Amt decimal(18,4) not null)
go
create view V
as
select
FirstName,
LastName,
ROW_NUMBER() OVER (PARTITION BY FirstName,LastName ORDER BY TID desc) - 1 as InvoiceID,
Amt
from
T
go
And then set up your initial data:
insert into T(FirstName,LastName,Amt) values
('John','Doe',5),
('Bill','Jane',6)
go
insert into T(FirstName,LastName,Amt) values
('John','Doe',3)
And the new John Doe row:
insert into T(FirstName,LastName,Amt) values
('John','Doe',3.5)
And finally the Jane Smith row:
insert into T(FirstName,LastName,Amt) values
('Jane','Smith',2)
And then select from the view:
FirstName LastName InvoiceID Amt
---------- ---------- -------------------- ---------------------------------------
Bill Jane 0 6.0000
Jane Smith 0 2.0000
John Doe 0 3.5000
John Doe 1 3.0000
John Doe 2 5.0000
And so we have no concern that InvoiceID might be incorrect because we never store it.
select * from V
Trying to select records that are all for the same customer, but where the address is different.
So I can later let the user choose Bob Yonkers, then choose to update all of Bob's records to a specific address. So I want to show all the available records.
Data Example:
CUSTOMER_NAME, CUSTOMER_ADDRESS
Bob Yonkers , 42 Satellite Cir
Bob Yonkers , 667 Orbit St
Bob Yonkers , 42 Satellite Cir
Bob Yonkers , 667 Orbit St
David Boom , 5959 Bush Ave
David Boom , 5959 Bush Ave
David Boom , 5959 Bush Ave
David Boom , 5959 Bush Ave
David Boom , 5959 Bush Ave
Ruby Tuesday , 123 Highway Ln Apt#1
Ruby Tuesday , 123 Highway Ln
David Boom ,5959 Bush Ave
David Boom ,5959 Bush Ave
David Boom ,5959 Bush Ave
So the query would bring back these results...
Result Example:
CUSTOMER_NAME, CUSTOMER_ADDRESS
Bob Yonkers , 42 Satellite Cir
Bob Yonkers , 667 Orbit St
Ruby Tuesday , 123 Highway Ln Apt#1
Ruby Tuesday , 123 Highway Ln
Any help would be appreciated.
SELECT *
FROM [table] t1
INNER JOIN [table] t2 ON t1.Name=t2.Name AND t1.Address<>t2.Address
This is a refinement of Joel's:
SELECT distinct t1.*
FROM [table] t1
INNER JOIN [table] t2 ON t1.Name=t2.Name AND t1.Address<>t2.Address
give this a try...
select * from (select count(customername) as ct, customername, address from table group by customername, address) t1
where t1.ct>1
This intrigued me since a friend had asked me something similar. The query below will solve the problem, albeit in-efficiently:
mysql> select DISTINCT CUSTOMER_NAME,CUSTOMER_ADDRESS from CUST_ADDR
where CUSTOMER_NAME in (select CUSTOMER_NAME from CUST_ADDR GROUP BY
CUSTOMER_NAME HAVING COUNT(DISTINCT CUSTOMER_ADDRESS) > 1 );
+---------------+----------------------+
| CUSTOMER_NAME | CUSTOMER_ADDRESS |
+---------------+----------------------+
| Bob Yonkers | 42 Satellite Cir |
| Bob Yonkers | 667 Orbit St |
| Ruby Tuesday | 123 Highway Ln Apt#1 |
| Ruby Tuesday | 123 Highway Ln |
+---------------+----------------------+
4 rows in set (0.01 sec)