join issue, what are the best alternatives? - asp.net

Q:
I have 5 or 6 tables , and i need data from these tables :
i make the following join statement to accomplish my query.
SELECT d.batch_no,d.studytype_code,d.camp_code,d.dep_code,d.start_date,d.end_date,a.year,a.term_code,c.studytype_name ,e.dep_name_ar,f.camp_name_ar
FROM llkbatch a , llkbatch_category b , mm19studytype c ,llkbatch_exception d ,llkdepartment e,llkcamp f
WHERE a.batch_no = b.batch_no
AND b.studytype_code = c.studytype_code
AND b.batch_no = d.batch_no
AND b.studytype_code = d.studytype_code
AND d.dep_code = e.dep_code
AND d.camp_code = f.camp_code
but i think that the join sometimes is less performance way to do things like that,is there any alternative to this in a programming way or in the database layer, i mean alternatives to joins in general and when should i go away from the joins.
thanks a lot.

There is a more modern ANSI join syntax that will make your joins more readable, but it should not affect performance:
SELECT d.batch_no,d.studytype_code,d.camp_code,d.dep_code,d.start_date,d.end_date,
a.year,a.term_code,c.studytype_name ,e.dep_name_ar,f.camp_name_ar
FROM ra1batch a
JOIN ra1batch_category b ON a.batch_no = b.batch_no
JOIN ra1studytype c ON b.studytype_code = c.studytype_code
JOIN ra1batch_exception d ON b.batch_no = d.batch_no
AND b.studytype_code = d.studytype_code
JOIN rr1department e ON d.dep_code = e.dep_code
JOIN rr2camp f ON d.camp_code = f.camp_code

The query you have written in your question follows the syntax of Oracle. But you use joins as if you are using SQL Server. It will give you better performance in comparison with your current query.
The below query will parse faster compared to your current query.
SELECT d.batch_no, d.studytype_code, d.camp_code, …
FROM ra1batch a
INNER JOIN ra1batch_category b on a.batch_no = b.batch_no

Related

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

SQLite Troubleshooting a missing table in R

Looking to Inner join the results from one query with another in the SQLite Package in R. So far I have the following code:
library(SQLite)
sql<-
"SELECT
telecast.telecast_id,
telecast.series_name,
affiliates.network_name
FROM telecast a
JOIN affiliates b
ON a.network_id = b.network_id limit 10;"
res <- dbSendQuery(con, sql)
df_ti <- dbFetch(res)
print(df_ti)
When I run the code, it says there is no table found but when I run just
SELECT telecast.telecast_id FROM telecast
The information shows up. I am not too good with troubleshooting in SQLite in R, any thoughts on how to fix this??
You create aliasses for your tables.
If you change your select query to use these, I had it working.
library(SQLite)
sql<-
"SELECT
a.telecast_id,
a.series_name,
b.network_name
FROM telecast a
JOIN affiliates b
ON a.network_id = b.network_id limit 10;"
res <- dbSendQuery(con, sql)
df_ti <- dbFetch(res)
print(df_ti)
I was able to figure it out. Your suggestion of creating aliases for the tables was spot on. I just declared each table an alias using AS. The code I was able to come up with is below, thanks for the help!
SELECT
t.series_name,
ti.num_views_per_telecast
FROM
(SELECT
ti.telecast_id,
ti.network_id,
count(*) as num_views_per_telecast
FROM
tunein AS ti
INNER JOIN
affiliates AS a
ON
ti.network_id = a.network_id
WHERE
ti.dvr_time_shift = 'L+SD' and
a.network_name = 'ABC'
group by
ti.telecast_id,
ti.network_id
)ti
inner join telecast AS t
On
t.telecast_id = ti.telecast_id
ORDER BY ti.num_views_per_telecast DESC

Doctrine DQL - sum of orders by brand and by date in one query

I'm working with Symfony 2.
I need to create a DQL query that would provide me with data to be displayed in the this form:
I need the sum of all orders by brand and by year.
The required tables in the database look like this:
Brand:
VOrder:
So far, I have managed to extract a year, order value and a brand for every orderthe following DQL, but this is nowhere close where I want to be:
$query = $em->createQuery(
"SELECT SUBSTRING(o.date, 1, 4), o.value, b.name
FROM AppBundle:VOrder o
LEFT JOIN AppBundle:Brand b WITH b.id = b
GROUP BY o.date"
);
My question boils down to these:
1. Is it possible to achieve the desired result (see the table above) with only one query?
2. Where do I go from here to get the needed data?
This can be done with a single query, but I think it's best to do this without DQL.
The documentation explains the drawbacks of using DQL for aggregate fields
Have a look at how to run native SQL with doctrine in the docs.
The query itself should do a GROUP BY YEAR(date), and a SUM(value):
SELECT
YEAR(o.date),
SUM(o.value),
b.name
FROM brands b
LEFT JOIN orders o
ON o.brand_id = b.id
GROUP BY YEAR(o.date);
With help from my colleague, I managed to find DQL that does exactly what I was looking for:
$query = $em->createQuery(
"SELECT SUBSTRING(o.date, 1, 4) AS year, SUM(o.value), b.name
FROM AppBundle:Brand b
LEFT JOIN AppBundle:VOrder o WITH o.brand = b.id
GROUP BY year, b.id"
);
There is no YEAR() function on DQL, so the workaround was to use SUBSTRING().
You need next query:
$query = $em->createQuery(
"SELECT YEAR(o.date), sum(o.value), b.name
FROM AppBundle:VOrder o
LEFT JOIN AppBundle:Brand b WITH b.id = o.brand_id
GROUP BY YEAR(o.date), b.name"
);

PL-SQL Lastest record within a group: looking for alternate (PARTITION BY?) approaches

Using PL-SQL, I need to find the record with the lastest INVC_LN_ITEM_STAT_START_DT value within a group of records that share the same value for SHPMNT_LN_ITEM_KEY and RPT_PER_KEY.
How else might this be done? Are there analytical functions for this type of query?
SELECT
m1.*
FROM
HD_INVC_LN_ITEM_STAT m1
LEFT OUTER JOIN HD_INVC_LN_ITEM_STAT m2
ON (
m1.SHPMNT_LN_ITEM_KEY = m2.SHPMNT_LN_ITEM_KEY
AND m1.RPT_PER_KEY = m2.RPT_PER_KEY
AND m1.INVC_LN_ITEM_STAT_START_DT < m2.INVC_LN_ITEM_STAT_START_DT)
WHERE
m2.SHPMNT_LN_ITEM_KEY IS NULL
ORDER BY
m1.SHPMNT_LN_ITEM_KEY
,m1.RPT_PER_KEY
,m1.INVC_LN_ITEM_STAT_CD
,m1.INVC_LN_ITEM_STAT_START_DT
How about this?
SELECT
HD_INVC_LN_ITEM_STAT1.*
FROM
HD_INVC_LN_ITEM_STAT HD_INVC_LN_ITEM_STAT1
INNER JOIN
(
SELECT
HD_INVC_LN_ITEM_STAT2.SHPMNT_LN_ITEM_KEY
,HD_INVC_LN_ITEM_STAT2.RPT_PER_KEY
,MAX(HD_INVC_LN_ITEM_STAT2.INVC_LN_ITEM_STAT_START_DT) AS MAX_INVC_LN_ITEM_STAT_START_DT
FROM
HD_INVC_LN_ITEM_STAT HD_INVC_LN_ITEM_STAT2
GROUP BY
HD_INVC_LN_ITEM_STAT2.SHPMNT_LN_ITEM_KEY
,HD_INVC_LN_ITEM_STAT2.RPT_PER_KEY
) HD_INVC_LN_ITEM_STAT2
ON
HD_INVC_LN_ITEM_STAT1.SHPMNT_LN_ITEM_KEY = HD_INVC_LN_ITEM_STAT2.SHPMNT_LN_ITEM_KEY
AND HD_INVC_LN_ITEM_STAT1.RPT_PER_KEY
= HD_INVC_LN_ITEM_STAT2.RPT_PER_KEY
AND HD_INVC_LN_ITEM_STAT1.INVC_LN_ITEM_STAT_START_DT = HD_INVC_LN_ITEM_STAT2.MAX_INVC_LN_ITEM_STAT_START_DT
ORDER BY
HD_INVC_LN_ITEM_STAT1.SHPMNT_LN_ITEM_KEY
,HD_INVC_LN_ITEM_STAT1.RPT_PER_KEY
,HD_INVC_LN_ITEM_STAT1.INVC_LN_ITEM_STAT_CD
,HD_INVC_LN_ITEM_STAT1.INVC_LN_ITEM_STAT_START_DT;
It's longer but arguably more intuitive. I would like other people's opinions on which is more efficient.

problem with nested inner joins in SQLIte

The sql statement below will not run in SQLite:
select *
from A
left join (B inner join C on B.fkC = C.pk) on A.optionalfkB = B.pk
I get a sqlException "unknown column B.pk"
According to the documentation # http://www.sqlite.org/lang_select.html this should work, and it will work in all other sql implementations. Am I doing something wrong?
It doesn't work because the "outer" query doesn't know what B is.
select *
from A
left join (B inner join C on B.fkC = C.pk) B on A.optionalfkB = B.pk
The (B inner join C on B.fkC = C.pk) is weird without any select, but the specification does say that it is valid.

Resources