如何使SQL查询将两列值显示为一列?

时间:2017-02-09 06:38:53

标签: c# sql

我有一个如下所示的表结构。我想在名称的父级(父级是分类帐组)下显示名称

I expected result like this example

Image 1

1 个答案:

答案 0 :(得分:1)

DECLARE @t1 TABLE
(
 ParentGroup varchar(20),
 LedgerName varchar(20),
 TotalDebit float,
 credit float
)

INSERT INTO @t1
SELECT *
  FROM Table1

INSERT INTO @t1
SELECT Distinct ParentGroup, ParentGroup, NULL, NULL
  FROM Table1

Select LedgerName, TotalDebit, credit from @t1
group by LedgerName, TotalDebit, credit
ORDER BY (CASE WHEN TotalDebit IS NULL THEN 0 ELSE TotalDebit END)