
24-10-2006, 15:42
|
|
|
|
חבר מתאריך: 24.10.06
הודעות: 2
|
|
|
הסבר
ציטוט:
|
במקור נכתב על ידי ישראל K
חבל שנסית להסביר יותר מדי טוב מה אתה רוצה, הסבר מיותר רק גורם לבלבול וגוזל זמן יקר.
אתה צריך לעשות סדר בדבריך:
1. יש לך טבלה עם עמודות x, y, z ?
2. יש לך טבלה נוספת עם עמודות a, b, c ?
3. אתה צריך ליצור שאילתה שתשלוף נתונים משתי הטבלאות? ליצור טבלה חדשה המורכבת משניהם? או שמא רק טבלת "VIEW" ?
צרפתי קובץ WORD אשר מסביר כי אני צריך לארגן את הטבלה הראשונה שתראה כמו הטבלה השניה .
אני מצרף את הפתרון שיש לי במידה ותוכל להחכים אותי אשמח . במידה ולא , תודה בכל מקרה.
ושאלת השאלות:
זו עבודה לסיום תואר ראשון, שני או סתם שיעורי בית ???
זו עבודה לצורכי העבודה , אני כבר מהנדס...
|
הכי טוב שתראה את הקובץ המצורף הוא יותר קריא.
https://2006-uploaded.fresh.co.il/2...24/27649856.doc
>> QUESTION: HOW DO I CREATE AN SQL QUERY THAT CHANGE THE TABLE\VIEW OR JUST PRESENT IT FROM THE FIRST VIEW TO THE SECOND (THE DESIRED VIEW)?
>>
>> TABLE 1 – THE CURRENT VIEW:
>> NEED TO CHANGE
>> ID1 ID CONNECTION DETAILS
>> 1 11 ADDR1 12345
>> 2 11 ADDR2 23456
>> 3 11 PHONE 02-345678
>> 4 11 FAX 02-456789
>> 5 11 EMAIL A@A.COM
>> 6 22 ADDR1 34567
>> 7 22 ADDR2 45678
>> 8 22 PHONE 03-456789
>> 9 22 FAX 03-567890
>> 10 22 EMAIL B@B.COM
>>
>> TABLE 2- AFTER WE RUN THE QUERY – THIS IS WHAT I WANT TO GET AS RESULT OF THE SQL (WHICH I DON’T KNOW HOW TO WRITE):
>> AFTER CHANGES
>> ID1 ID ADDR1 ADDR2 PHONE FAX EMAIL
>> 1 11 12345 23456 02-345678 02-456789 A@A.COM
>> 2 22 34567 45678 03-456789 03-567890 B@B.COM
>
> It is, in fact, called a cross-tab but most people never figure it out from the examples given in help because those are normally for SUMs and the like. This particular cross-tab collates a vertical, highly normalized table, like what you have, into a denormalized ID-centric assembly of rows and most folks would never think of using MIN or MAX here. Here's the code to do what you ask...
>
> SELECT ID,
> MIN(CASE WHEN Connection = 'ADDR1' THEN Details ELSE '' END) AS ADDR1,
> MIN(CASE WHEN Connection = 'ADDR2' THEN Details ELSE '' END) AS ADDR2,
> MIN(CASE WHEN Connection = 'PHONE' THEN Details ELSE '' END) AS PHONE,
> MIN(CASE WHEN Connection = 'FAX' THEN Details ELSE '' END) AS FAX,
> MIN(CASE WHEN Connection = 'EMAIL' THEN Details ELSE '' END) AS EMAIL
> GROUP BY ID
> ORDER BY ID
>
> If you are using Oracle, you would need to use ROWNUM to create ID1... If you are using SQL Server, you either need to insert from the query above into a temp table that has an IDENTITY column and then select from that, or, you could use the above query as a derived table with a trigangular join to create the running count to get ID1. I will warn you that triangular joins are extremely slothfull when it comes to performance because, in reality, they are a bit more than half a cross join.
>
> I see no merit in having the ID1 column, but if you really need it, post back with the type of database you are using (Oracle, SQL Server, ???) and we'll fix you up...
>
> Also, the code was written with the assumption that the DETAILS column is a VARCHAR of some sort. If that's not true, we'll need to make a change. The other assumption is that ID and CONNECTION make either a primary or unique key. For example, you would never have two ADDR1's for any given ID. If that's not true, you may be in deep Kimchie
>
> --Jeff Moden
https://2006-uploaded.fresh.co.il/2...24/27649856.doc
|