last day (15 days later) » 

2:09 PM
0
Q: MS Access SQL subquery

PeanutsMonkeyI need to be able to return a treeview like structure using SQL via MS Access. I am however unsure how i can perform an inner join subquery. My code thus far is SELECT o.name,o.pdata1,o.pack_ID,p.name,p.pack_ID,p.parent_ID FROM t_obj AS o INNER JOIN t_pack AS p on o.Pack_ID = p.Pack_...

 
What is the purpose of "WHERE o.pdata1 IN (SELECT o2.pdata1 FROM t_obj AS o2)"? That sub is referencing the same table (t_obj) as the parent query, meaning it is not doing anything, because obviously all values of pdata1 in table t_obj are in that table.
Can you provide a small amount of sample data and expected output?
 
@Brian DeMilia - I updated the post with some sample data. The expected output is to display a treelike view or at least a parent-child like relationship even if it is tabular.
@Gordon Linoff - The reason i didn't include desired results as i am unsure of what MS Access is capable of. Would i be able to show the relationships in tabular format or can i have Excel run the query and return the appropriate view?
 
@peanutsmonkey Although it depends on what you mean with respect to 'tree-like' you're probably going to have to create a 'report' in access that uses the sql result as its data source to achieve what you want. I'm just speculating though as I'm not sure what you want.
 
@Brian DeMilia - I mean some sort of hierarchical structure. What would be the best way to represent this in tabular format as i am unsure of the capabilities of Access.
 
@PeanutsMonkey if you add the desired output -- just based on the output you provided above -- we can probably better help. Is the output you're getting above what you want just not formatted in the fashion that you want or is the resulting data itself also not what you want?
 
2:09 PM
@Brian DeMilia - The resulting data to begin with is not what i expect it to be. I'm quite certain the SQL query isn't correct.
 
@PeanutsMonkey it would help significantly to know what output you are expecting. And also the rows from each of these two tables that lead to that result.
 
@Brian DeMilia - Hopefully you don't mind me moving this to a chat as it may be easier to explain
 
sure
is your database small enough to upload or email?
you can strip it down to a small set of sample data
 
If i limit myself to Access, i'm expecting to see tabular results
Unfortunately no. It's almost 500MB is size. The data i posted is only a subset.
 
most likely, you don't have analytic functions in access so it's hard to get tree-like reports, if i understand your notion of tree-like correctly
but you can create reports that use the query results to get tree-like reports
it would be helpful to have the rows of each of the 2 tables that gave you the result you provided
and the output you want
 
2:14 PM
The notion i have is of that of file explorer on Windows which i am aware isn't available in Access. I'm also using a third party tool to access the database as Access isn't installed on our PCs hence am limited to tabular views. I'm hoping i can show it in some tabular format
Not sure if this is possible.
I could use Excel to pull the data though. Would i be able to display a hierarchical view easily?
 
ah, if you were using access you could take the result of a query and put it on a report, and then group and sum on items similar to a pivot table, ex. employees in a department and then a total on each department, without repeating each department's value throughout down the report (what I think you're talking about) but that is done in the user interface, not the sql
it'd be easiest to answer once you actually have the correct data that you want, and i know how you want the data formatted
it depends
 
I know the data isn't correct
 
can you give like 10 rows from each of the 2 tables, the output you're currently getting based on those rows from each, and the output you want
 
The query isn't returning what i expect it to.
 
i know
but to help i need to have some sample data from each of the 2 tables
not your query result
but rows on the tables the query is using
t_obj and t_pack
and given a sample of a few rows from each, what result you want
your where clause is currently not doing anything, btw
you would get the same result even if it weren't there
 
2:20 PM
Can you drop me an email at peanutsmonkey at gmail dot com and i can send you a sample screenshot
 
alright
 
Thanks.
Just came through. Give me a minute and i'll email through screenshots.
 
Emailed them through
 
first is t_obj and second is t_pack?
 
2:28 PM
Yes
 
what output are you expecting?
 
I would like to see the relationship between a record in t_obj and t_pack that shows me the relationship between one another. I know i can't get a treeview however is there an alternative tabular view i can pull into Excel and create a pivot table
 
isn't the 'name' the only thing you would get, via a join between t_obj and t_pack
in that example
 
I saw the example however there is a relationship between pack_id and pdata1
 
in what way
611 doesn't appear as the package_id in any of the example data
does pdata1 sometimes correspond to a package_id?
 
2:38 PM
Yes it does. It corresponds when a package exists
 
why do 2 of the packages on t_obj have a null for pdata1 then?
 
Because they are records that reside under a package
 
how is that recorded in the table?
 
So when a package exists it has data in pdata1 including a package id and parent_ID
The record?
 
did you take a screenshot of all the columns?
 
2:41 PM
Yes
 
you said there is a parent_id column?
 
Sorry. I didn't include the parent_id. I thought i did. I'll email through an updated screenshot.
I just sent it your way
 
k
ok i just loaded it into a fiddle
so package_id between t_obj and t_pack are related
what other relationships are there
 
That's all i know of.
 
the pdata1 value of 611 for instance
that indicates a parent package?
 
2:49 PM
I'll bring up Alisha as a record.
Give me a sec
 
The parent package for the record
So imagine Alisha being a package
It has PDATA1 of 611
A record named 'ABC' would have it pack_ID set to that of 611
as it's a child of Alisha
 
the 611 doesn't appear anywhere as a package_id
should it?
is there another field called pack_id that you didn't include?
 
No. There isn't. I'll move the record into Alisha. Give me a sec
Dropped you an email with a new screenshot
 
okay so i understand the relationship between abc and alisha
but neither the package_id or pdata1 of alisha appear in the t_pack table
 
3:01 PM
I'll bring up Alisha with the update of ABC
Dropped it to you
You'll see the package ID now matches the ID in pdata1
The package id of ABC matches the PDATA1 of Alisha
 
in that screenshot the name is alisha, not the dates that you previously showed in that field of that table (t_pack)
 
Those are different packages akin to Alisha
 
before you showed t_pack as (name, package_id, parent_id)
where the name field appeared to be dates
in YYYYMMDD format
 
Yes
It's the name of a package
The dates in there are simply name values
like Alisha
 
and in the example you gave the name of the pack is 'Alisha'
okay as long as it's the same column
 
3:06 PM
Yes it is
The dates are in the format because they would be a child of another package e.g Alisha
Sent you an email with a new package that is in the format YYYYMMDD
You'll see it now has a parent_id of 611
Because it resides beneath Alisha
 
can you confirm this is the structure of t_pack
 
Yup. That's right
 
is this kind of what you're expecting?
 
Where would ABC appear?
 
there are no values in t_pack where 611 is the package_id
other than alisha
if you give me a screenshot of what you expect i can try to get that result
im still not really sure what you're after
 
3:20 PM
if you look at the screenshot of abc you'll see it has the id of 611
 
that's on t_obj
not t_pack
 
which matches the pdata of alisha
Yes
 
so pdata1 indicates a parent
that parent corresponds with parent_id on t_pack
you're saying it also can correspond with package_id on t_obj?
 
how about -
it's basically 3 queries connected by unions
the first identifies the parents
the second grabs those where a parent corresponds with parent_id on t_pack
the third grabs those where it corresponds instead to package_id on t_obj
ordered by pdata1 (which I'm thinking is the parent, representing the group), and package_id within each group
 
3:25 PM
That looks about right. Hopefully i can match values 611 with a name in a pivot table
So it displays a name and not a number
 
what name do you want it to display?
 
Is there a way to do that?
 
what name should 611 be?
alisha?
 
So for example, visually i would Alisha, abc, 20140720 with nodes
Yes
 
do you mean like in the email i just sent?
or do you actually want 611 to be replaced with Alisha?
 
3:30 PM
611 wouldn't display. It would display Alisha and a node with abc and 20140720
 
ah, ok hang a sec
 
Thanks
 
using that you should be able to do a pivot table and just not repeat the parent_name values going down
 
Would that query work on Access?
 
i don't use access often but it should, only thing that might have to be changed is the on clause, are you able to test it now?
i think access requires enclosing join conditions in ()
 
3:43 PM
I get an error
 
what does it say?
 
Type mismatch in JOIN expression
 
what's the data type of pdata1 on t_obj and parent_id on t_pack?
 
I suspect int however because i am using a third-party tool, i can't confirm. I can only see numbers
 
that error will usually pop up when they are of a different data type
for instance one might be an integer and the other a decimal or something of the like
 
3:47 PM
Running the first query on its own and it fails. I get the same error
 
if you make them the same and they both truly contain just numbers then you should be able to change the data type without actually affecting the data
the first query has a join between parent_id and pdata1
they have to be the same data type
both of those fields
i made both fields integers in the fiddle
they might not actually both be integer fields
one might be a decimal
or character
with numbers stored in the character field
 
Trying a shorter query
 
any way you can see the data types of each field?
 
Unfortunately no. Still getting the error
Is there a way to accommodate data types
 
the fields are likely of different data types
the ideal way would be to change the data types of both columns to be the same, since they should be the same if they are related fields
you can use functions list cast, extract, etc. in other databases to convert from one to another
not sure if there are similar functions in access
but they typically will result in performance degredation because unless the table has function based indexes the indexes won't get used
also even if access does have conversion functions you have to know what data type you're starting from
to know what to convert to to make them each the same
 
3:54 PM
unfortunately i can't change the datatypes as the third-party tool creates the database. I'll see if i can find out the data types. Hopefully you won't mind me emailing you once i find out.
Thanks so much for your help
I owe you big time
 
no problem, let me know
catch you later
 
Thanks
See ya
If you post your answer, i'll accept it
You there?
I tried changing the query
select o.name as parent_name, p.name, p.package_id, p.parent_id
from t_pack p
inner join t_obj o
on p.parent_id = o.pdata1
to
select o.name as parent_name, p.name, p.package_id, p.parent_id
from t_pack p
inner join t_obj o
on p.parent_id = o.package_id
and that works
it seems to fail with o.pdata1
 

last day (15 days later) »