Adding another column to select output SQL - sql

I wrote this
SELECT BrojRacuna
FROM Racun LEFT OUTER JOIN Stavka
ON IDRacun=RacunID,
SELECT UkupnaCijena
AS 'Ukupna cijena' FROM Stavka;
BrojRacuna means bill number, Racun means bill and UkupnaCijena means price total. Basically I want to add column which outputs price total and is called Ukupna cijena in the same query as the first SELECT LEFT OUTER JOIN. What is the way to do this?
Thanks in advance

Some guessing:
SELECT BrojRacuna, UkupnaCijena AS Ukupna_cijena
FROM Racun
LEFT OUTER JOIN Stavka
ON IDRacun=RacunID
You should add aliases to your tables and qualify the columns with that:
SELECT r.BrojRacuna, s.UkupnaCijena AS Ukupna_cijena
FROM Racun AS r
LEFT OUTER JOIN Stavka as s
ON r.IDRacun=s.RacunID
It makes it a whole lot easier to understand the query for the next person

just do
select
Racun.BrojRacuna,
Stavka.UkupnaCijena AS 'Ukupna cijena'
FROM Racun inner join Stavka
ON racun.RacunID= stavka.RacunID
in order to do the join right you need to join a a certain and similar column to both tables. like I did on RacounID (I dont know if both your tables have that column)

Related

Select and join returning duplicate data

I have some tables that can be accessed here and I would like to get a new table with EntryId from Entry table and ProtocolNumber from JudicialOrder table. For that I'm using this query:
SELECT DISTINCT ET.EntryId, JOA.ProtocolNumber FROM Entry AS ET
LEFT JOIN JudicialOrderAccount AS JOT ON JOT.AccountId = ET.OwnerAccountId
INNER JOIN JudicialOrder AS JOA ON JOA.JudicialOrderId = JOT.JudicialOrderId;
But the ProtocolNumber is duplicated, what could be wrong with my query?
As Kurt said only the combination of ET.EntryId, JOA.ProtocolNumber is unique. You will recognize it if you add an order by.
SELECT DISTINCT ET.EntryId, JOA.ProtocolNumber FROM Entry AS ET
LEFT JOIN JudicialOrderAccount AS JOT ON JOT.AccountId = ET.OwnerAccountId
INNER JOIN JudicialOrder AS JOA ON JOA.JudicialOrderId = JOT.JudicialOrderId
ORDER BY ET.EntryId, JOA.ProtocolNumber;
If you would really like to have unique protocol number you would need to group by ProtocolNumber and wrap EntryId in some string_agg function (depends on your database).
FYI: Your LEFT JOIN - INNER JOIN combination ends up being two INNER JOINs, see

LEFT JOIN help in sql

I have to make a list of customer who do not have any invoice but have paid an invoice … maybe twice.
But with my code (stated below) it contains everything from the left join. However I only need the lines highlighted with green.
How should I make a table with only the 2 highlights?
Select paymentsfrombank.invoicenumber,paymentsfrombank.customer,paymentsfrombank.value
FROM paymentsfrombank
LEFT OUTER JOIN debtors
ON debtors.value = paymentsfrombank.value
You only want to select columns from paymentsfrombank. So why do you even join?
select invoice_number, customer, value from paymentsfrombank
except
select invoice_number, customer, value from debtors;
(This requires exact matches as in your example, i.e. same amount for the invoice/customer).
There are two issues in your SQL. First, you need to join on Invoice number, not on value, as joining on value is pointless. Second, you need to only pick those payments where there are no corresponding debts, i.e. when you left-join, the table on the right has "null" in the joining column. The SQL would be something like this:
SELECT paymentsfrombank.invoicenumber,paymentsfrombank.customer,paymentsfrombank.value
FROM paymentsfrombank
LEFT OUTER JOIN debtors
ON debtors.InvoiceNumber = paymentsfrombank.InvoiceNumber
WHERE debtors.InvoiceNumber is NULL
in mysql we usually have this way to flip the relation and extract the rows that dosen't have relation.
Select paymentsfrombank.invoicenumber,paymentsfrombank.customer,paymentsfrombank.value
FROM paymentsfrombank
LEFT OUTER JOIN debtors
ON debtors.value = paymentsfrombank.value where debtors.value is null
You can use NOT EXISTS :
SELECT p.*
FROM paymentsfrombank p
WHERE NOT EXISTS (SELECT 1 FROM debtors d WHERE d.invoice_number = p.invoice_number);
However, the LEFT OUTER JOIN would also work if you add filtered with WHERE Clause to filtered out only missing customers that haven't any invoice information :
SELECT p.invoicenumber, p.customer, p.value
FROM paymentsfrombank P LEFT OUTER JOIN
debtors d
ON d.InvoiceNumber = p.InvoiceNumber
WHERE d.InvoiceNumber IS NULL;
Note : I have used table alias (p & d) that makes query to easier read & write.

join three table in oracle 11g

i have three table to join in select query .. this query not working
select policy_master.POLICY_REFER ,policy_master.CLIENT_NAME ,policy_master.ADRESS ,policy_master.POLICY_CLASS ,policy_master.POLICY_PRODUCT ,policy_master.EXECUTIVE_NAME ,policy_master.COMM_DATE ,
policy_master.EXPIRY_DATE ,policy_master.RENEWAL_DATE ,policy_master.GROSS ,policy_master.FED ,policy_master.FIF ,policy_master.STAMP_DUTY ,policy_master.PERMIUM ,policy_master.DESCRIPTION,
POLICY_INSURER_DETAIL.INSURER_NAME,POLICY_INSURER_DETAIL.POLICY_NUMBER,POLICY_INSURER_DETAIL.P_SHARE,POLICY_INSURER_DETAIL.G_PREMIUM,POLICY_INSURER_DETAIL.BROKER_P,POLICY_INSURER_DETAIL.LEVY,
POLICY_INSURER_DETAIL.LEVY,POLICY_SUBAGENT_DETAIL.SUBAGENT_NAME,POLICY_SUBAGENT_DETAIL.BUSSINES_SHARE,POLICY_SUBAGENT_DETAIL.COMM_P,POLICY_SUBAGENT_DETAIL.COMM_VALUE
from POLICY_MASTER INNER JOIN POLICY_INSURER_DETAIL
on policy_master.policy_refer = POLICY_INSURER_DETAIL.POLICY_REFER and
policy_master.policy_refer = POLICY_SUBAGENT_DETAIL.POLICY_REFER;
Please tell me what i should do
To simplify the answer I've removed all explicit columns and replaced them with select *.
You have only joined two tables. You are refering to policy_subagent_detail table inside a join to policy_insurer_detail (but you're not joining the subagent details table). You should join this table and specify joining conditions in order to be able to retrieve columns from it (as you did in your column list near select keyword).
I've also added table aliases to make your code shorter.
select *
from POLICY_MASTER pm
inner join POLICY_INSURER_DETAIL pid on
pm.policy_refer = pid.POLICY_REFER
inner join POLICY_SUBAGENT_DETAIL psd on -- added join
pm.policy_refer = psd.POLICY_REFER
do inner join of the third table required you missed iton the from clause . thats it .OR you can use where clause like
from table1 a,table2 b,table3 c
where a.colname= b.colname and
b.colname=c.colname.

Get some columns from table A and some from table B

I have two columns i need to show all the records in column [PR] and some columns from column [EM]. The below SQL statement does not return all the records from column [PR].
SELECT
[PR].[WBS1], [EM].[FirstName], [EM].[LastName], [EM].[EMail]
FROM
[VisionDemo].[dbo].[PR]
JOIN
[VisionDemo].[dbo].[EM] ON [VisionDemo].[dbo].[PR].[Principal] = [VisionDemo].[dbo].[EM].[Employee]
How do I do this ?
Use a LEFT JOIN:
SELECT [PR].[WBS1],[EM].[FirstName],[EM].[LastName], [EM].[EMail]
FROM [VisionDemo].[dbo].[PR]
LEFT JOIN [VisionDemo].[dbo].[EM]
ON [VisionDemo].[dbo].[PR].[Principal] = [VisionDemo].[dbo].[EM].[Employee]
Use LEFT JOIN. This is fundamental stuff, go check out the FAQ
SELECT [PR].[WBS1],[EM].[FirstName],[EM].[LastName], [EM].[EMail]
FROM [VisionDemo].[dbo].[PR]
LEFT JOIN [VisionDemo].[dbo].[EM] --Use a left join
ON [VisionDemo].[dbo].[PR].[Principal] = [VisionDemo].[dbo].[EM].[Employee]

Joining of 5 Tables

I need to join 5 tables to get particular bill number.
Tables are
bill,
Service_bill
Damage_cost
Extraperson_cost
Advance_cost
Except bill, all other tables are may store null values.
My query is like below...
select bill.bill_no,
bill.total,
bill.discount,
bill.to_be_paid,
isnull(Service_bill.total_amt,0) as ServiceCharge,
isnull(Damage_cost.total_amt,0) as DamageCost,
isnull(Extraperson_cost.total_amt,0) as ExtraCost,
isnull(Advance_cost.total_amount,0) as Advance
from bill
left join Advance_cost on bill.bill_no=Advance_cost.room_bill_no and bill.bill_no='57'
inner join Service_bill on bill.bill_no=Service_bill.room_bill_no
inner join Damage_cost on bill.bill_no=Damage_cost.room_bill_no
inner join Extraperson_cost on bill.bill_no=Extraperson_cost.room_bill_no
Now it returns data's, which joins conditions are getting true.
First table should have values then other tables are null only so it must be return first tables completely. But i don't know why it comes like this!
The inner joins mean that you are only finding bills for people who have records in all 4 other tables. Use outer joins instead.
Also, you are limiting the results to those items where there is a record in "advance_cost" for bill 57. That's probaly not the idea...
Thanks Neville k... i got it using outer join...
select bill.bill_no,bill.total,bill.discount,bill.to_be_paid,
isnull(Service_bill.total_amt,0) as ServiceCharge,
isnull(Damage_cost.total_amt,0) as DamageCost,
isnull(Extraperson_cost.total_amt,0) as ExtraCost,
isnull(Advance_cost.total_amount,0) as Advance
from bill
left join Advance_cost on bill.bill_no=Advance_cost.room_bill_no
left outer join Service_bill on bill.bill_no=Service_bill.room_bill_no
left outer join Damage_cost on bill.bill_no=Damage_cost.room_bill_no
left outer join Extraperson_cost on bill.bill_no=Extraperson_cost.room_bill_no
where bill.bill_no='57'