473,416 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Can we use PIVOT here?

OraMaster
135 100+
Hi Experts,

Here we go. I have below DDL,DML and what I expecting.

Expand|Select|Wrap|Line Numbers
  1. create table langdtl(id int,speak_flag int,read_flag int,ia_flag int,lit1_flag int,lit2_flag int,lit3_flag int,lang nvarchar(2))
Expand|Select|Wrap|Line Numbers
  1. insert into langdtl values(1,1,1,1,1,1,0,'fi')
  2. insert into langdtl values(1,1,1,0,0,0,1,'sv')
  3. insert into langdtl values(1,1,1,1,0,1,0,'en')
  4. insert into langdtl values(1,1,1,0,1,1,1,'de')
Expand|Select|Wrap|Line Numbers
  1. insert into langdtl values(2,1,1,0,0,1,1,'fi')
  2. insert into langdtl values(2,1,0,1,1,1,0,'sv')
  3. insert into langdtl values(2,1,1,0,1,0,1,'en')
select * from langdtl

I need output data as shown below
Expand|Select|Wrap|Line Numbers
  1. id   speak_flag   read_flag   ia_flag   lit1_flag   lit2_flag   lit3_flag
  2. -------------------------------------------------------------------------
  3. 1    de,en,fi,sv  de,en,fi,sv en,fi     de,fi       de,en,fi    de,sv
  4. 2    de,en,fi,sv  en,fi       sv        en,sv       fi,sv       en,fi
lang should have to display in alphabetical order separated by comma.

Thanks!
Bhushan
Oct 13 '10 #1

✓ answered by gpl

Thanks for reformatting, its a lot clearer now.

First up, create this function, it returns a list of languages in comma-separated format, depending on the parameters.
Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION dbo.AllLangs (@ID Int, @Which Varchar(255))
  2. RETURNS Varchar(1024)
  3. AS
  4. BEGIN
  5.  
  6. DECLARE @LangList varchar(1024)
  7.  
  8.   SELECT @LangList = COALESCE(@LangList + ',', '') + lang)
  9.   FROM langdtl
  10.   WHERE ID = @ID AND
  11.         Case @Which
  12.           When 'speak_flag' Then speak_flag
  13.           When 'read_flag'  Then read_flag
  14.           When 'ia_flag'    Then ia_flag
  15.           When 'lit1_flag'  Then lit1_flag
  16.           When 'lit2_flag'  Then lit2_flag
  17.           When 'lit3_flag'  Then lit3_flag
  18.         Else 0
  19.         End = 1  
  20.   Order by lang
  21.  
  22.     RETURN @LangList
  23. END
  24.  
Now your query should look like this
Expand|Select|Wrap|Line Numbers
  1. select  ID,
  2.         dbo.AllLangs(ID, 'speak_flag') as [speak_flag],
  3.         dbo.AllLangs(ID, 'read_flag')  as [read_flag],
  4.         dbo.AllLangs(ID, 'ia_flag')    as [ia_flag],
  5.         dbo.AllLangs(ID, 'lit1_flag')  as [lit1_flag],
  6.         dbo.AllLangs(ID, 'lit2_flag')  as [lit2_flag],
  7.         dbo.AllLangs(ID, 'lit3_flag')  as [lit3_flag]
  8. From lang
  9. Order by ID
  10.  
thsi should give you the results that you are after

4 1316
gpl
152 100+
Could you explain your requirement ?
From the data you posted, I would expected to have seen
Expand|Select|Wrap|Line Numbers
  1. 1 de,en,fi,sv 
  2. 2 en,fi,sv 
  3.  
ps, please use the code tags, it makes it easier to identify what is narrative and what is code and it makes the code clearer
Oct 13 '10 #2
OraMaster
135 100+
Okey here what I get after running one select SQL against my table langdtl.
Please see input in attached file

Now what I need in output is

Expand|Select|Wrap|Line Numbers
  1. id   a            b           c         d           e           f
  2. -------------------------------------------------------------------------
  3. 1    de,en,fi,sv  de,en,fi,sv en,fi     de,fi       de,en,fi    de,sv
  4. 2    de,en,fi,sv  en,fi       sv        en,sv       fi,sv       en,fi
Oct 13 '10 #3
gpl
152 100+
Thanks for reformatting, its a lot clearer now.

First up, create this function, it returns a list of languages in comma-separated format, depending on the parameters.
Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION dbo.AllLangs (@ID Int, @Which Varchar(255))
  2. RETURNS Varchar(1024)
  3. AS
  4. BEGIN
  5.  
  6. DECLARE @LangList varchar(1024)
  7.  
  8.   SELECT @LangList = COALESCE(@LangList + ',', '') + lang)
  9.   FROM langdtl
  10.   WHERE ID = @ID AND
  11.         Case @Which
  12.           When 'speak_flag' Then speak_flag
  13.           When 'read_flag'  Then read_flag
  14.           When 'ia_flag'    Then ia_flag
  15.           When 'lit1_flag'  Then lit1_flag
  16.           When 'lit2_flag'  Then lit2_flag
  17.           When 'lit3_flag'  Then lit3_flag
  18.         Else 0
  19.         End = 1  
  20.   Order by lang
  21.  
  22.     RETURN @LangList
  23. END
  24.  
Now your query should look like this
Expand|Select|Wrap|Line Numbers
  1. select  ID,
  2.         dbo.AllLangs(ID, 'speak_flag') as [speak_flag],
  3.         dbo.AllLangs(ID, 'read_flag')  as [read_flag],
  4.         dbo.AllLangs(ID, 'ia_flag')    as [ia_flag],
  5.         dbo.AllLangs(ID, 'lit1_flag')  as [lit1_flag],
  6.         dbo.AllLangs(ID, 'lit2_flag')  as [lit2_flag],
  7.         dbo.AllLangs(ID, 'lit3_flag')  as [lit3_flag]
  8. From lang
  9. Order by ID
  10.  
thsi should give you the results that you are after
Oct 13 '10 #4
ck9663
2,878 Expert 2GB
Here's an alternative code with no function and not an RBAR processing...

The catch:
1. You have to know how many records you have on the language table - You need this on the TOP x portion.
2. You have to know what the maximum number of language are there per ID - You need this on the COALESCE( MIN( CASE WHEN rownum = x ....)) portion, which you need to adjust depending on the value of x.

Expand|Select|Wrap|Line Numbers
  1.  
  2. ;with language_table
  3. as 
  4. (   select
  5.       id, 
  6.       rownum = row_number() over(partition by id order by id),
  7.       speak_lang = isnull(lang + space(nullif(0, speak_flag)) + ',',''),
  8.       read_lang = isnull(lang + space(nullif(0, read_flag))+ ',',''),
  9.       ia_lang = isnull(lang + space(nullif(0, ia_flag))+ ',',''),
  10.       lit1_lang = isnull(lang + space(nullif(0, lit1_flag))+ ',',''),
  11.       lit2_lang = isnull(lang + space(nullif(0, lit2_flag))+ ',',''),
  12.       lit3_lang = isnull(lang + space(nullif(0, lit3_flag))+ ',',''),
  13.       speak_flag, read_flag, ia_flag, lit2_flag, lit3_flag
  14.    from langdtl  
  15. ),
  16. sorted_table 
  17. as
  18. (
  19.    select TOP 10000000 * from language_table order by id asc, rownum desc
  20. ), 
  21. concatenated
  22. as
  23. (  
  24.    select 
  25.       id,   
  26.       speak_lang = 
  27.       COALESCE( MIN( CASE WHEN rownum = 4 THEN speak_lang END) , '' )  +
  28.        COALESCE( MIN( CASE WHEN rownum = 3 THEN speak_lang END) , '' ) + 
  29.        COALESCE( MIN( CASE WHEN rownum = 2 THEN speak_lang END) , '' ) +  
  30.        COALESCE( MIN( CASE WHEN rownum = 1 THEN speak_lang END) , '' ),
  31.  
  32.       read_lang = 
  33.       COALESCE( MIN( CASE WHEN rownum = 4 THEN read_lang END) , '' )  +
  34.        COALESCE( MIN( CASE WHEN rownum = 3 THEN read_lang END) , '' ) + 
  35.        COALESCE( MIN( CASE WHEN rownum = 2 THEN read_lang END) , '' ) +  
  36.        COALESCE( MIN( CASE WHEN rownum = 1 THEN read_lang END) , '' ),
  37.  
  38.       ia_lang = 
  39.       COALESCE( MIN( CASE WHEN rownum = 4 THEN ia_lang END) , '' )  +
  40.        COALESCE( MIN( CASE WHEN rownum = 3 THEN ia_lang END) , '' )  +
  41.        COALESCE( MIN( CASE WHEN rownum = 2 THEN ia_lang END) , '' )  +
  42.        COALESCE( MIN( CASE WHEN rownum = 1 THEN ia_lang END) , '' ), 
  43.  
  44.       lit1_lang = 
  45.       COALESCE( MIN( CASE WHEN rownum = 4 THEN lit1_lang END) , '' ) +
  46.        COALESCE( MIN( CASE WHEN rownum = 3 THEN lit1_lang END) , '' ) +
  47.        COALESCE( MIN( CASE WHEN rownum = 2 THEN lit1_lang END) , '' ) +
  48.        COALESCE( MIN( CASE WHEN rownum = 1 THEN lit1_lang END) , '' ), 
  49.  
  50.  
  51.       lit2_lang = 
  52.       COALESCE( MIN( CASE WHEN rownum = 4 THEN lit2_lang END) , '' ) +
  53.        COALESCE( MIN( CASE WHEN rownum = 3 THEN lit2_lang END) , '' ) +
  54.        COALESCE( MIN( CASE WHEN rownum = 2 THEN lit2_lang END) , '' ) +
  55.        COALESCE( MIN( CASE WHEN rownum = 1 THEN lit2_lang END) , '' ), 
  56.  
  57.       lit3_lang = 
  58.       COALESCE( MIN( CASE WHEN rownum = 4 THEN lit3_lang END) , '' ) +
  59.        COALESCE( MIN( CASE WHEN rownum = 3 THEN lit3_lang END) , '' ) +
  60.        COALESCE( MIN( CASE WHEN rownum = 2 THEN lit3_lang END) , '' ) +
  61.        COALESCE( MIN( CASE WHEN rownum = 1 THEN lit3_lang END) , '' ) 
  62.  
  63.    from  sorted_table
  64.    group by id
  65. )
  66. select id, 
  67.    speak_lang = substring(speak_lang,1, len(speak_lang)-1),
  68.    read_lang = substring(read_lang,1, len(read_lang)-1),
  69.    ia_lang = substring(ia_lang,1, len(ia_lang)-1),
  70.    lit1_lang = substring(lit1_lang,1, len(lit1_lang)-1),
  71.    lit2_lang = substring(lit2_lang,1, len(lit2_lang)-1),
  72.    lit3_lang = substring(lit3_lang,1, len(lit3_lang)-1)
  73. from concatenated   
  74.  
  75.  
The second CTE (the one with TOP 10000000) is necessary to sort the language in descending based on the position of the row. It looks like on your desired output, the last language per ID is the first on the string. The top 10M is a "guess-timate". Just in case your language table is that big. If you don't need the sorting of language remove that. You have to refer the CONCATENATED CTE to LANGUAGE_TABLE CTE instead.

The last CTE is needed to remove the last comma inserted.

If you can't deal with the "The Catch" part, convert the entire thing to a dynamic query.

Happy Coding!!!

~~ CK
Oct 13 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Rob | last post by:
I'm just getting around to using pivot tables and charts. I find the Pivot table interface to be INCREDIBLY frustrating. When I view a table in Design view, then choose Pivot table view, I get...
3
by: John | last post by:
Is there a way to code the button that's available in the query window--microsoft excel icon that exports to excel. I know transferspreadsheet will do this---but I want the query, which is in a...
4
by: Alan Lane | last post by:
Hello world: I'm including both code and examples of query output. I appologize if that makes this message longer than it should be. Anyway, I need to change the query below into a pivot table...
4
by: Ajay | last post by:
Hello all, I used to display reports in Excel earlier on my website. Now the client has requested that he would like see reports in Pivot table on the web . The backend is Sql2000. Can you please...
2
by: Carl Gilbert | last post by:
Hi I have a math kinda problem where I'm trying to split some lines when two or more lines connect two shapes. The reason I am doing this is to make it clear that there are multiple lines...
3
by: Jerry K via DotNetMonster.com | last post by:
I'm creating a pivot table using vb.net and the data is from sqlserver (desktop). I have been successful at creating the pivot table, which includes a 'date' column field. I'd like to group the...
9
by: PeteCresswell | last post by:
I've got something called "Reference Rates". The idea is that on a given day, we have various rates of return for various entities. e.g. Libor 3-month return, Libor 6-month return, US Treasury...
3
by: Nassa | last post by:
Hi everyone, I have 3 tables: Telbook(Id:int,Name:char,address:char,comment:ntext,owneruserid:int), PK:id...
3
by: Chance1234 | last post by:
Been searching all day through the web and many groups like this and there seems to be a diaspora of mix match information; This is what I am trying to do; I have a vb class called...
3
by: Thyag | last post by:
Hi All, I need to group multiple tables in to a pivot. Could some body help me. Thanks in Advance, Thyag
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.