SQl calculate column value based on reference table - sql

I want to select a column value based on the reference table in SQL Server.
These are the tables:
Tbl1
id Cost LineNo
-----------------------
1 10 01
2 20 02
3 30 03
Tbl2
id LineNo MaterialCost
--- ----- -----------
1 03 50
I want to calculate Totalcost (as line no * Materialcost) from tbl2 if there is a matching lineno in tbl2 from tbl1 else get the TotalCost (lineno * Cost) from tbl1
Below is the output I want
id Cost LineNo TotalCost
----------------------- ---------
1 10 01 10
2 20 02 40
3 30 03 150

You can use LEFT JOIN and coalesce as follows:
select t1.*, (t1.lineNo * coalesce(t2.materialcost, t1.cost)) as total
from table1 t1
left join table2 t2 on t1.lineno = t2.lineno

In sql server, you can use this:
select Tbl1.id,
Tbl1.Cost,
Tbl1.LineNo,
Tbl1.LineNo * ISNULL(tbl2.MaterialCost, Tbl1.Cost)
from Tbl1
left join Tbl2
on Tbl1.LineNo = Tbl2.LineNo

Related

Deleting rows in sql rows based on pair value

I have SQL test table as below:
Item
Doc_No
Code
Line_Item
1
abc1234
101
01
2
abc1234
102
01
3
def5678
101
01
4
def5678
102
01
5
ghi1234
101
01
6
ghi1234
101
02
7
jkl5678
101
01
I am trying to eliminate rows when duplicate values of "Doc_No" has pair values of "101" and "102" in "Code" column e.g abc1234 and def5678.
At the same time I want to maintain duplicate values of "Doc_No" without the pair value of "101" and "102" in "Code" column e.g. ghi1234. Final output as below:
Item
Doc_No
Code
Line_item
5
ghi1234
101
01
6
ghi1234
101
02
7
jkl5678
101
01
I tried to get the rows with duplicate values and exclude them but this will wrongly exclude "ghi1234" and not the final output table I want.
SELECT
a.*,
b.Count_Item
FROM dbo.test AS a
LEFT JOIN
(SELECT Doc_No, COUNT(*) AS Count_Item
FROM dbo.test
GROUP BY Doc_No
HAVING COUNT(*) > 1) AS b
ON a.Doc_No = b.Doc_No
WHERE b.Count_Item < 2
Item
Doc_No
Code
Line_Item
Count_Item
7
jkl5678
101
01
1
Can somebody help me with this?
Building on your construct:
SELECT a.*
FROM dbo.test a LEFT JOIN
(SELECT Doc_No, COUNT(*) AS Count_Item
FROM dbo.test
WHERE code IN (101, 102)
GROUP BY Doc_No
) b
ON a.Doc_No = b.Doc_No
WHERE b.Count_Item < 2 OR Count_Item IS NULL;
Note: This assumes that codes are not duplicated for a doc.
What about this:
select *
from test
group by Doc_No, Line_Item
having count(*) = 1
or this:
select *
from test
where Doc_No not in (
select Doc_No
from test
join test as test2 using (Doc_No)
where test.Code = 101 and test2.Code = 102
);

Oracle SQL - Joining tables to include one column to the other (vice versa)

These are my sample tables, columns and records...
Table: tbl1
-----------------------
Columns: ID | DEPT | WK | MANHRS
Records: 01 A 1 8
02 A 2 2
Table: tbl2
--------------------------------
Columns: ID | DEPT | WK | WAGES
Records: 01 A 1 3
02 A 2 5
Scenario:
I want to have a result where two tables are joined and MANHRS and WAGES columns are both together in the result set.
Expected output of the result table:
Columns: ID | DEPT | WK | MANHRS | WAGES
01 A 1 8 3
02 A 2 2 5
I tried UNION but didn't get my expected result. :(
How to do this?
The proper way to write the query is:
SELECT t1.*, t2.WAGES
FROM tbl1 t1 JOIN
tbl2 t2
ON t1.DEPT = t2.DEPT and t1.WK = t2.WK;
Notes:
Never use commas in the FROM clause. Always use proper, explicit JOIN syntax.
I'm not sure if ID should be in the JOIN conditions.
If you want all rows in both tables, but some might be missing, then use FULL JOIN.
You can write the query with the USING clause:
SELECT ID, DEPT, WK, t1.MANHRS, t2.WAGES
FROM tbl1 t1 JOIN
tbl2 t2
USING (ID, DEPT, WK);
This is particularly useful if you are using a FULL JOIN.
Assuming that you should join by DEPT and WK:
SELECT t1.*, t2.WAGES
FROM tbl1 t1, tbl2 t2
where t1.DEPT = t2.DEPT and t1.WK = t2.WK

ignore records that exists in two tables sql server

table 1
ID Department Category
555 16 test
888 16 test
0001 16 test
table 2
ID Department Date
555 67 2015-04-28
111 58 2015-04-28
000 45 2015-04-28
how do i create a stored procedure if i had to pass in the parameter value table1.department=16 it must retreive all the records from table1 but if the id is in table 2 it must ignore that record.
expected output if i pass parameter table1.department=16
output
ID DEpartment Category
888 16 test
0001 16 test
id 555 should get ignored.
what was attempted
select *
from table1 as t1 inner join table2 as t2 on t1.ID=t2.ID
where t1.department='16'
This query will return your desired Result,
SELECT Table1.ID,
Table1.Dept,
Table1.Category
FROM table1 WHERE Table1.ID NOT IN (SELECT Table2.ID FROM table2)

How to update a table according records in another table

Consider this two tables :
tbl 1
Qsno City Status Year Month
--------------------------------------
1 01 3 1990 1
2 01 3 1990 1
1 02 3 1990 2
2 02 3 1990 2
1 03 3 1990 1
2 03 1 1990 1
3 03 1 1990 1
and :
tbl 2
Qsno City
---------------
1 01
2 01
1 03
2 03
3 03
Qsno + City are unique
Ok, I want to update row from tbl1 that has row in tbl2 and set Month = 3.
How I can do this?
thanks
update tbl1
set Month = 3
where exists
(select *
from tbl2
where tbl1.Qsno = tbl2.Qsno and
tbl1.City = tbl2.City)
Also you can use this
use tempdb
go
create table #tbl1 (Qsno int,City int, intMonth int)
create table #tbl2 (Qsno int,City int)
insert into #tbl1 values (1,2,1),(1,3,1),(2,2,1),(2,3,1),(3,1,1)
insert into #tbl2 values (1,2),(2,2)
UPDATE t1
SET intMonth=3
FROM #tbl1 t1
JOIN #tbl2 t2 ON t1.Qsno=t2.Qsno AND t1.City=t2.City
SELECT * FROM #tbl1
DROP TABLE #tbl1
DROP TABLE #tbl2
And don't use reserved words such as Month, Year, Status etc as names of Tables and Columns thus you will avoid a lot of headaches.

SQL Access help in sum from 2 different tables

i have these tables
table 1
id price
1 30
2 40
3 50
table 2
id price
1 70
2 5
3 10
i want a query that would sum the the price value based on the ID
like if table1.id=table2.id then sum table1.price and table2.price
the end result should be something like this
table 3
id price
1 100
2 45
3 60
You can;
SELECT
TABLE1.ID,
TABLE1.PRICE+TABLE2.PRICE
FROM TABLE1
INNER JOIN TABLE2 ON TABLE1.ID = TABLE2.ID;
Or if there are duplicate IDs in either table;
SELECT
TABLE1.ID,
SUM(TABLE1.PRICE+TABLE2.PRICE)
FROM TABLE1
INNER JOIN TABLE2 ON TABLE1.ID = TABLE2.ID
GROUP BY TABLE1.ID;