It works fine untill I start using UNION ALL!
Have to use classified here because I'm not allowed to share too much information
It should show the Reden Uitstroom and NOT the ID. I have really no clue why this happens...
Look-up Table=
ID Description
1 classified
2 classified
3 classified
4 classified
5 classified
6 classified
7 classified
8 classified
9 classified
Code =
SELECT Count(Hertoetsing.[Reden uitstroom1]) AS [Aantal Uitstroom 2014],
(Hertoetsing.[Reden uitstroom1]) AS [Reden Uitstroom]
FROM Klantinformatie
INNER JOIN Hertoetsing
ON Klantinformatie.KlantID=Hertoetsing.Klantid
WHERE (((Year(Hertoetsing.[Datum uitstroom1]))=2014))
GROUP BY Hertoetsing.[Reden uitstroom1]
UNION ALL
SELECT Count(Hertoetsing.[Reden uitstroom2]) AS [Aantal Uitstroom 2014],
(Hertoetsing.[Reden uitstroom2]) AS [Reden Uitstroom]
FROM Klantinformatie
INNER JOIN Hertoetsing
ON Klantinformatie.KlantID=Hertoetsing.Klantid
WHERE (((Year(Hertoetsing.[Datum uitstroom2]))=2014))
GROUP BY Hertoetsing.[Reden uitstroom2]
UNION ALL
SELECT Count(Hertoetsing.[Reden uitstroom3]) AS [Aantal Uitstroom 2014],
(Hertoetsing.[Reden uitstroom3]) AS [Reden Uitstroom]
FROM Klantinformatie
INNER JOIN Hertoetsing
ON Klantinformatie.KlantID=Hertoetsing.Klantid
WHERE (((Year(Hertoetsing.[Datum uitstroom3]))=2014))
GROUP BY Hertoetsing.[Reden uitstroom3];
Result =
Aantal Uitstroom 2014 Reden Uitstroom
27 1
25 2
46 3
1 4
3 5
9 6
17 8
3 9
4 1
2 2
5 3
1 4
1 6
1 1
1 2
1 3
2 8
Correcting Group By =
SELECT Sum([Aantal Uitstroom 2014]) AS [Uitstroom 2014],
[Reden Uitstroom] AS [Uitstroom]
FROM [Reden Uitstroom1 2014]
GROUP BY [Reden Uitstroom];
Result =
Uitstroom 2014 Uitstroom
32 1
28 2
52 3
2 4
3 5
10 6
19 8
3 9
EDIT - Another Issues with another query =
Query runs properly but once I put it into a report then it starts showing the ID from the look-up table instead of description.... This is so weird....
Related
Can someone help. Been trawling through Google and loads of forums but can't seem to find what I am looking for. I need some kind of running ID added to my data. See example below.
This is my data
ID
A
B
C
1
22
WP1234
C
2
22
WP1235
C
3
22
WP1236
O
4
24
WP1237
C
5
24
WP1238
C
6
24
WP1239
O
7
26
WP1240
C
8
26
WP1241
C
9
28
WP1242
C
I need to get some kind of running ID based on columns and A, C.
Desired outcome would be
ID
A
B
C
RunningID
1
22
WP1234
C
1
2
22
WP1235
C
2
3
22
WP1236
O
1
4
24
WP1237
C
1
5
24
WP1238
C
2
6
24
WP1239
O
1
7
26
WP1240
C
1
8
26
WP1241
C
2
9
28
WP1242
C
1
I think you want row_number():
select t.*,
row_number() over (partition by a, c order by id) as running_id
from t;
Simple question. Say I have a table with two columns, PersonID and CardID and 10 rows. I have another Table that contains 1 column, Code and 10 rows. I simply want to stack the codes to the other table. Note that is does not matter which person gets which code, they should all just get one.
At the moment I find myself adding rownumbers in both tables and updating were TableA.ID = TableB.ID. But this seems overly complex for such a simple taks.
Any suggestions?
TableA
PersonID CardID
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
Table B
Code
Random1
Random2
Random3
Random4
Random5
Random6
Random7
Random8
Random9
Random10
Desired Result:
PersonID CardID Code
1 1 Random1
2 2 Random2
3 3 Random3
4 4 Random4
5 5 Random5
6 6 Random6
7 7 Random7
8 8 Random8
9 9 Random9
10 10 Random10
Again, code order does not matter. Each person, one code. That's it.
You need not to append 1 extra column to each row, You can use CTE with RowNum like :
;WITH CTE_Results
AS (
SELECT ROW_NUMBER() OVER (ORDER BY Code) as ID, Code
from TableB B
)
select A.PersonId, A.CardID, CTE_Results.Code from CTE_Results
INNER JOIN TableA A ON A.PersonId= CTE_Results.ID
Working SQL Fiddle is : http://sqlfiddle.com/#!18/f8f86/9
Result will be like :
PersonId CardID Code
1 1 Random1
2 2 Random10
3 3 Random2
4 4 Random3
5 5 Random4
6 6 Random5
7 7 Random6
8 8 Random7
9 9 Random8
10 10 Random9
Pardon the title gore. I'm having trouble finding a good way to express my question, which is endemic to the problem.
The Tables
season
id name
---- ------
1 Season 1
2 Season 2
3 Season 3
episode
id season_id number title
---- ----------- -------- ---------------------------------------
1 1 1 Pilot
2 1 2 1x02 - We Got Picked Up
3 1 3 1x03 - This is the Third Episode
4 2 1 2x01 - We didn't get cancelled.
5 2 2 2x02 - We're running out of ideas!
6 3 1 3x01 - We're still here.
7 3 2 3x02 - Okay, this game show is dying.
8 3 3 3x03 - Untitled
score
id episode_id score contestant_id (table not given)
---- ------------ ------- ---------------------------------
1 1 35 1
2 1 -12 2
3 1 8 3
4 1 5 4
5 2 13 1
6 2 -2 5
7 2 3 3
8 2 -14 6
9 3 -14.5 1
10 3 -3 2
11 3 1.5 7
12 3 9.5 5
13 4 22.8 1
14 4 -3 8
15 5 2 1
16 5 13.5 9
17 5 7 3
18 6 13 1
19 6 -84 10
20 6 12 11
21 7 3 1
22 7 10 2
23 8 29 1
24 8 1 5
As you can see, you have multiple episodes per season, and multiple scores per episode (one score per contestant). Contestants can reappear in later episodes (irrelevant), scores are floating point values, and there can be an arbitrary number of scores per episode.
So what am I looking for?
I'd like to get the average total episode score per season, where the total episode score is the sum of all the scores in an episode. Mathematically, this comes out to be the sum of all scores in a season divided by the number of episodes. Easy enough to comprehend, but I have had trouble doing it in a single query and getting the correct result. I'd like an output like the following:
name average_total_episode_score
---------- -----------------------------
Season 1 9.83
Season 2 21.15
Season 3 -5.33
The top-level query needs to be on the season table as it will be combined with other, similar queries on the same table. It's easy enough to do this with an aggregate in a subquery, but an aggregation executes the subquery, failing my single-query requirement. Can this be done in a single query?
Hope this should work
Select s.id, avg(score)
FROM Season S,
Episode e,
Score sc
WHERE s.id = e.season_id
AND e.id = sc.episode_id
Group by s.id
Okay, just figured it out. As usual, I had to write and post a whole book before the simple solution descended upon me.
The problem in my query (which I didn't give in the question) was the lack of a DISTINCT count. Here is a working query:
SELECT
"season"."id",
"season"."name",
(SUM("score"."score") / COUNT(DISTINCT "episode"."id")) AS "average_total_episode_score"
FROM "season"
LEFT OUTER JOIN "episode"
ON ("season"."id" = "episode"."season_id")
LEFT OUTER JOIN "score"
ON ("episode"."id" = "score"."episode_id")
GROUP BY "season"."id"
select Se.id AS Season_Id, sum(score) As season_score, avg(score) from score S join episode E ON S.episode_id = E.id
join Season se ON se.id = e.season_id group by se.id
I need to create some checks to make sure that students are enrolled in the correct courses with the correct number of units. Here is my SQL at the moment.
SELECT StudentID
,AssessmentCode
,BoardCode
,BoardCategory
,BoardUnits
,sum(cast(boardunits as int)) over (partition by studentid,boardcategory) as UnitCount
,Count(boardcategory) over (partition by studentid) as SubjectCount
FROM uvNCStudentSubjectDetails
where fileyear = 2015
and filesemester = 1
and studentyearlevel = 11
and StudentIBFlag = 0
order by Studentnameinternal,BoardCategory
This gives me the following info...
StudentID AssessmentCode BoardCode BoardCategory BoardUnits UnitCount SubjectCount
61687 11TECDAT 11080 A 2 11 7
61687 11PRS1U 11350 A 1 11 7
61687 11MATGEN 11235 A 2 11 7
61687 11LANGRB 11870 A 2 11 7
61687 11ENGSTD 11130 A 2 11 7
61687 11GEOGEO 11190 A 2 11 7
64549 11TECIND 11200 A 2 10 7
64549 11SCIPHY 11310 A 2 10 7
64549 11SCIEAE 11100 A 2 10 7
64549 11MATGEN 11235 A 2 10 7
64549 11ENGSTD 11130 A 2 10 7
64549 11TECHOS 26501 B 2 2 7
64549 11MUSDRS 63212 C 1 1 7
45461 11ECOECO 11110 A 2 13 7
45461 11ENGADV 11140 A 2 13 7
45461 11HISMOD 11270 A 2 13 7
45461 11HISLST 11220 A 2 13 7
45461 11MATMAT 11240 A 2 13 7
45461 11PRS1U 11350 A 1 13 7
45461 11SCIBIO 11030 A 2 13 7
Note for the first student, I have a count of Category A subject Units (11 in total) He is only doing Category A subjects. For the second student, he has 10 units of Category A subjects, he is doing 1 Category B subject worth 2 units and one category C subject worth 1 unit. the final student just has 13 Category A units.
Now what I would really like is something like this...!
StudentID Sum A Units Sum B Units Sum C Units Sum A Units + Sum B Units Count of Subjects
61687 11 0 0 11 7
64549 10 2 1 12 7
45461 13 0 0 13 7
So I would like some aggregated functions with a student grouped onto only 1 row and the sum of his different units as separate fields. I would also like a field which sums the Category A and B Units and also a field which gives a count of the total number of subjects they are doing. I could then use this data to set up some warning messages if a student is not doing the correct number of A or B Units etc
I have played around with common table expressions, subqueries etc but am not really sure what I am doing and am not sure which is the correct way about getting the data in the form I want.
Is anyone able to help?
SELECT
STUDENTID,
SUM(CASE BOARDCATEGORY WHEN 'A' THEN 1 ELSE 0 END) AS SUM_A_UNITS,
SUM(CASE BOARDCATEGORY WHEN 'B' THEN 1 ELSE 0 END) AS SUM_B_UNITS,
SUM(CASE BOARDCATEGORY WHEN 'C' THEN 1 ELSE 0 END) AS SUM_C_UNITS,
SUM(CASE BOARDCATEGORY WHEN 'A' THEN 1 WHEN 'B' THEN 1 ELSE 0 END) AS SUM_A_UNITS+SUM_B_UNITS,
COUNT(BOARDCODE) AS COUNT_OF_SUBJECTS
FROM (
SELECT StudentID
,AssessmentCode
,BoardCode
,BoardCategory
,BoardUnits
,sum(cast(boardunits as int)) over (partition by studentid,boardcategory) as UnitCount
,Count(boardcategory) over (partition by studentid) as SubjectCount
FROM uvNCStudentSubjectDetails
where fileyear = 2015
and filesemester = 1
and studentyearlevel = 11
and StudentIBFlag = 0
order by Studentnameinternal,BoardCategory
)
GROUP BY STUDENTID;
Wrapped your SQL statement in the solution, so that you can see what the solution does straight away.
Use SUM and CASE (i.e. SUM only when a condition is met).
Say I have two tables, geom_levels and taz_geoms where taz_geoms has the columns as follows:
taz_geoms
id(int) state(int) county(int) taz(int) geom(geometry(MultiPolygon,4326))
and geom_levels looks like this:
geom_levels
TAZ COUNTY STATE DISTRICT
1 1 29 1
2 1 29 1
3 1 29 1
4 2 29 2
5 2 29 2
6 2 29 2
7 2 29 3
8 3 29 3
9 3 29 3
10 3 29 4
11 3 29 4
12 3 29 4
13 4 29 5
14 4 29 5
15 4 29 5
16 4 29 6
17 4 29 6
How would I go about combining these taz geometries into county, state, and district geometries? I would like to have a county_geoms, state_geoms, and district_geoms table. I have see that you can use ST_UNION with a geom array, but how would I generate such an array for counties or districts?
I was thinking something like this for counties:
SELECT ST_UNION(SELECT geom from taz_geoms GROUP BY county);
and for districts:
SELECT ST_UNION(SELECT geom from taz_geoms t LEFT JOIN geom_levels gl ON gl.taz = t.taz GROUP BY district);
But those options do not see possible.
Ideas?
try with:
SELECT ST_UNION( ARRAY( 'YOUR SELECT geoms QUERY' ) );
in your case:
SELECT ST_UNION(ARRAY( (SELECT geom from taz_geoms t LEFT JOIN geom_levels gl ON gl.taz = t.taz GROUP BY district) ));
I had the same problem and got it to work with postgre usin the ARRAY() function ;)