I need to update a table that looks like this:
No. Item Location Score Available Price some_more_text
1 Water London 0,00
1 Water Amsterdam 1 yes 1,11 alpha
1 Water Burges 1 yes 1,11 alpha
2 Honey London 0,00
2 Honey Amsterdam 5 yes 5,55 omega
2 Honey Burges 5 yes 5,55 omega
3 Spoon London 4 yes 3,33 gamma
3 Spoon Amsterdam 4 yes 3,33 gamma
3 Spoon Burges 4 yes 3,33 gamma
4 Books London 0,00
4 Books Amsterdam 1 no 2,55 alpha
4 Books Burges 1 no 2,55 alpha
5 Juice London 0,00
5 Juice Amsterdam 5 yes 5,55 beta
5 Juice Burges 5 yes 5,55 beta
...
In the end every item in Londen should have the same properties as the according item in Burges (or Amsterdam, doesn't matter).
This can't be done manually as there are so many - but I cant find a way to somehow "batch" a SQL-command to update every item with the same no.
One more problem: As it is a proprietary software I can't definitely say which language is used - but I assume it's Oracle.
You can join the table on itself in an update.
UPDATE table_X lon, table_X bur
SET lon.item=bur.item, lon.score=bur.score, etc
WHERE lon.No=bur.NO AND lon.location="London" AND bur.location="Burges";
That updates London with the data from Burges
In case it's actually SQL Server:
update lon
set lon.item = bur.item, lon.score = bur.score, etc
from table_X lon
inner join table_X bur
on lon.No = bur.NO
where lon.location='London' AND bur.location='Burges';
If you're actually using Oracle you could use a merge statement (which should work with MSSQL too with a slightly different syntax):
MERGE INTO table1
USING (
SELECT
t2.no t2_no,
t2.score t2_score,
t2.available t2_available,
t2.price t2_price,
t2.some_more_text t2_some_more_text
FROM table1 t1
JOIN table1 t2 ON t1.no = t2.no
WHERE t1.Location='London' AND t2.Location='Burges'
) ON (no = t2_no)
WHEN MATCHED THEN UPDATE
SET score = t2_score,
available = t2_available,
price = t2_price,
some_more_text = t2_some_more_text;
See the sample SQL Fiddle
Reference documentation for merge (Oracle 11g).
Related
I have 4 tables:
Teams
codTeam: 1
year: 1995
codYears: 1
codType: 1
name: FCP
points: 3
codTeam: 2
year: 1990
codYears: 1
codType: 1
name: SLB
points: 3
codTeam: 3
year: 1995
codYears: 3
codType: 2
name: BCP
points: 0
Trainers (People who train a team)
codTrainer: 1
name: Peter
street: Ghost street
cellphone: 252666337
birthdayDate: 1995-02-01
BI: 11111111
number: 121212121
codTrainer: 1
name: Pan
street: Ghost street Remade
cellphone: 253999666
birthdayDate: 1995-01-01
BI: 22222222
number: 212121212
TeamsTrainers (In which team is someone training)
codTeamTrainer: 1
codTeam: 1
codTrainer: 2
dataInicio: 1998-05-05
codTeamTrainer: 2
codTeam: 2
codTrainer: 2
dataInicio: 1998-06-07
codTeamTrainer: 3
codTeam: 2
codTrainer: 1
dataInicio: 1999-09-09
Games
codGame: 1
date: 2015-02-12 13:00:00
codTeamHome: 1
codTeamAgainst: 2
goalsHome: 3
goalsAgainst: 2
codTypeGame: 1
codGame: 2
date: 2015-02-12 15:00:00
codTeamHome: 2
codTeamAgainst: 1
goalsHome: 1
goalsAgainst: 2
codTypeGame: 3
So basically I want to:
Get the table Games and show:
Team Name | Trainer Name | Goals Home | Goals Against | Points | Ammout of Games from the Home Team
I have the following code for that in SQLQuery:
SELECT Teams.name, Trainers.name, Games.goalsHome,
Games.goalsAgainst, Teams.points, COUNT(*)
FROM Teams, Trainers, Games, TeamsTrainers
WHERE Games.codTeamHome = Teams.codTeam AND
TeamsTrainers.codTeam = Teams.codTeam AND
TeamsTrainers.codTrainer = Trainers.codTrainer
GROUP BY Teams.name, Trainers.name, Games.goalsHome,
Games.goalsAgainst, Teams.points
(May have some errors as I translated)
Yet, the COUNT only shows 1 (Probably because on the WHERE it has "teamHome" so it only counts 1), yet, if it's because of that, how do I fix it?
Result:
FCP | Pan | 3 | 2 | 3 | 1 (Count)
SLB | Peter | 1 | 2 | 3 | 1 (Count)
SLB | Pan | 1 | 2 | 3 | 1 (Count)
It should be 2 for each one on the Count
Any idea?
The reason you get wrong result is of wrong joing data type. You should use repsectivelly: left, right or inner join instead of joing data via using where clause. Your data model provides 1 to N relationship, so you should use specific type of join.
See: http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins
EDIT
SELECT Te.name, Tr.name, Ga.goalsHome, Ga.goalsAgainst, Te.points,
(SELECT COUNT(*)
FROM Games
WHERE codTeamHome = Te.codTeam OR codTeamAgainst = Te.codTeam)
AS CountOfGames
FROM TeamsTrainers AS Tt
LEFT JOIN Teams AS Te ON Tt.codTeam = Te.codTeam
LEFT JOIN Trainers AS Tr ON Tt.codTrainer = Tr.codTrainer
LEFT JOIN Games AS Ga ON Ga.codTeamHome = Te.codTeam
SQL Fiddle
You can change your WHERE clause by saying
[what you have] OR (Games.codTeamAgainst = Teams.codTeam AND ...)
However, this probably causes other problems because you probably care about whether a particular team scores the goals, not whether the home team scores the goals in games that team plays on either side.
You might not notice the other problems for a while because your GROUP BY clause is probably pretty far from what you want, and you might want to be selecting aggregate functions for a much simpler grouping.
I have limited experience with SQL and I am trying to build a query that 'automatically' uses the xyzDesc record in place of xyzID references in the result of the query.
I've included a sample of what I am looking for. It is important to keep in mind, that the recordset I am trying to produce has ~ 35 columns (where it is necessary to initially outer join 3 very large tables) where ~10 columns need to be xref as I hope will be demonstrated by the example. Additionally, the database I am querying has the underlining tables containing millions of rows.
Project table:
projectID projectDesc capitalSpend regionID statusID
-------------------------------------------------------------
1 Project A 200 1 7
2 Project B 300 1 2
3 Project C 200 1 5
4 Project D 100 2 4
5 Project E 300 2 3
6 Project F 500 3 1
7 Project G 400 3 1
StatusXref table
statusID statusDesc
------------------------
1 Proposed
2 Prelim
3 Scheduled
4 Execute
5 Completed
6 On Hold
7 Decline
RegionXref table:
regionID regionDesc
------------------------
1 New York
2 Houston
3 Los Angeles
4 Chicago
5 Denver
6 Dallas
7 Boston
Expected results when executing query:
projectID projectDesc capitalSpend Region Status
---------------------------------------------------------------
1 Project A 200 New York Decline
2 Project B 300 New York Prelim
3 Project C 200 New York Completed
4 Project D 100 Houston Execute
5 Project E 300 Houston Scheduled
6 Project F 500 Los Angeles Proposed
7 Project G 400 Los Angeles Proposed
This seems like it should be 'easy' as it would be a simple vlookup in excel but I'm reluctant to pull all the data into excel and then do these lookups as excel row limitations prevent full data.
Try something like this:
select p.projectID, p.projectDesc, p.capitalSpend, r.regionDesc, s.statusDesc
from Project p
inner join StatusXref s on p.statusID = s.statusID
inner join RegionXref r on p.regionID = r.regionID
A JOIN is exactly what you are looking for. There are several types of joins, but the most common one is an inner join.
Example query:
SELECT p.projectID, P.projectDesc, P.capitalSpend, R.regionDesc, S.statusDesc
FROM Project P
JOIN StatusXref S ON P.statusID = S.statusID
JOIN RegionXref R ON P.regionID = R.regionID;
This SQLFiddle will let you run queries on your small dataset. Be sure to use the select the correct version of SQL in the top left corner.
http://sqlfiddle.com/#!3/0ebc89
P.S. When querying on your large dataset, you may find the LIMIT clause to be useful to test and see if your query is working properly without running across the millions of rows.
How do you check if the content of a field is a multiple of 2 in oracle sql
n_id | n_content
-------------------------
1 | Balloon
2 | Drill
3 | Cup
4 | Bottle
5 | Pencil
6 | Ball
I have tried:
select*from num_w where (n_id%2>0);
and also
select*from num_w where n_id%2=0;
Neither of these worked
Wasn't this multiple of 5 a few seconds ago? :)
Anyway, in Oracle I believe the query would be:
select * from num_w where MOD(n_id,2) = 0;
I'm trying to create a select statement which will return all linked rows based on a "LINK_SEQUENCE" column. The column however, only links two rows together. A simple query to combine the two makes perfect sense to me. However, in the case that three or more links in the "chain" are present, I want to make sure that all codes are returned.
How would I go about returning something similar to this when only handing the query one of the codes involved? EX: 3245.
Not sure if it much matters in this situation, but this is for an Oracle database. Thank you all very much!
Source data from SQL Fiddle:
ID CODE LINK_SEQUENCE NAME
1 3267 1 Potato
2 3245 1 Potato
3 3245 2 Potato
4 3975 2 Potato
5 3975 3 Potato
6 5478 3 Potato
7 2368 4 Apricot
8 4748 4 Apricot
9 8957 (null) Carrot
SELECT * FROM LinkedTable lt
WHERE ft.link_sequence IN
( SELECT link_sequence FROM LinkedTable WHERE code = 3245 AND link_sequence IS NOT NULL )
ORDER BY ft.ID;
See my SQL Fiddle DEMO.
SECOND ATTEMPT:
SELECT DISTINCT *
FROM LinkedTable
START WITH code = 3245
CONNECT BY NOCYCLE
PRIOR code = code AND PRIOR link_sequence+1 = link_sequence OR
PRIOR code <> code AND PRIOR link_sequence = link_sequence
ORDER BY link_sequence, code
;
Updated SQL Fiddle with this code. Please try to break it.
Based on your data (starting with 3245) it gives the following chain:
ID CODE LINK_SEQUENCE NAME
2 3245 1 Potato
1 3267 1 Potato
3 3245 2 Potato
4 3975 2 Potato
5 3975 3 Potato
6 5478 3 Potato
I have the following 3 tables:
1) Sweetness Table
FruitIndex CountryIndex Sweetness
1 1 10
1 2 20
1 3 400
2 1 50
2 2 123
2 3 1
3 1 49
3 2 40
3 3 2
2) Fruit Name Table
FruitIndex FruitName
1 Apple
2 Orange
3 Peaches
3) Country Name Table
CountryIndex CountryName
1 UnitedStates
2 Canada
3 Mexico
I'm trying to perform a CrossTab SQL query to end up with:
Fruit\Country UnitedStates Canada Mexico
Apple 10 20 400
Orange 50 123 1
Peaches 49 40 2
The challenging part is to label the rows/columns with the relevant names from the Name tables.
I can use MS Access to design 2 queries,
create the joins the fruit/country names table with the Sweetness table
perform crosstab query
However I'm having trouble doing this in a single query. I've attempted nesting the 1st query's SQL into the 2nd, but it doesn't seem to work.
Unfortunately, my solution needs to be be wholly SQL, as it is an embedded SQL query (cannot rely on query designer in MS Access, etc.).
Any help greatly appreciated.
Prembo.
How about:
TRANSFORM First(Sweetness.Sweetness) AS FirstOfSweetness
SELECT Fruit.FruitName
FROM (Sweetness
INNER JOIN Fruit
ON Sweetness.FruitIndex = Fruit.FruitIndex)
INNER JOIN Country
ON Sweetness.CountryIndex = Country.CountryIndex
GROUP BY Fruit.FruitName
PIVOT Country.CountryName;
I hate to rely on an outside post and present it as my answer, but this is a pretty steep topic and I can't do it justice. So I suggest you look at this article.