I have a list of Clients like:
Table_Client
Now I want to ad some variable data to each client, so I have created a second table like:
Table_Client_Data
Now I want to have the next table as result in one query:
First I have made a VIEW to create, I had used a inner Join
SELECT Table_Client.ID,
Table_Client_Data.FieldDataString AS CustomerNr,
Table_Client_Data_1.FieldDataString AS Dog
FROM Table_Client_Data
INNER JOIN
Table_Client_Data ON Table_Client.ID = Table_Client_Data.ClientID
INNER JOIN
Table_Client_Data as Table_Client_Data_1 ON Table_Client.ID = Table_Client_Data_1.ClientID
WHERE (Table_Client_Data.FieldName = 'CustomerNr') AND (Table_Client_Data_1.FieldName = 'Dog')
View_CliuentData
Now I join View_CliuentData with Table_Client and I have the right result.
Now my question,
Is there any way to skip the View an do this all in a join. I have tried several things but ... no result.
Tks Bart
Hi bart,
I′m thinking about your problem, and one way to solve this problem, are you using the temporary tables.
All data are copied to temporary tables and you make a join with this object.
The best thing about this, are no necessary physical objects stored in bank.
|||Hello,I'm not using temporary tables.
But will this table be regenerated every time I do a request?
If yes: this will not be so performant as a view I guess.
Is there no way to do this in one query.
Gr Bart
|||Would it not be more extensible and performant to use a single XML type to represent your data? It would still be searchable, fields would be stored by their appropriate type, and association would be explicit while extensibility of the type is guaranteed generally by the XML specification.|||
First, let me say that is a very strange design, and one that will prove to continually be a problem. As with most denormalized databases, maintenance and scaling could prove to be a nightmare.
However, this suggestion provides your desired results from a single query.
Code Snippet
SET NOCOUNT ON
DECLARE @.Clients table
( [ID] int,
[Name] varchar(20)
)
INSERT INTO @.Clients VALUES ( 10, 'Bill' )
INSERT INTO @.Clients VALUES ( 11, 'Frank' )
INSERT INTO @.Clients VALUES ( 12, 'Carl' )
INSERT INTO @.Clients VALUES ( 13, 'Rita' )
INSERT INTO @.Clients VALUES ( 14, 'Jan' )
INSERT INTO @.Clients VALUES ( 15, 'Bonny' )
INSERT INTO @.Clients VALUES ( 16, 'Bart' )
INSERT INTO @.Clients VALUES ( 17, 'George' )
INSERT INTO @.Clients VALUES ( 18, 'Ann' )
DECLARE @.ClientsData table
( [ID] int,
ClientID varchar(10),
FieldName varchar(10),
FieldStr varchar(20),
FieldInt int,
FieldDate datetime,
FieldBool bit
)
INSERT INTO @.ClientsData VALUES ( 2, 10, 'CustomerNr', 'g146', NULL, '', NULL )
INSERT INTO @.ClientsData VALUES ( 4, 11, 'CustomerNr', 'g121', NULL, '', NULL )
INSERT INTO @.ClientsData VALUES ( 5, 12, 'CustomerNr', 'g147', NULL, '', NULL )
INSERT INTO @.ClientsData VALUES ( 6, 13, 'CustomerNr', 'g236', NULL, '', NULL )
INSERT INTO @.ClientsData VALUES ( 7, 15, 'CustomerNr', 'g245', NULL, '', NULL )
INSERT INTO @.ClientsData VALUES ( 9, 10, 'Dog', 'yes', NULL, '', NULL )
INSERT INTO @.ClientsData VALUES ( 10, 11, 'Dog', 'No', NULL, '', NULL )
INSERT INTO @.ClientsData VALUES ( 10, 12, 'Dog', 'yes', NULL, '',NULL )
INSERT INTO @.ClientsData VALUES ( 10, 13, 'Dog', 'No', NULL, '', NULL )
INSERT INTO @.ClientsData VALUES ( 10, 15, 'Dog', 'yes', NULL, '', NULL )
SELECT
c.[ID],
CustomerNr = CASE cd1.FieldName WHEN 'CustomerNr' THEN cd1.FieldStr END,
Dog = CASE cd2.FieldName WHEN 'Dog' THEN cd2.FieldStr END
FROM @.Clients c
JOIN @.ClientsData cd1
ON c.[ID] = cd1.ClientID
AND cd1.FieldName = 'CustomerNr'
JOIN @.ClientsData cd2
ON c.[ID] = cd2.ClientID
AND cd2.FieldName = 'Dog'
-- -- --
10 g146 yes
11 g121 No
12 g147 yes
13 g236 No
15 g245 yes
No comments:
Post a Comment