I am using this code for inventory management system, in which i want to retrieve stock in hand from four tables. i have tried with two table and got accurate result as i need it.please help me out.
Table Schema
Productmastertb
prod_id,
Product_name
salesdetailstb
sales_id,
Prod_id,
Prod_qty
estimatedetailstb
est_id,
Prod_id,
Prod_qty
Purchasedetailstb
est_id,
Prod_id,
Prod_qty
Query example (working):
SELECT
productmastertb.prod_id,
productmastertb.prod_name,
sum(estimatedetailstd.prod_qty) as Est_qty
FROM
productmaster
INNER JOIN
estimatedetailstb ON productmastertb.prodid = estimatedetails.prodid
GROUP BY
productmastertb.prod_id, productmastertb.prod_name
Similarly I have to retrieve sum of salesdetailstb.qty and purchasedetailstb.qty
Thanks in advance
You want to summarize across different "dimensions" -- that is tables. One good approach is to aggregate before doing the JOINs. Or to use subqueries. Here is the latter approach:
SELECT pm.prod_id, pm.prod_name,
(SELECT SUM(ed.prod_qty)
FROM estimatedetailstb as ed
WHERE ed.prodid = ed.prodidas
) as Est_qty,
(SELECT SUM(sd.prod_qty)
FROM salesdetailstb as sd
WHERE sd.prodid = pm.prodidas
) as Sales_qty,
(SELECT SUM(pd.prod_qty)
FROM purchasedetailstb as pd
WHERE pd.prodid = pm.prodid
) as Sales_qty
FROM productmaster pm;
This will give you all products, even those missing from one or more of the other tables.
You can add multiple joins.
SELECT t1.id, t4.name, count(t4.name)
FROM Table1 AS t1
INNER JOIN Table2 AS t2 -- the AS statement renames the table within
-- this query to t2. Columns from this table can be used
-- as t2.columnname. This needs to be done when you have
-- columns with the same name in different tables.
ON t1.id = t2.id
INNER JOIN Table3 as t3
ON t1.id = t3.id
INNER JOIN Table4 as t4
ON t3.name = t4.name
GROUP BY t1.id, t4.name
Related
I have two original tables: product table and component tables
Firstly, I need to do an inner merge between these two tables, then create a new table called T1:
select a.Product, a.Plant, b.component, b.position, b.valid_date from Product a
inner join component b on a.Product=b.Product
where a.Plant='A'
The result T1 looks like in the following way:
Furthermore, I need to create a new table named T2 based on T1
select T1.Product, T1.Plant,T1.position,max(T1.valid_date) as valid_date from T1
Where T1.Plant='A'
Group by T1.Product, T1.Plant,T1.position
The result T2 is:
Finally, I want to merge T1 and T2 based on Product, Plant, position, and valid_date for a final table:
select T2.Product, T2.Plant,T1.Component, T2.position, T2.valid_date from T1
INNER JOIN T2 on T1.Product=T2.Product and T1.Plant=T2.Plant and T1.position=T2.position and T1.valid_date=T2.valid_date
where T1.Plant='A'
The final table:
I know this whole process can be done in one hive SQL script. I am confused with multiple tables in one query. I appreciate someone can help me for that. Thank you
isn't it that you just want to do this?
select
a.Plant,b.Product,b.position,b.component,
max(valid_date) as valid_date
from Product a
inner join component b on a.Product = b.Product
where a.Plant = 'A'
group by a.Plant,b.Product,b.position,b.component
db<>fiddle here
the query above produces what you show in output , but based on your comment , I think this is what you are looking for :
select t.Plant,t.Product,t.position,t.component, max(t.valid_date) valid_date
(
select
b.Product, a.Plant,b.component,b.position,max(valid_date) over (partition by a.Plant,b.Product,b.position) as valid_date
from Product a
join component b on a.Product = b.Product
where a.Plant = 'A'
) t
group by t.Plant,t.Product,t.position,t.component;
So say I have two tables in Oracle SQL (not actually data but for ease should highlight my question)
Table1 that contains only Order_id and Order_quantity and Table2 that contains only Order_id and Order_price
Then I join them as follows
Select T1.Order_id,
T1.Order_quantity,
T2.Order_price,
T1.Order_quantity*T2.Order_price As "Order_amount",
Sum(Order_amount) As "Total_Sales"
from Table1 T1
inner join Table2 T2
on T1.Order_id = T2.Order_id
So essentially I want to have two extra columns, one as the product of columns from the two tables, and another as the sum of that column in my joined table(so every entry will be the same). However as you need to
SUM(variable_name) From Table_Name
Can I assign a variable name to my new table and then refer to that. I tried the following but I'm getting a SQL command not properly ended error
Select T1.Order_id,
T1.Order_quantity,
T2.Order_price,
T1.Order_quantity*T2.Order_price As "Order_amount",
Sum(Order_amount) from New_Table As "Total_Sales"
from (Table1 T1
inner join Table2 T2
on T1.Order_id = T2.Order_id) As New_Table
Thanks for any assistance, apologies as I have a pretty naive understanding of SQL at present
I think you just want a window function:
select T1.Order_id, T1.Order_quantity, T2.Order_price,
T1.Order_quantity*T2.Order_price As order_amount,
sum(T1.Order_quantity*T2.Order_price) over () As Total_Sales
from Table1 T1 inner join
Table2 T2
on T1.Order_id = T2.Order_id
You cannot re-use the alias order_amount in the select. You need to repeat the expression -- or use a subquery or CTE to define it.
If your DBMS doesn't have a window function supports then you can use subquery instead
select order_id, Order_quantity,
(select t1.Order_quantity * t2.Order_price
from table2 t2
where t2.Order_id = t1.Order_id) as Order_amount,
(select sum(t1.Order_quantity * t2.Order_price)
from table2 t2
where t2.Order_id = t1.Order_id) as Total_Sales
from table1 t1;
I'm creating a joined view of two tables, but am getting unwanted duplicates from table2.
For example: table1 has 9000 records and I need the resulting view to contain exactly the same; table2 may have multiple records with the same FKID but I only want to return one record (random chosen is ok with my customer). I have the following code that works correctly, but performance is slower than desired (over 14 seconds).
SELECT
OBJECTID
, PKID
,(SELECT TOP (1) SUBDIVISIO
FROM dbo.table2 AS t2
WHERE (t1.PKID = t2.FKID)) AS ProjectName
,(SELECT TOP (1) ASBUILT1
FROM dbo.table2 AS t2
WHERE (t1.PKID = t2.FKID)) AS Asbuilt
FROM dbo.table1 AS t1
Is there a way to do something similar with joins to speed up performance?
I'm using SQL Server 2008 R2.
I got close with the following code (~.5 seconds), but 'Distinct' only filters out records when all columns are duplicate (rather than just the FKID).
SELECT
t1.OBJECTID
,t1.PKID
,t2.ProjectName
,t2.Asbuilt
FROM dbo.table1 AS t1
LEFT JOIN (SELECT
DISTINCT FKID
,ProjectName
,Asbuilt
FROM dbo.table2) t2
ON t1.PKID = t2.FKID
table examples
table1 table2
OID, PKID FKID, ProjectName, Asbuilt
1, id1 id1, P1, AB1
2, id2 id1, P5, AB5
3, id4 id2, P10, AB2
5, id5 id5, P4, AB4
In the above example returned records should be id5/P4/AB4, id2/P10/AB2, and (id1/P1/AB1 OR id1/P5/AB5)
My search came up with similar questions, but none that resolved my problem. link, link
Thanks in advance for your help. This is my first post so let me know if I've broken any rules.
This will give the results you requested and should have the best performance.
SELECT
OBJECTID
, PKID
, t2.SUBDIVISIO,
, t2.ASBUILT1
FROM dbo.table1 AS t1
OUTER APPLY (
SELECT TOP 1 *
FROM dbo.table2 AS t2
WHERE t1.PKID = t2.FKID
) AS t2
Your original query is producing arbitrary values for the two columns (the use of top with no order by). You can get the same effect with this:
SELECT t1.OBJECTID, t1.PKID, t2.ProjectName, t2.Asbuilt
FROM dbo.table1 t1 LEFT JOIN
(SELECT FKID, min(ProjectName) as ProjectName, MIN(asBuilt) as AsBuilt
FROM dbo.table2
group by fkid
) t2
ON t1.PKID = t2.FKID
This version replaces the distinct with a group by.
To get a truly random row in SQL Server (which your syntax suggests you are using), try this:
SELECT t1.OBJECTID, t1.PKID, t2.ProjectName, t2.Asbuilt
FROM dbo.table1 t1 LEFT JOIN
(SELECT FKID, ProjectName, AsBuilt,
ROW_NUMBER() over (PARTITION by fkid order by newid()) as seqnum
FROM dbo.table2
) t2
ON t1.PKID = t2.FKID and t2.seqnum = 1
This assumes version 2005 or greater.
If you want described result, you need to use INNER JOIN and following query will satisfy your need:
SELECT
t1.OID,
t1.PKID,
MAX(t2.ProjectName) AS ProjectName,
MAX(t2.Asbuilt) AS Asbuilt
FROM table1 t1
JOIN table2 t2 ON t1.PKID = t2.FKID
GROUP BY
t1.OID,
t1.PKID
If you want to see all rows from left table (table1) whether it has pair in right table or not, then use LEFT JOIN and same query will gave you desired result.
EDITED
This construction has good performance, and you dont need to use subqueries.
There are three tables,we have to select data from these table using one primary key and foreign key. But in the one table there is lot of data in the third table. We have to sum the data on the base of the primary key.
BAl = Balance, met = Method, amo = amount, cst_id, cut_id, cut_i = customer_id
Now we have to sum the on the basis of method and sum for 10 cust id in the same query. Can anyone help me on this?
;WITH cte
AS
(
SELECT
*,
ROW_NUMBER() OVER (ORDER BY t1.cst_id) RowNum
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.cst_id = t2.cut_id
INNER JOIN Table3 t3
ON t2.cut_id = t3.customer_id
AND t2.BAL = t3.Balance
AND t2.amo = t3.amount
)
SELECT SUM(*)
FROM cte
WHERE RowNum Between 1 AND 10
-- You can add a GROUP BY here
If you give some sample data it will be easier to write queries to help you.
But if your MET field is numerical and you want to sum it then you need.
select
t1.cst_n, t2.bal,
sum(t3.met) as met,
sum(t3.amo) as amo
from table1 as t1
inner join table2 as t2 on t2.cut_id = t1.cst_id
inner join table3 as t3 on t3.cut_i = t1.cst_id
group by t1.cst_n, t2.bal
well if you want to sum data for all 10 customers into one number, may be you just need
select
sum(t3.met) as met,
sum(t3.amo) as amo
from table3 as t3
where t3.cut_i in (select t.customerid from #<your variable table with cust. ids> as t)
I'm having trouble figuring out the solution to this SQL query.
Schema
Edit: Adding Item Table
Item Table
PK ItemID
lots of other columns
Linking Table
FK ItemID uniqueidentifier
FK TransactionID uniqueidentifier
Transaction Table
PK ID uniqueidentifier
EntryDateTime DateTime
(several other rows of int, varchar...)
Edit : I think I haven't made the relationships clear. Each ITEM (table not shown) can have multiple transactions. Multiple items can share the same transaction (hence the linking table).
Please see the bottom for my current Query. I have left this striked to show the progression of the question.
I want to do something like this query. The trick is I want the t.varchar and t.int columns to be whatever values are in the MAX(t.EntryDateTime) row. I don't even know if group by is the right way to do this query.
SELECT lt.ItemID, MAX(t.EntryDateTime), t.varchar, t.int
FROM LinkingTable lt
LEFT JOIN Transactions t ON lt.TransactionID = t.ID
GROUP BY lt.ItemID
This table is going to be joined against in this SQL query, so please try and give me the most performant solution . Assume Table1 will contain millions of records.
SELECT
(many columns)
FROM Table1
LEFT JOIN Table2 ON Table1.Table2ID = Table2.ID
LEFT JOIN Table3 ON ....
LEFT JOIN Table4 ON (Table2.ID = Table4.Table2ID and Table4.LocaleID = 127 and Table4.Type = 0)
LEFT JOIN **the query above** AS vTable1 ON vTable1.ItemID = Table1.ID
WHERE Table1.CheckID IN (SELECT ID FROM Checks WHERE ....)
Edit : This is the query I have that is working, but I'm not sure its the most efficient. LinkingTable has ~ 200k records and its taking 6 seconds to run.
SELECT DISTINCT lt.ItemID, t.EntryDateTime, t.varchar, t.int
FROM LinkingTable lt
LEFT JOIN Transactions t ON t.id = (SELECT Top 1 t2.id FROM LinkingTable lt2
LEFT JOIN Transactions t2 on lt2.TransactionID = t2.ID
where lt2.ItemID = lt.ItemID ORDER BY t2.PrintTime DESC)
Try this,
SELECT i.*, outerT.EntryDateTime, outerT.varchar, outerT.int
FROM Item i
LEFT JOIN
(SELECT ItemId AS outerItemId, EntryDateTime, varchar, int
FROM (SELECT ROW_NUMBER() OVER (PARTITION BY lt.ItemId ORDER BY t.EntryDateTime) AS RowNumber, lt.ItemId, t.EntryDateTime, t.varchar, t.int
FROM Tranaction t INNER JOIN LinkingTable lt ON lt.TransactionId = t.ID) innerT
WHERE RowNumber = 1) outerT ON outerT.outerItemId = Item.ID
Hope this solves your problem
Even with a million plus records, you will have some performance hits, but I would ensure and index on the transaction table based on the ( ItemID, Primary Key ). The reason Primary Key and not the date -- if its auto-incremented, and it's date/time stamped at time the transaction occurs, they will be in-essence, one-in-the-same. The last entry in the file will always have the latest date. That said, an ID column should be faster with index than a date/time. This also prevents need of looking at BOTH elements of most recent date, and the transaction ID associated with that date. Here is how I would FIRST attempt the query.
select
I.*,
T2.*
from
Item I
JOIN
( select T.ItemID, MAX( T.PrimaryKey ) as LastEntryPerItem
from Transactions T
group by T.ItemID ) MaxPerItem
ON I.ItemID = T.ItemID
JOIN Transactions T2
on MaxPerItem.LastEntryPerItem = T2.PrimaryKey
order by
whatever
select lt.ItemId, t.entrydatetime, t.varchar, t.int
from LinkingTable lt
left join transactions t
on lt.transactionId = t.id
and t.entryDateTime = (select max(t.EntryDateTime)
from transactions t2
where t2.id = t.id)
I had a similar question before
( SQL Join to get value belong with most recent date). There's another solution by JNK involving two joins which may be faster. I've posted below. You'll need to test to see which performs better.
select lt.ItemId, t.entrydatetime, t.varchar, t.int
from LinkingTable lt
inner join transactions t
on lt.ItemId= t.ItemId
Inner join (SELECT ItemId, MAX(entrydatetime) entrydatetime
FROM transactions t2
GROUP BY ItemId) SubQ
ON SubQ.ItemId= t.ItemId
AND SubQ.entrydatetime= t.entrydatetime
Why don't you create a view that has all your "many columns" and then run a query against that view?