我写了一个可怕的问题查询
SELECT TOP 25
uinv = (CASE
WHEN exported = 1 THEN 'Sent To Accounts'
WHEN queued = 1 THEN 'Finalised'
WHEN reviewed = 1 THEN 'Pro Forma'
WHEN started = 1 THEN 'New'
END)
,
(
CAST(acc_id AS VARCHAR) + ' / ' +
CAST(transactiontype AS VARCHAR) + ' / ' +
CAST(reference AS VARCHAR) + ' / '
)
AS label
FROM tablename;
工作正常,但我需要在AS标签中获取uinv字段。我尝试了各种各样的排列,但我无法做到这一点......
请帮忙吗?
谢谢,克里斯
答案 0 :(得分:1)
SELECT
tbl.uinv,
(
tbl.uinv+
CAST(acc_id AS VARCHAR) + ' / ' +
CAST(transactiontype AS VARCHAR) + ' / ' +
CAST(reference AS VARCHAR) + ' / '
)
AS label
FROM
(
SELECT TOP 25
uinv = (CASE
WHEN exported = 1 THEN 'Sent To Accounts'
WHEN queued = 1 THEN 'Finalised'
WHEN reviewed = 1 THEN 'Pro Forma'
WHEN started = 1 THEN 'New'
END),
tablename.acc_id,
tablename.transactiontype,
tablename.reference
FROM tablename
) AS tbl
答案 1 :(得分:1)
如果您只想将中的uinv改为作为单独的列,那么您需要做的就是将案例移到内部;
SELECT TOP 25
(
CAST(acc_id AS VARCHAR) + ' / ' +
CAST(transactiontype AS VARCHAR) + ' / ' +
CAST(reference AS VARCHAR) + ' / ' +
CASE
WHEN exported = 1 THEN 'Sent To Accounts'
WHEN queued = 1 THEN 'Finalised'
WHEN reviewed = 1 THEN 'Pro Forma'
WHEN started = 1 THEN 'New'
END
)
AS label
FROM tablename;
如果你想在标签中加上作为一个单独的列,你可以使用CTE;
WITH cte AS (
SELECT *,CASE
WHEN exported = 1 THEN 'Sent To Accounts'
WHEN queued = 1 THEN 'Finalised'
WHEN reviewed = 1 THEN 'Pro Forma'
WHEN started = 1 THEN 'New'
END uinv
FROM tablename
)
SELECT uinv,
CAST(acc_id AS VARCHAR) + ' / ' +
CAST(transactiontype AS VARCHAR) + ' / ' +
CAST(reference AS VARCHAR) + ' / ' +
uinv
AS label
FROM cte;