SELECT a.name,
nvl(c.bill_amount,0),
b.status
FROM table_1 a left outer join table_2 b
ON a.name = b.name and
b.status = 'YES'
left outer join table_3 c on B.phone_number = C.phone_number
AND B.email = C.email
where
a.VALID = 'Y';
I wrote this query recently, the condition b.status = 'yes' is not fulfilled in the above query and status shows NULL value in it. I want to refine my records according to this condition b.status ='yes'.
Try the following:
SELECT a.name, nvl(c.bill_amount,0), b.status
FROM table_1 a left outer join table_2 b
ON a.name = b.name
left outer join table_3 c on b.phone_number = c.phone_number
where
a.VALID = 'Y' and
b.status = 'YES' and
b.email = c.email
Related
Is it possible to add a case using a column from other tables than first in the from section?
I can't use anything like C.code or Y.anything in the SELECT part:
SELECT
fromTableA, fromTableA, fromTableA,
CASE
WHEN fromTableA = anyValue THEN 'is_ok'
WHEN B.fromTableB = anyValue THEN 'couldnt.be.bound'
WHEN fromTableB = anyValue THEN 'invalid.column.name'
END AS X
FROM
(SELECT fromTableA,
SUM(fromTableA),
CASE WHEN A.fromTableA = 'anything' THEN 'still ok'
WHEN C.fromTableC = 'allowed' THEN 'no problem'
FROM tableA A
JOIN tableB B ON A.id = B.id
JOIN tableC C ON C.id = B.id
Having SUM(fromTableA) > 0
) AS Y
Edit: What I need is to use columns from table B or C in the outer Select ( I just can't remove the inner select because i would lost cases and aggregation operations there are in the inner select).
The following should be all you need, a derived table here doesn't accomplish anything.
select A.Cols...,
case
when A.col1 = anyValue then 'is_ok'
when B.Col1 = anyValue then 'couldnt.be.bound'
when B.Col2 = anyValue then 'invalid.column.name'
end as X
from tableA A
join tableB B on A.id = B.id
join tableC C on C.id = B.id;
I have this query:-
select distinct pav.ProductID,pav.String_Value as Description ,pav1.String_Value as StockNumber,pav2.String_Value as ProductImage,
pav3.String_Value as SpecSheet,pav4.String_Value as Price
from core.ProductAttributesValues pav join core.Attribute a on pav.AttributeID=a.ID and a.Name='Description'
inner join core.ProductAttributesValues pav1 on pav1.ProductID=pav.ProductID
inner join core.Attribute a1 on pav1.AttributeID=a1.ID and a1.Name='StockNumber'
inner join core.ProductAttributesValues pav2 on pav2.ProductID=pav1.ProductID
inner join core.Attribute a2 on pav2.AttributeID=a2.ID and a2.Name='ProductImage'
inner join core.ProductAttributesValues pav3 on pav3.ProductID=pav2.ProductID
inner join core.Attribute a3 on pav3.AttributeID=a3.ID and a3.Name='SpecSheet'
inner join core.ProductAttributesValues pav4 on pav4.ProductID=pav3.ProductID
inner join core.Attribute a4 on pav4.AttributeID=a4.ID and a4.Name='Price'
I want to pass a1.Names(i.e:- Description ,StockNumber, ProductImage, etc), dynamically.
Try this...
SELECT
pav.ProductID
,CASE WHEN a.Name = 'Description' THEN pav.String_Value ELSE NULL END AS Description
,CASE WHEN a.Name = 'StockNumber' THEN pav.String_Value ELSE NULL END AS StockNumber
,CASE WHEN a.Name = 'ProductImage' THEN pav.String_Value ELSE NULL END AS ProductImage
,CASE WHEN a.Name = 'SpecSheet' THEN pav.String_Value ELSE NULL END AS SpecSheet
,CASE WHEN a.Name = 'Price' THEN pav.String_Value ELSE NULL END AS Price
FROM core.ProductAttributesValues pav
INNER JOIN core.Attribute a ON pav.AttributeID = a.ID
AND a.name IN ('Description', 'StockNumber', 'ProductImage', 'SpecSheet','Price')
I have the following query:
SELECT
a.Name,
ISNULL(CAST(sum((b.qty * b.unit_rate)* b.Eng_RPQ )/100 AS DECIMAL(8,1)),0) AS [EngHours],
SUM(BR.BlendedRate)
FROM
Activity_Details b
INNER JOIN
Activity c on b.activity_id = c.id
INNER JOIN
Project p on p.id = c.project_id
RIGHT OUTER JOIN
Discipline a on c.discipline_id = a.id
INNER JOIN
(SELECT
a.Name, c.id,
CAST(f.POH * (d.HourlyRate * (1-(r.Discount/100))/100) AS DECIMAL(8,2)) AS BlendedRate
FROM
Activity_Details b
INNER JOIN
Activity c on b.activity_id = c.id
INNER JOIN
Team f on f.activity_id = c.id
INNER JOIN
SOF_Details d on d.id = f.sof_detail_id
INNER JOIN
Project p on p.id = c.project_id
INNER JOIN
Rate r on r.projectid = p.id
INNER JOIN
Teammate_Type tt on tt.id = f.team_type_id
RIGHT OUTER JOIN
Discipline a on c.discipline_id = a.id
GROUP BY
a.Name, c.id, f.POH, d.HourlyRate, r.Discount) AS BR ON BR.id = c.id
GROUP BY
a.Name
ORDER BY
a.Name
Which yields:
Name EngHours BlendedRate
Architechtural 80.8 38.48
Architechtural 80.8 55.33
Architechtural 80.8 55.40
I want to SUM this BlendedRate and ROUND it but if i try SUM(BR.BlendedRate) to the SELECT and remove the BR.BlendedRate in the GROUP BY
I get:
Name EngHours BlendedRate
Architechtural 242.3 895.26
I was expecting BlendedRate to equal 149.21
Any idea what i am doing wrong?
unable to comment due to reputation. It is a crude solution, but your code is returning duplicated (seemingly 6) records. The code should be fixed elsewhere, but without sample data it is difficult. In the mean time a crude solution would be to add a distinct clause to the sum function
SUM( DISTINCT BR.BlendedRate)
There are three table A,B,C
A table columns are Name,ID,Address
B table columns are ID,Address,School
C table columns are School,address,cafe
If Name='A'
Then Table A and Table B will join based on A.ID =B.ID
IF Name='B'
Then Table A and Table B will join based on A.ID =B.ID and A.Address=B.Address
And this result with join with Table C
Use OR and AND operator to simulate if condition in ON clause. Try this Join
SELECT *
FROM A
INNER JOIN B
ON ( A.name = 'A'
AND A.ID = B.ID )
OR ( A.Name = 'B'
AND A.ID = B.ID
AND A.Address = B.Address )
INNER JOIN C
ON B.school = C.school
You can try below code :
SELECT *
FROM A
INNER JOIN B
ON ( (B.Name = 'A' AND A.ID = B.ID)
OR (B.Name = 'B' AND A.ID = B.ID AND A.Address=B.Address)
)
You can now join the result with table C
Thank You
assuming there A.Name is either A or B:
SELECT *
FROM A
JOIN B ON ( A.ID = B.ID )
AND ( A.Name = 'A' OR A.Address = B.Address )
JOIN C ON B.school = C.school
SELECT * FROM A INNER JOIN B
ON A.ID = B.ID
INNER JOIN C
ON B.SCHOOL = C.SCHOOL
WHERE A.NAME = 'A'
UNION ALL
SELECT * FROM A INNER JOIN B
ON A.ID =B.ID and A.Address=B.Address
INNER JOIN C
ON B.SCHOOL = C.SCHOOL
WHERE A.NAME = 'B'
This might work
I want to show/display if the product is found each transaction.
tblProducts
ID PRODCODE PRODDESC
1 PFX-321 MILK CHOCO
2 PDF-875 COFFEE JELLY
3 PWA-718 MILK SHAKE
tblTransactions
TCODE PRODCODE
BMX2213391 PFX-321
BMX2213391 PDF-875
PDFSD92851 PDF-875
I want the results to display like this
TCODE PRODCODE FOUND
BMX2213391 PFX-321 YES
BMX2213391 PDF-875 YES
BMX2213391 PWA-718 NO
PDFSD92851 PFX-321 NO
PDFSD92851 PDF-875 YES
PDFSD92851 PWA-718 NO
I tried, INNER JOIN, FULL OUTER JOIN, LEFT JOIN and RIGHT JOIN but I don't get the exact data I need.
Here are the queries I test.
SELECT * FROM tblProducts a INNER JOIN tblTransactions b ON a.PRODCODE = b.PRODCODE
SELECT * FROM tblProducts a FULL OUTER JOIN tblTransactions b ON a.PRODCODE = b.PRODCODE
SELECT * FROM tblProducts a LEFT JOIN tblTransactions b ON a.PRODCODE = b.PRODCODE
SELECT * FROM tblProducts a RIGHT JOIN tblTransactions b ON a.PRODCODE = b.PRODCODE
I'm pretty sure this works - SQLFiddle here: http://sqlfiddle.com/#!3/65eb1/23
WITH AllVals AS
(SELECT a.PRODCODE, b.TCODE
FROM tblProducts a
CROSS JOIN tblTransactions b)
SELECT DISTINCT c.PRODCODE,
c.TCODE,
CASE WHEN d.PRODCODE IS NULL THEN 'NO' ELSE 'YES' END AS FOUND
FROM AllVals c
LEFT OUTER JOIN tblTransactions d
ON c.PRODCODE = d.PRODCODE
AND c.TCODE = d.TCODE
http://sqlfiddle.com/#!3/65eb1/24
select DT.TCODE, DT.PRODCODE, case when (Tr2.TCODE IS null and Tr2.PRODCODE IS null) then 'No' else 'Yes' END as FOUND
from tblTransactions Tr2 right join
(
select distinct Tr.TCODE, p.PRODCODE
from tblProducts p cross join tblTransactions Tr
) DT
on DT.PRODCODE = Tr2.PRODCODE and DT.TCODE = Tr2.TCODE;