Hi Experts,
Here we go. I have below DDL,DML and what I expecting. - 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))
- insert into langdtl values(1,1,1,1,1,1,0,'fi')
-
insert into langdtl values(1,1,1,0,0,0,1,'sv')
-
insert into langdtl values(1,1,1,1,0,1,0,'en')
-
insert into langdtl values(1,1,1,0,1,1,1,'de')
- insert into langdtl values(2,1,1,0,0,1,1,'fi')
-
insert into langdtl values(2,1,0,1,1,1,0,'sv')
-
insert into langdtl values(2,1,1,0,1,0,1,'en')
select * from langdtl
I need output data as shown below - id speak_flag read_flag ia_flag lit1_flag lit2_flag lit3_flag
-
-------------------------------------------------------------------------
-
1 de,en,fi,sv de,en,fi,sv en,fi de,fi de,en,fi de,sv
-
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
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. -
CREATE FUNCTION dbo.AllLangs (@ID Int, @Which Varchar(255))
-
RETURNS Varchar(1024)
-
AS
-
BEGIN
-
-
DECLARE @LangList varchar(1024)
-
-
SELECT @LangList = COALESCE(@LangList + ',', '') + lang)
-
FROM langdtl
-
WHERE ID = @ID AND
-
Case @Which
-
When 'speak_flag' Then speak_flag
-
When 'read_flag' Then read_flag
-
When 'ia_flag' Then ia_flag
-
When 'lit1_flag' Then lit1_flag
-
When 'lit2_flag' Then lit2_flag
-
When 'lit3_flag' Then lit3_flag
-
Else 0
-
End = 1
-
Order by lang
-
-
RETURN @LangList
-
END
-
Now your query should look like this - select ID,
-
dbo.AllLangs(ID, 'speak_flag') as [speak_flag],
-
dbo.AllLangs(ID, 'read_flag') as [read_flag],
-
dbo.AllLangs(ID, 'ia_flag') as [ia_flag],
-
dbo.AllLangs(ID, 'lit1_flag') as [lit1_flag],
-
dbo.AllLangs(ID, 'lit2_flag') as [lit2_flag],
-
dbo.AllLangs(ID, 'lit3_flag') as [lit3_flag]
-
From lang
-
Order by ID
-
thsi should give you the results that you are after
4 1316
Could you explain your requirement ?
From the data you posted, I would expected to have seen
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
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 - id a b c d e f
-
-------------------------------------------------------------------------
-
1 de,en,fi,sv de,en,fi,sv en,fi de,fi de,en,fi de,sv
-
2 de,en,fi,sv en,fi sv en,sv fi,sv en,fi
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. -
CREATE FUNCTION dbo.AllLangs (@ID Int, @Which Varchar(255))
-
RETURNS Varchar(1024)
-
AS
-
BEGIN
-
-
DECLARE @LangList varchar(1024)
-
-
SELECT @LangList = COALESCE(@LangList + ',', '') + lang)
-
FROM langdtl
-
WHERE ID = @ID AND
-
Case @Which
-
When 'speak_flag' Then speak_flag
-
When 'read_flag' Then read_flag
-
When 'ia_flag' Then ia_flag
-
When 'lit1_flag' Then lit1_flag
-
When 'lit2_flag' Then lit2_flag
-
When 'lit3_flag' Then lit3_flag
-
Else 0
-
End = 1
-
Order by lang
-
-
RETURN @LangList
-
END
-
Now your query should look like this - select ID,
-
dbo.AllLangs(ID, 'speak_flag') as [speak_flag],
-
dbo.AllLangs(ID, 'read_flag') as [read_flag],
-
dbo.AllLangs(ID, 'ia_flag') as [ia_flag],
-
dbo.AllLangs(ID, 'lit1_flag') as [lit1_flag],
-
dbo.AllLangs(ID, 'lit2_flag') as [lit2_flag],
-
dbo.AllLangs(ID, 'lit3_flag') as [lit3_flag]
-
From lang
-
Order by ID
-
thsi should give you the results that you are after
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. -
-
;with language_table
-
as
-
( select
-
id,
-
rownum = row_number() over(partition by id order by id),
-
speak_lang = isnull(lang + space(nullif(0, speak_flag)) + ',',''),
-
read_lang = isnull(lang + space(nullif(0, read_flag))+ ',',''),
-
ia_lang = isnull(lang + space(nullif(0, ia_flag))+ ',',''),
-
lit1_lang = isnull(lang + space(nullif(0, lit1_flag))+ ',',''),
-
lit2_lang = isnull(lang + space(nullif(0, lit2_flag))+ ',',''),
-
lit3_lang = isnull(lang + space(nullif(0, lit3_flag))+ ',',''),
-
speak_flag, read_flag, ia_flag, lit2_flag, lit3_flag
-
from langdtl
-
),
-
sorted_table
-
as
-
(
-
select TOP 10000000 * from language_table order by id asc, rownum desc
-
),
-
concatenated
-
as
-
(
-
select
-
id,
-
speak_lang =
-
COALESCE( MIN( CASE WHEN rownum = 4 THEN speak_lang END) , '' ) +
-
COALESCE( MIN( CASE WHEN rownum = 3 THEN speak_lang END) , '' ) +
-
COALESCE( MIN( CASE WHEN rownum = 2 THEN speak_lang END) , '' ) +
-
COALESCE( MIN( CASE WHEN rownum = 1 THEN speak_lang END) , '' ),
-
-
read_lang =
-
COALESCE( MIN( CASE WHEN rownum = 4 THEN read_lang END) , '' ) +
-
COALESCE( MIN( CASE WHEN rownum = 3 THEN read_lang END) , '' ) +
-
COALESCE( MIN( CASE WHEN rownum = 2 THEN read_lang END) , '' ) +
-
COALESCE( MIN( CASE WHEN rownum = 1 THEN read_lang END) , '' ),
-
-
ia_lang =
-
COALESCE( MIN( CASE WHEN rownum = 4 THEN ia_lang END) , '' ) +
-
COALESCE( MIN( CASE WHEN rownum = 3 THEN ia_lang END) , '' ) +
-
COALESCE( MIN( CASE WHEN rownum = 2 THEN ia_lang END) , '' ) +
-
COALESCE( MIN( CASE WHEN rownum = 1 THEN ia_lang END) , '' ),
-
-
lit1_lang =
-
COALESCE( MIN( CASE WHEN rownum = 4 THEN lit1_lang END) , '' ) +
-
COALESCE( MIN( CASE WHEN rownum = 3 THEN lit1_lang END) , '' ) +
-
COALESCE( MIN( CASE WHEN rownum = 2 THEN lit1_lang END) , '' ) +
-
COALESCE( MIN( CASE WHEN rownum = 1 THEN lit1_lang END) , '' ),
-
-
-
lit2_lang =
-
COALESCE( MIN( CASE WHEN rownum = 4 THEN lit2_lang END) , '' ) +
-
COALESCE( MIN( CASE WHEN rownum = 3 THEN lit2_lang END) , '' ) +
-
COALESCE( MIN( CASE WHEN rownum = 2 THEN lit2_lang END) , '' ) +
-
COALESCE( MIN( CASE WHEN rownum = 1 THEN lit2_lang END) , '' ),
-
-
lit3_lang =
-
COALESCE( MIN( CASE WHEN rownum = 4 THEN lit3_lang END) , '' ) +
-
COALESCE( MIN( CASE WHEN rownum = 3 THEN lit3_lang END) , '' ) +
-
COALESCE( MIN( CASE WHEN rownum = 2 THEN lit3_lang END) , '' ) +
-
COALESCE( MIN( CASE WHEN rownum = 1 THEN lit3_lang END) , '' )
-
-
from sorted_table
-
group by id
-
)
-
select id,
-
speak_lang = substring(speak_lang,1, len(speak_lang)-1),
-
read_lang = substring(read_lang,1, len(read_lang)-1),
-
ia_lang = substring(ia_lang,1, len(ia_lang)-1),
-
lit1_lang = substring(lit1_lang,1, len(lit1_lang)-1),
-
lit2_lang = substring(lit2_lang,1, len(lit2_lang)-1),
-
lit3_lang = substring(lit3_lang,1, len(lit3_lang)-1)
-
from concatenated
-
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Nassa |
last post by:
Hi everyone,
I have 3 tables:
Telbook(Id:int,Name:char,address:char,comment:ntext,owneruserid:int),
PK:id...
|
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...
|
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
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |