add value from different tables and insert total value into another table - sql

I have several tables (say 5 tables) and I want to sum a specific entry from each of the 5 tables and put that sum in one table.
Example:
Table_One
product_one_id name price
1 item1 50.25
2 item2 100.25
Table_Two
product_two_id name price
1 item3 25.25
2 item4 70.25
Table_Total
product_total_id name price_total
1 total1 120.50 //get total from item1 + item4
2 total2 125.50 //get total from item2 + item3
this my code: is not a correct syntax but to illustrate what I'm trying to do
INSERT INTO Table_Total(product_total_id, name, price_total)
VALUES (1, 'total1', ((select price from Table_One where product_one_id = 1) + (select price from Table_Two where product_two_id = 4));

While I'm not completely sure why you need to do this, this should work using a cross join which essentially produces a cartesian product:
insert into table_total
select 1, 'total1', t1.price + t2.price
from table_one t1 cross join table_two t2
where t1.table_one_id = 1
and t2.table_two_id = 2;
SQL Fiddle Demo

Related

SQL query 'Join'

TABLE A >>
ItemNo Name
1 Item1
2 Item2
3 Item3
4 Item4
TABLE B >>
ItemNo Status
1 available
1 onhold
1 Sold
1 Transit
------------------
2 available
2 onhold
2 Sold
-----------------
3 Transit
-----------------
4 onhold
There are four status on table B onhold,available,sold,Transit
Expected result: if anyone of the itemNo in table B has on either available or Transit,
then I need to get only the table A record of that ItemNo
Output:
Table A:
ItemNo Name
1 Item1
2 Item2
3 Item3
Please advise on how to write MS SQL query for this. Many thanks in advance!
You can solve with a correlated subquery:
SELECT *
FROM tableA ta
WHERE tA.ItemNo IN
(
SELECT tB.ItemNo
FROM tableB tB
WHERE tB.Status in ('available','Transit')
AND tB.ItemNo = ta.ItemNo
)
Join the tables and use SELECT DISTINCT to get just one row from table A.
SELECT DISTINCT a.*
FROM TableA AS a
JOIN TableB AS b ON a.itemNo = b.itemNo
WHERE b.status in ('available', 'Transit')

SQL: Join Table A With Table B [( a.Col1 = b.Col1 && a.Col2 = b.Col2) and max(transacrionTime)]

I need help, in building SQL (Query or CTE) for below scenario
Problem : I need to join my Master Table A with detail Table B to find out matching record/row(for each record of TableA) from Table B having same itemType, country having max(TransactionTime) && TransactionTime < EntryTime from table A for corresponding record.
OrderTable
id itemType country EntryTime
1. Item1 IND 12:01:20:291
2. Item2 USA 14:11:22:299
3. Item4 LON 18:01:17:112
4. Item1 SIN 20:05:30:020
5. Item3 HKG 22:02:23:442
StockPrice Table
id itemType country TransactionTime Price
1. Item1 IND 12:01:20:291 10.12
2. Item2 USA 14:11:22:299 50.12
3. Item4 LON 18:01:17:112 02.12
4. Item1 SIN 20:05:30:020 10.67
5. Item3 HKG 22:02:23:442 11.22
6. Item1 IND 12:01:20:291 10.14
7. Item2 USA 14:11:22:299 50.11
8. Item4 LON 18:01:17:112 02.10
9. Item1 SIN 20:05:30:020 10.90
10. Item3 HKG 22:02:23:442 11.37
11. Item1 IND 12:01:20:291 10.10
12. Item2 USA 14:11:22:299 50.01
13. Item4 LON 18:01:17:112 02.11
14. Item1 SIN 20:05:30:020 10.89
15. Item3 HKG 22:02:23:442 11.90
Please help to advise, let me know in comment if anymore details are required
Tried Solution
Select o.id, o.itemType, o.country, o.EntryTime, o.sp.price
from OrderTable o join
StockPrice sp
ON o.country = sp.country and o.itemType = sp.itemType and
o.EntryTime = (select top 1 TransactionTime from StockPrice spIN where o.country = spIN.country and o.itemType = spIN.itemType and
and spIN.spIN.TransactionTime < o.EntryTime order by spIN.TransactionTime)
Somehow result set have more then expected rows
Issues with above query..
From above query i am getting result for only first row from Table A.
In case Table B has more then one row having exact time as max TransactionTime, returned result will be corresponding to number of rows in table b.
See if this works:
select o.id, o.itemType, o.country, o.EntryTime, sp.price
from OrderTable o cross apply (
select top 1 * from StockPrice spIN
where o.country = spIN.country and o.itemType = spIN.itemType
and spIN.TransactionTime <= o.EntryTime
order by TransactionTime desc
) sp
order by id;

Combining item and it's category into single column

I'm in need of combining two columns into one in a specific way.
Here are the columns.
Column A (Items) Column B (Category)
Item 1 Category A
Item 2 Category A
Item 3 Category B
Item 4 Category B
Item 5 Category C
Item 6 Category C
...
What i want to achieve is something like this
Column AB
Category A
Item 1
Item 2
Category B
Item 3
Item 4
Category C
Item 5
Item 6
Please advise as i have a feeling that there is a simple resolution for this issue i just can't find it.
Thanks !
Try this
with wt1
as(
select distinct t2.col2 as col1,t2.col2
from tst t2
union all
select t1.col1,t1.col2
from tst t1
)
select col1
from wt1
order by col2,col1;
Output:
COL1
A
item1
item2
B
item3
item4
C
item5
item6
If I understood well, you want all items plus the distinct categories.
The result should be ordered by the category first and then by the item.
You could try something like this:
SELECT ColumnAB
FROM (
SELECT columnB AS Category
columnA AS ColumnAB,
2 AS ColumnType
FROM my_table
UNION ALL
SELECT DISTINCT
columnB AS Category
columnB AS ColumnAB,
1 AS ColumnType
FROM my_table
ORDER BY Category,ColumnType,ColumnAB
)

Group values based on calculation dependent on another column

I have a dataset that I want to sum the values for by category (item), however before I sum them together I need to convert the currency of some values dependent on the currency in another column.
Sample dataset:
Item Currency Value
1 USD 10
1 PHP 100
2 USD 50
2 PHP 1000
3 PHP 500
select ITEM,
(case when CURRENCY='usd' then sum(VALUE*2) else sum(VALUE) end ) VALUE
from TABLE1
inner join TABLE2 on TABLE1.ID=TABLE2.ID
inner join TABLE3 on TABLE3.X=TABLE1.X
inner join TABLE4 on TABLE1.Y=TABLE4.Y
where A=1 and B=2 and C=5
group by ITEM, CURRENCY
ORDER BY ITEM asc
Desired outcome (using x2 as a factor to go from USD to PHP):
Item Value
1 120
2 1100
3 500
However, I am getting the following, which has correctly converted the currency, but is not grouping by item (ie. i'm getting duplicated rows for item rather than 1 row with the summed value):
Item Value
1 20
1 100
2 100
2 1000
3 500
The case should be the argument to the SUM():
select ITEM,
sum(case when CURRENCY='usd' then VALUE*2 else VALUE
end ) as VALUE
from TABLE1 join
TABLE2
on TABLE1.ID = TABLE2.ID join
TABLE3
on TABLE3.X = TABLE1.X join
TABLE4
on TABLE1.Y = TABLE4.Y
where A = 1 and B = 2 and C = 5
group by ITEM
ORDER BY ITEM asc;
Note: The GROUP BY is only by ITEM, because that defines the rows you want in the result set.

Get unique common records with all records from both tables?

Hi I have two tables structure is like this
Table 1
Customer Company price qty item invno
1 a 89 8 item1 23
2 b 80 4 item2 22
3 c 90 3 item1 45
4 d 19 6 item3 12
table 2
Customer Company price qty item invno
1 a 89 8 item1 23
2 b 80 4 item2 18
3 c 90 3 item1 45
4 d 19 6 item3 15
basically table1 contains the current records and table2 current+past records and they both have other columns
what i want is get the all records from the table1 and table2 but in case of the duplication of invno i need that record from the table1 in this case resultset will contains the invno-23(table1),22(table1),45(table1),12(table1),18(table2),15(table2)
I tried using UNION but it gives different results on different column selection i stuck here any help would be great .
Here is one method, using union all and a filter:
select *
from table1 t1
union all
select *
from table2 t2
where not exists (select *
from table1 t1
where t1.invno = t2.invno
);
SELECT t2.Customer,
t2.Company,
t2.price,
t2.qty,
t2.item,
IFNULL(t1.invno,t2.invno)
FROM table2 AS t2
LEFT JOIN table1 AS t1
ON t2.Customer=t1.Customer