I have tried looking up this issue but maybe I'm just not searching correctly.
I was wondering if its possible in SQL to concatenate based off the results of another column in the same row.
Let me give an example
+-------------+------------+--------+
| STATE | CITY | Region |
+-------------+------------+--------+
| Georgia | Atlanta | East |
| Los Angeles | California | West |
+-------------+------------+--------+
Lets say I have this table and I want to make a new column in it by doing some kind of concatenation
The output should look like this
+-----------+------------+-------------+--------+
| SIDE | STATE | CITY | Region |
+-----------+------------+-------------+--------+
| East_Side | Georgia | Atlanta | East |
| West_Side | California | Los Angeles | West |
+-----------+------------+-------------+--------+
So the Side column should look into region and ask What Region is it? And then say ok so this is "?_Side" Then concatenate the appropriate "Region_Side" = "East_Side"
This is just a simple example, I know in this example I could use a case statement but in my actual application I can't since its a lot of "Regions"
Is this possible or no?
Does this do what you want?
select Region || '_Side', state, city, region
from t;
|| is the string concatenation operator in Oracle (and in standard SQL as well).
Just use column and concat the string
select region ||'_Side', STATE , CITY , Region
from my_table
Related
Product: Sybase ASE 11/12/15/16
I am looking to update a Stored Procedure that gets called by different applications, so changing the application(s) isn't an option. What is needed is best explained in examples:
Current results:
type | breed | name
------------------------------------
dog | german shepherd | Bernie
dog | german shepherd | James
dog | husky | Laura
cat | british blue | Mr Fluffles
cat | other | Laserchild
cat | british blue | Sleepy head
fish | goldfish | Goldie
What I need is for the First column's data to be cleared on duplicates. For example, the above data should look like:
type | breed | name
------------------------------------
dog | german shepherd | Bernie
| german shepherd | James
| husky | Laura
cat | british blue | Mr Fluffles
| other | Laserchild
| british blue | Sleepy head
fish | goldfish | Goldie
I know I can do a cursor, but there are around 10,000 records and that doesn't seem proficient. Looking for a select command, don't want to change the data in the database.
After mulling over this, I found a solution that would work and not use a cursor.
select Type,breed,name
into #DontDisplay
from #MyDataList as a1
group by breed
Having breed= (select max(name)
from #MyDataList a2
where a1.breed= a2.breed)
order by breed, name
select n.Type,d.Breed,d.Name
from #MyDataList as d
left join #DontDisplay as n
on d.Breed= n.Breed and d.Name= n.Name
order by Breed
Works great and the solution was based on another solution Sybase SQL Select Distinct Based on Multiple Columns with an ID
(Using PL/SQL anonymous program block)
I have a table tblROUTE2 of Mexican state highways:
+-----------------+------------+---------+----------+----------+----------------------------+-----------+--------+
| TYPE | ADMN_CLASS | TOLL_RD | RTE_NUM1 | RTE_NUM2 | STATEROUTE | LENGTH_KM | STATE |
+-----------------+------------+---------+----------+----------+----------------------------+-----------+--------+
| Paved Undivided | Federal | N | 81 | | Tamaulipas Federal Hwy 81 | 124.551 | NULL |
| Paved Undivided | Federal | N | 130 | | Hidalgo Federal Hwy 130 | 76.347 | NULL |
| Paved Undivided | Federal | N | 130 | | Mexico Federal Hwy 130 | 68.028 | NULL |
+-----------------+------------+---------+----------+----------+----------------------------+-----------+--------+
and tblSTATE2 of Mexican states:
+------+-----------------------+---------+-----------+
| CODE | NAME | POP1990 | AREA_SQMI |
+------+-----------------------+---------+-----------+
| MX02 | Baja California Norte | 1660855 | 28002.325 |
| MX03 | Baja California Sur | 317764 | 27898.191 |
| MX18 | Nayarit | 824643 | 10547.762 |
+------+-----------------------+---------+-----------+
I need to update the STATE field in tblROUTE2 with the CODE field found in tblSTATE2, based on the route name in tblROUTE2. Basically, I need to somehow take the first string or two (some routes have two names)-- before the string 'Federal'-- of the STATEROUTE field in tblROUTE2 and make sure it matches with the string in the NAME field in tblSTATE2. Then since the states are matched with a CODE, update those codes in the STATE field of tblROUTE2.
I have started a code:
DECLARE
state_code
tblROUTE2.STATE%TYPE;
state_name
tblSTATE2.NAME%TYPE;
BEGIN
SELECT STATE, NAME
INTO state_code
FROM tblROUTE2 r, tblSTATE2 s
WHERE STATEROUTE LIKE '%Federal';
END;
As well, I will need to remove the state name from the route name. For example, the string in STATEROUTE 'Tamaulipas Federal Hwy' becomes 'Federal Hwy'. I have started a code, not sure if it's right:
UPDATE tblROUTE2
SET STATEROUTE = TRIM(LEADING FROM 'Federal');
Using MERGE update :
MERGE INTO tblROUTE2 A
USING
(
SELECT CODE, NAME FROM tblSTATE2
) B
ON
(
upper(SUBSTR(A.STATEROUTE, 0, INSTR(UPPER(A.STATEROUTE), UPPER('FEDERAL'))-2)) = upper(B.NAME)
)
WHEN MATCHED THEN UPDATE
SET A.STATE = B.CODE;
Here in FIDDLE I've replicated your tables and added additional record where STATEROUTE matches one of the records in NAME. Although Fiddle return an error, I ran it in my Oracle DB, and one record was updated correctly as the following screenshot:
How can I get all the countries from the DB, from this table:
city | country | info
Jerusalem | Israel | Capital
Tel Aviv | Israel |
New York | USA | Biggest
Washington DC | USA | Capital
Berlin | Germany | Capital
How can I get, using SQL, the countries only: Israel, USA, Germany?
Which database server are you using?
Assuming that the top row is the column name and you are using MySQL then you should be able to just do
"SELECT distinct(country) FROM <table-name>;"
This is probably in the documentation for the database software that you are using.
So I have this table
City | Status | District | Revenue
------------------------------------------
Oakland | Executed | North | $9.50
Los Angeles| Cancelled| South | $0.05
Oakland | Executed | North | $0.99
Oakland | Cancelled| North | $98.40
Sacramento | Executed | North | $43.50
Sacramento | Cancelled| North | $5.40
Los Angeles| Cancelled| South | $5.30
So I need this report that reads like this:
North District | Executed | Cancelled | Revenue
--------------------------------------------------------
Oakland | 2 | 1 | Sum of revenue
Sacramento | 1 | 1 | Sum of revenue
--------------------------------------------------------
South District | Executed | Cancelled | Revenue
--------------------------------------------------------
Los Angeles | 0 | 2 | Sum of revenue
But I'm stuck on how to create a query that groups and counts instances of specific values inside that group.
I mean I know syntax of group statements and count statements, but the counting a specific number of instances of a row inside a group seems pretty different than a regular count.
Can anyone guide me in the right direction? I'm not asking anyone to do my work (this isn't even a full sample of what I have to do) but if someone can help me with a statement that groups and counts specific rows in the group, with a SQL statement or an Access function, that would be awesome. From there I'd be able to figure out everything else.
Hey I ran across an answer actually. I just had to use Sum(IIF()) and it worked correctly.
SELECT
Test.City,
=Sum(IIf(Status="Cancelled",1,0))
FROM Test
Group BY Test.City
Ok, the problem looks like this. I have a set of n database rows with empty column position. I need to use address data (different combinations etc. - it doesn't matter) from that set (from 3 separated columns) to compare them with the another set of m elements (also from database, which contain address data and needed location).
Because these sets are very large (about milion records, and the operation is executed quite often), I need some pretty fast algorithm to compare these two sets and find the data I need.
I tried to find something, but I have no idea if it's any well-known mathematical problem (in graph theory maybe?).
[edit]
The structures are too large to describe them here. But I will make an example for that.
Set 1.
|[ID] | [CITY] | [STREET] | [POSTCODE] | [LOCATION] |
|-----|--------|----------|------------|------------|
| 1 | City1 | Street1 | 00000 | NULL |
| 2 | City2 | Street2 | 11111 | NULL |
| 3 | City3 | Street3 | 22222 | NULL |
Set 2.
|[ID] | [SOME_KIND_OF_ADDRESS] | [LOCATION] |
|-----|-------------------------------------|------------|
| 1 | Street 1 in City 1, 00000 blah blah | SOME_XY1 |
| 2 | Street 2 in City 1, 00001 blah blah | SOME_XY2 |
| 3 | Street 2 in City 2, 11111 blah blah | SOME_XY3 |
| 4 | Street 1 in City 4, 33333 blah blah | SOME_XY4 |
Now for each element in Set 1, I want to try to find something in Set 2. In this case only City2, Street2 and City1, Street1 will be matched. So the result will look like:
|[ID] | [CITY] | [STREET] | [POSTCODE] | [LOCATION] |
|-----|--------|----------|------------|------------|
| 1 | City1 | Street1 | 00000 | SOME_XY1 |
| 2 | City2 | Street2 | 11111 | SOME_XY3 |
The proper way to do this is to parse the addresses in set 2 and then create indexes on each of the fields. Then your comparisons will be really fast.
Absent that, what are your options? Well, you basically have to scan all of the addresses in set 2 for the comparisons. Some SQL engines optimize comparisons at the beginning of the string (using indexes), so one comparison could use an index. If you have a function to extract the street/city/postcode, then some databases can support "functional" indexes where the elements are t the results of function calls.
Another option is full text search. This would let you search for the components, using a structure called an inverted index.
However, my advice is to fix the addresses and extract the pieces that you want for comparison. Address rectification/standardization, although neither cheap nor fast, usually pays for itself in the medium term by greatly simplifying requests such as this.
i'd use the following algorithm:
Sort tables A,B
Create 2 pointers (ptrA, ptrB) at the beginning of the tables
While (ptrA has not ended and ptrB has not ended)
{
if (ptrA->value=ptrB->value) update column position
if (ptrA->value>ptrB->value) move prtB forward
else move ptrA forward
}