January 18, 2012 at 8:07 am
I was able to get the query results I need, but I am looking to now present the results as the total count of trouble tickets assigned to each person. I can get a total count of rows returned, but I am trying to get a count of tickets assigned to each person. I don't work a lot with aggregate functions so this one is stumping me.
SELECT
DISTINCT TroubleTicket.TicketNumber,
Asset.Person
FROM
Asset
RIGHT JOIN TroubleTicket
ON Asset.Id = TroubleTicket.AssetId
RIGHT JOIN Item
ON (Asset.Person = Item.Person and Asset.CompanyId = Item.CompanyId)
WHERE
(TroubleTicket.UT02 IS NOT NULL AND TroubleTicket.UT03 IS NOT NULL)
AND Item.Model NOT LIKE 'V1%'
January 18, 2012 at 8:13 am
jennigirl (1/18/2012)
I was able to get the query results I need, but I am looking to now present the results as the total count of trouble tickets assigned to each person. I can get a total count of rows returned, but I am trying to get a count of tickets assigned to each person. I don't work a lot with aggregate functions so this one is stumping me.SELECT DISTINCT TroubleTicket.TicketNumber, Asset.Person
FROM Asset
RIGHT JOIN TroubleTicket ON Asset.Id = TroubleTicket.AssetId
RIGHT JOIN Item ON (Asset.Person = Item.Person and Asset.CompanyId = Item.CompanyId)
WHERE (TroubleTicket.UT02 IS NOT NULL AND TroubleTicket.UT03 IS NOT NULL)
AND Item.Model NOT LIKE 'V1%'
So your select statement would be something like this.
SELECT Asset.Person, COUNT(*)
FROM Asset
RIGHT JOIN TroubleTicket ON Asset.Id = TroubleTicket.AssetId
RIGHT JOIN Item ON (Asset.Person = Item.Person and Asset.CompanyId = Item.CompanyId)
WHERE (TroubleTicket.UT02 IS NOT NULL AND TroubleTicket.UT03 IS NOT NULL)
AND Item.Model NOT LIKE 'V1%'
GROUP BY Asset.Person
Fraggle
January 18, 2012 at 8:23 am
That makes more sense now. Thank you so much
January 18, 2012 at 8:38 am
jennigirl (1/18/2012)
I was able to get the query results I need, but I am looking to now present the results as the total count of trouble tickets assigned to each person. I can get a total count of rows returned, but I am trying to get a count of tickets assigned to each person. I don't work a lot with aggregate functions so this one is stumping me.SELECT
DISTINCT TroubleTicket.TicketNumber,
Asset.Person
FROM
Asset
RIGHT JOIN TroubleTicket
ON Asset.Id = TroubleTicket.AssetId
RIGHT JOIN Item
ON (Asset.Person = Item.Person and Asset.CompanyId = Item.CompanyId)
WHERE
(TroubleTicket.UT02 IS NOT NULL AND TroubleTicket.UT03 IS NOT NULL)
AND Item.Model NOT LIKE 'V1%'
It's unusual for folks to use right joins - are you sure this is correct?
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
January 18, 2012 at 9:17 am
I used a right join because I need the results from the TroubleTicket Table, but the Asset table is the one that links Asset to Item and Asset to TroubleTicket. I've never had any training on this and am pretty much teaching myself as I go, so there may be an easier way to do this that I have not learned yet.
January 18, 2012 at 9:27 am
This might help. It represent joins as venn diagrams.
February 13, 2012 at 11:46 pm
ChrisM@home (1/18/2012)
jennigirl (1/18/2012)
I was able to get the query results I need, but I am looking to now present the results as the total count of trouble tickets assigned to each person. I can get a total count of rows returned, but I am trying to get a count of tickets assigned to each person. I don't work a lot with aggregate functions so this one is stumping me.SELECT
DISTINCT TroubleTicket.TicketNumber,
Asset.Person
FROM
Asset
RIGHT JOIN TroubleTicket
ON Asset.Id = TroubleTicket.AssetId
RIGHT JOIN Item
ON (Asset.Person = Item.Person and Asset.CompanyId = Item.CompanyId)
WHERE
(TroubleTicket.UT02 IS NOT NULL AND TroubleTicket.UT03 IS NOT NULL)
AND Item.Model NOT LIKE 'V1%'
It's unusual for folks to use right joins - are you sure this is correct?
You should see some of my code. Combination of left, right, inner, outer, and full joins as well as "skipped ON" statements where multiple ON statements are listed together. One of these days, I'll have to write about it if I can actually come up with an example that something other than my right brain will actually understand. π
--Jeff Moden
Change is inevitable... Change for the better is not.
February 15, 2012 at 1:51 am
Jeff Moden (2/13/2012)
ChrisM@home (1/18/2012)
jennigirl (1/18/2012)
I was able to get the query results I need, but I am looking to now present the results as the total count of trouble tickets assigned to each person. I can get a total count of rows returned, but I am trying to get a count of tickets assigned to each person. I don't work a lot with aggregate functions so this one is stumping me.SELECT
DISTINCT TroubleTicket.TicketNumber,
Asset.Person
FROM
Asset
RIGHT JOIN TroubleTicket
ON Asset.Id = TroubleTicket.AssetId
RIGHT JOIN Item
ON (Asset.Person = Item.Person and Asset.CompanyId = Item.CompanyId)
WHERE
(TroubleTicket.UT02 IS NOT NULL AND TroubleTicket.UT03 IS NOT NULL)
AND Item.Model NOT LIKE 'V1%'
It's unusual for folks to use right joins - are you sure this is correct?
You should see some of my code. Combination of left, right, inner, outer, and full joins as well as "skipped ON" statements where multiple ON statements are listed together. One of these days, I'll have to write about it if I can actually come up with an example that something other than my right brain will actually understand. π
Heh only you, Jeff! If you can do this all in one query and it works, it would be a real showstopper π
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
February 15, 2012 at 12:29 pm
jennigirl (1/18/2012)
I can get a total count of rows returned, but I am trying to get a count of tickets assigned to each person. I don't work a lot with aggregate functions so this one is stumping me.
Consider using COUNT(*) OVER (PARTITION BY ________) which does NOT require the use of GROUP BY. π
--Jeff Moden
Change is inevitable... Change for the better is not.
February 16, 2012 at 3:28 am
Jeff Moden (2/15/2012)
jennigirl (1/18/2012)
I can get a total count of rows returned, but I am trying to get a count of tickets assigned to each person. I don't work a lot with aggregate functions so this one is stumping me.Consider using COUNT(*) OVER (PARTITION BY ________) which does NOT require the use of GROUP BY. π
In my tests, I've found this to often be slower than a subquery with a group by joining on primary key back to the table. Not all the time, just often. So make sure you test on your actual data π
February 16, 2012 at 6:49 am
Cadavre (2/16/2012)
Jeff Moden (2/15/2012)
jennigirl (1/18/2012)
I can get a total count of rows returned, but I am trying to get a count of tickets assigned to each person. I don't work a lot with aggregate functions so this one is stumping me.Consider using COUNT(*) OVER (PARTITION BY ________) which does NOT require the use of GROUP BY. π
In my tests, I've found this to often be slower than a subquery with a group by joining on primary key back to the table. Not all the time, just often. So make sure you test on your actual data π
Actually, true enough. The sort behind the scenes can really take some time. Thanks for the reminder.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply