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;
Related
I need to right join 2 tables with 3 conditions but the resulting table is bigger than left or right table.
left_table a is like the following:
capacity value group_id level_id tags
100 3 a ab
120 5 a afb lala
122 4 b afg hhh
122 6 c adfg
right table b is like the following: bigger than left table
user group_id level_id tags
adsf a ab
af a abf df
sf a afb lala
dsf b afg hhh
sdf c adfg
I want to append the value and capacity value to the right table b. I have used the following query but the resulting table is larger than the right table. I noticed that it is due to the NULL in tags in both the right and left tables, but i am wondering how to resolve this issue.
select a.capacity, a.value, b.*
from a
right join b
on a.group_id = b._group_id
and a.level_id = b.level_id
and a.tags = b.tags
I noticed that it is due to the NULL in tags in both the right and left tables
No, this is not the cause of duplicates. In fact NULL values fail the comparison, so you will not get a match at all if either value is NULL. That is, the row in b will be returned with NULL values for the columns from a.
If you want NULL values to match as being equal, then you need a NULL-safe comparison -- and Presto supports the SQL Standard is not distinct from. I also strongly prefer left join over right join:
select a.capacity, a.value, b.*
from b left join
a
on a.group_id = b._group_id and
a.level_id = b.level_id and
a.tags is not distinct from b.tags;
If you are getting duplicates, it is because you have duplicates in a. You can check for this using:
select group_id, level_id, tags, count(*)
from a
group by group_id, level_id, tags
having count(*) >= 2;
I have sql query where I have to join three tables. One of this is a table with data of invoice, it looks like this:
INVOICE
ID CUSTOMER_NAME TAXID NUMBER LABEL GUID
1 CUSTOMER1 8739281100 FV001/2019 1 04EABFB3-0B9D-4749-B99D-A4EBEE079633
POSITION OF INVOICE
ID ID_INV POSITION_NAME COUNT
1 1 NAME1 3
2 1 NAME2 2,5
TABLE WITH LABEL
ID NAME VALUE GUID_INV
1 LABEL1 true 04EABFB3-0B9D-4749-B99D-A4EBEE079633
When I want to run this query I have statement like this multiple rows in singleton select.
This is for Firebird 2.5.
SELECT
a.ID,
a.GUID,
a.NUMBER,
a.CUSTOMER_NAME,
b.COUNT,
(select usrd.LABEL from USER_FIELD_DEFS usrd
where usrd.GUID_INV=a.GUID and (usrd.ID=1 and usrb.VALUE='true')) as LABEL_NAME
FROM INVOICE a
join POSITION_INVOICE b ON a.ID=b.ID_INV
I want to get result like this
1 04EABFB3-0B9D-4749-B99D-A4EBEE079633 FV001/2019 CUSTOMER1 3 LABEL1
1 04EABFB3-0B9D-4749-B99D-A4EBEE079633 FV001/2019 CUSTOMER1 2,5 LABEL1
Please help with this. I know that solution maybe is very simple but I have some eclipse of the mind:)
This should give you the rows you want based on the 3 tables you provided. If there is a chance that an invoice has no position then simply replace the inner join with left join
SELECT
I.[Id]
,I.[GUID]
,I.[NUMBER]
,I.[CUSTOMER_NAME]
,IP.[POSITION_NAME]
,L.[NAME]
FROM [INVOICE] I
INNER JOIN [IN_P] IP ON IP.ID_INV = I.Id
LEFT JOIN [LABEL] L ON L.[GUID_INV] = I.[GUID]
You are just missing one more join here. Assuming USER_FIELD_DEFS is the same as TABLE WITH LABEL that you have mentioned here
SELECT
a.ID,
a.GUID,
a.NUMBER,
a.CUSTOMER_NAME,
b.COUNT,
c.NAME
FROM INVOICE a
JOIN POSITION_INVOICE b ON a.ID=b.ID_INV
JOIN USER_FIELD_DEFS c ON c.GUID_INV = a.GUID AND c.ID=1 and c.VALUE='true'
I have two tables customer_details and address_details. I want to display customer details with their corresponding address, so I was using a LEFT JOIN, but when I'm executing this query, SQL Server drops rows where street_no of customer_details table doesn't match with the street_no in address_detials table and displays only rows where `street_no' of customer_detials = street_no of address_details table. I need to display a complete customer_details table and in case if street_no doesn't matches it should display empty string or anything. Am I doing anything wrong in my SQL join?
Table customer_details:
case_id customer_name mob_no street_no
-------------------------------------------------
1 John 242342343 4324234234234
1 Rohan 343233333 43332
1 Ankit 234234233 2342332423433
1 Suresh 234234324 2342342342342
1 Ranjeet 343424323 32233
1 Ramu 234234333 2342342342343
Table address_details:
s_no streen_no address city case_id
------------------------------------------------------
1 4324234234234 Roni road Delhi 1
2 2342332423433 Natan street Lucknow 1
3 2342342342342 Koliko road Herdoi 1
SQL JOIN query:
select
a.*, b.address
from
customer_details a
left join
address_details b on a.street_no = b.street_no
where
b.case_id = 1
Now that it became clear that you used b.case_id=1, I will explain why it filters:
The LEFT JOIN itself returns some rows that contain all NULL values for table b in the result set, which is what you want and expect.
But by using WHERE b.case_id=1, the rows containing NULL values for table b are filtered out because none of them matches the condition (all those rows have b.case_id=NULL so they don't match).
It might work to instead use WHERE a.case_id=1, but we don't know if a.case_id and b.case_id are always the same value for matching rows (they might not be; and if they are always the same, then we just identified a potential redundancy).
There are two ways to fix this for sure.
(1) Move b.case_id = 1 into the left join condition:
left join address_details b on a.street_no = b.street_no and b.case_id = 1
(2) Keep b.case_id = 1 in the WHERE but also allow for NULLED-out b values:
left join address_details b on a.street_no = b.street_no
where b.case_id = 1
or b.street_no IS NULL
Personally I'd go for (1) because that is the most clear way to express that you want to filter b on two conditions, without affecting the rows of a that are being returned.
I do think that Wilhelm Poggenpohl answer is kind of right. You just need to change the last join condition a.case_id=1 to b.case_id=1
select a.* , b.address
from customer_details a
left join address_details b on a.street_no=b.street_no
and b.case_id=1
This query will show every row from customer_details and the corresponding adress if there is a match of street_no and the adress meets the condition case_id=1.
This is because of the where clause. Try this:
select a.* , b.address
from customer_details a
left join address_details b on a.street_no=b.street_no
and a.case_id=1
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
I have 3 tables A, B, and C. I want to get the subId and text for each id. I also want to know IF the id has some eId linked to it.
I've used INNER JOIN on A and B and then LEFT JOIN'd that result with tabel C. My SQL string so far is:
SELECT DISTINCT A.id,A.subId, B.text, C.eId
FROM A
INNER JOIN B ON A.id=B.id
LEFT JOIN C ON A.id=C.id
WHERE B.text='something'
The problem is that C.eId has multiple entries for each id. So I'm getting output like this:
=================================
id | subId | text | eId
1 e12 etc
2 e12 etc
2 t23 etc p1111
3 e12 etc
4 e12 etc p1234
4 e12 etc p4325
I want to remove the lines like the last one ("4 e12 etc p4325") because I already know that 4, e12 has some other eId linked to it. I need id and subId to be PrimaryKeys.
How do I do this? DISTINCT worked until I added multiple id's to an eId.
Edit: I use MSSQL if that makes a difference.
I don't recall if it works this way, try it and tell me:
SELECT DISTINCT A.id,A.subId, B.text,
(select top 1 C.eId from C where C.id = A.id) AS eId
FROM A
INNER JOIN B ON A.id=B.id
WHERE B.text='etc'