Same-table Tree Table Query in SQL Server - sql

I've searched but found nothing that could help.
I have the following table in a SQL Server 2005 database:
Parent Child Value
---- -------- ---------
America Mexico 8
America Canada 1
Asia Japan 5
Asia Korea 7
Europe Spain 0
Europe Italy 2
Africa Zimbabwe 1
Mexico Baja California 0
America USA 3
USA California 1
USA Texas 2
Parent and Child are Primary Key, value is not important (IMO). I would like to create a view that results in something like this:
Parent Child Value
---- -------- ---------
America USA 3
USA California 1
USA Texas 2
I would search for America, and the result will give back every nested child there is, recursively, no matter how many it has, since I could include cities, localities, etc.
What I need is similar to what some call a BOM explosion.

Here is how you can do it:
with cte as (
select parent, child
from t
union all
select cte.parent, t.child
from cte join
t
on cte.child = t.parent
)
select cte.*
from cte
where parent = 'America';
Here is a small SQL Fiddle example.

Related

How to combine two columns on the same table using Hive

Right now I have:
Scorecard
team1
team2
Winner
Margin
Ground
Match Date
Year
ODI # 1
Australia
England
Australia
5 wickets
Melbourne
5-Jan-71
1971
ODI # 2
England
Australia
England
6 wickets
Manchester
24-Aug-72
1972
ODI # 3
England
Australia
Australia
5 wickets
Lord's
26-Aug-72
1972
ODI # 4
England
Australia
England
2 wickets
Birmingham
28-Aug-72
1972
ODI # 5
New Zealand
Pakistan
New Zealand
22 runs
Christchurch
11-Feb-73
1973
And what I want to is combine team1 and team2 and then get distant list
Example based on what I have above:
teams
Australia
England
New Zealand
Pakistan
I am using Cloudera Hive- I was trying to get a union to work.
I also tried:
SELECT concat_ws('^',(SPLIT('${team1,team2}',',')));
However, the output is just giving me:
${team1^team2}
easiet way would be to use union:
select team1 as teams from tablename
union distinct
select team2 from tablename
Here is another ways using sub query :
Select distinct teams from (
select team1 as teams from tablename
union
select team2 from tablename
) t

In Oracle SQL, Add max values (row by row) from another table when other columns of the table are already populated

I have two tables A and B. Table B has 4 columns(ID,NAME,CITY,COUNTRY), 3 columns has values and one column (ID) has NULLS. I want to insert max value from table A column ID to table B where the ID field in B should be in increasing order.
Screenshot
TABLE A
ID NAME
------- -------
231 Bred
134 Mick
133 Tom
233 Helly
232 Kathy
TABLE B
ID NAME CITY COUNTRY
------- ------- ---------- -----------
(NULL) Alex NY USA
(NULL) Jon TOKYO JAPAN
(NULL) Jeff TORONTO CANADA
(NULL) Jerry PARIS FRANCE
(NULL) Vicky LONDON ENGLAND
ID in column in B should be populated as MAX(ID) +1 from table A. The output should look like this:
TABLE B
ID NAME CITY COUNTRY
------ -------- ---------- -----------
234 Alex NY USA
235 Jon TOKYO JAPAN
236 Jeff TORONTO CANADA
237 Jerry PARIS FRANCE
238 Vicky LONDON ENGLAND
Perhaps the simplest method is to create a one-time sequence for the update:
create sequence temp_b_seq;
update b
set id = (select max(id) from a) + temp_b_seq.nextval;
drop sequence temp_b_seq;
You could actually initialize the sequence with the maximum value from a, but that requires dynamic SQL, so this seems like the simplest approach. Oracle should be smart enough to run the subquery only once.

sql select 1 item from list

i want to select a column from a table which can have another column reference many times.
select t1.name
from ccp.ENTITIES t1
Non
Albania
Australia
China
Czech Republic
Egypt
Germany
Greece
Group
Hungary
India
Ireland
Italy
Luxembourg
Malaysia
Malta
Netherlands
Portugal
Romania
Spain
Turkey
UK
US
this will give me a list of names of which i want 1 row from another table
v_networks_by_lm this table holds records with column t1.name and network. i want the column network only once for each item in the list. v_networks_by_lmcan hold many t1.name
entity name
a Spain
b Spain
c Spain
d Spain
e Spain
f Spain
g Spain
h Germany
i Germany
j Germany
k Germany
l Germany
m Germany
n Germany
o Germany
p UK
q Germany
r Spain
s Spain
t Portugal
u Portugal
v Portugal
q Portugal
from the above data which is in v_networks_by_lm i only want name returned once with any value of entity. but i want to pick the name from ENTITIES as it can be dynamic
I think aggregation does what you want:
SELECT MAX(n.network) as network, e.name
FROM ccp.ENTITIES e JOIN
ccp.v_networks_by_lm n
ON n.name = e.name
GROUP BY e.name;
Sounds like you want a subquery to get the single instance of name from the table, and then you do the join against entities.
Select sub.one_of_entity_values, sub.name
from ccp.entities e
inner join (
select max(entity) as one_of_entity_values, name
from v_networks_by_lm
group by name) sub on e.name = sub.name

Sams Teach Yourself SQL in 10 minutes - Question about GROUP BY

i read the book "Sams Teach Yourself SQL in 10 minutes, Third Edition" and in the lesson 10 "Grouping Data", section "Creating Groups", i can't understand the following:
"Aside from the aggregate calculations statements, every column in your SELECT statement must be present in the GROUP BY clause."
Why? I tried this and i think that it is not true.
For example, consider a table 'World' with the columns 'continent', 'country', 'population'.
SELECT continent, country
FROM World
GROUP BY continent;
According to the book, this should lead to an error, right? But it doesn't. I can group my data depending on the continent (so we have at the results 7 continents) and next to each continent, a random country name.
Like this
continent country
North America Canada
South America Brazil
Europe France
Africa Cameroon
Asia Japan
Australia New Zealand
Antarctica TuxLand
You are most probably using MySQL which allows ungrouped and unaggregated expressions in SELECT clause.
This is violation of standard of course.
This is intended to simplify GROUP BY with joins on a PRIMARY KEY:
SELECT a.*, SUM(b.value)
FROM a
JOIN b
ON b.a_id = a.id
GROUP BY
a.id
Normally, you would have either to add all columns from a into the GROUP BY clause or use a subquery.
MySQL allows you not to do it since all values from a are guaranteed to be the same for a given value of the PRIMARY KEY (which is grouped on).
This is correct and should produce no error in some forms of SQL such as MySQL. You may optionally use the GROUP BY statement on more than one column but it's not required.
GROUP BY will list the first result of the columns specified - so in your case, it would return the first country/continent pair.
PostgreSQL and MySQL allow this, using one field for the group by.
The tutorial probably assumes you should use GROUP BY on all fields so from what you select, you don't lose any data - it would show every country/continent in the above example, but only once.
Here's an example table:
Continent | Country | Random_Field
---------------------------------------------
North America Canada Cake
North America Canada Dog
South America Brazil Cat
Europe France Frog
Africa Cameroon House
Asia Japan Gadget
Asia India Dance
Australia New Zealand Frodo
Antarctica TuxLand Linux
In your first statement:
SELECT continent, country
FROM World
GROUP BY continent;
The output would be:
Continent | Country
--------------------------
North America Canada
South America Brazil
Europe France
Africa Cameroon
Asia Japan
Australia New Zealand
Antarctica TuxLand
Notice one of the Asia rows was lost, despite being different.
Using a GROUP BY on both:
SELECT continent, country
FROM World
GROUP BY continent, country;
Would yield:
Continent | Country
-----------------------------
North America Canada
South America Brazil
Europe France
Africa Cameroon
Asia Japan
Asia India
Australia New Zealand
Antarctica TuxLand

How to do multi-table joins in MySQL involving composite foreign keys?

Sample tables are as follows:
SCENARIO_NATIONS
[scenID] [side] [nation]
scen001 1 Germany
scen001 2 Britain
scen001 2 Canada
SCENARIO_NEEDUNITS
[scenID] [unitID]
scen001 0001
scen001 0003
scen001 0107
scen001 0258
scen001 0759
UNIT_BASIC_DATA
[unitID] [nation] [name]
0001 Germany Mortars
0003 Germany Infantry
0107 Britain Lt
0258 Britain Infantry
0759 Canada Kilted Yaksmen
Goal: given a scenID, pull a list of units from the database sorted by side, nation, name.
I can do everything except for the side inclusion with:
SELECT scenario_needunits.scenID, unit_basic_data.nation, unit_basic_data.name
FROM scenario_needunits
LEFT OUTER JOIN unit_basic_data
ON scenario_needunits.unitID=unit_basic_data.unitID
WHERE scenario_needunits.scenID='scen001'
ORDER BY unit_basic_data.nation ASC, unit_basic_data.name ASC
I've tried just dropping the SCENARIO_NATIONS table in as a LEFT OUTER JOIN on scenID but what ends up happening is that ALL units come back with a side of 1 because that's always the first side listed for the scenID in the SCENARIO_NATIONS table.
Conceptually, what I think needs to happen is SCENARIO_NATIONS must be joined to both the scenID (to restrict it to just that scenario) and to each unit's nation but I don't have any idea how to do that.
OMG Ponies' code results in each unit being listed twice, once per side, rather than only for the side which its parent nation is on:
[scenID] [side] [nation] [name]
BaBu001 1 America CAPT
BaBu001 1 America HMG
BaBu001 1 Germany CAPT
BaBu001 1 Germany GREN
BaBu001 2 America CAPT
BaBu001 2 America HMG
BaBu001 2 Germany CAPT
BaBu001 2 Germany GREN
correct results would be
[scenID] [side] [nation] [name]
BaBu001 1 America CAPT
BaBu001 1 America HMG
BaBu001 2 Germany CAPT
BaBu001 2 Germany GREN
And to get that we modify the code like so:
SELECT sn.side, snu.scenid, ubd.nation, ubd.unitname
FROM sn
JOIN snu
ON snu.scenid=sn.scenid AND snu.scenid = 'scenID'
JOIN ubd
ON ubd.nation=sn.nation AND ubd.unitid=snu.unitid //double join is the key change
ORDER BY sn.side, ubd.nation, ubd.unitname
If you only want UNIT_BASIC_DATA rows/records with a relationship in the SCENARIO_NEEDUNITS table, use:
SELECT snu.scenid,
sn.side,
ubd.nation,
ubd.name
FROM UNIT_BASIC_DATA ubd
JOIN SCENARIO_NEEDUNITS snu ON snu.unitid = ubd.unitid
AND snu.scenid = ?
JOIN SCENARIO_NATIONS sn ON sn.scenid = snu.scenid
ORDER BY snu.scenid, sn.side, ubd.nation, ubd.name
Replace the ? with whatever scenid you wish to look for.
You don't need to specify ASC - it's the default.