如何更改SQL表中的行和列

时间:2014-01-06 13:56:41

标签: c# sql .net

SQL TABLE结果就像这样

InvoiceId Tax1 Tax1value  Tax2  Tax2Value Tax3   Tax3Value
10         GST   2.0       PST    2.8      GRT     3.1

查询以获取结果

SELECT InvoiceId,Tax1,Tax1value,Tax2,Tax2value,Tax3,Tax3Value
FROM Invoice
WHERE InvoicId=10

现在我需要Table OutPut像Blow for Same Invoice Table

GST    PST    GRT
2.0    2.8    3.1

如何获得这样的结果

2 个答案:

答案 0 :(得分:0)

喜欢什么?

SELECT Tax1value AS GST, Tax2value AS PST, Tax3Value AS GRT
FROM Invoice
WHERE InvoiceId = 10

来自http://www.1keydata.com/sql/sql-as.html

  

关键字AS用于为列或表分配别名。

结果将是;

| GST | PST | GRT |
|-----|-----|-----|
|   2 |   3 |   3 |

这里有 SQL Fiddle DEMO

答案 1 :(得分:0)

请参阅此question,也许对您有用。

适用于您的案例将是这样的:

SELECT
  invoiceid,
  MAX(CASE WHEN (tax1= 'GST') THEN tax1value ELSE NULL END) AS GST,
  MAX(CASE WHEN (tax2 = 'PST') THEN tax2value ELSE NULL END) AS PST,
  MAX(CASE WHEN (tax3 = 'GRT') THEN tax3value ELSE NULL END) AS GRT
FROM
  invoice
GROUP BY invoiceid
ORDER BY invoiceid