pl sql. 0 rows merged - sql

I am new to merge so this question may appear too dumb.
I'm running
MERGE INTO regions r
USING regions_temp t
ON (r.id_region = t.id_region)
WHEN NOT MATCHED THEN INSERT (r.id_region, r.region_name)
VALUES (t.id_region, t.region_name);
Before I run the command the tables have following records (id_region, region_name):
Regions:
1 1 Kiev
2 2 Asia
3 3 Australia
4 4 South America
5 5 Africa
Regions_temp:
1 1
2 2 Asia
3 3 Australia
4 4 South America
The command runs successfully but on the bottom of the window it says 0 rows merged and no records are merged.
Little help?

MERGE INTO regions r
USING regions_temp t
ON (r.id_region = t.id_region)
WHEN MATCHED THEN update set r.region_name = t.region_name
is what i actually needed

According to the merge_insert_clause documentation:
The merge_insert_clause specifies values to insert into the column of the target table if the condition of the ON clause is false.
Since there aren't records in Regions_temp whose id_region doesn't match a record in Regions, there are no rows to be merged.

Related

SQL update from same table / multiple lists and items

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).

SQL Server Cross Reference Query

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.

SPSS Chi-square test on raw data

I want to run a chi square test on my raw data but SPSS is not reading the raw data as I want it to.
The raw data summarizes several test conditions, locations, groups of participants and results in one file. Example data:
# country gender Question 1 Question 2
1 chile f yes no
2 chile f no no
3 chile m no yes
4 chile m yes no
5 chile f yes no
6 chile f yes yes
7 chile m no yes
8 france f yes no
9 france f no no
10 france f no no
11 france m yes yes
12 france m yes no
13 france f yes no
14 france f no yes
15 china m yes yes
16 china f no yes
17 china f no yes
18 china f yes yes
19 china m yes yes
20 china m yes no
.....
I want to do do a chi-square test on comparing French men and Chilean women and their responses to question 1 through 14.
Problem 1: A filter will not make SPSS ignore the remaining rows such as Chinese participants.
Problem 2: Furthermore if I just try and compare male and female participants SPSS produces the following error message after calculating:
No statistics are computed because (Question) is a constant.<
I cannot accept that I am forced to summarize the data manually into a frequency table, so how do I make SPSS automatically do this?
EDIT: Allow me to rephrase. (Protip: don't ask online when you are frustrated by the problem)
Problem 1 again: I could just move the do the rows I need to a different file but if/when I need to do so many times it becomes increasingly cumbersome. Is there a way to select a combination of filters (for example) for the two columns in the contingency table?
I was using filters ("Select Cases...") wrong!
Solution: I first make a filter for the data I want to compare. (Pseudo code the menu Data>Select Cases...) Filter: IF French AND Male.
Now the filter (I label it 'FilterA' in the Variable View) and it creates a new filter column in the Data View with row variable = 1 for French and male, and zero for the rest.
Now the women from Chile are zero as I want them in the filter (this is important) but so are all the Chinese, the French women and so on.
So I define a second filter: IF Chile AND Female OR French AND Male
Now all the data is filtered out as shown by a diagonal line through the row-counter column on the far left in SPSS. Now when a statistical test is run this data will not be included in the test.
Now I can finally perform the test chi-square: I choose Descriptive Statistics>Crosstabs.
For column I choose "FilterA" which I created. For row I choose the QuestionX/whatever data I want calculated.
Fin.

SQL comparing two tables with common id but the id in table 2 could being in two different columns

Given the following SQL tables:
Administrators:
id Name rating
1 Jeff 48
2 Albert 55
3 Ken 35
4 France 56
5 Samantha 52
6 Jeff 50
Meetings:
id originatorid Assitantid
1 3 5
2 6 3
3 1 2
4 6 4
I would like to generate a table from Ken's point of view (id=3) therefore his id could be possibly present in two different columns in the meetings' table. (The statement IN does not work since I introduce two different field columns).
Thus the ouput would be:
id originatorid Assitantid
1 3 5
2 6 3
If you really just need to see which column Ken's id is in, you only need an OR. The following will produce your example output exactly.
SELECT * FROM Meetings WHERE originatorid = 3 OR Assistantid = 3;
If you need to take the complex route and list names along with meetings, an OR in your join's ON clause should work here:
SELECT
Administrators.name,
Administrators.id,
Meetings.originatorid,
Meetings.Assistantid
FROM Administrators
JOIN Meetings
ON Administrators.id = Meetings.originatorid
OR Administrators.id = Meetings.Assistantid
Where Administrators.name = 'Ken'

MS Access CrossTab query - across 3 tables

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.