SQL nested Select....I think? - sql

I have 2 tables UnitProd and Unit.
Unit = unitproductivityid,unitid, unitnumber, fleet
UnitProd = unitproductivityid, day, shipweight, stops
I have multiple units in each table and I am trying to do group by functions to get counts of different things.(The tables have more fields than specified this is just example purposes.)
So basically I have the following:
SELECT
u.[Fleet]
,u.[Unit]
,up.[Day]
,((SUM(up.[Shipment_Weight]))/2000) AS [ShipmentWeight]
,((SUM(up.[Shipment_Weight]))/COUNT(up.[Stops])) AS [ShpmntAvg]
FROM
[dbo].[UnitProductivity] u
INNER JOIN [dbo].[UnitProductivityDetails] up
ON u.UnitProductivityId = up.UnitProductivityId
GROUP BY u.fleet, u.unit
So basically the issue I am having is that some up.[Stops] fields have a 0 in them so I want to exclude these. So basically a unit has 1-30 days no matter what and some of those days have a 0 as [Stop] so I want to count(ONLY DAYS with a stop). Would I use a nested select here and how?
Thanks

Unless I am misunderstanding the question, you don't need a nested SELECT.
Just add the following before your GROUP BY:
WHERE up.[Stops] > 0

It doesn't have to be nested, but here's a simple way:
SELECT * FROM Unit WHERE unitproductivityid IN (SELECT unitproductivityid FROM UnitProd WHERE stops > 0) as UP
Good luck!

after your GROUP BY line, add HAVING count(up.[Stops]) > 0

With existing code you can do HAVING clause:
SELECT
u.[Fleet]
,u.[Unit]
,up.[Day]
,((SUM(up.[Shipment_Weight]))/2000) AS [ShipmentWeight]
,((SUM(up.[Shipment_Weight]))/COUNT(up.[Stops])) AS [ShpmntAvg]
FROM
[dbo].[UnitProductivity] u
INNER JOIN [dbo].[UnitProductivityDetails] up
ON u.UnitProductivityId = up.UnitProductivityId
GROUP BY u.fleet, u.unit
HAVING(COUNT(up.[Stops]) > 0
More about HAVING clause on: http://www.w3schools.com/sql/sql_having.asp

Related

sql oracle sum valaues in multiple columns

im looking for solutions to my problem.
i have a query
select em_name, sum(abs_day_left)
from pp_employees,
pp_types_abs,
pp_abs
where em_id = abs_em_id and abs_abs_id = abs_id and
abs_kod in ('12','13','14','15')
group by em_name
i want to make more columns with another abs_kod number (image attachment)
for example
second column
... abs_kod in
('656','44','323','33')
third column
... abs_kod in
('63','55','565','556')
and more..
example table
thanks for help and nice weekend
One more thing...
the formula counts days from the whole month
how to make it count correctly the days when it sets the parameters for the half month, for example from 1980-01-01 to 1980-03-15
thanks in advance
bob
I think that you are looking for conditional aggregation:
select
em_name,
sum(case when abs_kod in (12,13,14,15) then abs_day_left end) abs_day_left_1,
sum(case when abs_kod in (656,44,323,33) then abs_day_left end) abs_day_left_2,
sum(case when abs_kod in (63,55,565,556) then abs_day_left end) abs_day_left_3
from pp_employees
inner join pp_abs on em_id = abs_em_id
inner join pp_types_abs on abs_abs_id = abs_id
where and abs_kod in (12,13,14,15,656,44,323,33,63,55,565,556)
group by em_name
Notes:
always use explicit joins instead of old-shool, implicit joins - I tried to fix this but I am unsure I did it correctly, for a reason that lies in the following point...
always qualify the columns in the query with the table they belong to

mSSQL, SQL view, select, percentage query

So this is the requirement I need to meet:
Aggregated data of all the schools in the ESD, grouped by
SchoolDistrict.SchoolDistrictID
(get the same data as the school district scenario above, then add grouping by district, filtered to
EducationServiceDistrict. EducationServiceDistrictID
)
Also calculate percentage of pass, fail, and untested
How do I calculate the percentage pass, fail, and untested?
This is the query I have written so far.
CREATE VIEW district_map AS
SELECT * and SchoolDistrictID,
EducationServiceDistrict
FROM SchoolDistrict_View
and SchoolDistrict,
EducationServiceDistrict
GROUP BY EducationServiceDistrict.EducationServiceDistrictID
ORDER BYLeadWaterTestLocation.PassFail
This is the general idea of how these problems are solved - if you understand this simplified version you will be able to solve your problem.
select d.districtName,
s.studentCount,
case when s.studentCount=0 then 0 else s.passed / s.studentCount * 100 end as PassedPct
from district d
join (select districtId,
sum(studentCount) as studentCount,
sum(passed) as passed
from schools
group by districtId) as s
on d.districtId = s.districtId
order by d.districtName

SQL query, sub queries

I have a table storing sports results for a series of events: ONS_Skippers
The relevant columns from this table for the question are:
FK_EventID, FK_SkipperID and intResult.
I'm presenting different statistics from this database, but I've not succeeded to generate the query for the most advanced one: I would like to list average performance for each participant (FK_SkipperID). I've defined performance to be 100% for an event win, 0% for last place in an event and performance on a linear curve between the two extents. The formula for this is:
Performance = 100*(1-(intResult-1)/(NumberOfParticipantsInTheEvent-1))
NumberOfParticipantsInTheEvent varies from each event, hence needs to be counted from each group of FK_EventID. All my attempts so far has failed:
Example:
SELECT FK_SkipperID, AVG((1-(intResult-1.0)/((SELECT Count(FK_EventID)
FROM ONS_Skippers AS ONS_Skippers2
WHERE ONS_Skippers.FK_EventID = ONS_Skippers2.FK_EventID AND FK_SkipperID > 0
GROUP BY FK_EventID)-1))*100)
FROM ONS_Skippers
GROUP BY FK_SkipperID
This gives error messages "Cannot perform an aggregate function on an expression containing an aggregate or a subquery".
Any idea on how to produce the wanted output?
Try to join to the subquery instead:
SELECT
FK_SkipperID,
AVG((1-(intResult-1.0)/(e.events-1))*100)
FROM ONS_Skippers o
INNER JOIN
(
SELECT Count(FK_EventID) AS events
FROM ONS_Skippers AS ONS_Skippers2
WHERE FK_SkipperID > 0
GROUP BY FK_EventID
) e
ON o.FK_EventID = e.FK_EventID
GROUP BY FK_SkipperID
I think you could achieve this by joining to an inline table as follows...
select SkipperID,
AVG(100*(1-(Result-1)/(p.NumParticipants-1))) as Performance
from Spike.Skippers s
inner join (
select EventId, Count(EventId) as NumParticipants -- Or Max(Result)
from Spike.Skippers
group by EventID
) p on s.EventID = p.EventID
group by SkipperID
[Edit] Apologies for not sticking to your column naming conventions - my OCD insisted I adhere to my own personal standard. Fussy, I know. [/Edit]

SQL SUM function doubling the amount it should using multiple tables

My query below is doubling the amount on the last record it returns. I have 3 tables - activities, bookings and tempbookings. The query needs to list the activities and attached information and pull the total number (using the SUM) of places booked (as BookingTotal) from the booking table by each activity and then it needs to calculate the same for tempbookings (as tempPlacesReserved) providing the reservedate field inside that table is in the future.
However the first issue is that if there are no records for an activity in the tempbookings table it does not return any records for that activity at all, to get around this i created dummy records in the past so that it still returns the record, but if I can make it so I don't have to do this I would prefer it!
The main issue I have is that on the final record of the returned results it doubles the booking total and the places reserved which of course makes the whole query useless.
I know that I am doing something wrong I just haven't been able to sort it, I have searched similar issues online but am unable to apply them to my situation correctly.
Any help would be appreciated.
P.S. I'm aware that normally you wouldn't need to fully label all the paths to the databases, tables and fields as I have but for the program I am planning to use it in I have to do it this way.
Code:
SELECT [LeisureActivities].[dbo].[activities].[activityID],
[LeisureActivities].[dbo].[activities].[activityName],
[LeisureActivities].[dbo].[activities].[activityDate],
[LeisureActivities].[dbo].[activities].[activityPlaces],
[LeisureActivities].[dbo].[activities].[activityPrice],
SUM([LeisureActivities].[dbo].[bookings].[bookingPlaces]) AS 'bookingTotal',
SUM (CASE WHEN[LeisureActivities].[dbo].[tempbookings].[tempReserveDate] > GetDate() THEN [LeisureActivities].[dbo].[tempbookings].[tempPlaces] ELSE 0 end) AS 'tempPlacesReserved'
FROM [LeisureActivities].[dbo].[activities],
[LeisureActivities].[dbo].[bookings],
[LeisureActivities].[dbo].[tempbookings]
WHERE ([LeisureActivities].[dbo].[activities].[activityID]=[LeisureActivities].[dbo].[bookings].[activityID]
AND [LeisureActivities].[dbo].[activities].[activityID]=[LeisureActivities].[dbo].[tempbookings].[tempActivityID])
AND [LeisureActivities].[dbo].[activities].[activityDate] > GetDate ()
GROUP BY [LeisureActivities].[dbo].[activities].[activityID],
[LeisureActivities].[dbo].[activities].[activityName],
[LeisureActivities].[dbo].[activities].[activityDate],
[LeisureActivities].[dbo].[activities].[activityPlaces],
[LeisureActivities].[dbo].[activities].[activityPrice];
Your current query is using an INNER JOIN between each of the tables so if the tempBookings table has no records, you will not return anything.
I would advise that you start to use JOIN syntax. You might also need to use subqueries to get the totals.
SELECT a.[activityID],
a.[activityName],
a.[activityDate],
a.[activityPlaces],
a.[activityPrice],
coalesce(b.bookingTotal, 0) bookingTotal,
coalesce(t.tempPlacesReserved, 0) tempPlacesReserved
FROM [LeisureActivities].[dbo].[activities] a
LEFT JOIN
(
select activityID,
SUM([bookingPlaces]) AS bookingTotal
from [LeisureActivities].[dbo].[bookings]
group by activityID
) b
ON a.[activityID]=b.[activityID]
LEFT JOIN
(
select tempActivityID,
SUM(CASE WHEN [tempReserveDate] > GetDate() THEN [tempPlaces] ELSE 0 end) AS tempPlacesReserved
from [LeisureActivities].[dbo].[tempbookings]
group by tempActivityID
) t
ON a.[activityID]=t.[tempActivityID]
WHERE a.[activityDate] > GetDate();
Note: I am using aliases because it is easier to read
Use new SQL-92 Join syntax, and make join to tempBookings an outer join. Also clean up your sql with table aliases. Makes it easier to read. As to why last row has doubled values, I don't know, but on off chance that it is caused by extra dummy records you entered. get rid of them. That problem is fixed by using outer join to tempBookings. The other possibility is that the join conditions you had to the tempBookings table(t.tempActivityID = a.activityID) is insufficient to guarantee that it will match to only one record in activities table... If, for example, it matches to two records in activities, then the rows from Tempbookings would be repeated twice in the output, (causing the sum to be doubled)
SELECT a.activityID, a.activityName, a.activityDate,
a.activityPlaces, a.activityPrice,
SUM(b.bookingPlaces) bookingTotal,
SUM (CASE WHEN t.tempReserveDate > GetDate()
THEN t.tempPlaces ELSE 0 end) tempPlacesReserved
FROM LeisureActivities.dbo.activities a
Join LeisureActivities.dbo.bookings b
On b.activityID = a.activityID
Left Join LeisureActivities.dbo.tempbookings t
On t.tempActivityID = a.activityID
WHERE a.activityDate > GetDate ()
GROUP BY a.activityID, a.activityName,
a.activityDate, a.activityPlaces,
a.activityPrice;

MySQL to PostgreSQL: GROUP BY issues

So I decided to try out PostgreSQL instead of MySQL but I am having some slight conversion problems. This was a query of mine that samples data from four tables and spit them out all in on result.
I am at a loss of how to convey this in PostgreSQL and specifically in Django but I am leaving that for another quesiton so bonus points if you can Django-fy it but no worries if you just pure SQL it.
SELECT links.id, links.created, links.url, links.title, user.username, category.title, SUM(votes.karma_delta) AS karma, SUM(IF(votes.user_id = 1, votes.karma_delta, 0)) AS user_vote
FROM links
LEFT OUTER JOIN `users` `user` ON (`links`.`user_id`=`user`.`id`)
LEFT OUTER JOIN `categories` `category` ON (`links`.`category_id`=`category`.`id`)
LEFT OUTER JOIN `votes` `votes` ON (`votes`.`link_id`=`links`.`id`)
WHERE (links.id = votes.link_id)
GROUP BY votes.link_id
ORDER BY (SUM(votes.karma_delta) - 1) / POW((TIMESTAMPDIFF(HOUR, links.created, NOW()) + 2), 1.5) DESC
LIMIT 20
The IF in the select was where my first troubles began. Seems it's an IF true/false THEN stuff ELSE other stuff END IF yet I can't get the syntax right. I tried to use Navicat's SQL builder but it constantly wanted me to place everything I had selected into the GROUP BY and that I think it all kinds of wrong.
What I am looking for in summary is to make this MySQL query work in PostreSQL. Thank you.
Current Progress
Just want to thank everybody for their help. This is what I have so far:
SELECT links_link.id, links_link.created, links_link.url, links_link.title, links_category.title, SUM(links_vote.karma_delta) AS karma, SUM(CASE WHEN links_vote.user_id = 1 THEN links_vote.karma_delta ELSE 0 END) AS user_vote
FROM links_link
LEFT OUTER JOIN auth_user ON (links_link.user_id = auth_user.id)
LEFT OUTER JOIN links_category ON (links_link.category_id = links_category.id)
LEFT OUTER JOIN links_vote ON (links_vote.link_id = links_link.id)
WHERE (links_link.id = links_vote.link_id)
GROUP BY links_link.id, links_link.created, links_link.url, links_link.title, links_category.title
ORDER BY links_link.created DESC
LIMIT 20
I had to make some table name changes and I am still working on my ORDER BY so till then we're just gonna cop out. Thanks again!
Have a look at this link GROUP BY
When GROUP BY is present, it is not
valid for the SELECT list expressions
to refer to ungrouped columns except
within aggregate functions, since
there would be more than one possible
value to return for an ungrouped
column.
You need to include all the select columns in the group by that are not part of the aggregate functions.
A few things:
Drop the backticks
Use a CASE statement instead of IF() CASE WHEN votes.use_id = 1 THEN votes.karma_delta ELSE 0 END
Change your timestampdiff to DATE_TRUNC('hour', now()) - DATE_TRUNC('hour', links.created) (you will need to then count the number of hours in the resulting interval. It would be much easier to compare timestamps)
Fix your GROUP BY and ORDER BY
Try to replace the IF with a case;
SUM(CASE WHEN votes.user_id = 1 THEN votes.karma_delta ELSE 0 END)
You also have to explicitly name every column or calculated column you use in the GROUP BY clause.