sqlite query to get instances were no items returned on join - sqlite

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

Related

Selecting incorrect records

In following query, what is happening is that, the 3rd join is not being done. we are getting pharmacy match and then the display is showing patients in other facilities who share the same pharmacy, can you see why this would be happening?
Insert Into #tblNDC
SELECT ROW_NUMBER() OVER(ORDER BY ID_KEY DESC) AS RN,*
From
(
Select distinct A.PHARMACYNPI,
f.FACILITY_NAME,
ID_KEY,
[BATCH] AS column1,
[IMPORTDATE],
[DATEBILLED],
[RX],
[DATEDISPENSED],
[DAYSUPPLY],
[PAYTYPE],
A.[NPI],
[PHYSICIAN],
[COST],
[QUANTITY],
[MEDICATION],
A.[NDC],
f.FACILITY_ID
FROM [PBM].[T_CHARGES] A
LEFT OUTER JOIN [OGEN].[NDC_M_FORMULARY] B ON A.[NDC] = B.[NDC]
--Left Outer Join PBM.FACILITY f on A.FACILITYNPI = f.FACILITY_NPI
Left Outer Join PBM.PHARMACY_NPI pn on A.PHARMACYNPI = pn.NPI
Inner join PBM.PHARMACY_FACILITY pp on pn.PHARMACY_ID = pp.PHARMACY_ID
Inner Join PBM.FACILITY f on pp.FACILITY_ID = f.FACILITY_ID
Where [STAT] not in (3, 4, 5)
AND [TIER] <> 'T1'
AND f.FACILITY_ID IN
(
select FacilityID from #tblFacility
)
AND f.FACILITY_ID IN
(
SELECT * FROM [PBM].[Split1] (#selectedFacility)
)
--- it seems 3rd condition not being done ----------------------------------
Its hard to see for sure without knowing the data, but my first thoughts are that the Left Outer joins will be giving you joinage you might not want.
Go through each join and remove it until you start to get dodgy records back, if it is the 3rd one when it suddenly goes strange, then i suspect you have multiple IDs for a facility.

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.

Drupal db_query get all rows

I'm starting to build themes for Drupal.
I have a question about drupal queries, but I didn't find any answer...
I have a db_query in one of my modules, but only return a few rows (13 rows always, and table has 20) How can I select all rows from one table?
My query is like this
$result = db_query("SELECT * FROM bv_calendar c INNER JOIN bv_countries p ON c.country_id = p.country_id INNER JOIN bv_vaccinate v ON c.vaccinate_id = v.vaccinate_id ORDER BY $orderby ASC");
while ($class = $result->fetchAssoc()) {
$classes[$class["calendar_id"]] = $class;
}
How can I get all rows?
Thank you
From the looks of it I'd say you're not getting all the rows from the *bv_calendar* because you're using INNER JOIN on your SQL query which requires to be matching records on your *bv_countries* and *bv_vaccinate* tables.
Try replacing the INNER JOIN for LEFT JOIN as in the later "If there is no matching row for the right table in the ON or USING part in a LEFT JOIN, a row with all columns set to NULL is used for the right table." from http://dev.mysql.com/doc/refman/5.0/en/join.html.
If that's the result you want then your SQL query should be:
SELECT * FROM bv_calendar c LEFT JOIN bv_countries p ON c.country_id = p.country_id LEFT JOIN bv_vaccinate v ON c.vaccinate_id = v.vaccinate_id ORDER BY $orderby ASC

Inner Join & Count, Microsoft SQL

I am trying to do a count inside a nested statement with inner join
select a.app_id, a.first_name, a.last_name, d.svd_id
from wwhs_app a inner join
wwhs_svc d on a.app_id = d.app_id
where a.app_id in(
select top 50 app_id
from wwhs_app
Where app_create_dt > '2012-07-23 00:00:00')
I need a count of svd_id as well, but I keep getting errors every way I try. Suggestions?
You need to count for svd_id but it's not in the query.
Did you mean 'app_id'?
Try this...
SELECT a.app_id, a.first_name, a.last_name, d.svd_id
FROM wwhs_app a
INNER JOIN wwhs_svc d on a.app_id = d.app_id
WHERE a.app_id in (
SELECT TOP 50 app_id, COUNT(*) as id_count
FROM wwhs_app
WHERE app_create_dt > '2012-07-23 00:00:00'
GROUP BY app_id ORDER BY id_count)

Querying on count value having ()

I'm having difficulty with a query which displays records according to their fill rate.
For instance, a vacancy can have no bookings or some bookings. If a vacancy has bookings, they can be in the form of 'active [1]', 'pending [0]'. The query I have written so far works if the vacancy has booking records but I can't get it to work if it doesn't have booking records.
My query (which works) for vacancies with a booking is as follows:-
SELECT v.*, j.job_category_name, bu.business_unit_name
FROM vacancy v
INNER JOIN job_category j ON j.job_category_id = v.job_category_id
INNER JOIN business_unit bu ON bu.business_unit_id = v.business_unit_id
INNER JOIN booking b ON b.vacancy_id = v.vacancy_id
INNER JOIN booking_status bs ON bs.id = b.booking_status_id
WHERE
v.vacancy_status <> 'revoked' AND
v.vacancy_reference <> 'auto-generated booking' AND
v.business_unit_id IN (series of primary keys) AND
(bs.booking_status_type_id = 1 OR bs.booking_status_type_id = 2)
GROUP BY v.vacancy_id
HAVING v.vacancy_limit > count(b.booking_id)
ORDER BY v.vacancy_id DESC
I thought by changing the join of b and bs to LEFT JOIN would have worked, but it hasn't.
Any ideas?
Without a copy of your schema to work from, it's difficult to tell exactly, but when you changed booking and bookingstatus to LEFT JOINs, did you also modify your WHERE clause so that it read something like:
WHERE
v.vacancy_status <> 'revoked' AND
v.vacancy_reference <> 'auto-generated booking' AND
v.business_unit_id IN (series of primary keys) AND
(ISNULL(bs.booking_status_type_id, 1) = 1 OR ISNULL(bs.booking_status_type_id, 2) = 2)
i.e. Ensured that the full WHERE clause would be satisfied, thus not stripping out the records where all the values for columns from booking and bookingstatus were NULL?
Try LEFT OUTER JOIN for the tables for which the joins may return 0 matches.
For eg:- in your case have LEFT OUTER JOIN for b and bs and check

Resources