September 11, 2011 at 12:04 am
Hi
I have two Tables:
Table1:
ID1 Int
FileNumber Int
Table2:
ID2 Int
FileNumber Int
Pic nvarchar(50)
Tag Bit
for each record in table1, i have two records in Table2, for example:
Table1:
ID1------------FileNumber
1--------------100
2--------------200
Table2:
ID2--------FileNumber--------Pic----------Tag
1----------100---------- ----aaaaa-----True
2----------100---------------bbbbb-----Fales
3----------200---------------ccccc-----True
4----------200---------------ddddd-----Fales
i want to write store procedure to have this result:
FileNumber------Pic-------Tag--------Pic----------Tag
100---------- -aaaaa-----True-------bbbb--------Fales
would you please help me?
September 11, 2011 at 1:15 am
Are you wanting to pass in file number as a parameter or will the sproc iterate through all file numbers?
September 11, 2011 at 1:19 am
thanks alot for your notification.
i want all of filenumbers.
September 11, 2011 at 1:58 am
And what would you like to do if there are more than 2 (or even more) ID2's for a given File Number? Or are you willing to guarantee that no more than two rows will ever appear with the same File Number?
--Jeff Moden
Change is inevitable... Change for the better is not.
September 11, 2011 at 1:59 am
No, i am sure that in Table2 only i have two records for each "Filenumber"
September 11, 2011 at 2:48 am
I guess Jeff asked to decide whether a CrossTab or a DynamicCrossTab would be required.
Both concepts are described in the related links in my signature. Disclaimer: Jeff is the original author, not me 😉
Following please find the "static" CrossTab method (please note how I presented the sample data in a ready to use format...). What I'm wondering though: What would you need table1 for in the given task?
DECLARE @tbl1 TABLE (ID1 INT, FileNumber int)
INSERT INTO @tbl1 VALUES(1,100),(2,200)
DECLARE @tbl2 TABLE(ID2 INT, FileNumber INT, Pic VARCHAR(10), Tag bit)
INSERT INTO @tbl2
VALUES
(1,100,'aaaaa',1),
(2,100,'bbbbb',0),
(3,200,'ccccc',1),
(4,200,'ddddd',0);
WITH cte AS
(
SELECT
FileNumber,
Pic,
CASE WHEN tag =0 THEN 'False' ELSE 'True' END AS Tag,
ROW_NUMBER() OVER(PARTITION BY FileNumber ORDER BY ID2) AS row
FROM @tbl2
)
SELECT
FileNumber,
MAX(CASE WHEN row = 1 THEN Pic ELSE NULL END) AS Pic,
MAX(CASE WHEN row = 1 THEN Tag ELSE NULL END) AS Tag,
MAX(CASE WHEN row = 2 THEN Pic ELSE NULL END) AS Pic,
MAX(CASE WHEN row = 2 THEN Tag ELSE NULL END) AS Tag
FROM cte
GROUP BY FileNumber
September 11, 2011 at 12:06 pm
LutzM (9/11/2011)
I guess Jeff asked to decide whether a CrossTab or a DynamicCrossTab would be required.
As usual, spot on, Lutz. Not my job to judge but nicely done with the "preaggregation" and the Cross Tab.
What I'm wondering though: What would you need table1 for in the given task?
Again... I absolutely agree... if FK's are present on Table 2 for Table 1, there's no need to refer to Table 1 at all for this task as it is currently presented.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 11, 2011 at 9:46 pm
CELKO (9/11/2011)
There are no magi val universal "id" in RDBMS.
Sure there is... it's called an IDENTITY column and it has several purposes... one is to provide uniqueness in the absense of any reasonable natural key (as in a Customer table) and another is to prevent massive page fragmentation on tables that have rows entered out of the desired order when the column is used with a clustered index. It even works well when there is a natural key but the data is entered out of order according to the key. It also acts as a temporal tie breaker and a good ol' fashioned 1950's style "record keeper" when processing 3rd party data, whose condition is unknown, in a wonderful "scratch" table known as a Temporary Staging Table where data may be cleaned, deduplicated, and validated against other tables before being allowed anywhere near previously validated data.
Besides, SQL Server supposedly doesn't actually meet all of Codd's rules to be an RDBMS... it's really nothing more than a sophisticated file system. 😀
Of course, you being an SQL genius and all, you already know all that, right? 😉
How do you enforce this business rule in the DDL you never bothered to post?
Now THAT's the question of the day. I'd like to hear more about that, as well.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply