Expected output required

Analyst_SQL 3,576 Reputation points
2023-11-08T05:38:35+00:00

i want countra Head ,

Create table #Tbl_Customer (C_ID int,C_Name varchar(50))

Create table #tbl_Ledger (ID int,E_Date date,Debit_Head_ID int,Credit_Head_ID int,T_Amount Decimal(10,2),Countra_ID int )


insert into #Tbl_Customer values (1001,'Akhter')
insert into #Tbl_Customer values (1002,'Noman')
insert into #Tbl_Customer values (1003,'Bank')
insert into #Tbl_Customer values (1004,'Akram')

Insert into #tbl_Ledger values (1,'2023-09-01',1001,Null,4000,1003) 
Insert into #tbl_Ledger values (1,'2023-09-01',Null,1003,4000,Null) 
Insert into #tbl_Ledger values (1,'2023-09-01',1002,Null,1000,1003) 
Insert into #tbl_Ledger values (1,'2023-09-01',1004,Null,1000,1003) 


if i pass parameter 1003 ,then below output must display

User's image

if i pass parameter 1001 ,then below output must display

User's image

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,124 questions
SQL Server Reporting Services
SQL Server Reporting Services
A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented reports and interactive, web-based reports.
2,949 questions
SQL Server Analysis Services
SQL Server Analysis Services
A Microsoft online analytical data engine used in decision support and business analytics, providing the analytical data for business reports and client applications such as Power BI, Excel, Reporting Services reports, and other data visualization tools.
1,295 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,665 questions
{count} votes

Accepted answer
  1. Viorel 118.4K Reputation points
    2023-11-08T10:20:39.1133333+00:00

    Do you need something like this?

    select E_Date, string_agg( C_Name, CHAR(13) + CHAR(10)) as Countra_Head
    from #Tbl_Customer c
    inner join
    (
        select E_Date, Countra_ID
        from #tbl_Ledger where Debit_Head_ID = @Parameter
        union
        select E_Date, Debit_Head_ID
        from #tbl_Ledger where Countra_ID = @Parameter
    ) t on t.Countra_ID = c.C_ID
    group by E_Date
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.