SQL query get record table A based on parentid Table B - sql

[SOLVED: thanks to bluefeet and Brian I got to build this working query (turned out I also needed a fourth table / view)]"
SELECT A.SalesLineID, A.ArticleResultID, B.ID, C.ID, D.Value
FROM VIEWA AS A
INNER JOIN TABLEB AS B ON A.ArticleResultID = B.ArticleResultID
AND B.Name = N'Collect' AND DAPR.Value = 1
INNER JOIN TABLEC AS C ON B.ArticleResultID = C.ID
AND C.Name='Assemble'
AND C.Value = 1
INNER JOIN TABLED AS D ON D.ArticleResultID = C.ParentId
AND D.Name = 'IndexY'
WHERE (A.SalesID = #SalesID)
[UPDATE: I've made a mistake assuming IndexY/Color and ProdId where fields Table A is some kind of parameter / property table with only 5 columns ID - NAME - VALUE - ARTICLERESULTID - PRODID. IndexY is a value of the Name field..]
I'm having trouble building the right sql query to get this trick done:
I have the following 2 tables:
Table A
ID Name Value ArticleResultID ProdID Color
1 Operation Collect 110 10 BLACK
2 IndexY 10 110 10 -
3 Operation Collect 101 11 WHITE
Table B
ID ParentID Name Value
101 110 Assemble 1
101 100 Assemble 0
Steps:
Find record in A with Name = Operation and Value = Collect and ProdId = 11 AS ORG_A
Find record in B With B.ID = ORG_A.ArticleResultId AND B.NAME = 'Assemble'AND B.VALUE = 1 AS B
Find record in A With A.ArticleResultID = B.ParentID as NEW_A
In the above scenario thats ORG_A.ArticleResultID = 11 --> B.ParentID = 110 --> NEW_A.ARTICLERESULTID = 110 --> (IndexY - Value - 10)
Much appreciated if someone can tell me how to build this query..
Best regards,
Mike D
[OLD DESCRIPTION:]
I'm having trouble building the right sql query to get this trick done:
I have the following 2 tables:
Table A
Name Value ArticleResultID IndexY Color ProdID
Operation Collect 110 10 - 0
Operation Collect 101 _ Black 100
Table B
ID ParentID Name Value Dx Dy
101 110 Assemble 1 1000 500
101 100 Assemble 0 400 300
I want to fetch all records from A where NAME equals 'operation', VALUE equals 'Collect' and PRODID = '100' but I also want (here's my problem) the IndexY value of the record in Table A with the PARENTID in Table B which joins on TABLE B.ID = A.ArticleResultID AND Name = 'Assemble' and VALUE = '1'
In the above scenario thats ParentID 110 which gives me the record in Table A with ArticleResultID 110 with IndexY (10).
Much appreciated if someone can tell me how to build this query..
Best regards,
Mike D

Since your requirements are not the clearest. How about this:
SELECT a1.*, a2.indexy as additionalIndexY
FROM ta a1
INNER JOIN tb b
ON a1.articleresultid = b.id
INNER JOIN ta a2
ON b.parentid = a2.articleresultid
WHERE a1.name = 'Operation'
AND a1.prodid = 100
AND b.name = 'Assemble'
AND b.value = 1
Here is a sqlfiddle with a working version
If you just want the record with the 110 articleresultid, then:
SELECT a2.*
FROM ta a1
INNER JOIN tb b
ON a1.articleresultid = b.id
INNER JOIN join ta a2
ON b.parentid = a2.articleresultid
WHERE a1.name = 'Operation'
AND a1.prodid = 100
AND b.name = 'Assemble'
AND b.value = 1
see the sqlfiddle for the second example

Does this work for you?
SELECT a.*,c.IndexY
FROM [TableA] a
JOIN [TableB] b ON a.ArticleResultId = b.id
AND b.Name ='Assemble'
AND b.Value = 1
JOIN [TableA] c ON b.ParentId = c.ArticleResultID
WHERE a.name = 'Operation'
AND a.Value = 'Collect'
AND a.ProdId = 100

Related

sql function case returns more than one row

Going to use this query as a subquery, the problem is it returns many rows of duplicates. Tried to use COUNT() instead of exists, but it still returns a multiple answer.
Every table can only contain one record of superRef.
The below query I`ll use in SELECT col_a, [the CASE] From MyTable
SELECT CASE
WHEN
EXISTS (SELECT 1 FROM A WHERE
A_superRef = myTable.sysno AND A_specAttr = 'value')
THEN 3
WHEN EXISTS (SELECT 1 FROM B
INNER JOIN С ON С_ReferenceForB = B_sysNo WHERE C_superRef = myTable.sysno AND b_type = 2)
THEN 2
ELSE (SELECT C_intType FROM C
WHERE C_superRef = myTable.sysno)
END
FROM A, B, C
result:
3
3
3
3
3
3...
What if you did this? Because Im guessing you are getting an implicit full outer join A X B X C then running the case statement for each row in that result set.
SELECT CASE
WHEN
EXISTS (SELECT 1 FROM A WHERE
A_superRef = 1000001838012)
THEN 3
WHEN EXISTS (SELECT 1 FROM B
INNER JOIN С ON С_ReferenceForB = B_sysNo AND C_superRef = 1000001838012 )
THEN 2
ELSE (SELECT C_type FROM C
WHERE C_superRef = 1000001838012)
END
FROM ( SELECT COUNT(*) FROM A ) --This is a hack but should work in ANSI sql.
--Your milage my vary with different RDBMS flavors.
DUAL is what I needed, thanks to Thorsten Kettner
SELECT CASE
WHEN
EXISTS (SELECT 1 FROM A WHERE
A_superRef = 1000001838012)
THEN 3
WHEN EXISTS (SELECT 1 FROM B
INNER JOIN С ON С_ReferenceForB = B_sysNo AND C_superRef = 1000001838012 )
THEN 2
ELSE (SELECT C_type FROM C
WHERE C_superRef = 1000001838012)
END
FROM DUAL

How to Update Using Join (Linked Server)

I want to match the data between 2 databases.
I have 2 Databases. Aa and Bb, and I want to compare Aa to Bb. Database Bb is in linked server
I have join code like this
SELECT
B.Employee_Name, B.Employee_NIP, B.DomainName, A.NAMA, A.NIP,
A.StatusEmployee, A.ActiveStatus
FROM
[SERVER-B].Bb.dbo.employee_hierar AS B RIGHT OUTER JOIN
Bb AS B ON B.NIP = A.Employee_NIP
and I want update A.StatusEmployee from Y to N if there is NULL data on B.Employee_Name and B.Employee_NIP
note:
SQLServer
Please Advice
You can use a join. Something like:
update a
set StatusEmployee = 'N'
from bb a LEFT JOIN
[SERVER-B].Bb.dbo.employee_hierar b
on B.NIP = A.Employee_NIP
where b.EmployeeName is null and b.Employee_NIP is null and
a.StatusEmployee = 'Y';
This is same to update multi tables in one database. Standard sql should be written like this:
UPDATE A
SET StatusEmployee = "N"
WHERE NOT EXISTS
( SELECT * FROM B WHERE B.NIP = A.Employee_NIP )

Display Summary Result in SQL Server

I have the following table structure also I have mention my expected output please help me with query as I don't know much about SQL query
Table 1 : Category
Name CatId
A 1
B 2
C 3
Table 2 : Emp Details
FName Id Dob CatId
Pratik 1 1958-04-06 2
Praveen 3 1972-05-12 1
Nilesh 2 1990-12-12 2
So far I have tried to get all result with:
SELECT A.Code,A.EmpName,A.DOB,B.cname
FROM EMPMASTER A
JOIN CATMASTER B ON A.cCode = B.ccode AND A.Compcode = B.CompCode
WHERE A.compcode = 'C0001' AND month(A.DOB) >= 1
AND MONTH(A.DOB) <= 12 AND A.termflag='L'
ORDER BY A.DOB
But my problem is, I also want summary results to be displayed
Expected Summary Output :
Grouping No Of Employees
A 1
B 2
C 0
I think you can use LEFT JOIN, GROUP BY and COUNT as follows:
SELECT [Grouping] = c.Name,
[No Of Employees] = COUNT(e.ID)
FROM Category AS c
LEFT JOIN EmpDetails AS e
ON e.CatId = c.CatId
GROUP BY c.Name;
TRY THIS:
SELECT A.NAME,
(SELECT COUNT(*) FROM #EMP B WHERE A.CATID = B.CATID) AS COUNT
FROM #TEMP A

sql update table from a join query

hi I have table as below named enrolment, I need to update the null averagemark from the data from another table so that the average of all assignments done by a student will populate the field. Can I anyone advise how to do this using Isql as the dbms?
enrolment-
student_id- course code - averagemark
1 a1 - 0
2 b2 - 0
3 c3 - 0
4 d4 - 0
assignment-
student_id- course code - assignment number mark
1 a1 1 - 50
1 b2 2 - 55
2 a1 1 - 60
2 b2 2 - 65
This example is based on Microsoft SQL Server. Copying the sample from How can I do an UPDATE statement with JOIN in SQL?:
update u
set u.assid = s.assid
from ud u
inner join sale s on
u.id = s.udid
Try something like this (providing the name of the column in the Student table is AverageMark)
UPDATE s
SET s.AverageMark = AVG(mark)
FROM Student s LEFT OUTER JOIN enrolment e
on e.Student_Id = e.Student_Id
LEFT OUTER JOIN assignment a
ON e.student_id = a.student_id
AND e.course_code = a.course_code
GROUP BY e.student_id, e.course_code

Cross table SQL Query to exclude from results records based on keyword

Here's a head-scratcher (for me)...
I have two tables A and B. A contains product descriptions, B contains product codes, quantity, and accounts. The wrinkle here is that B has a parent, child relationships on product codes.
What I would like to do is put together a result set where I see Account, Product, Description and Qty, where one of the product Qty's = 0, EXCEPT if there's a keyword in the Description in the product (whose Qty I don't care about).
So, here's my query right now:
SELECT Bp.Account, Bp.Product, A.Description, Bp.Qty FROM B
AS Bp JOIN A as Prod ON (Bp.Product = Prod.Product)
WHERE EXISTS (Select * from B AS Bc
WHERE Bp.Account = Bc.Account
AND Qty = 0)
And this returns a very nice result set of:
Account Product Description Qty
ABC XYZ DESC-BLAH 1
ABC XYY DESC-DEE 0
ABC ZXY DESC-BILP 1
BDE XAZ DESC-OOPS 1
BDE XYY DESC-DEE 0
BDE ZXY DESC-BLIP 1
So lets say I want to exclude DESC-OOPS, and all products relating to it from my result set. I would like to see:
Account Product Description Qty
ABC XYZ DESC-BLAH 1
ABC XYY DESC-DEE 0
ABC ZXY DESC-BILP 1
Is this possible to do in a single query?
Many thanks in advance!
Perhaps you could just add to your WHERE criteria:
SELECT Bp.Account, Bp.Product, A.Description, Bp.Qty FROM B
AS Bp JOIN A as Prod ON (Bp.Product = Prod.Product)
WHERE EXISTS (Select * from B AS Bc
WHERE Bp.Account = Bc.Account
AND Qty = 0)
AND Bp.ACCOUNT NOT IN (SELECT ACCOUNT FROM Table WHERE Description = 'DESC-OOPS'
I'm not sure why that doesn't work for you, but perhaps:
SELECT *
FROM (SELECT Bp.Account, Bp.Product, A.Description, Bp.Qty
FROM B AS Bp
JOIN A as Prod
ON (Bp.Product = Prod.Product)
WHERE EXISTS (Select * from B AS Bc
WHERE Bp.Account = Bc.Account
AND Qty = 0)
)sub
WHERE ACCOUNT NOT IN (SELECT ACCOUNT FROM Table WHERE Description = 'DESC-OOPS'