DEV Community

Judy
Judy

Posted on

4 1 1 1 1

SQL, output associated records having many-to-many relationship row by row #eg26

Two database tables – Primary and Secondary – have same structure, where W, H and D are primary key fields.

Image description
Secondary table:

Image description
Task: Find records meeting the condition W=100, H=500, and D=300 from both tables, and output the eligible record in Primary table first and then each related record in Secondary table. Below is the expected result:

Image description
Write the following SPL code:

Image description
A1, A2: Run simple SQL statements to perform conditional queries.

A3: Return null if A2 is empty; and if A2 isn’t empty, loop through each record of A1 (represented by ~), concatenate ~ and A2, and then concatenate results of all rounds of loops.

Source

SPL open source address

Top comments (0)

Billboard image

Try REST API Generation for MS SQL Server.

DevOps for Private APIs. With DreamFactory API Generation, you get:

  • Auto-generated live APIs mapped from database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay