2016-04-08 57 views
0

Ben PK/FK ilişkileri benim adlandırma kuralları den kendiliğinden gelirBirden çok dinamik pivot ile bir sorgu yazmak mümkün mü?

Partners 
=============== 
id | name 
--------------- 
1 | "John" 
2 | "Jacob" 


    Regions 
==================== 
id | name 
-------------------- 
1 | "Antarctica" 
2 | "Coruscant" 
3 | "Iraq" 

     Destinations 
============================ 
id | partner_id | region_id 
---------------------------- 
1 |  1  | 1 
2 |  1  | 2 
3 |  2  | 2 

    Surveys 
================ 
id | title 
---------------- 
1 | "Part 1" 
2 | "Bonus" 


     Versions 
====================== 
id | title 
---------------------- 
1 | "First Version" 
2 | "Version #2" 


       Permissions 
========================================== 
id | partner_id | survey_id | version_id 
------------------------------------------ 
1 |  1  |  1  |  1 
2 |  1  |  1  |  2 
3 |  2  |  1  |  1 


      Sections 
======================================= 
id | survey_id | title 
--------------------------------------- 
1 | 1  | "Some Section" 
2 | 1  | "Some Other Section" 
3 | 2  | "Yet Another Section" 


     Subsections 
================================= 
id | title | section_id 
-------------------------------- 
1 | "Subsec" |  1 
2 | "Subsecc" |  1 
3 | "Ss"  |  2 
4 | "kasjhja" |  3 
5 | "aslkdjas" |  3 


         Questions 
=================================================================== 
id | subsection_id |    qtext   | version_id 
------------------------------------------------------------------- 
1 |  1  | "What is 1+1?"    |  1 
2 |  1  | "What is 2+2?"    |  1 
3 |  1  | "What is one plus one?"  |  2 
4 |  1  | "What is two plus two?"  |  2 
5 |  2  | "How are you doing?"   |  1 
6 |  2  | "What's up?"     |  2 
7 |  3  | "What would you rate her?" |  1 
8 |  3  | "What would you rate her?" |  2 
9 |  4  | "Number of bits in a byte?" |  1 
10 |  5  | "What year is it?"   |  1 
11 |  5  | "The year is ...."?   |  2 

       Answers 
======================================== 
id | question_id | partner_id | val 
---------------------------------------- 
1 |  1  |  1  | 2 
2 |  1  |  2  | 2 
3 |  2  |  1  | 4 
4 |  2  |  2  | 4 
5 |  3  |  1  | 2 
6 |  4  |  1  | 69 
7 |  5  |  1  | 55 
8 |  6  |  1  | 10 
9 |  7  |  1  | 9 
10 |  8  |  1  | 10 
11 |  9  |  1  | 8 
12 |  10  |  1  | 2016 
13 |  11  |  1  | 2016 


      MarkedAsFinished 
=========================================== 
id | partner_id | survey_id | version_id 
------------------------------------------- 
    1 | 1  | 1  |  1 
    2 | 1  | 2  |  1 
    3 | 1  | 1  |  2 
    4 | 2  | 1  |  1 

gibi tablolar var. Mümkünse, 1 eşit (Versions.id karşılık gelir) versid ile yapılan ise, örneğin, aşağıdaki tablo dönmek bir sorgu

CREATE PROCEDURE AnswerDump 
    @versid INT 
AS 
    ... 

istiyorum.

name | Destined for Antarctica? | Destined for Coruscant? | Destined for Iraq? | Finished with Part 1? | Finished with Bonus? | Permission to contact about Part 1? | Permission to contact about Bonus? | What is 1+1? | What is 2+2? | How are you doing? | What would you rate her? | Number of bits in a byte? | What year is it? 
============================================================================================================================================================================================================================================================================================================================================= 
"John" |   Yes    |   Yes   |  No   |   Yes   |   Yes   |    Yes     |     No    |  2  |  4  |  55   |    9   |    8    |  2016 
"Jacob" |   No    |   Yes   |  No   |   Yes   |   No   |    Yes     |     No    |  2  |  4  |  0   |    0   |    0    |  0 

Peki temelde yaptığı satırlar olarak Partners ve sütunlar gibi tüm diğer ilgili bilgileri kullanmaktır. Henüz doldurulmamış sorular için sıfırın cevaplarını doldurduğunu görebilirsiniz. Bir SQL görünümü içinde bir soruya "benim için kod yazın", ama bunu yazmaya içine çaba koymak vermedi gibi bu görünüyor eğer ...

Maalesef

+1

örnek tabloları veriyle şekilde yüklenmesini Büyük ama http: //sqlfiddle.com çok daha iyi olacak. Metni komut dosyasına dönüştürmek zaman alır. – lad2025

+0

İşte başlamak için harika bir yer. http://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/ –

+0

Bu bana bir soru için kod yazmam gibi görünmüyor - BECAUSE BİR SORUNUZ YOKTUR. Ancak, sorunuz ne yazmalıyım, o zaman benim için bir kod yazayım. Peki, sorunuz nedir? – Hogan

cevap

0

Evet, olarak mümkün, ama hayır, SQL işlevi veya SP'de bunları geliştirmek gerekir. Bir SQL pivotun

Bir Örnek aşağıdaki gibi olacaktır:

Normal SQL:

USE AdventureWorks2008R2 ; 
GO 
SELECT DaysToManufacture, AVG(StandardCost) AS AverageCost 
FROM Production.Product 
GROUP BY DaysToManufacture; 

Pivot SQL:

-- Pivot table with one row and five columns 
SELECT 'AverageCost' AS Cost_Sorted_By_Production_Days, 
[0], [1], [2], [3], [4] 
FROM 
(SELECT DaysToManufacture, StandardCost 
    FROM Production.Product) AS SourceTable 
PIVOT 
(
AVG(StandardCost) 
FOR DaysToManufacture IN ([0], [1], [2], [3], [4]) 
) AS PivotTable; 
İlgili konular