Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
dockies
Starting Member
25 Posts |
Posted - 2010-02-10 : 07:45:26
|
Hi,I received a request from my clients that i need to make a cross tab report out of this record.I created 2 tables one that handles the columns and the other one is for the rows:1st table (tbl_fields) (columns)ID-----FieldName-----Department1---------Name---------AB2---------Age----------AB3---------email--------AB4---------gender-------AB5---------location-----AB2nd Table (tbl_rows) (Rows)ID-----tbl_field_id-----FieldValue1----------1---------------Cray2----------2---------------253----------3---------------craygmail.com4----------4---------------male5----------5---------------canada6----------1---------------Carlo7----------2---------------288----------3---------------carlo.otaneg@gss.com9----------4---------------male10---------5---------------hawaiiI need a result like this one:Name-----Age-----Email---------------------Gender---LocationCray ---25 -----craygmail.com-----------------Male----CanadaCarlo ---28 -----carlo.otaneg@gss.com----------Male----HawaiiWhat i did so far is i created a temp table that will handle the columns based in my Column table and i will update the columns to insert the appropriate rows based on their tbl_field_id.the problem the processing takes too long even if i was just loading the top 50 records of it. i think the process was so big especially because the fields that needs to be converted to columns are usually 20 rows or more. can anyone help me with this... is there a way that i can speed up the process? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-02-10 : 08:33:01
|
Your data is not in a good format.Ideally you'd want to get the data like this:[personId] | [field] | [value]-----------+---------+-------------1 | 1 | Cray1 | 2 | 251 | 3 | craygmail.com1 | 4 | male1 | 5 | Canada2 | 1 | Carlo2 | 2 | 28.... Basically you need a column that associates the various fields so you know which person each field belongs to.Here there is a [personId] field so you know that all the fields for [personId] 1 all belong to the same person.THEN you can pivot the rows very quickly and efficiently either by using 2005's PIVOT function or by writing a dynamic cross tab report.At the moment the format you have your data doesn't let you do that as you can't (set based) work out which person each field belongs to.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
dockies
Starting Member
25 Posts |
Posted - 2010-02-10 : 09:01:04
|
Ok this is a clearer version of my problem. As you can see the look up id that can be use to connect the columns and rows table is the tbl_field_id.Also the columns are not fixed to 5 sometimes different department has more columns than the others so it has to be dynamic.Like for my example the yellow ones are the AB department and the blue one is the IT which has more columns than the AB dept.the tbl_field_id corresponds to the columns so when tbl_values has tbl_field_id 1 and field_value Cray it should look like this:Name = tbl_field_id 1Cray = Value |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-10 : 09:29:31
|
Here's a mock up of your situation with solutionSELECT * INTO tbl_fields FROM(SELECT 1 AS ID,'Name' AS FieldName,'AB' AS Department UNION ALLSELECT 2,'Age','AB' UNION ALLSELECT 3,'email','AB' UNION ALLSELECT 4,'gender','AB' UNION ALLSELECT 5,'location','AB' UNION ALLSELECT 6,'Name','IT' UNION ALL SELECT 7,'Age','IT' UNION ALLSELECT 8,'email','IT' UNION ALLSELECT 9,'gender','IT' UNION ALLSELECT 10,'location','IT' UNION ALLSELECT 11,'phone','IT' UNION ALLSELECT 12,'id number','IT' )t1SELECT * INTO tbl_rows FROM(SELECT 1 AS ID,1 AS tbl_field_id,'Cray' AS FieldValue UNION ALLSELECT 2,2,'25' UNION ALLSELECT 3,3,'craygmail.com' UNION ALLSELECT 4,4,'male' UNION ALLSELECT 5,5,'canada' UNION ALLSELECT 6,1,'Carlo' UNION ALLSELECT 7,2,'28' UNION ALLSELECT 8,3,'carlo.otaneg@gss.com' UNION ALLSELECT 9,4,'male' UNION ALLSELECT 10,5,'hawaii' UNION ALLSELECT 11,6,'James' UNION ALLSELECT 12,7,'28' UNION ALLSELECT 13,8,'james_john@yahoo.com' UNION ALLSELECT 14,9,'male' UNION ALLSELECT 15,10,'canada' UNION ALLSELECT 16,11,'02-784-4877' UNION ALLSELECT 17,12,'1248' )t2DECLARE @FieldNameList varchar(max),@Sql varchar(max),@department varchar(100)SET @department='AB'SELECT @FieldNameList= COALESCE(@FieldNameList+'],[','')+FieldNameFROM tbl_fieldsWHERE Department= @departmentSELECT @FieldNameList= '['+@FieldNameList+']'SET @Sql='SELECT '+ @FieldNameList +'FROM(SELECT Seq,FieldValue,FieldName,DepartmentFROM(SELECT ROW_NUMBER() OVER(PARTITION BY tbl_field_id ORDER BY ID) AS Seq,*FROM tbl_rows)rINNER JOIN tbl_fields fON r.tbl_field_id=f.IDWHERE Department = ''' + @department + ''')tPIVOT(MAX(FieldValue) FOR FieldName IN (' + @FieldNameList + '))p'EXEC(@SQL)drop table tbl_fieldsdrop table tbl_rowsoutput-------------------------------for Department='AB'Name Age email gender location-------------------- -------------------- -------------------- -------------------- --------------------Cray 25 craygmail.com male canadaCarlo 28 carlo.otaneg@gss.com male hawaiiFor department='IT'Name Age email gender location phone id number-------------------- -------------------- -------------------- -------------------- -------------------- -------------------- --------------------James 28 james_john@yahoo.com male canada 02-784-4877 1248------------------------------------------------------------------------------------------------------SQL Server MVP |
 |
|
|
dockies
Starting Member
25 Posts |
Posted - 2010-02-11 : 08:10:44
|
| Thanks visakh16 Works like a charm! :D |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-11 : 08:13:34
|
welcome ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
dockies
Starting Member
25 Posts |
Posted - 2010-02-12 : 01:05:19
|
Sorry visakh16 but i need you help again...It turns out that the table structure was a little different than i first described it...now we only have 1 table handling the rows and columns.as seen in my example below: Now this is what i did:--select top 10 * from simpleformDECLARE @FieldNameList nvarchar(4000),@Sql varchar(max),@URL varchar(100)SET @URL='http://www.sample.url'SELECT @FieldNameList= COALESCE(@FieldNameList+'],[','')+ a.field_nameFROM (select field_name from simpleform where URL like @URL and field_name <> '' group by field_name) aSELECT @FieldNameList= '['+@FieldNameList+']'PRINT @FieldNameListSET @Sql='SELECT '+ @FieldNameList +'FROM(SELECT seq,f.field_name,cast(f.field_value as nvarchar(4000)) as Field_ValueFROM(SELECT ROW_NUMBER() OVER(PARTITION BY field_name ORDER BY ID) AS Seq,ID,field_nameFROM simpleformWHERE URL like '''+@URL+''' and field_name <> ''''group by ID,field_name)rINNER JOIN simpleform fON r.ID = f.IDWHERE URL like ''' + @URL + ''')tPIVOT(MAX(Field_Value) FOR Field_Name IN (' + @FieldNameList + '))p'EXEC(@SQL)That works fine displaying all the dynamic columns and their rows. but i also need to display the batch no. :( problem is when i display the batch no everything became so mess up. :( most of the time the records are being doubled :(i wish i am that great with sql :(could you help me again? thanks in advance |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-12 : 01:21:39
|
| then how would you determine what all you want to view? where has department gone?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
dockies
Starting Member
25 Posts |
Posted - 2010-02-12 : 01:35:16
|
| oops i am very sorry visakh16 the department is in the table i forgot to include it. There is a "department field" right after the "batch_no"Thank you again... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-12 : 01:41:00
|
quote: Originally posted by dockies oops i am very sorry visakh16 the department is in the table i forgot to include it. There is a "department field" right after the "batch_no"Thank you again...
can you post your table data once in select format like i posted yesterday.ie. likeSELECT * INTO tbl_rows FROM(SELECT 1 AS ID,1 AS tbl_field_id,'Cray' AS FieldValue UNION ALLSELECT 2,2,'25' UNION ALLSELECT 3,3,'craygmail.com' UNION ALLSELECT 4,4,'male' UNION ALLSELECT 5,5,'canada' UNION ALLSELECT 6,1,'Carlo' UNION ALLSELECT 7,2,'28' UNION ALLSELECT 8,3,'carlo.otaneg@gss.com' UNION ALLSELECT 9,4,'male' UNION ALLSELECT 10,5,'hawaii' UNION ALLSELECT 11,6,'James' UNION ALLSELECT 12,7,'28' UNION ALLSELECT 13,8,'james_john@yahoo.com' UNION ALLSELECT 14,9,'male' UNION ALLSELECT 15,10,'canada' UNION ALLSELECT 16,11,'02-784-4877' UNION ALLSELECT 17,12,'1248' )t2 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
dockies
Starting Member
25 Posts |
Posted - 2010-02-12 : 02:02:21
|
| Im sorry for the late reply... sure here it is:SELECT * INTO tbl_fields FROM(SELECT 1 as ID,1 as Batch_No,'AB' as Department,'Name' as Field_Name,'Cray' as Field_Value UNION ALLSELECT 2 as ID,1 as Batch_No,'AB' as Department,'Age' as Field_Name,'25' as Field_Value UNION ALLSELECT 3 as ID,1 as Batch_No,'AB' as Department,'email' as Field_Name,'cray@gmail.com' as Field_Value UNION ALLSELECT 4 as ID,1 as Batch_No,'AB' as Department,'gender' as Field_Name,'Male' as Field_Value UNION ALLSELECT 5 as ID,1 as Batch_No,'AB' as Department,'location' as Field_Name,'Canada' as Field_Value UNION ALLSELECT 6 as ID,2 as Batch_No,'AB' as Department,'Name' as Field_Name,'Carlo' as Field_Value UNION ALLSELECT 7 as ID,2 as Batch_No,'AB' as Department,'Age' as Field_Name,'28' as Field_Value UNION ALLSELECT 8 as ID,2 as Batch_No,'AB' as Department,'email' as Field_Name,'carlo.fox@gmail.com' as Field_Value UNION ALLSELECT 9 as ID,2 as Batch_No,'AB' as Department,'gender' as Field_Name,'Male' as Field_Value UNION ALLSELECT 10 as ID,2 as Batch_No,'AB' as Department,'location' as Field_Name,'Hawaii' as Field_Value UNION ALLSELECT 11 as ID,3 as Batch_No,'IT' as Department,'Name' as Field_Name,'James' as Field_Value UNION ALLSELECT 12 as ID,3 as Batch_No,'IT' as Department,'Age' as Field_Name,'28' as Field_Value UNION ALLSELECT 13 as ID,3 as Batch_No,'IT' as Department,'email' as Field_Name,'james_john@yahoo.com' as Field_Value UNION ALLSELECT 14 as ID,3 as Batch_No,'IT' as Department,'gender' as Field_Name,'male' as Field_Value UNION ALLSELECT 15 as ID,3 as Batch_No,'IT' as Department,'location' as Field_Name,'canada' as Field_Value UNION ALLSELECT 16 as ID,3 as Batch_No,'IT' as Department,'phone' as Field_Name,'02-784-4877' as Field_Value UNION ALLSELECT 17 as ID,3 as Batch_No,'IT' as Department,'id number' as Field_Name,'1248' as Field_Value UNION ALLSELECT 18 as ID,4 as Batch_No,'IT' as Department,'Name' as Field_Name,'Tina' as Field_Value UNION ALLSELECT 19 as ID,4 as Batch_No,'IT' as Department,'Age' as Field_Name,'28' as Field_Value UNION ALLSELECT 20 as ID,4 as Batch_No,'IT' as Department,'email' as Field_Name,'tina_mey@yahoo.com' as Field_Value UNION ALLSELECT 21 as ID,4 as Batch_No,'IT' as Department,'gender' as Field_Name,'female' as Field_Value UNION ALLSELECT 22 as ID,4 as Batch_No,'IT' as Department,'location' as Field_Name,'canada' as Field_Value UNION ALLSELECT 23 as ID,4 as Batch_No,'IT' as Department,'phone' as Field_Name,'07-781-7777' as Field_Value UNION ALLSELECT 24 as ID,4 as Batch_No,'IT' as Department,'id number' as Field_Name,'5874' as Field_Value )t1select * from tbl_fieldsdrop table tbl_fields |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-12 : 07:36:09
|
and here's your solution. SELECT * INTO tbl_fields FROM(SELECT 1 as ID,1 as Batch_No,'AB' as Department,'Name' as Field_Name,'Cray' as Field_Value UNION ALLSELECT 2 as ID,1 as Batch_No,'AB' as Department,'Age' as Field_Name,'25' as Field_Value UNION ALLSELECT 3 as ID,1 as Batch_No,'AB' as Department,'email' as Field_Name,'cray@gmail.com' as Field_Value UNION ALLSELECT 4 as ID,1 as Batch_No,'AB' as Department,'gender' as Field_Name,'Male' as Field_Value UNION ALLSELECT 5 as ID,1 as Batch_No,'AB' as Department,'location' as Field_Name,'Canada' as Field_Value UNION ALLSELECT 6 as ID,2 as Batch_No,'AB' as Department,'Name' as Field_Name,'Carlo' as Field_Value UNION ALLSELECT 7 as ID,2 as Batch_No,'AB' as Department,'Age' as Field_Name,'28' as Field_Value UNION ALLSELECT 8 as ID,2 as Batch_No,'AB' as Department,'email' as Field_Name,'carlo.fox@gmail.com' as Field_Value UNION ALLSELECT 9 as ID,2 as Batch_No,'AB' as Department,'gender' as Field_Name,'Male' as Field_Value UNION ALLSELECT 10 as ID,2 as Batch_No,'AB' as Department,'location' as Field_Name,'Hawaii' as Field_Value UNION ALLSELECT 11 as ID,3 as Batch_No,'IT' as Department,'Name' as Field_Name,'James' as Field_Value UNION ALLSELECT 12 as ID,3 as Batch_No,'IT' as Department,'Age' as Field_Name,'28' as Field_Value UNION ALLSELECT 13 as ID,3 as Batch_No,'IT' as Department,'email' as Field_Name,'james_john@yahoo.com' as Field_Value UNION ALLSELECT 14 as ID,3 as Batch_No,'IT' as Department,'gender' as Field_Name,'male' as Field_Value UNION ALLSELECT 15 as ID,3 as Batch_No,'IT' as Department,'location' as Field_Name,'canada' as Field_Value UNION ALLSELECT 16 as ID,3 as Batch_No,'IT' as Department,'phone' as Field_Name,'02-784-4877' as Field_Value UNION ALLSELECT 17 as ID,3 as Batch_No,'IT' as Department,'id number' as Field_Name,'1248' as Field_Value UNION ALLSELECT 18 as ID,4 as Batch_No,'IT' as Department,'Name' as Field_Name,'Tina' as Field_Value UNION ALLSELECT 19 as ID,4 as Batch_No,'IT' as Department,'Age' as Field_Name,'28' as Field_Value UNION ALLSELECT 20 as ID,4 as Batch_No,'IT' as Department,'email' as Field_Name,'tina_mey@yahoo.com' as Field_Value UNION ALLSELECT 21 as ID,4 as Batch_No,'IT' as Department,'gender' as Field_Name,'female' as Field_Value UNION ALLSELECT 22 as ID,4 as Batch_No,'IT' as Department,'location' as Field_Name,'canada' as Field_Value UNION ALLSELECT 23 as ID,4 as Batch_No,'IT' as Department,'phone' as Field_Name,'07-781-7777' as Field_Value UNION ALLSELECT 24 as ID,4 as Batch_No,'IT' as Department,'id number' as Field_Name,'5874' as Field_Value )t1DECLARE @FieldNameList varchar(max),@Sql varchar(max),@department varchar(100)SET @department='IT'SELECT @FieldNameList= '[' + LEFT(t.u,LEN(t.u)-2)FROM (SELECT Field_Name + '],[' FROM tbl_fieldsWHERE Department= @department GROUP BY Field_Name ORDER BY MIN(ID) FOR XML PATH('') )t(u)SET @Sql='SELECT Batch_No,'+ @FieldNameList +'FROM(SELECT Field_Value,Field_Name,Department,Batch_NoFROM(SELECT *FROM tbl_fields)rWHERE Department = ''' + @department + ''')tPIVOT(MAX(Field_Value) FOR Field_Name IN (' + @FieldNameList + '))pORDER BY Batch_No'EXEC(@SQL)drop table tbl_fieldsoutputfor department 'AB'Batch_No Name Age email gender location1 Cray 25 cray@gmail.com Male Canada2 Carlo 28 carlo.fox@gmail.com Male Hawaiifor department 'IT'Batch_No Name Age email gender location phone id number3 James 28 james_john@yahoo.com male canada 02-784-4877 12484 Tina 28 tina_mey@yahoo.com female canada 07-781-7777 5874------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
dockies
Starting Member
25 Posts |
Posted - 2010-02-12 : 08:24:24
|
| Your example is working perfectly fine.... but when i changed the table name to the original table it says Msg 208, Level 16, State 1, Line 6Invalid object name 'simpleform'. simpleform is the name of the table.Why is that? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-12 : 08:26:58
|
| did you change it every where? can you show your used query?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
dockies
Starting Member
25 Posts |
Posted - 2010-02-12 : 08:30:18
|
| sorry my bad... i was disconnected to the server that why it cant find the table sorry about that...ill check this right away.thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-12 : 08:31:09
|
ok check and let me know ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
dockies
Starting Member
25 Posts |
Posted - 2010-02-12 : 08:41:57
|
| Wow man your the best! it sucks that i don't really understand how your code works:p but the thing is it works... damn i wish i can understand how did this code works hehe |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-12 : 08:45:16
|
Welcome ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
dockies
Starting Member
25 Posts |
Posted - 2010-02-12 : 08:56:45
|
| :) thanks... the thing that i dont understand quite well is this part:SELECT @FieldNameList= '[' + LEFT(t.u,LEN(t.u)-2)FROM (SELECT Field_Name + '],[' FROM tbl_fieldsWHERE Department= @department GROUP BY Field_Name ORDER BY MIN(ID) FOR XML PATH('') )t(u) |
 |
|
|
Next Page
|
|
|
|
|