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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 need help -- very urgent

Author  Topic 

sensyguy
Starting Member

7 Posts

Posted - 2009-07-25 : 01:40:50
i have a table's like this

table a


id name add1 add2
1 A xxx yyy
2 B aaa bbb
3 C ccc ddd

table b
id exam
1 a
2 b
3 c
4 d
5 e
6 f
7 g

table c

id table_aid table_bid total
1 1 1 80
2 1 2 60
3 1 3 50
4 1 4 40
5 1 5 30
6 2 1 30
7 2 2 40
8 2 3 50
9 2 6 60
10 2 7 50



i want to display the data like

id name
add
exam

1 A
xxx
1

yyy
2

3

4

5

2 B
aaa
1

bbb
2

3

6

7

3 C
ccc

ddd




how can i do this? is there any way to do this... please suggest me.....

thanks in advance.

Thanks & Regards
Sensy_Guy

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2009-07-25 : 02:03:12
Achieve this in your front end application.

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

sensyguy
Starting Member

7 Posts

Posted - 2009-07-25 : 02:07:13
Can't we do this in backend?

Thanks & Regards
Sensy_Guy
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-07-25 : 03:54:13
I believe a series of INNER JOIN and LEFT JOIN would work.



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2009-07-26 : 12:47:34
quote:
Originally posted by sensyguy

Can't we do this in backend?

Thanks & Regards
Sensy_Guy



Sure... but I think that most folks would like to see what you've tried before they spend any time on it.

--Jeff Moden
"Your lack of planning DOES constitute an emergency on my part... SO PLAN BETTER! "
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row"

Go to Top of Page

sensyguy
Starting Member

7 Posts

Posted - 2009-07-26 : 23:51:05
folks show me some way to achieve this..............

Thanks & Regards
Sensy_Guy
Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2009-07-27 : 07:52:20
I guess you need to explain a bit more how you got what you did for rows 3,4,5 and 8,9,10 and why no such rows appear after row 12. You want an answer? Ask a better question. ;-)

It would still be nice to see what you've tried.

--Jeff Moden
"Your lack of planning DOES constitute an emergency on my part... SO PLAN BETTER! "
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row"

Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2009-07-27 : 08:27:46
My first suggestion, is follow all the links in my signature

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

sensyguy
Starting Member

7 Posts

Posted - 2009-07-30 : 07:21:20
let me explain a bit clearly

in table A we have id,name & address columns.

First id is having two addresses.. (xxx,yyy). In the same way the rest is also having two addresses.

now i need to display like

first id values in two rows... because it's having two addresses. if it's having more than that we need to increase according to that.

But one thing here i want to tell you id & the name should not get displayed in the second row. only we should display
address2.

like this i need to do for all the records...

id name address
1 A xxx

null null yyy

(i have mentioned 'null' to saying that there is no value in that field)

Thanks & Regards
Sensy_Guy
Go to Top of Page

sensyguy
Starting Member

7 Posts

Posted - 2009-07-30 : 23:40:27
Is there anyone to solve my query... plz help me.... .

thanks in advance

Thanks & Regards
Sensy_Guy
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-30 : 23:51:56
seriously . . it is much easier and efficient to do this in your front end. Doing this in SQL will required some looping or reading back the previous rows to compare etc.

Why must you do this in the query ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

sensyguy
Starting Member

7 Posts

Posted - 2009-07-30 : 23:57:03
Hi khtan

thanks for your reply...

kindly help me how to do this....

if it is more tricky then ill ask my ppl to do it in front end..

please help me... i need this query ... very urgent.

thanks in advance

Thanks & Regards
Sensy_Guy
Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2009-07-31 : 01:11:40
quote:
Originally posted by khtan

seriously . . it is much easier and efficient to do this in your front end. Doing this in SQL will required some looping or reading back the previous rows to compare etc.

Why must you do this in the query ?


KH
[spoiler]Time is always against us[/spoiler]




No... no looping...

--Jeff Moden
"Your lack of planning DOES constitute an emergency on my part... SO PLAN BETTER! "
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row"

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-31 : 01:23:20
quote:
Originally posted by Jeff Moden

quote:
Originally posted by khtan

seriously . . it is much easier and efficient to do this in your front end. Doing this in SQL will required some looping or reading back the previous rows to compare etc.

Why must you do this in the query ?


KH
[spoiler]Time is always against us[/spoiler]




No... no looping...

--Jeff Moden
"Your lack of planning DOES constitute an emergency on my part... SO PLAN BETTER! "
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row"





yes. i know . At least not with 2005.

There are somethings can be done in SQL and there are something that should be done in SQL. Can be done doesn't mean that it is easier. This with running total falls in the group of "can and should not"


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2009-07-31 : 01:30:23
quote:
Originally posted by sensyguy

Hi khtan

thanks for your reply...

kindly help me how to do this....

if it is more tricky then ill ask my ppl to do it in front end..

please help me... i need this query ... very urgent.

thanks in advance

Thanks & Regards
Sensy_Guy



C'mon... finish up the definition. Why isn't ID 3 from TableA included in the bridge table (TableC)? Is is just an oversight or is that the way it will be in real life?

And it sure would be nice if you'd read the articles about how to post a question. Provide folks with CREATE TABLE statements for your 3 tables and INSERT statements for your data and people will likely jump on the opportunity to help. Even if you're absolutely loath to do this, we still need for you to finish describing the real problem as I said above.

--Jeff Moden
"Your lack of planning DOES constitute an emergency on my part... SO PLAN BETTER! "
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row"

Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2009-07-31 : 02:19:07
Heh... ok... you outwaited me. Do take the time to read the links that DonAtWork posted before you need to post another "urgent" question... you'll get an a good answer much quicker. And, I'm assuming that ID 3 from TableA is truly an "orphan" from the bridge table. If that's not true, I'm sure you be able to figure out the two sections of code that you need to delete.

--===== Define the tables and insert some test data
DECLARE @TableA TABLE (id INT,name VARCHAR(5),add1 VARCHAR(5),add2 VARCHAR(5))
INSERT INTO @TableA (id,name,add1,add2)
SELECT '1','A','xxx','yyy' UNION ALL
SELECT '2','B','aaa','bbb' UNION ALL
SELECT '3','C','ccc','ddd'
;
DECLARE @TableB TABLE (id INT ,exam VARCHAR(5))
INSERT INTO @TableB
SELECT '1','a' UNION ALL
SELECT '2','b' UNION ALL
SELECT '3','c' UNION ALL
SELECT '4','d' UNION ALL
SELECT '5','e' UNION ALL
SELECT '6','f' UNION ALL
SELECT '7','g'
;
DECLARE @TableC TABLE (id INT,table_aid INT,table_bid INT,total INT)
INSERT INTO @TableC (id,table_aid,table_bid,total)
SELECT '1','1','1','80' UNION ALL
SELECT '2','1','2','60' UNION ALL
SELECT '3','1','3','50' UNION ALL
SELECT '4','1','4','40' UNION ALL
SELECT '5','1','5','30' UNION ALL
SELECT '6','2','1','30' UNION ALL
SELECT '7','2','2','40' UNION ALL
SELECT '8','2','3','50' UNION ALL
SELECT '9','2','6','60' UNION ALL
SELECT '10','2','7','50'
;
--===== Solve the problem
WITH
cteAgg AS
(--==== Bridge table info
SELECT Table_AID,
Table_BID
FROM @TableC
UNION ALL
--==== Orphaned TableA entries for ADD1
SELECT a.ID AS Table_AID,
'' AS Table_BID
FROM @TableA a
WHERE NOT EXISTS (SELECT 1 FROM @TableC c WHERE c.Table_AID = a.ID)
UNION ALL
--==== Orphaned TableA entries for ADD2
SELECT a.ID AS Table_AID,
'' AS Table_BID
FROM @TableA a
WHERE NOT EXISTS (SELECT 1 FROM @TableC c WHERE c.Table_AID = a.ID)
)
,
cteSeq AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY Table_AID ORDER BY Table_AID, Table_BID) AS Seq,
Table_AID,
Table_BID
FROM cteAgg
)
SELECT CASE WHEN s.Seq = 1 THEN CAST(a.ID AS VARCHAR(10)) ELSE '' END AS ID,
CASE WHEN s.Seq = 1 THEN a.Name ELSE '' END AS Name,
CASE WHEN s.Seq = 1 THEN a.Add1 WHEN s.Seq = 2 THEN a.Add2 ELSE '' END AS Addr,
ISNULL(CAST(b.ID AS VARCHAR(10)),'') AS ExamID
FROM cteSeq s
FULL OUTER JOIN @TableA a ON s.Table_AID = a.ID
FULL OUTER JOIN @TableB b ON s.Table_BID = b.ID


--Jeff Moden
"Your lack of planning DOES constitute an emergency on my part... SO PLAN BETTER! "
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row"

Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2009-07-31 : 02:23:03
quote:
Originally posted by khtan This with running total falls in the group of "can and should not"


KH
[spoiler]Time is always against us[/spoiler]




Any bets on the running total thing?

--Jeff Moden
"Your lack of planning DOES constitute an emergency on my part... SO PLAN BETTER! "
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row"

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-31 : 03:43:27
[code]
DECLARE @TableA TABLE
(
[ID] int,
[Name] varchar(5),
Add1 varchar(5),
Add2 varchar(5)
)

INSERT INTO @TableA
SELECT 1, 'A', 'xxx', 'yyy' UNION ALL
SELECT 2, 'B', 'aaa', 'bbb' UNION ALL
SELECT 3, 'C', 'ccc', 'ddd'

DECLARE @TableB TABLE
(
[ID] int,
exam varchar(5)
)

INSERT INTO @TableB
SELECT 1, 'a' UNION ALL
SELECT 2, 'b' UNION ALL
SELECT 3, 'c' UNION ALL
SELECT 4, 'd' UNION ALL
SELECT 5, 'e' UNION ALL
SELECT 6, 'f' UNION ALL
SELECT 7, 'g'

DECLARE @TableC TABLE
(
ID int,
Table_AID int,
Table_BID int,
TOTAL int
)

INSERT INTO @TableC
SELECT 1, 1, 1, 80 UNION ALL
SELECT 2, 1, 2, 60 UNION ALL
SELECT 3, 1, 3, 50 UNION ALL
SELECT 4, 1, 4, 40 UNION ALL
SELECT 5, 1, 5, 30 UNION ALL
SELECT 6, 2, 1, 30 UNION ALL
SELECT 7, 2, 2, 40 UNION ALL
SELECT 8, 2, 3, 50 UNION ALL
SELECT 9, 2, 6, 60 UNION ALL
SELECT 10, 2, 7, 50

;WITH data (ID, Name, Addr, ExamID, row_no, ID_row, NM_row)
AS
(
SELECT [ID] = CONVERT(varchar(10), coalesce(a.ID, c.Table_AID)),
[Name] = coalesce(a.Name, ''),
[Addr] = coalesce(a.Addr, ''),
ExamID = coalesce(CONVERT(varchar(10), c.Table_BID), ''),
row_no = row_number() OVER (ORDER BY coalesce(a.ID, c.Table_AID, ''), c.Table_BID),
ID_row = row_number() OVER (PARTITION BY coalesce(a.ID, c.Table_AID, '') ORDER BY c.Table_BID),
NM_row = row_number() OVER (PARTITION BY coalesce(a.ID, c.Table_AID, ''), coalesce(a.Name, '') ORDER BY c.Table_BID)
FROM
(
SELECT [ID],
[Name],
Addr,
row_no = row_number() OVER (PARTITION BY ID ORDER BY Addr)
FROM @TableA a
unpivot
(
Addr
FOR AddrCol IN (Add1, Add2)
) up
) a
FULL JOIN @TableC c ON a.ID = c.Table_AID
AND a.row_no = c.Table_BID
)
SELECT [ID] = CASE WHEN ID_row = 1 THEN [ID] ELSE '' END,
[Name] = CASE WHEN NM_row = 1 THEN [Name] ELSE '' END,
[Addr],
[ExamID]
FROM data
ORDER BY row_no

/*
ID Name Addr ExamID
---------- ----- ----- ----------
1 A xxx 1
yyy 2
3
4
5
2 B aaa 1
bbb 2
3
6
7
3 C ccc
ddd

(12 row(s) affected)
*/

[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-31 : 03:54:29
quote:
Originally posted by Jeff Moden

quote:
Originally posted by khtan This with running total falls in the group of "can and should not"


KH
[spoiler]Time is always against us[/spoiler]




Any bets on the running total thing?

--Jeff Moden
"Your lack of planning DOES constitute an emergency on my part... SO PLAN BETTER! "
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row"





SQL vs Front end ?
Can vs Should not ?

Burning midnight oil again ?

Put it out . . save energy and go to bed


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-31 : 04:02:05
this is the section code that just perform the query of getting the result, without the logic of formatting to the result that you required. The query cost is only half of the query with additional logic to handle the formatting of result.

You can also try it out with Jeff's query. His query is faster than the one i posted.


SELECT [ID] = CONVERT(varchar(10), coalesce(a.ID, c.Table_AID)),
[Name] = coalesce(a.Name, ''),
[Addr] = coalesce(a.Addr, ''),
ExamID = coalesce(CONVERT(varchar(10), c.Table_BID), '')
FROM
(
SELECT [ID],
[Name],
Addr,
row_no = row_number() OVER (PARTITION BY ID ORDER BY Addr)
FROM @TableA a
unpivot
(
Addr
FOR AddrCol IN (Add1, Add2)
) up
) a
FULL JOIN @TableC c ON a.ID = c.Table_AID
AND a.row_no = c.Table_BID



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2009-07-31 : 12:16:37
quote:
Originally posted by khtanSQL vs Front end ?
Can vs Should not ?

Burning midnight oil again ?

Put it out . . save energy and go to bed



Heh... ok... I'm rested...

I would much rather do a minor bit of formatting (it was just case statements if you think about it) on the SQL Server side than to turn the whole query over to a bunch of folks who may not write anything efficient to get the job done.

As a compromise, though... I'll agree that writing the query as a stored proc and no formatting is the best thing to do. Then, let the front end suppress the data where it needs to be supressed.



--Jeff Moden
"Your lack of planning DOES constitute an emergency on my part... SO PLAN BETTER! "
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row"

Go to Top of Page
    Next Page

- Advertisement -