Web
Site
Search
Home
Agenda
Links
Bloggers
Over PASS Nederland
User benefits
Geregistreerd... en dan?
Sponsoring
Artikelen
Cursussen en certificering
SQL Server 2000
SQL Server 2005
Examenrecensies
Archief nieuwsbrieven
2005
Nieuwsbrief 1 / 09-10-2005
2006
Nieuwsbrief 2 / 16-01-2006
Nieuwsbrief 3 / 11-03-2006
Nieuwsbrief 4 / 11-04-2006
Nieuwsbrief 5 / 08-05-2006
Nieuwsbrief 6 / 18-06-2006
Nieuwsbrief 7 / 27-08-2006
Nieuwsbrief 8 / 20-10-2006
Nieuwsbrief 9 / 22-12-2006
2007
Nieuwsbrief 10 / 04-02-2007
Nieuwsbrief 11 / 05-03-2007
Nieuwsbrief 12 / 02-04-2007
Nieuwsbrief 13 / 01-05-2007
Nieuwsbrief 14 / 01-06-2007
Nieuwsbrief 15 / 02-09-2007
Nieuwsbrief 16 / 04-10-2007
Nieuwsbrief 17 / 05-11-2007
Nieuwsbrief 18 / 06-12-2007
2008
Nieuwsbrief 19 / 12-01-2008
Nieuwsbrief 20 / 14-02-2008
Nieuwsbrief 21 / 02-05-2008
Nieuwsbrief 22 / 16-06-2008
Nieuwsbrief 23 / 01-08-2008
Nieuwsbrief 24 / 13-11-2008
Nieuwsbrief 25 / 01-12-2008
2009
Nieuwsbrief 26 / 07-02-2009
Nieuwsbrief 27 / 09-03-2009
Nieuwsbrief 28 / 01-04-2009
Nieuwsbrief 29 / 04-06-2009
Nieuwsbrief 30 / 02-09-2009
Nieuwsbrief 31 / 06-10-2009
Nieuwsbrief 32 / 07-11-2009
Nieuwsbrief 33 / 04-12-2009
Forum
onze sponsors
Forum
Login
|
Register
Forum
Unanswered
Active Topics
Forums
Search
Forums
>
Forums
>
Ontwikkelen
Subject: Hulp bij een query om totalen te berekenen
Prev
Next
You are not authorized to post a reply.
Author
Messages
Oldest First
Newest First
Stephan Bussing
Posts:67
05-12-2008 14:23:59
Alert
Hoi ik ben bezig om een query te schrijven die een aantal totalen teruggeeft.
Toelichting:
Ik heb een aantal tabellen die hierarchisch met elkaar zijn verbonden. uiteindelijk tot 4 niveaus diep. elke parent heeft 1..N children. Nu wil ik op basis van de hoofdentiteit berekenen hoeveel children er zijn van de verschillende typen. Als eerste heb ik een view gemaakt die van een hoofentiteit alle records bij elkaar zoekt. Dit is dan ook mijn uitgangspunt. Met een select beperkt ik het aantal velden wat ik terug wil hebben. Vervolgens doe ik voor elk veld een aparte query die ik group. In totaal dus 5 querys. De resultaten gaan in aparte tempTables en aan het einde stop ik de resultaten van de tempTables in een TotaleTable (ook een temptable). Ik denk dat het een beetje omslachtig is, vandaar dat ik mij tot jullie richt.
Het werkt allemaal wel, maar volgens mij kan het wel iets handiger.
zie de code:
[code]
DROP TABLE #Totals
CREATE TABLE #Totals
(
Patterns INT,
Problems INT,
MainGoals INT,
WorkGoals INT,
Disciplines INT
)
SELECT COUNT(*) Patterns
INTO #tempPatterns
FROM [dbo].[vw_HCP]
WHERE [HealthCarePlanId]=117
GROUP BY [PatternId]
--if object_id(N'[tempdb].[dbo].[#tempPatterns]') is not null
SELECT COUNT(*) Problems
INTO #tempProblems
FROM [dbo].[vw_HCP]
WHERE [HealthCarePlanId]=117
GROUP BY [PatternProblemId]
--if object_id(N'[tempdb].[dbo].[#tempProblems]') is not null
SELECT COUNT(*) MainGoals
INTO #tempMainGoals
FROM [dbo].[vw_HCP]
WHERE [HealthCarePlanId]=117
GROUP BY [ProblemMainGoalId]
--if object_id(N'[tempdb].[dbo].[#tempMainGoals]') is not null
SELECT COUNT(*) WorkGoals
INTO #tempWorkGoals
FROM [dbo].[vw_HCP]
WHERE [HealthCarePlanId]=117
GROUP BY [MainGoalWorkGoalId]
--if object_id(N'[tempdb].[dbo].[#tempWorkGoals]') is not null
SELECT COUNT(*) Disciplines
INTO #tempDisciplines
FROM [dbo].[vw_HCP]
WHERE [HealthCarePlanId]=117
GROUP BY [WorkGoalDisciplineId]
DECLARE @Patterns INT
DECLARE @Problems INT
DECLARE @MainGoals INT
DECLARE @WorkGoals INT
DECLARE @Disciplines INT
SELECT @Patterns=COUNT(*) FROM #tempPatterns
SELECT @Problems=COUNT(*) FROM #tempProblems
SELECT @MainGoals=COUNT(*) FROM #tempMainGoals
SELECT @WorkGoals=COUNT(*) FROM #tempWorkGoals
SELECT @Disciplines=COUNT(*) FROM #tempDisciplines
DROP TABLE #tempProblems
DROP TABLE #tempWorkGoals
DROP TABLE #tempPatterns
DROP TABLE #tempMainGoals
DROP TABLE #tempDisciplines
INSERT INTO #Totals VALUES (@Patterns,@Problems,@MainGoals,@WorkGoals,@Disciplines)
SELECT * FROM #Totals
[/code]
David Scheltens
Posts:33
05-12-2008 14:32:16
Alert
Als je sql 2005 of hoger gebruikt, dan zou je dit moeten kunnen oplossen met een CTE (common table expression)
Stephan Bussing
Posts:67
05-12-2008 16:22:48
Alert
Hallo David,
Goeie tip, maar ik kan (nog) geen 2005 gebruiken. Ik moet het nog doen met good old 2000.
Bedankt voor je reactie.
Gr.
Vladimir Eric
Posts:6
12-12-2008 16:49:09
Alert
Hi Stephan,
Ik zou "count(distinct " proberen:
SELECT COUNT(distinct PatternId) Patterns,
COUNT(distinct PatternProblemId) Problems,
COUNT(distinct ProblemMainGoalId) MainGoals,
COUNT(distinct MainGoalWorkGoalId) WorkGoals,
COUNT(distinct WorkGoalDisciplineId) Disciplines
FROM [dbo].[vw_HCP]
WHERE [HealthCarePlanId]=117
gr.
You are not authorized to post a reply.
Algemeen
--Forum regels
PASS Nederland
--Aankondigingen
--Bijeenkomsten
--PASS Nederland Algemeen
Forums
--DBA
--Ontwikkelen
--Business Intelligence
--Metadata (SIG)
--Performance (SIG)
--High Availability (SIG)
--XML (SIG)
--Algemeen
SQL Server
Forums
>
Forums
>
Ontwikkelen
> Hulp bij een query om totalen te berekenen
ActiveForums 3.6
Copyright (c) 2012 PASS Nederland
Privacy Statement
Terms Of Use