Ok so i have one table with lets call it A(there are more items here):
ITEMID NAME
10001 Boat
I have another table lets call it B(there are more items here):
Itemid Price Pricetype Dimension Valid_from Valid_To
10001 10 1 Allblank 2014-10-10 2014-10-25
10001 5 2 200 2014-10-09 2014-10-20
10001 99 2 200 2014-10-08 2014-10-10
10001 20 1 Allblank 2014-10-08 2014-10-10
10001 22 2 500 2014-10-10 2014-10-19
Price type determines wherever item is on sale or no so i want to list the regular price at its dimension and the sales price at the dimension so the table should look like this (assuming that 1 is regular price and 2 is sale price):
Itemid Itemname RegularPrice DiscountPrice200 DiscountPrice500
10001 boat 10 5 22
I've inner joined tables, but i cant manage to manage the fields right , case statement returns a lot of duplicates on inner join. I hope i made myself clear :)
My query:
SELECT a.itemid,
case when b.inventdimid = '00000101_090' and (b.labelissue ='1' or b.labelissue = '2' and (b.todate > getdate() or b.todate ='1900-01-01 00:00:00.000') ) then b.amount else null end as Price500
FROM a
inner join b
on a.itemid = b.itemrelation
Try this query:
SELECT b.itemid,
a.name,
b.price,
b200.price,
b500.price
FROM tb b
JOIN ta a
ON b.itemid = a.itemid
JOIN tb b200
ON b.itemid = b200.itemid
AND b200.valid_to > getdate()
AND b200.pricetype = 2
AND b200.dimension = 200
JOIN tb b500
ON b.itemid = b500.itemid
AND b500.valid_to > getdate()
AND b500.pricetype = 2
AND b500.dimension = 500
WHERE b.valid_to > getdate()
AND b.pricetype = 1
Related
I want to write a sql script which fetches data from 2 tables one is parent and second is child table
Bid
BidId | Code | Date
1 | H | 2022-05-12
2 | B | 2022-05-13
BidResult
BidResultId | BidId | Emi
12 | 1 | 50
13 | 1 | 20
14 | 2 | 30
Bid and BidResult
i want fetch column Date,Code,BidId from Bid Table
and BidResultId, EMI from BidResult but want a another column of Minimum EMI
from above example i want to fetch for bidId 1
Code H,date 2022-05-12,BidResultId 12, Emi 50 and Min Emi between 50 and 20 which is 20
so i have written following query
Select B.BidId,BR.EMI As Installment,
(Select Min(BR.EMI ) from BidResult BR
Inner Join Bid B on B.BidId = BR.BidResultId
where B.BidId = 5) As MinInstallment,
B.Code,
BR.BidResultId,
CONVERT(DATE, B.Date) As BidDate
from Bid B
Inner Join BidResult BR On B.BidId = BR.BidId
where B.BidId= 5 and B.TypeId = 1
All Field are starightforward except the minimum EMI value from BidResult for that BidId
please check the query and suggest if any modifications
Solution 1: With group by
select BID, min(EMI) EMI
into #temp
from BIDResult
group by BID
select b.Date, b.Code, b.BidId, br.BidResultId, br.EMI
from BID b
inner join #temp on t.BID = b.BID
inner join BIDResult br on br.BID = t.BID and br.EMI = t.EMI
Solution 2: Without group by
select *, ROW_Number() over(PARTITION by BID order by EMI) RowNumber
into #temp
from BIDResult
select b.Date, b.Code, b.BidId, br.BidResultId, br.EMI
from BID b
inner join #temp t on t.BID = b.BID and t.RowNumber = 1
I need to work out the freight percentage value on each shipment the company brings in. The invoices for the freight and the goods are stored in the same table, VSI denotes a goods invoice, VBL denotes a freight invoice, they are joined together via a job cost. I have tried to self join the sets of tables, but the end sum is always multiplied by the amount of matching records. Any help would be appreciated.
Tables
JobCost
JobCostID JobCostDescription JobCostNumber
4910 ITS-1005104 JBC-1004880
4911 ITS-1005105 JBC-1004881
PurchaseInvoice
PIID PINumber ExchangeRate VendorId
1 VSI-1 1 1
2 VSI-2 1 2
3 VBL-1 1 3
PurchaseInvoiceItem
PIItemID PIID Item
1 1 ProductA
2 1 ProductB
3 2 ProductA
4 2 ProductB
5 3 Fuel
6 3 Handling
PurchaseInvoiceItemDetail
PIItemDetailID QtyShipped Cost PIItemID JobCostID
1 2 2500 1 4910
2 2 2500 2 4910
3 2 2500 3 4911
4 2 2500 4 4911
5 1 25 5 4910
6 1 75 6 4910
Vendor
VendorId VendorCode VendorName
1 VEN1 Vendor1
2 VEN2 Vendor2
3 Freight1 Agent1
Desired result
JobCostID JobCostDescription VendorCode VendorName FreightAgentCode FreightAgentName InvoiceTotal FreightTotal FreightPercentage
4910 ITS-1005104 VEN1 Vendor1 Freight1 Agent1 10000 100 1%
4920 ITS-1005105 VEN2 Vendor2 10000
SELECT tblJobCost1.JobCostID, tblJobCost1.JobCostDescription, tblVendor1.VendorCode, tblVendor1.VendorName, tblVendor2.VendorCode AS 'FreightAgentCode', tblVendor2.VendorName AS 'FreightAgentName',
SUM(PurchaseInvoiceItemDetail1.QtyShipped*PurchaseInvoiceItemDetail1.Cost*PurchaseInvoice1.ExchangeRate) AS 'InvoiceTotal',
SUM(PurchaseInvoiceItemDetail2.QtyShipped*PurchaseInvoiceItemDetail2.Cost*PurchaseInvoice2.ExchangeRate) AS 'FreightTotal',
SUM(PurchaseInvoiceItemDetail2.QtyShipped*PurchaseInvoiceItemDetail2.Cost*PurchaseInvoice2.ExchangeRate)/SUM(PurchaseInvoiceItemDetail1.QtyShipped*PurchaseInvoiceItemDetail1.Cost*PurchaseInvoice1.ExchangeRate)*100 AS 'FreightPercentage'
FROM tblJobCost AS tblJobCost1
LEFT JOIN tblJobCost AS tblJobCost2 ON tblJobCost1.JobCostID = tblJobCost2.JobCostID
LEFT JOIN tblPurchaseInvoiceItemDetail AS tblPurchaseInvoiceItemDetail1 ON tblJobCost1.JobCostID = tblPurchaseInvoiceItemDetail1.JobCostID
LEFT JOIN tblPurchaseInvoiceItemDetail AS tblPurchaseInvoiceItemDetail2 ON tblJobCost2.JobCostID = tblPurchaseInvoiceItemDetail2.JobCostID
LEFT JOIN tblPurchaseInvoiceItem AS tblPurchaseInvoiceItem1 ON tblPurchaseInvoiceItem1.PIItemID = tblPurchaseInvoiceItemDetail1.PIItemID
LEFT JOIN tblPurchaseInvoiceItem AS tblPurchaseInvoiceItem2 ON tblPurchaseInvoiceItem2.PIItemID = tblPurchaseInvoiceItemDetail2.PIItemID
LEFT JOIN tblPurchaseInvoice AS tblPurchaseInvoice1 ON tblPurchaseInvoiceItem1.PIID = tblPurchaseInvoice1.PIID
LEFT JOIN tblPurchaseInvoice AS tblPurchaseInvoice2 ON tblPurchaseInvoiceItem2.PIID = tblPurchaseInvoice2.PIID
LEFT JOIN tblVendor AS tblVendor1 ON tblVendor1.VendorId = tblPurchaseInvoice1.VendorID
LEFT JOIN tblVendor AS tblVendor2 ON tblVendor2.VendorId = tblPurchaseInvoice2.VendorID
WHERE tblPurchaseInvoice1.PINumber LIKE 'VSI%' AND tblPurchaseInvoice2.PINumber LIKE 'VBL%'
GROUP BY tblJobCost1.JobCostID, tblJobCost1.JobCostDescription, tblVendor1.VendorCode, tblVendor1.VendorName, tblVendor2.VendorCode, tblVendor2.VendorName
Then you are missing a critical condition:
...
LEFT JOIN tblPurchaseInvoiceItemDetail AS tblPurchaseInvoiceItemDetail2
ON tblJobCost2.JobCostID = tblPurchaseInvoiceItemDetail2.JobCostID
AND tblPurchaseInvoiceItemDetail1.JobCostID = tblPurchaseInvoiceItemDetail2.JobCostID
...
I just want to get the result which displays the reference which is not tallied in sum of table2. when i run my query below it will give me an wrong sum which it gets doubled even if group by cusid ,refno.
Table 1
RefNo
CusID
TotalAmount
1
1001
50
2
1001
30
3
1002
40
Table 2
RefNo
CusID
Particular
Amount
1
1001
Paper
30
1
1001
Pencil
30
2
1001
Ball
15
2
1001
Rubber
20
3
1002
Laptop
50
select * from Table1 a
INNER JOIN (Select CusID,RefNo, SUM(Amount) as CorrectTotal from Table2 group by
CusID,RefNo,
)b
ON b.CusID= a.CusID AND b.RefNo= a.RefNo
where a.TotalAmount != CorrectTotal
Expected Result
If you do it with FULL JOIN and with GROUP BY, you will also get rows where there is no record in the other table.
SELECT COALESCE(a.RefNo, b.RefNo) AS RefNo
, COALESCE(a.CusID, b.CusID) AS CusID
, a.TotalAmount
, SUM(b.Amount) AS CorrectTotal
FROM table1 a
FULL JOIN table2 b ON a.RefNo = b.RefNo
AND a.CusID = b.CusID
GROUP BY COALESCE(a.RefNo, b.RefNo)
, COALESCE(a.CusID, b.CusID)
, a.TotalAmount
ORDER BY 1, 2
Output
RefNo
CusID
TotalAmount
CorrectTotal
1
1001
50
60
2
1001
30
35
3
1002
40
50
8
888
88
(null)
9
999
(null)
99
See running demo on SQL Fiddle.
The other answer will work, but if you don't want to mess with a GROUP BY on the whole query you can also use an APPLY to do this:
SELECT a.*, c.CorrectAmount
FROM Table1 a
OUTER APPLY (
SELECT SUM(Amount) AS CorrectAmount
FROM Table2 b
WHERE b.CusID = a.CusID AND b.RefNo = a.RefNo
) c
WHERE a.TotalAmount <> c.CorrectAmount
I Have 3 tables Customer, Bank and BankTransaction.
On My View I want to display each customer with their Balance in the bank account.
Here is my tables
Customer
Id Name
---------
1 John
2 Jack
Bank
Id CustomerId BankName
----------------------------------
1 1 HSBC
2 2 HSBC
BankTransaction
Id BankID MoneyIn MoneyOut Balance
---------------------------------------------
1 1 1000 0 1000
2 1 0 500 500
3 2 2000 0 2000
4 2 2000 0 4000
5 2 1000 0 5000
Now I want to Display following data view query
John 500
Jack 5000
Last Balance of each customer
Try this
SELECT A.Name,
Sum(C.MoneyIN) - Sum(C.MoneyOut) AS Balance
FROM #Customer A
JOIN #Bank B ON A.Id = B.id
JOIN #BankTransaction C ON B.Id = C.BankID
GROUP BY A.Name
Using Window function you can get the result. Try this.
;WITH cte
AS (SELECT Row_number() OVER (partition BY b.id ORDER BY a.id DESC) rn,
c.Name,
a.balance
FROM BankTransaction a
JOIN bank b ON a.BankID = b.Id
JOIN Customer c ON c.Id = b.CustomerId)
SELECT name, balance
FROM cte
WHERE rn = 1
This is my orders table. I want to report all orders which all details are Ready For Shipment.
orderNo detailNo statusId status
10001 1 40 Ready For Shipment
10002 1 40 Ready For Shipment
10002 2 20 Canceled
10002 3 30 Pending
10003 1 40 Ready For Shipment
10003 2 40 Ready For Shipment
10004 1 10 New Order
10004 2 20 Canceled
10004 3 40 Ready For Shipment
10004 4 40 Ready For Shipment
Expected results are:
Orders Ready For Shipment
10001
10003
Are there any effective method to achieve ready orders list without using subqueries?
Group by the orderno and use a havingto get only those groups having no other status
select orderno
from your_table
group by orderno
having sum(case when status <> 'Ready For Shipment' then 1 end) = 0
or with the statusId
select orderno
from your_table
group by orderno
having sum(case when statusid <> 40 then 1 end) = 0
select Distinct a.orderId
from ordersTable a
inner join
(
select orderNo, Avg(statusId)
from ordersTable
group by orderNo
having Avg(statusId) = 40) b
on a.orderNo = b.orderNo
Have you tried this?
SELECT orderNo from <TABLE NAME>
WHERE status="Ready For Shipment" ORDER BY orderNo