全ては時の中に… : 【sql server】共通テーブルを利用...

Download 全ては時の中に… : 【SQL Server】共通テーブルを利用 …blog.livedoor.jp/akf0/archives/51395599.htmlTranslate this page同一のストアドプロシージャ内で同じテーブルを複数のSQLステートメントで使用することがあります。

If you can't read please download the document

Upload: buitruc

Post on 30-Jan-2018

224 views

Category:

Documents


9 download

TRANSCRIPT

: SQL Server

< SQL ServerOSQLVBS0 >

2008/5/2623:10

SQL Server

SQL

SQL

WITH [] AS

(

SELECT 1, 2 ...

FROM

)

SQLSELECT, INSERT, UPDATE, DELETE

Sales

No, SalesDate, IDArticleID, UnitPrice, Count

SalesDate"YYYYMMDD"

Articles

IDArticleID, Name

(SampleCTE.zip)

, , , ,

--

--

Declare @Month char(2)

Set @Month = '04'

Declare @ThisYear char(4)

Set @ThisYear = '2008'

Declare @LastYear char(4)

Set @LastYear = '2007';

WITH MonthlySales AS

(

SELECT SubString(SalesDate, 1, 6) As YearMonth,

ArticleID,

SUM(UnitPrice * Count) As TotalPrice

FROM Sales

GROUP BY SubString(SalesDate, 1, 6),

ArticleID

)

SELECT @Month As Month,

a.ArticleID,

a.Name,

SUM(ThisYear.TotalPrice) As ThisYearTotal,

SUM(LastYear.TotalPrice) As LastYearTotal

FROM Articles a

LEFT OUTER JOIN MonthlySales ThisYear

ON a.ArticleID = ThisYear.ArticleID

AND ThisYear.YearMonth = @ThisYear + @Month

LEFT OUTER JOIN MonthlySales LastYear

ON a.ArticleID = LastYear.ArticleID

AND LastYear.YearMonth = @LastYear + @Month

GROUP BY a.ArticleID,

a.Name

--

--

Declare @Month char(2)

Set @Month = '04'

Declare @ThisYear char(4)

Set @ThisYear = '2008'

Declare @LastYear char(4)

Set @LastYear = '2007';

SELECT @Month As Month,

a.ArticleID,

a.Name,

SUM(ThisYear.TotalPrice) As ThisYearTotal,

SUM(LastYear.TotalPrice) As LastYearTotal

FROM Articles a

LEFT OUTER JOIN

(SELECT SubString(SalesDate, 1, 6) As YearMonth,

ArticleID,

SUM(UnitPrice * Count) As TotalPrice

FROM Sales

GROUP BY SubString(SalesDate, 1, 6),

ArticleID) ThisYear

ON a.ArticleID = ThisYear.ArticleID

AND ThisYear.YearMonth = @ThisYear + @Month

LEFT OUTER JOIN

(SELECT SubString(SalesDate, 1, 6) As YearMonth,

ArticleID,

SUM(UnitPrice * Count) As TotalPrice

FROM Sales

GROUP BY SubString(SalesDate, 1, 6),

ArticleID) LastYear

ON a.ArticleID = LastYear.ArticleID

AND LastYear.YearMonth = @LastYear + @Month

GROUP BY a.ArticleID,

a.Name

WITHSQL;

SQL Server 2005

Microsoft MSDN

SQL Server

SQL ServerSQL

SQL Server With

akf0Comments( 0 )Trackbacks( 0 )

URL--12345

< SQL ServerOSQLVBS0 >

Blog

livedoor

TagCloud

C#C++CExcelMicrosoftOfficeOraclePowerShellPrintDocumentServerSMOSpreadSQLSQLServerStudioVB.NETVBAVirtualVistaVISUALWindowsWMI

Windows

VB.NET

Web

MS Office

Cstrtok()

C

Linux

(7)

Web (1)

(4)

(1)

(1)

(4)

(1)

(2)

(2)

Microsoft (3)

Windows (53)

(32)

PowerShell (19)

Linux (11)

(8)

IDE (15)

GrapeCity (14)

VB.NET (290)

Visual Basic (2)

ASP.NET (4)

AJAX (1)

C/C++ (20)

C# (7)

Java (5)

Google Android (3)

Lua (3)

HTML (4)

CSS (2)

VBScript (10)

WSH (5)

Ruby (2)

PowerBuilder (8)

(11)

SQL (75)

Oracle (26)

SQL Server (61)

Microsoft Office (11)

Excel (39)

Word (4)

Access (5)

VBA(MS-Office) (39)

(27)

(11)

Eee PC (6)

(1)

(2)

201512

201212

201203

201008

201007

201003

200912

200910

200908

200907

200905

200904

200903

200902

200901

200812

200811

200810

200809

200808

200807

200806

200805

200804

200803

200802

200801

200712

200711

200710

200709

200708

200707

200706

Recent Comments

Recent TrackBacks

log4net()

VB.NETlog4netBlackBack()

VB.NET()

MS-WORDExcelWindowsPEUSB(YKMI)

WindowsUSBWindows PEBCPSelectcsv()

SQL ServerBCP[oracle](Bugle Diary)

SQLsystem (BLUEPIXY)

C/C++system()unixODBC + FreeTDS + perlMS SQL Server(fujishinko )

LinuxLinuxSQL ServerWindows PE boots from USB(RISCRISKY)

WindowsUSBWindows PE(ADO.Net)mdb ConnectionString (3)

VB.NETVB.NETAccess

Profile

MCP(70-290)

powered by

Links

KITOHA

PictMaster()

Amazon

Crammedia.com

Amazon

Powered by