Unpivot with column name 需要帮助
Unpivot with column name help needed
我有3个table
1.User
2.Item
3.Data
Table 架构:
1.CREATE TABLE User (
'UserID' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
'UserName' TEXT NOT NULL,
'TINNUMBER' TEXT)
2.CREATE TABLE ITEM(
'ItemNo' INTEGER NOT NULL,
'InvNumber' INTEGER not null
'ITEMID' TEXT,
'ItemName'TEXT,
'Tax1' INTEGER,
'Tax2' INTEGER,
'Tax3' INTEGER,
'Tax4' INTEGER,)
3.CREATE TABLE DATA(
'InvNumber' INTEGER not null
'UserID' INTEGER,
'UserName' TEXT,
'TotalAmount' NUMERIC
)
select P.TINNUMBER
,SI.InvNumber
,SI.TotalAmount
,SII.ItemName
,SII.Tax1
,SII.Tax2
,SII.Tax3
,SII.Tax4
from SalesInvoices SI
Inner Join user P ON SI.UserID=P.UserID
Left Outer Join Item SII on SI.InvNumber=SII.InvNumber
我加入了 3 个 table,但我希望 tax1、tax2、tax3 和 tax4 数据与其他列数据一起显示在行中。需要帮助提前致谢
'<table><tr><th>TINNumber</th><th>InvNumber</th><th>TotalAmount</th><th>ItemName</th><th>Tax1</th></tr>
<tr><th>TINNumber</th><th>InvNumber</th><th>TotalAmount</th>
<th>ItemName</th><th>Tax2</th></tr>
<tr><th>TINNumber</th><th>InvNumber</th><th>TotalAmount</th>
<th>ItemName</th><th>Tax3</th></tr>
<tr><th>TINNumber</th><th>InvNumber</th><th>TotalAmount</th>
<th>ItemName</th><th>Tax4</th></tr></table>'
使用 UNION ALL 对其进行反透视。不幸的是,Sqlite 没有 pivot/unpivot 快捷方式。尝试
select P.TINNUMBER
,SI.InvNumber
,SI.TotalAmount
,SII.ItemName
,SII.Tax
from SalesInvoices SI
Inner Join user P ON SI.UserID=P.UserID
Left Outer Join (
Select InvNumber, ItemName, Tax1 as Tax
From Item
Union All
Select InvNumber, ItemName, Tax2 as Tax
From Item
Union All
Select InvNumber, ItemName, Tax3 as Tax
From Item
Union All
Select InvNumber, ItemName, Tax4 as Tax
From Item
) SII on SI.InvNumber=SII.InvNumber
我有3个table
1.User
2.Item
3.Data
Table 架构:
1.CREATE TABLE User (
'UserID' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
'UserName' TEXT NOT NULL,
'TINNUMBER' TEXT)
2.CREATE TABLE ITEM(
'ItemNo' INTEGER NOT NULL,
'InvNumber' INTEGER not null
'ITEMID' TEXT,
'ItemName'TEXT,
'Tax1' INTEGER,
'Tax2' INTEGER,
'Tax3' INTEGER,
'Tax4' INTEGER,)
3.CREATE TABLE DATA(
'InvNumber' INTEGER not null
'UserID' INTEGER,
'UserName' TEXT,
'TotalAmount' NUMERIC
)
select P.TINNUMBER
,SI.InvNumber
,SI.TotalAmount
,SII.ItemName
,SII.Tax1
,SII.Tax2
,SII.Tax3
,SII.Tax4
from SalesInvoices SI
Inner Join user P ON SI.UserID=P.UserID
Left Outer Join Item SII on SI.InvNumber=SII.InvNumber
我加入了 3 个 table,但我希望 tax1、tax2、tax3 和 tax4 数据与其他列数据一起显示在行中。需要帮助提前致谢
'<table><tr><th>TINNumber</th><th>InvNumber</th><th>TotalAmount</th><th>ItemName</th><th>Tax1</th></tr>
<tr><th>TINNumber</th><th>InvNumber</th><th>TotalAmount</th>
<th>ItemName</th><th>Tax2</th></tr>
<tr><th>TINNumber</th><th>InvNumber</th><th>TotalAmount</th>
<th>ItemName</th><th>Tax3</th></tr>
<tr><th>TINNumber</th><th>InvNumber</th><th>TotalAmount</th>
<th>ItemName</th><th>Tax4</th></tr></table>'
使用 UNION ALL 对其进行反透视。不幸的是,Sqlite 没有 pivot/unpivot 快捷方式。尝试
select P.TINNUMBER
,SI.InvNumber
,SI.TotalAmount
,SII.ItemName
,SII.Tax
from SalesInvoices SI
Inner Join user P ON SI.UserID=P.UserID
Left Outer Join (
Select InvNumber, ItemName, Tax1 as Tax
From Item
Union All
Select InvNumber, ItemName, Tax2 as Tax
From Item
Union All
Select InvNumber, ItemName, Tax3 as Tax
From Item
Union All
Select InvNumber, ItemName, Tax4 as Tax
From Item
) SII on SI.InvNumber=SII.InvNumber