久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

T-SQL SELECT 查詢返回多個表的組合結果

T-SQL SELECT query to return combined result of multiple tables(T-SQL SELECT 查詢返回多個表的組合結果)
本文介紹了T-SQL SELECT 查詢返回多個表的組合結果的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想知道是否有可能并且更有效地做一些我目前在代碼中做的事情,而不是在 T-SQL 中做.

I am wondering if it is possible and more efficient to do something that I am presently doing in code, to do in T-SQL instead.

我有一個包含課程的數據庫.每門課程都可以有不同的課程,這些課程在不同的地點和不同的獎項中是不同的.

I have a database with courses. Each course can have different offerings which are variations of the course at different locations and at different awards.

這是我的(簡化的)數據庫結構和一些示例數據:

Here's my (simplified) database structure and some sample data:

CREATE TABLE tblCourse (CourseId int, CourseName varchar(50))
CREATE TABLE tblOffering (OfferingId int, CourseId int, LocationId int, AwardId int)
CREATE TABLE tblLocation (LocationId int, LocationName varchar(50))
CREATE TABLE tblAward (AwardId int, AwardName varchar(50))

INSERT INTO tblCourse VALUES (1, 'Course A')
INSERT INTO tblCourse VALUES (2, 'Course B')

INSERT INTO tblOffering VALUES (1, 1, 1, 1)
INSERT INTO tblOffering VALUES (2, 1, 2, 1)
INSERT INTO tblOffering VALUES (3, 1, 3, 1)
INSERT INTO tblOffering VALUES (4, 1, 1, 2)
INSERT INTO tblOffering VALUES (5, 2, 3, 1)

INSERT INTO tblLocation VALUES (1, 'Location A')
INSERT INTO tblLocation VALUES (2, 'Location B')
INSERT INTO tblLocation VALUES (3, 'Location C')

INSERT INTO tblAward VALUES (1, 'Award A')
INSERT INTO tblAward VALUES (2, 'Award B')

我想從 SQL 中檢索的是每個課程/獎項組合的一行.每一行都有每個位置的列,以及該 CourseId/AwardId 組合的課程是否可用.現在會有沒有既定課程/獎勵組合的行.

What I want to retrieve from SQL is a single row for each course/award combination. Each row would have columns for each location and whether a course of that CourseId/AwardId combination was available. There would be now rows for course/award combinations that have no offerings.

來自樣本數據的所需結果將是這樣的記錄集:

The required result, from the sample data, would be a recordset like this:

CourseId | CourseName | AwardId | AwardName | LocationA | LocationB | LocationC
---------+------------+---------+-----------+-----------+-----------+----------
1        | Course A   | 1       | Award A   | True      | True      | True
1        | Course A   | 2       | Award B   | True      | NULL      | NULL
2        | Course B   | 1       | Award A   | NULL      | NULL      | True

(NULL 也可能是 False)

(NULL could also be False)

目前我正在做一個帶有各種 JOINS 的簡單 SELECT 語句,它為每個課程/獎項組合提供多行,然后我遍歷代碼中的所有行并構建所需的結果.但是,我認為這不是那么有效,因為我還需要對結果進行分頁.

At present I am doing a simple SELECT statement with various JOINS which gives me multiple rows for each course/award combination, then I loop through all rows in my code and build the required result. However, I don't think this is so efficient as I also need to page results.

我認為我可以通過創建一個臨時表和一堆單獨的查詢在存儲過程中很容易地做到這一點,但我認為這不會太有效.想知道在 T-SQL 中是否有更好的方法?

I think I could do this fairly easily in a stored procedure by creating a temporary table and a bunch of separate queries, but I don't think that would be too efficient. Wondering if there is a better way of doing it in T-SQL???

所以澄清一下,我正在尋找的是一個 T-SQL 查詢或存儲過程,它將生成上述示例記錄集,并且我可以適應分頁.

So to clarify, what I am looking for is a T-SQL query or stored procedure that will produce the above sample recordset, and which I could adapt paging to.

注意.我使用的是 SQL Server 2008

NB. I am using SQL Server 2008

推薦答案

對于動態列:

DECLARE  @COLUMNS VARCHAR(max)
        ,@query varchar(1024)
        ,@True varchar(6)


SELECT @COLUMNS = 
COALESCE(
@Columns + ',[' + L.LocationName + ']',
'[' + L.LocationName +']'
)
FROM tblLocation L

SELECT @True = '''True'''

SELECT @QUERY = 'SELECT C.CourseName
                 ,A.AwardName
                 , pvt.*
FROM (SELECT O.OfferingID AS OID
            ,O.AwardID AS AID
            ,O.CourseID AS CID
            ,L.LocationName AS LID
       FROM tblOffering O Inner Join tblLocation L on L.LocationID = O.LocationID) AS S
PIVOT
(
    count(oID) For LID IN (' +@COLUMNS+ ')
) As pvt
inner join tblCourse C on C.CourseID = CID
inner join tblAward A on A.AwardID = pvt.AID'

EXEC (@QUERY)
GO

這篇關于T-SQL SELECT 查詢返回多個表的組合結果的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 夜夜操操操| 一区二区欧美在线 | 久久久久国产精品一区二区 | 三级免费 | 国产一级片一区二区 | www.久久| 亚洲a视| 国产视频第一页 | 国产成人精品福利 | 国产9999精品| 国产一区不卡在线观看 | 黄色一级毛片免费看 | 欧美中文字幕一区二区三区亚洲 | 成人午夜视频在线观看 | 中文字幕在线观看av | 午夜在线| 日日摸日日碰夜夜爽亚洲精品蜜乳 | 日韩精品久久久 | 久久久久久av | 国产农村妇女毛片精品久久麻豆 | 欧美白人做受xxxx视频 | 亚洲天堂久久新 | 欧美视频第三页 | 涩涩视频在线观看 | 欧美久久久久 | 天堂av免费观看 | 99r在线| 久久久.com| 欧美精品一区二区三区在线四季 | 龙珠z国语版在线观看 | 黄色片网站在线观看 | 伊人成人免费视频 | 国产精品久久久久久久7777 | 亚洲精品日韩一区二区电影 | 国产成人免费 | 亚洲视频在线免费观看 | 91最新视频 | 欧美成人手机视频 | 一级看片免费视频囗交动图 | 欧美视频免费在线观看 | 国产精品一区二区视频 |