Hi
I have below code . I want if A1.Invtype = 18 then alias A0 should be PCH1 , if A1.Invtype = 19 then alias A0 should be RPC1
SELECT T1."TransType", T1."Account",Sum(T1."Debit") As "Debit", Sum(T1."Credit") As "Credit",
CASE
WHEN T1."TransType" = '46' THEN
(Select max(A0."LocCode") from PCH1 A0 inner join VPM2 A1 on A0."DocEntry" = A1."DocEntry"
inner join OVPM A2 on A2."DocEntry" = T1."CreatedBy")
Else '0'
END AS "Location"
FROM OJDT T0 INNER JOIN JDT1 T1 ON T0."TransId" = T1."TransId"
Thanks
Prasad RaveendranPosted Jan 26, 2025, 9:59 PM
Please check whether this would help
Tuhin PaulPosted Jan 26, 2025, 9:38 AM
Previously I have worked on a system a Library Management System scenario where we needed to dynamically select a table based on the type of resource (e.g.,
BookorJournal) and retrieve relevant information. This is similar to your query, where the table name (PCH1orRPC1) is dynamically selected based onA1.InvType. Also your query was designed to retrieve financial transaction data from a database, specifically focusing on debit and credit amounts, and optionally fetching a location code based on certain conditions.So talking about the scenario where I was in similar situation that you are now, in that library management system, we have two tables:
Books: Stores information about books (
BookID,Title,Author,LocationCode, etc.).Journals: Stores information about journals (
JournalID,Title,Publisher,LocationCode, etc.).We want to write a query that dynamically selects either the
BooksorJournalstable based on theResourceType(e.g.,BookorJournal) and retrieves theLocationCodefor a given resource.Tables and Columns:
Dynamic SQL Query for Library Management System
Dynamic Table Selection:
The
CASEstatement determines the table name (BooksorJournals) based on theResourceTypecolumn in theResourcestable.Dynamic SQL Construction:
The
@SQLvariable constructs the query dynamically, selecting theLocationCodefrom the appropriate table (BooksorJournals).Execution:
The
EXEC sp_executesqlstatement executes the dynamically constructed SQL query.The
@ResourceIDparameter is passed to filter the results for a specific resource.Handling Invalid ResourceType:
If the
ResourceTypeis neitherBooknorJournal, the query prints'Invalid ResourceType'.Resources Table
Books Table
Journals Table
Output for
ResourceID = 1Output for
ResourceID = 2This approach dynamically selects the table (
BooksorJournals) based on theResourceType.Tuhin PaulPosted Jan 26, 2025, 9:16 AM
To achieve the desired logic where the alias
A0is dynamically namedPCH1orRPC1based on the value ofA1.InvType, you can use dynamic SQL or conditional table selection in your query. SQL does not directly support dynamic aliases in a single query. Instead, you can use aCASEstatement to conditionally select from the appropriate table (PCH1orRPC1) based onA1.InvTypeIf your database supports dynamic SQL (e.g., in PL/SQL or T-SQL), you can construct the query dynamically based on the value of
A1.InvType.Ramco RamcoPosted Jan 25, 2025, 7:30 AM
Hi Jaish
It slows down the query when i write below code
Thanks
Jaish MathewsPosted Jan 25, 2025, 7:19 AM
Try below.
You can modify your query to dynamically set the alias for the
Explanation of the Changes:A0table based on the value ofA1."Invtype". To achieve this, use a conditional expression to determine which table to use in the subquery. Here's how you can rewrite your query:Dynamic Table Reference:
CASEstatement within the subquery determines whether to usePCH1orRPC1based onA1."Invtype".Subquery Logic:
A1."Invtype" = 18, it selects theMAX(A0."LocCode")fromPCH1.A1."Invtype" = 19, it selects theMAX(A0."LocCode")fromRPC1.Nested Queries:
Grouping and Aggregation:
GROUP BYforT1."TransType"andT1."Account"to work with aggregate functions likeSUM().This approach allows dynamic aliasing based on your condition (
A1."Invtype") and ensures that the query produces the expected results.