我试图按月每个日期获取所有数据。
我有这张桌子。我在sqlfiddle创建它时遇到问题。
CREATE TABLE Outlet
(
IdOutlet [int] NOT NULL,
OutletCode [varchar](100) NOT NULL,
Description [varchar](225) NULL
);
出口记录
1 | K-AEON | Desc1
2 | K-LFTEJM | Desc2
和这张表
CREATE TABLE [dbo].[SalesOrderHeader]
(
[TransactionNo] [varchar](32) NOT NULL,
[DocumentNo] [varchar](32) NOT NULL,
[TransDate] [datetime] NOT NULL,
[ETADate] [datetime] NOT NULL,
[ExternalDocument] [varchar](32) NOT NULL,
[Reference] [varchar](64) NOT NULL,
[RefTransNo] [varchar](32) NOT NULL,
[Category] [varchar](32) NOT NULL,
[CustomerCode] [varchar](16) NOT NULL,
[Remark] [varchar](64) NOT NULL,
[Remark2] [varchar](64) NOT NULL,
[TermOfPayment] [datetime] NOT NULL,
[Currency] [varchar](16) NOT NULL,
[Rate] [decimal](38, 20) NOT NULL,
[UseVAT] [int] NOT NULL,
[SubTotal] [decimal](38, 20) NOT NULL,
[DiscPercent] [decimal](38, 20) NOT NULL,
[DiscAmount] [decimal](38, 20) NOT NULL,
[GrandTotal] [decimal](38, 20) NOT NULL,
[TotalReceivable] [decimal](38, 20) NOT NULL,
[FromCustomerCode] [varchar](32) NOT NULL,
[PostingStatus] [int] NULL,
[PostingBy] [varchar](16) NULL,
[PostingDate] [datetime] NULL,
[CreatedDate] [datetime] NOT NULL,
[CreatedBy] [varchar](16) NOT NULL,
[ModifiedDate] [datetime] NOT NULL,
[ModifiedBy] [varchar](16) NOT NULL,
[CustomerReceivable] [varchar](16) NULL,
[LocationCode] [varchar](16) NOT NULL,
[VatPercent] [float] NOT NULL,
[VatAmount] [float] NULL,
[FinalReleasedDate_Outlet] [datetime] NULL,
[CancelStatus] [int] NULL,
[CancelReason] [varchar](100) NULL,
[CancelledBy] [varchar](50) NULL,
CONSTRAINT [PK__SalesOrd__554342D81FCDBCEB]
PRIMARY KEY CLUSTERED ([TransactionNo] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
如你所见,我有两张桌子。现在我有了这个查询来获取我的所有数据。
select
CustomerCode, CONVERT(date, transdate) tanggal,
SUM(totalreceivable) total
from
SalesOrderHeader
where
TransDate between '$start_date' and '$end_date'
group by
CONVERT(date, transdate), CustomerCode
以下是一个示例结果
K-AEON 2016-07-18 2841600.00000000000000000000
K-LFTEJM 2016-07-17 4862190.00000000000000000000
但我想要这个结果
|1 | 2 | 3 | 4-16 | 17 | 18 | 19-29 |30|
K-AEON |..| | | ...... | 2841600 | | ..... |..|
K-LFTEJM |..| | | | | 4862190 | |..|
我怎样才能实现这一目标?我可以使用什么方法?
我想把它放在sqlfiddle
上,但我得到了这个结果Create script error
答案 0 :(得分:2)
考虑您的示例结果,
DECLARE @T AS TABLE (NAME VARCHAR(50), TransDate DATETIME, TotalReceivable DECIMAL(38, 20))
INSERT INTO @T VALUES ('K-AEON', '2016-07-18', 2841600.00000000000000000000)
INSERT INTO @T VALUES ('K-LFTEJM', '2016-07-17' ,4862190.00000000000000000000)
SELECT * FROM (SELECT day(Transdate) DayOfTrans,*FROM @T) T
PIVOT(Max(TotalReceivable) FOR DayOfTrans
IN ([1], [2], [3], [17], [18], [19], [20])) FinalRes
所需的输出是,
Name TransDate 1 2 3 17 18 19 20
-------- ----------------------- ------- ------- ------- ------- ------- ------- -------
K-LFTEJM 2016-07-17 00:00:00.000 NULL NULL NULL 4862190.00000000000000000000 NULL NULL NULL
K-AEON 2016-07-18 00:00:00.000 NULL NULL NULL NULL 2841600.00000000000000000000 NULL NULL
答案 1 :(得分:1)
对于选定的日期;
CREATE TABLE #Test (OutletCode VARCHAR(50), Date_ DATETIME, TotalAmount DECIMAL(38, 20))
DECLARE @SelectedDate DATETIME = '2016.07.01'
DECLARE @CountOfDay INT = 1
DECLARE @DayCountForSelectedMonth INT = DATEDIFF(day, @SelectedDate, dateadd(month, 1, @SelectedDate))
DECLARE @Days NVARCHAR(MAX)
DECLARE @Query NVARCHAR(MAX)
INSERT INTO #Test VALUES ('K-AEON', '2016-07-18', 2841600.00000000000000000000)
INSERT INTO #Test VALUES ('K-LFTEJM', '2016-07-17' ,4862190.00000000000000000000)
;WITH CountOfDayTable(CountOfDay)
AS
(
SELECT @CountOfDay
UNION ALL
SELECT CountOfDay + 1
FROM CountOfDayTable
WHERE CountOfDay < @DayCountForSelectedMonth
)
SELECT
@Days = COALESCE(@Days + ', ','') + '[' + CAST(CountOfDay AS NVARCHAR(4)) + ']'
FROM CountOfDayTable C
-- SELECT @Days -- [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31]
SET @Query =
'SELECT * FROM (SELECT day(Date_) DayOfTrans, * FROM #Test) T
PIVOT(Max(TotalAmount) FOR DayOfTrans IN -- OR MIN
(' +
@Days
+
')
) FinalRes';
-- SELECT @Query
EXEC sp_executesql @Query