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
Related
users
id
first_name
last_name
email
1
John
Doe
john.doe#john.doe.com
2
Mary
Doe
mary.doe#mary.doe.com
user_details
id
user_id
ethnicity_id
education_id
location_id
1
John
1
1
1
2
Mary
4
3
2
locations
id
location
lat
long
1
London, UK
53.2321
-1.2321
2
Berlin, Germany
52.2321
-1.5345
I have a dataset which is of following nature. I would like to replace the names in "MAKE" column if the column contains "PQR" per unique country.
country MAKE
1 USA PQR
2 USA ABC
3 UK PQR
4 UK DEF
5 JPN DEF
6 JPN LMN
Desired Output:
country MAKE
1 USA PQR
2 USA PQR
3 UK PQR
4 UK PQR
5 JPN OTHERS
5 JPN OTHERS
One option is conditional aggregation with analytic functions:
SELECT
country,
CASE WHEN SUM(CASE WHEN MAKE = 'PQR' THEN 1 ELSE 0 END) OVER (PARTITION BY country) > 0
THEN 'PQR' ELSE 'OTHERS' END AS MAKE
FROM yourTable
ORDER BY
country;
Demo
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'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.
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)