Cant navigate into a specific column sqlite - sqlite

The first link is the problems.
I am very inexperienced in SQLite and needed some help.
Thanks in advance!
https://imgur.com/v7BdVe3
These are all the tables displayed open
https://imgur.com/a/VMOxAuc
https://imgur.com/a/LrDcCBQ
The schema
https://imgur.com/a/bv5KTHN
This is as far as I could get which is close but I couldn't figure out how to sort it also by marina 1.
SELECT BOAT_NAME, OWNER.OWNER_NUM, LAST_NAME, FIRST_NAME from OWNER inner join MARINA_SLIP on OWNER.OWNER_NUM = MARINA_SLIP.OWNER_NUM;
If you know anything else bout the other questions feel free to help me with those too, Thanks!

I believe that you want
SELECT BOAT_NAME, OWNER.OWNER_NUM, LAST_NAME, FIRST_NAME
FROM OWNER INNER JOIN MARINA_SLIP ON OWNER.OWNER_NUM = MARINA_SLIP.OWNER_NUM
WHERE MARINA_NUM = 1
ORDER BY BOAT_NAME;
The second question involves multiple joins.
The third question asks you to use the count(*) function, noting that this is an aggregate function and will result in the number of rows for the GROUP as per the GROUP BY clause (if no GROUP BY clause then there is just the one GROUP i.e. all resultant rows).
The fourth question progresses a little further asking you to extend the GROUP BY clause with the HAVING clause (see link above for GROUP BY).

Related

Teradata SQL selecting successive batch of rows

I have 300000 entries in my db and am trying to access entry 50000-100000 (to 50000 total).
My query is as follows:
query = 'SELECT TOP 50000* FROM database ORDER BY col_name QUALIFY ROW_NUMBER() BETWEEN 50000 and 100000'
I only found the BETWEEN KEYWORD in one source however and am suspecting I am not using it correctly since it says it can't be used on a non-ordered database. I assume the QUALIFY then gets evaluated before the ORDER BY.
So I tried something along the lines of
query_second_try = 'SELECT TOP 50000* FROM database QUALIFY ROW_NUMBER() OVER (ORDER BY col_name)'
to see if this fixes the problem (without taking into account the specific rows I want to select). This is also not the case.
I have tried using qualify with rank, but this doesn't seem to be exactly what I need either, I think the BETWEEN statement would be a better fit.
Can someone push me in the right direction here?
I am essentially trying to do the equivalent of 'ORDER BY col_name OFFSET BY 50000' in teradata.
Any help would be appreciated.
Few problems here.
row_number requires an order by. And it needs to be granular enough to ensure it's deterministic. You can also play around with rank, dense_rank, and row_number, depending on what you want to do with ties.
You're also mixing top N and qualify.
Try this:
select
*
from
<table>
qualify row_number() over (order by <column(s)>) between X and Y

How to stop a row from show on Null count in query Access 2010

Here is the problem. I have a table and the table is 100% completed. There are no null values in the table. The table is broken down to:
Division > Region > RAPM > Status > Disposition
I want to count how many times "Training" is in [Disposition] for a [Region] using a query.
The error I get is when a [Region] has 0 "Training" in [Disposition] the count is a coming back as Null so the entire row is not shown.
How do i get the count to come back as "0" so I can keep the [Division], [Region], & [RAPM] in the results for reporting even if there is 0 count for training.
I have tried NZ() but this will not work because there is technically no Null cell to be converted.
Here is the statement:
SELECT tblAlignment.Division, tblAlignment.Region,tblAlignment.RAPM, Count(tblCase.Dispostion) AS CountofTraining
FROM tblCase INNER JOIN tblAlignment ON (tblCase.Region = tblAlignment.Region) AND (tblCase.Store = tblAlignment.[Store Number])
Where (((tblCase.Status)="Closed") AND ((tblCase.Disposition)="Training")
Group BY tblAlignment.Division, tblAlignment.Region, tblAlignment.RAPM
HAVING (((tblAlignment.Division)=[Forms]![frmDashboardNative]![NavigationSubform].[Form]![NavigationSubform].[Form]![Combo16]))
This is not the complete answer, but my reputation is not high enough to comment so this is the only way I can respond. To get a fuller answer you would need to provide more detailed table structure including primary keys and relationships between the tables. Making guesses from what you have provided I can make a couple of suggestions, but your post raises a few questions:
You say you want to count related entries based on same Region, but your join links on Region and Site. Is there a relationship between Site and Region? Can a single site only ever appear in one Region? If so then I think this information should possibly be in a separate table.
I think the HAVING condition should actually be in the WHERE clause.
I think you may have duplicated the ![NavigationSubform].[Form]
Anyway, a slightly more generic example of one way of achieving what you're after would be:
SELECT a.Region, Nz(b.RecordCount, 0) AS FinalCount
FROM TableA a
LEFT JOIN (SELECT Region, Count(*) AS RecordCount
FROM TableB
WHERE Status = "Closed" AND Disposition = "Training"
GROUP BY Region) AS b ON a.Region = b.Region
WHERE a.Division = [Combo16]
Hope this is of some help.

Sqlite Join confusion

My first ever Stackoverflow question so go easy on me, I am not very experienced with SQLite.
I have a table of football teams
CREATE TABLE IF NOT EXISTS teams (
teamId INTEGER PRIMARY KEY NOT NULL,
name TEXT,
);
and a table of matches
CREATE TABLE IF NOT EXISTS matches (
matchId INTEGER PRIMARY KEY NOT NULL,
homeTeamId INTEGER,
awayTeamId INTEGER,
);
I am trying to work out the SELECT statement that would display the list of matches but would replace both the homeTeamId and the awayTeamId numbers with the team names.
I have tried several variants but because two fields in the matches table join back to the same teams table I am getting either syntax errors or ambiguous column errors.
Sorry for a fairly basic question, any help appreciated.
Geoff
Try this:
select m.matchId, h.name, a.name
from matches m
inner join teams h on h.teamId = m.homeTeamId
inner join teams a on a.teamId = m.awayTeamId
You have not posted the variants you have tried, so I just assume that you the problem was caused by not using aliases (in my example h and a) for the table teams. You have to use at least one alias, if you need to join one table twice.
Thanks for the speedy reply. In the typical way I stumbled on the solution about 15 minutes after I posted my question, my solution was identical to yours except that I didn't bother aliasing the matches table.
Your answer had a little typo in it I think
SELECT m.matchId, h.name, a.name
FROM matches m
INNER JOIN teams h ON h.teamId = m.homeTeamId
INNER JOIN teams a ON a.teamId = a.awayTeamId
-- ^ should be m.awayTeamId
But thanks again for your help!
Geoff

select * from (select...) sqlite python

I'm working on a sqlite database and try to make a special request between two tables.
In the first table (table1 for example), i have two columns named "reference" and "ID". I want to search an ID in it, get it value in "reference" and display all informations from the table which have this value as name.
I try to find something on the internet but I didn't find an answer.
This is the request I made:
select * from (select Reference from table1 where Name='Value1')
It only give me the result of
select Reference from table1 where Name='Value1'
EDIT:
I want
select Reference from table1 where Name='Value1' => name of table
select * from name of table => show all elements
I'm new in sqlite but I hope you can help me.
Thank you by advance
Matt
If I understand your question correctly, I don't think there's a way to do it in sql completely (or at least not in a portable way). I'd recommend one of 3 solutions:
Do exactly what you want, but do some processing in Python. That means query your master table, then construct new query based on each of the rows returned.
If you have many tables, possibly changing dynamically - it may be a good idea to rethink your database design. Maybe you can move some of the changing table names into a new column and put your data in one table?
If you have only a few tables available as the Reference and they never change, you could join all the possible tables, like:
SELECT ... FROM table1
LEFT JOIN table2
ON table1.id = table2.id AND table1.Reference = "table2"
LEFT JOIN table3 ...
But you may need to explain it all a bit better...

How can I count how many duplicates there are for each distinct value in sqlite?

I have a table:
ref,type
1,red
2,red
3,green
4,blue
5,black
6,black
I want the result of a sqlite query to be:
red,2
green,1
blue,1
black,2
I think the hardest thing to do is find a question to match my problem? Then I am sure the answer is around the corner....
:)
My quick google with the terms "count unique values sqlite3" landed me on this post. However, I was trying to count the overall number of unique values, instead of how many duplicates there are for each category.
From Chris's result table above, I just want to know how many unique colors there are. The correct answer here would be four [4].
This can be done using
select count(DISTINCT type) from table;
A quick google gave me this: http://www.mail-archive.com/sqlite-users#sqlite.org/msg38339.html
select type, count(type) from table group by type;

Resources