Ok, hier ist die Info nach Ihrem Leitfaden. Dies ist das Tabellenskript + einige Tabellen.SQL-Anweisung mit zwei Tabellen verbinden und eine andere Tabelle
USE [tempdb]
GO
CREATE TABLE [dbo].[table1](
[cn] [nvarchar](1024) NULL,
[member] [nvarchar](1024) NULL,
[description] [nvarchar](1024) NULL,
[Date] [datetime] NULL,
[IsArchived] [bit] null
)
GO
CREATE TABLE [dbo].[table2](
[cn] [nvarchar](1024) NULL,
[member] [nvarchar](1024) NULL,
[description] [nvarchar](1024) NULL,
[Date] [datetime] NULL,
[IsArchived] [bit] null
)
GO
CREATE TABLE [dbo].[table4](
[cn] [nvarchar](1024) NULL,
[member] [nvarchar](1024) NULL,
[description] [nvarchar](1024) NULL,
[Date] [datetime] NULL,
[IsArchived] [bit] null
)
GO
CREATE TABLE [dbo].[table3](
[ID] [int] not NULL,
[ProductID] [nvarchar](1024) NULL,
[ProductOmschrijving] [nvarchar](1024) NULL,
[ProductPrijs] [money] NULL,
)
GO
INSERT INTO table1 VALUES ('grp-sec-spla-office','john; chris; jack; marc;','112-112','', '');
INSERT INTO table1 VALUES ('grp-sec-spla-office-prof','jack; marc;','114-114','', '');
INSERT INTO table2 VALUES ('grp-sec-spla-office','cees; klaas','112-112','', '');
INSERT INTO table2 VALUES ('grp-sec-spla-office-prof','jan; piet','114-114','', '');
INSERT INTO table4 VALUES ('grp-sec-spla-office-prof','jack; marc;','114-114','', '');
INSERT INTO table4 VALUES ('grp-sec-spla-office','piet; ellen','112-112','', '');
INSERT INTO table4 VALUES ('grp-sec-spla-visio','henk; alwin','112-116','', '');
INSERT INTO table3 VALUES (1,'112-112','grp-sec-spla-office-eng','10.12');
INSERT INTO table3 VALUES (2,'114-114','grp-sec-spla-office-prof-2016','5.45');
INSERT INTO table3 VALUES (3,'112-116','grp-sec-spla-visio-blabla','7.12');
INSERT INTO table3 VALUES (4,'112-118','grp-sec-ac-office-sta-eng','2.45');
INSERT INTO table3 VALUES (5,'112-120','grp-sec-ac-office-pro-eng','2,50');
GO
Mein Ergebnis mit der Abfrage ist:
+-------------+-------------------------------+--------------+-------+
| description | ProductOmschrijving | ProductPrijs | Total |
+-------------+-------------------------------+--------------+-------+
| 112-112 | grp-sec-spla-office-eng | 10.12 | 9 |
| 114-114 | grp-sec-spla-office-prof-2016 | 5.45 | 8 |
+-------------+-------------------------------+--------------+-------+
Aber ich brauche zu bekommen:
+-------------+-------------------------------+--------------+-------+
| description | ProductOmschrijving | ProductPrijs | Total |
+-------------+-------------------------------+--------------+-------+
| 112-112 | grp-sec-spla-office-eng | 10.12 | 9 |
| 114-114 | grp-sec-spla-office-prof-2016 | 5.45 | 8 |
| 112-116 | grp-sec-spla-visio-blabla | 7.12 | 2 |
| 112-118 | grp-sec-ac-office-sta-eng | 2.45 | 0 |
| 112-120 | grp-sec-ac-office-pro-eng | 250.00 | 0 |
+-------------+-------------------------------+--------------+-------+
Dies ist die Abfrage i jetzt verwenden:
SELECT
table1.description,
table3.ProductOmschrijving,
table3.ProductPrijs,
sum((isnull(LEN(table1.member) - LEN(REPLACE(table1.member, ';', '')),-1) + 1) +
isnull(LEN(table2.member) - LEN(REPLACE(table2.member, ';', '')),-1) + 1 +
isnull(LEN(table4.member) - LEN(REPLACE(table4.member, ';', '')),-1) + 1) AS Total
FROM table1
inner join table3 ON table1.description = table3.ProductID
left outer join table2 ON table1.description = table2.description
AND table2.IsArchived = 0
left outer join table4 on table1.description = table4.description
and table4.IsArchived = 0
where table1.IsArchived = 0
GROUP BY table1.description
, ProductOmschrijving
, ProductPrijs
Die Tabelle1, Tabelle2, Tabelle4 kann sich ändern (insbesondere das Element fi eld). Wenn ich später (in der Zukunft) eine neue table5 zur Abfrage hinzufüge, muss das Ergebnis gleich sein, aber nur das Mitglied muss zum Ergebnis hinzugefügt werden. Auch wenn in der neuen Tabelle keine Mitglieder vorhanden sind5.
Ist das klarer? Danke für deine Zeit und Hilfe.
Ahhghh, wenn ich den ganzen Beitrag machte mir klar, dass die table3 (die Produkttabelle) muss natürlich die führende Tabelle sein. Also habe ich die Abfrage bearbeitet, um von table3 statt table1 zu gehen. Nein, es funktioniert perfekt !!!
SELECT
table3.ProductID,
table3.ProductOmschrijving,
table3.ProductPrijs,
sum((isnull(LEN(table1.member) - LEN(REPLACE(table1.member, ';', '')),-1) + 1) +
isnull(LEN(table2.member) - LEN(REPLACE(table2.member, ';', '')),-1) + 1 +
isnull(LEN(table4.member) - LEN(REPLACE(table4.member, ';', '')),-1) + 1)
AS Total
FROM table3
left join table1 ON table3.productid = table1.description
and table1.IsArchived = 0
left join table2 ON table3.productid = table2.description
AND table2.IsArchived = 0
left join table4 on table3.productid = table4.description
and table4.IsArchived = 0
GROUP BY table3.productid
, ProductOmschrijving
, ProductPrijs
und das Ergebnis:
+-----------+-------------------------------+--------------+-------+
| ProductID | ProductOmschrijving | ProductPrijs | Total |
+-----------+-------------------------------+--------------+-------+
| 112-112 | grp-sec-spla-office-eng | 10.12 | 9 |
| 112-116 | grp-sec-spla-visio-blabla | 7.12 | 2 |
| 112-118 | grp-sec-ac-office-sta-eng | 2.45 | 0 |
| 112-120 | grp-sec-ac-office-pro-eng | 250.00 | 0 |
| 114-114 | grp-sec-spla-office-prof-2016 | 5.45 | 8 |
+-----------+-------------------------------+--------------+-------+
Die fehlenden Spalten sind nicht in Ihrer Abfrage. Sie sind in der Unterabfrage, fehlen aber in der Hauptabfrage. –
Hallo Sean, ich weiß. Aber wenn ich sie addiere, erhalte ich einen Fehler. Meldung 4104, Ebene 16, Status 1, Zeile 12 Die mehrteilige Kennung "Product.ProductOmschrijving" konnte nicht gebunden werden. – viest
Nur zu sagen, dass Sie einen Fehler erhalten, ist nicht sehr hilfreich. Es wäre hilfreich, den Fehler anzugeben. In diesem Fall vermute ich, dass es daran liegt, dass Sie sie nicht zur Gruppe hinzugefügt haben. –