Count Cases and Group the totals - count

I am trying to stratify the different types of injury severity and get a count for each over a set time time period (I have not added that part but feel free to assist). I cannot get the count function to work.
Select
ER.[EventReportNumber],
ER.[EventDate],
EPI.[ConsumerEventDateWard],
EINJ.[InjuryType],
count (EINJ.[InjurySeverity]) ,
EPI.[Status],
EPI.[DMHID],
ER.[ProgramCategory],
EPI.[IsInjury],
ER.[ResponsibleOrgFacilityCode]
From [Data_Central].[EMTEvent].[Report] AS ER with (nolock)
Inner join [Data_Central].[EMTEvent].[PersonInvolved] as EPI with (nolock)
On ER.[EventReportKey] = EPI.[EventReportKey]
Inner join [Data_Central].[EMTEvent].[Injury] as EINJ with (nolock)
On EINJ.[PersonInvolvedKey] = EPI.[PersonInvolvedKey]
Where ER.[ReportingOrgFacilityCode] in ('001','005','026','021','009','002','009')
--EINJ.[InjurySeverity]
--Group by EINJ.[InjurySeverity]

Related

UNION operator does not return any results in SQLite

I am working in SQLite and trying to make a query to return a table containing top five jobs in each of four regions (coastal, alpine, regional and metro) based on their respective incomes.
Each SELECT statement for a specific region returns correct result however, appending these separate results in one table using UNION does not return any table where UNION is underlined in red:
--
select job,
-- income as "Top_incomes",
1 as "income",
region.name
from customers
JOIN customer_region on customers.id = customer_region.customer_id
JOIN region on region.id = customer_region.region_id
where region.name = "regional"
ORDER by income DESC limit 5;
UNION
select job,
-- income as "Top_incomes",
2 as "income",
region.name
from customers
JOIN customer_region on customers.id = customer_region.customer_id
JOIN region on region.id = customer_region.region_id
where region.name = "coastal"
ORDER by income DESC limit 5
Although the number of columns per each statement is 3 and they all have the same column names for each statement, I am not sure why UNION does not return any result as a single table. Is there any wrong query syntax here? Thanks
--
select job,
-- income as "Top_incomes",
1 as "income",
region.name
from customers
JOIN customer_region on customers.id = customer_region.customer_id
JOIN region on region.id = customer_region.region_id
where region.name = "regional"
ORDER by income DESC limit 5;
UNION
select job,
-- income as "Top_incomes",
2 as "income",
region.name
from customers
JOIN customer_region on customers.id = customer_region.customer_id
JOIN region on region.id = customer_region.region_id
where region.name = "coastal"
ORDER by income DESC limit 5
I was expecting to get a single table containing results from each SELECT statement: 5 top-income jobs from the 'regional' region followed by the 5 top-income jobs from the 'coastal' region. I did not get any results back.
In a union query, the outermost ORDER BY clause applies to the entire query, not any individual select. You may use the following syntax:
SELECT *
FROM
(
SELECT job,
income AS Top_incomes,
1 AS income,
r.name
FROM customers c
INNER JOIN customer_region rc ON c.id = cr.customer_id
INNER JOIN region r ON r.id = cr.region_id
WHERE r.name = 'regional'
ORDER BY income DESC
LIMIT 5
)
UNION
SELECT *
FROM
(
SELECT job,
income,
2,
r.name
FROM customers c
INNER JOIN customer_region cr ON c.id = cr.customer_id
INNER JOIN region r ON r.id = cr.region_id
WHERE r.name = 'coastal'
ORDER BY income DESC
LIMIT 5
);

sqlite query to get instances were no items returned on join

I am querying the static backend db for a game I play (trying to keep up on my coding), and I am having an issue getting the full results that I want.
So the query that I have so far is:
select ms.security, mc.constellationName, mr.regionName, ms.solarSystemName, count(it.typename) as NumberOfBelts
from mapSolarSystems as ms
join mapConstellations as mc on ms.constellationID == mc.constellationID
join mapRegions as mr on ms.regionID == mr.regionID
join invItems as ii on ii.locationID = ms.solarSystemID
join invTypes as it on it.typeID == ii.typeID
where it.groupID = 9
group by solarSystemName
the problem comes when there are no rows where it.groupID == 9. What I need is for the count to return 0 and I can't for the life of me figure out how to get this to work.
I tried doing left outer join on the final join statement, but no joy.
Change the last join to a left join and set the condition in the on clause istead of the where clause:
select ms.security, mc.constellationName, mr.regionName, ms.solarSystemName, count(it.typename) as NumberOfBelts
from mapSolarSystems as ms
join mapConstellations as mc on ms.constellationID = mc.constellationID
join mapRegions as mr on ms.regionID = mr.regionID
join invItems as ii on ii.locationID = ms.solarSystemID
left join invTypes as it on it.typeID = ii.typeID and it.groupID = 9
group by solarSystemName

Why would oracle subquery with AND & OR return returning wrogn results set

I have two subqueries. as shown below. the first query works fine but the second query which is basically the first query that I modified to use AND & OR, doesn't work in the sense that it doesn't return ID as expected. any suggestions on what is happening here?
1. (SELECT * FROM (SELECT EMPID FROM EVENT_F
INNER JOIN WCINFORMATION_D
ON EVENT_F.JOB_INFO_ROW_WID= WCINFORMATION_D.ROW_WID
INNER JOIN WCANDIDATE_D ON WCCANDIDATE_D.ROW_WID = VENT_F.CANDIDATE_ROW_WID
WHERE STEP_NAME = 'Offer'
AND WCINFORMATION_D.JOB_FAMILY_NAME IN ('MDP','ELP','Emerging Leader Program','Other')
AND TITLE NOT IN ('Student Ambassador Program for Eligible Summer Interns','Student Ambassador')
AND PI_CANDIDATE_NUM = OUTERAPP.PI_CANDIDATE_NUM
--limit 1
ORDER BY CREATION_DT ASC
) T1 WHERE ROWNUM=1) AS A_ID,
2.(SELECT * FROM (SELECT EMPID FROM EVENT_F
INNER JOIN WCINFORMATION_D
ON EVENT_F.JOB_INFO_ROW_WID= WCINFORMATION_D.ROW_WID
INNER JOIN WCANDIDATE_D ON WCCANDIDATE_D.ROW_WID = VENT_F.CANDIDATE_ROW_WID
WHERE STEP_NAME = 'Offer'
AND WCINFORMATION_D.JOB_FAMILY_NAME IN ('MDP','ELP','Emerging Leader Program','Other') or WCINFORMATION_D.JOB_FAMILY_NAME NOT IN ('MDP','ELP','Emerging Leader Program','Other')
AND TITLE NOT IN ('Student Ambassador Program for Eligible Summer Interns','Student Ambassador')
AND PI_CANDIDATE_NUM = OUTERAPP.PI_CANDIDATE_NUM
--limit 1
ORDER BY CREATION_DT ASC
) T1 WHERE ROWNUM=1) AS A_ID,
If you're wanting to get the count of people in one set of job families, plus a count of people in another set, you need to use a conditional count, e.g. something along the lines of:
SELECT COUNT(CASE WHEN wid.job_family_name IN ('MDP', 'ELP', 'Emerging Leader Program', 'Other') THEN 1 END) job_family_grp1,
COUNT(CASE WHEN wid.job_family_name IS NULL OR wid.job_family_name NOT IN ('MDP', 'ELP', 'Emerging Leader Program', 'Other') THEN 1 END) job_family_grp2
FROM event_f ef
INNER JOIN wcinformation_d wid
ON ef.job_info_row_wid = wid.row_wid
INNER JOIN wcandidate_d wcd
ON wcd.row_wid = ef.candidate_row_wid
WHERE step_name = 'Offer' -- alias this column name
AND title NOT IN ('Student Ambassador Program for Eligible Summer Interns', 'Student Ambassador') -- alias this column name;
You will most likely need to amend this to work for your particular case (it'll have to go as a join into your main query, given there are two columns being selected) since you didn't provide enough information in your question to give us the wider context.

Inner join + Count + Where in Psequel

New learner here.
I'm working with two tables in : one with publisher data and one with book data.
I'm interested in titles published by a specific publisher in a specific country (ex. Random House, US) within an INNER JOIN with WHERE clause. So far so good:
SELECT
book_infotable.tblbook_title,
tblpublisher_publisher,
tblpublisher_country
FROM book_infotable
INNER JOIN pub_infotable
ON book_infotable.pubid = pub_infotable.pubid
WHERE tblpublisher_publisher = 'Oxford University Press' AND
tblpublisher_country = 'GB';
I'd like to add COUNT to this to count the number of titles published by a specific publisher's office.
goal table
But, when I try the following with COUNT,
SELECT COUNT(tblbook_title)
AS title_count, pub_infotable.tblpublisher_publisher, tblpublisher_country
FROM book_infotable
INNER JOIN pub_infotable
ON book_infotable.pubid = pub_infotable.pubid
WHERE tblpublisher_publisher = 'Knopf' AND tblpublisher_country = 'GB'
GROUP BY pub_infotable.tblpublisher_publisher, tblpublisher_country,
book_infotable.tblbook_title;
I wind up with
table with persistent "1s" in count column
Any help is much appreciated!
Thank you!
Try executing this query
SELECT COUNT(1) AS title_count, tblpublisher_publisher, tblpublisher_country
from (
SELECT pub_infotable.tblpublisher_publisher AS tblpublisher_publisher,
tblpublisher_country
FROM book_infotable
INNER JOIN pub_infotable
ON book_infotable.pubid = pub_infotable.pubid
WHERE tblpublisher_publisher = 'Oxford University Press' AND
tblpublisher_country = 'GB'
GROUP BY book_infotable.tblpublisher_publisher, tblpublisher_country,
book_infotable.tblbook_title ) temp
GROUP BY tblpublisher_publisher, tblpublisher_country;
Sorted it! I wound up using
SELECT pub_infotable.tblpublisher_publisher,
COUNT(book_infotable.tblbook_title) AS title_count
FROM book_infotable
INNER JOIN pub_infotable
ON book_infotable.pubid = pub_infotable.pubid
WHERE tblpublisher_publisher = 'Oxford University Press' AND tblpublisher_country = 'GB'
GROUP BY pub_infotable.tblpublisher_publisher;

Asp.Net multiple join tables issue from Sqldatasoruce Query Builder into one gridview

In Visual Studio 2010 with ASP.NET 4, I am trying to join several tables together to put the results in a gridview and details view with sqldatasource. In the sqldatasource wizard I have chosen to specify a custom SQL statement or stored procedure and then in the Query Builder to define complex queries such as JOINs, I have tried to generate a SQL statement to join the problem table with speficific columns from other tables. But when I try to test the query I get an error message which says "Cannot call methods on varchar". I am new to sql statements so please can you advise on what is wrong with the statement.
Here is the generated sql statement below
SELECT Problem.ProblemID, Problem.CustomerID, Problem.Summary,
Problem.DateLogged, Problem.DateUpdated, Status.Status, Priority.Priority,
Technician.Name, Technician.Surname, [Skill Group].[Skill Group],
HelpdeskOperator.Name AS Expr1,
HelpdeskOperator.Surname AS Expr2, Problem.NoteID, Problem.ResolutionID
FROM Problem
INNER JOIN Status ON Problem.StatusID = Status.Status.StatusID
INNER JOIN HelpdeskOperator ON
Problem.HelpdeskID = HelpdeskOperator.HelpdeskID AND Status.StatusID = HelpdeskOperator.StatusID
INNER JOIN Priority ON Problem.PriorityID = Priority.PriorityID
INNER JOIN [Skill Group] ON Problem.SkillGroupID = [Skill Group].SkillGroupID
INNER JOIN Technician ON Problem.ProblemID = Technician.ProblemID
AND Status.StatusID = Technician.StatusID AND
Priority.PriorityID = Technician.PriorityID
AND [Skill Group].SkillGroupID = Technician.SkillGroupID
Thank you in advance
Fixed your query:
SELECT p.ProblemID, p.CustomerID, p.Summary, p.DateLogged, p.DateUpdated, s.Status, pr.Priority, t.Name, t.Surname,
sg.* , ho.Name AS Expr1, ho.Surname AS Expr2, p.NoteID, p.ResolutionID
FROM Problem p
INNER JOIN Status s ON p.StatusID = s.StatusID
INNER JOIN HelpdeskOperator ho ON p.HelpdeskID = ho.HelpdeskID AND s.StatusID = ho.StatusID
INNER JOIN Priority pr ON p.PriorityID = pr.PriorityID
INNER JOIN [Skill Group] sg ON p.SkillGroupID = sg.SkillGroupID
INNER JOIN Technician t ON p.ProblemID = t.ProblemID AND s.StatusID = t.StatusID AND pr.PriorityID = t.PriorityID
AND sg.SkillGroupID = t.SkillGroupID
You had duplicate table identifier in your join clause Status.Status.StatusID
I doubt that your Skill Group table contains column [Skill Group] so changed it to return all values from Skill Group
I just think those were the errors, if not I will need more info about your query and table structure.
EDIT:
First it did not return anything for HelpdeskOperator, look at our query:
INNER JOIN HelpdeskOperator ho ON p.HelpdeskID = ho.HelpdeskID AND s.StatusID = ho.StatusID
that meanse that here is no such HelpdeskOperator record that is assigned to our problem AND statusid, so either problem id points to noexisting helpdeskoperator or statusid of
this operator is different that problem status id.
next is Skill Group
INNER JOIN [Skill Group] sg ON p.SkillGroupID = sg.SkillGroupID
again our problem point to no existing skill group
then Technican
INNER JOIN Technician t ON p.ProblemID = t.ProblemID AND s.StatusID = t.StatusID AND pr.PriorityID = t.PriorityID
AND sg.SkillGroupID = t.SkillGroupID
here is more work as more checks, technicas must be assigned to our problem with given status and priorityt and be in skill group, BUT our skill group is null? so to check if
there is technican for our problem remove AND sg.SkillGroupID = t.SkillGroupID so you get
INNER JOIN Technician t ON p.ProblemID = t.ProblemID AND s.StatusID = t.StatusID AND pr.PriorityID = t.PriorityID
and see if now we get any technican.
I hope this points you into right direction. You must be sure that there are matching record in every joining table.

Resources