-2

And this Response is from the Answer suggested below. Here i need to get hubxdataitem array for every categoryName

[{"Categoryname":"General","DisplayOrder":1,
"hubxDataItems":[{"Id":3,"PatientId":40,"CategoryId":1,"ItemTitle":"Height","ItemValue":"158","ItemUnit":"cms","IsTestDone":true,"ClinicianHubXSign":0,"isAprrovedStatus":false,"isActive":true,"isDeleted":false,"Createdby":1,"CreatedDate":"2022-06-30T22:59:18.7448072","IsTestApproved":true,"PhySignDate":"2022-08-29T05:40:32.2733333"}]},
{"Categoryname":"Mental","DisplayOrder":27},
   i expected "hubxDataItems" array here too
{"Categoryname":"Hemoglobin","DisplayOrder":28},
   i expected "hubxDataItems" array here too
{"Categoryname":"SP02","DisplayOrder":3}]
   i expected "hubxDataItems" array here too
4
  • Your code (.. [dbo].[GetClinicianHubXSign] ..) seems to be specific for MS SQL, not for MySQL... Commented Sep 5, 2022 at 11:55
  • made the changes Commented Sep 5, 2022 at 12:02
  • can we achieve the above code?? Commented Sep 5, 2022 at 12:17
  • I don't think that sql server knows arrays, maybe this can help you Commented Sep 5, 2022 at 13:00

1 Answer 1

0

Your desired results are unclear. But it seems you could use something like a nested FOR JSON clause.

Select  
  hdc.Categoryname,
  hb.hubxDataItems
FROM HubxDataCategory hdc
OUTER APPLY (
 SELECT
  hb.Id,
  hb.PatientId,
  hb.CategoryId,
  hb.ItemTitle,
  hb.ItemValue,
  hb.ItemUnit,
  hb.IsTestDone,
  isNull([dbo].[GetClinicianHubXSign] (hb.patientId,hb.HTAID),0) ClinicianHubXSign,
  isnull([dbo].[GetHubXApproveStatus] (hb.patientId,hb.HTAID,hb.CategoryID),0) isAprrovedStatus,
  hb.categoryname,
  hb.isActive,
  hb.isDeleted,
  hb.Createdby,
  hb.CreatedDate,
  hb.UpdatedBy,
  hb.UpdatedDate,
  hb.DeletedBy,
  hb.DeletedDate,
  htap.IsTestApproved,
  htas.createddate as PhySignDate,
 FROM HubxDataItems hb  
 left join HubxTestApprovedForPatient htap on htap.HTAID = hb.HTAID
 left join HubxTestApprovedSignature htas on htas.HTAID = htap.HTAID
 WHERE hdc.Id = hb.categoryId
   AND hb.isActive = 1
   AND hb.IsDeleted = 0   
 FOR JSON PATH
) hb(hubxDataItems)
FOR JSON PATH;
Sign up to request clarification or add additional context in comments.

9 Comments

i want to add hubxdataitem array and pass it the above data that i shown. My question to your question is will the code after Outer apply will act as an array or not??
Have you tried it? Yes it will return a JSON array. SQL Server does not have a native Array type
I have one question after the new array starts it is not printing hubxdataitems array data again
I'm not sure what you mean, and I can't repro it because you haven't given sample data. Please give a minimal reproducible example, maybe use dbfiddle.uk to construct your data and query
I have updated the question.Under my expected result i have shared a json response data. In that response data "hubxDataItems": array is printing for every categoryId. But the answer you gave it is printing hubxDataItems array only once.Ill share the response i get from the answer you gave for better understanding
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.