adding and sorting a column in sql - sql

I am trying to combine two tables in sql and am having trouble.
table g looks like this
id name
*************************
1234 john
3456 sarah
7890 ben
table f looks like this
id count
**************************
3456 2
1234 7
7890 5
I would like to insert a column into table g equal to the count in table f, sorted according to ID.
I've tried using the INNER JOIN and UNION commands, but neither of them seem to have worked. Does anyone know a solution? it seems like a pretty common problem.

SELECT g.id, g.name, f.count
FROM f,g
WHERE f.id = g.id
ORDER BY g.id;
The above is a simple join query.
In simple join queries, you select the information you want from both tables and set the where condition to be equal to the rows you want to join.
Also, you probably should set f.id and g.id as primary keys and set f.id as a foreign key reference to g.id.

I guess you want a select like this:
select g.id,g.name,f.count
from go
join f on g.id=f.id
order by g.id
but if you really want to phisicaly add a column(count) to table g and update it as mentioned in question you can do this:
alter table g
add column count int
and update the tale g like this:
update table g
set g.count=f.count
from g
join f on g.id=f.id

Related

Combining object and property

I have 3 tables.
First table name: Objects
ID
Name
1
Ahmet
2
Hasan
Second table name: Properties
ID
Name
1
EyeColor
2
Height
Third table name: Data
ObjectID
PropertyID
Value
1
1
Blue
1
2
1.70
2
1
Green
2
2
1.90
Using the above three tables, I want to create the following list with SQL.
Name
EyeColor
Height
Ahmet
Blue
1.70
Hasan
Green
1.90
I tried this transmission as sample SQL
SELECT Distinct Objects.Name, (SELECT * FROM Datas where PropertyID = Datas.PropertyID )
FROM Datas
INNER JOIN Objects ON Datas.ObjectID = Objects.ID
INNER JOIN Properties ON Datas.PropertyID = Properties.ID
Can someone help me please?
Start from the table that has the rows you want in your final result i.e. Objects (although I would strongly recommend never using this as a user table name since its also a system table name).
Then join on the information you need for your other columns.
I highly recommend the use of table aliases to make your query easier to read and understand.
select [Name], E.[Value] EyeColor, H.[Value] Height
from dbo.[Objects] O
inner join dbo.[Data] H on H.ObjectID = O.id
and H.PropertyID = (select P.id from dbo.Properties P where P.[Name] = 'Height')
inner join dbo.[Data] E on E.ObjectID = O.id
and E.PropertyID = (select P.id from dbo.Properties P where P.[Name] = 'EyeColor');
I will help you to figure how to solve this problem for now but you need to see a SQL tutorials,
So first of all you need in the O/P you want to select both EyeColor and Height for the person so how can you figure out that this value is for the specific person you need to check for that.
It will be a query like this:
SELECT Objects.Name AS NAME, EyeColor.Value AS EyeColor, Height.Value AS Height
FROM Objects AS Obj
INNER JOIN Data AS EyeColor ON EyeColor.ObjectID = Obj.ID
AND EyeColor.PropertyID = 1
INNER JOIN Data AS Height ON Height.ObjectID = Obj.ID
AND Height.PropertyID = 2;
You need to test it and modify it.
Note that: there are a lot of other solution so always try to figure what you want well first and then start divide the problem and then try to start writing your query.
Check #Dale K solution too at the same question as it can help you a lot.

show different columns from different table with inner join

Hi i have a lot of inner in a query but i can't print column in different table, for example:
where
Table A
id f
-----------
xxx gggg
bbb kkkk
Table B
name code
-----------
ccc dddd
bbb oooo
My code:
select A.id,A.F from (query1)as stima
join
(select B.name,B.code from B as aaaa query)as noome
on noome.code=stima.F;
But if i want put name columns of B table in 'select A.id,A.F' how can i do?
My output is id and colum F but i want output: id,F and name columns.
Any time you have a column in an inner query that you want to use in an outer query, you have to make sure the inner query selects it. It will then become part of the query block with the new alias you give it:
SELECT
a.Name,
a.SomethingElse,
b.ColumnX,
b.ColumnYYY
FROM
(
SELECT c.Name, d.SomethingElse FROM c JOIN d ON ..
) a
JOIN
(
SELECT e.ColumnX, f.ColY as ColumnYYY FROM e JOIN f ON ..
) b
When they're in the inner queries the columns are like d.SomethingElse and f.ColY but when they pass outside of those brackets, they get a new alias, because they are part of the data block that is aliased by a or b, so you no longer refer to them as their inner names.. Particularly in the case of f.ColY it was renamed to ColumnYYY and also given a new "table" alias of b..
You can use any columns from the tables of the from clause.
I think you need this:
select A.id,A.F, noome.name -- this
from (query1)as stima
join
(select B.name,B.code from B as aaaa query)as noome
on noome.code=stima.F;

SQL JOIN to get the compare two values/row from table "A" and the same row from table "B"

I have two tables with the following rows
Table A (transaction)
Order Seller Customer
1 300 500
Table B (Persons)
PersonID FullName
300 Peter White
500 Scott Bold
I want a result like this
Order Seller Customer FullName (Seller) FullName (customer)
1 300 500 Peter White Scott Bold
I've tried multiple things however which makes more sense is a join a table twice, however I'm getting:
Ambiguous column name
This is SQL Server 2019.
Basically I'm looking to retrieve info from the same table instead of creating additional tables. Is that possible? If yes, how do you do? Thank you in advance.
As #jarlh wrote in comment:
select t.order, t.seller, t.customer, sel.fullname, cust.fullname
from transaction t
join persons sel -- sel is an alias to persons table
on sel.personid = t.seller
join persons cust
on cust.personid = t.customer;
Query with join will return the result as long as both seller and customer exist in persons table -- here it should as source table names transactions :).
I have another form of query it still join table B twice.
This is archaic syntax which I don't recommend but for beginner know the concept of JOIN:
select t.*,B.FullName as FullName (customer) from
(
select A.Order,A.Seller,A.Customer,B.FullName as FullName(Seller)
from A,B where A.Seller=B.PersionID
) t, B where t.Customer=B.PersionID
The proper way of JOIN:
select t.*,B.FullName as FullName (customer) from
(
select A.Order,A.Seller,A.Customer,B.FullName as FullName(Seller)
from A JOIN B ON A.Seller=B.PersionID
) t JOIN B ON t.Customer=B.PersionID
Hoping this can help you.

SQl Left Join Query

These are the tables that i have(I got this):
table building: b_id(key relation with table build-works b_id):1 2 3
field1: buildingA, buildingB,
buildingC
table build-works: b_id:1 1 2 3 3 3
w_id: 1 2 1 1 2 3
table works: w_id(key relation with table build-works w_id): 1 2 3 4
field1: electricity, sanitary, shell,
roofing
Now I want to know the works per building?
How can i do this with sql, and can you give my the example also with zend_db?
Thanks
using left joins since its in the title
SELECT *
FROM building b
LEFT JOIN buildworks bw
ON b.b_id = bw.b_od
LEFT JOIN works w
ON bw.w_id = w.w_id
Assuming b_id is the primary key for building, (b_id, w_id) is key for build_works, and w_id is key for works you can do it as follows:
Project_building.field1,works.field1(building JOIN build_works JOIN works)
Note that you have to rename field1 of works to something else when doing JOIN.
Also note that this might not be the most efficient way to do it.

Remove Duplicates from LEFT OUTER JOIN

My question is quite similar to Restricting a LEFT JOIN, with a variation.
Assuming I have a table SHOP and another table LOCATION. Location is a sort of child table of table SHOP, that has two columns of interest, one is a Division Key (calling it just KEY) and a "SHOP" number. This matches to the Number "NO" in table SHOP.
I tried this left outer join:
SELECT S.NO, L.KEY
FROM SHOP S
LEFT OUTER JOIN LOCATN L ON S.NO = L.SHOP
but I'm getting a lot of duplicates since there are many locations that belong to a single shop. I want to eliminate them and just get a list of "shop, key" entries without duplicates.
The data is correct but duplicates appear as follows:
SHOP KEY
1 XXX
1 XXX
2 YYY
3 ZZZ
3 ZZZ etc.
I would like the data to appear like this instead:
SHOP KEY
1 XXX
2 YYY
3 ZZZ etc.
SHOP table:
NO
1
2
3
LOCATION table:
LOCATION SHOP KEY
L-1 1 XXX
L-2 1 XXX
L-3 2 YYY
L-4 3 YYY
L-5 3 YYY
(ORACLE 10g Database)
You need to GROUP BY 'S.No' & 'L.KEY'
SELECT S.NO, L.KEY
FROM SHOP S
LEFT OUTER JOIN LOCATN L
ON S.NO = L.SHOP
GROUP BY S.NO, L.KEY
EDIT Following the update in your scenario
I think you should be able to do this with a simple sub query (though I haven't tested this against an Oracle database). Something like the following
UPDATE shop s
SET divnkey = (SELECT DISTINCT L.KEY FROM LOCATN L WHERE S.NO = L.SHOP)
The above will raise an error in the event of a shop being associated with locations that are in multiple divisions.
If you just want to ignore this possibility and select an arbitrary one in that event you could use
UPDATE shop s
SET divnkey = (SELECT MAX(L.KEY) FROM LOCATN L WHERE S.NO = L.SHOP)
I had this problem too but I couldn't use GROUP BY to fix it because I was also returning TEXT type fields. (Same goes for using DISTINCT).
This code gave me duplicates:
select mx.*, case isnull(ty.ty_id,0) when 0 then 'N' else 'Y' end as inuse
from master_x mx
left outer join thing_y ty on mx.rpt_id = ty.rpt_id
I fixed it by rewriting it thusly:
select mx.*,
case when exists (select 1 from thing_y ty where mx.rpt_id = ty.rpt_id) then 'Y' else 'N' end as inuse
from master_x mx
As you can see I didn't care about the data within the 2nd table (thing_y), just whether there was greater than zero matches on the rpt_id within it. (FYI: rpt_id was also not the primary key on the 1st table, master_x).