Combining sqlite data into second table when grouped by name - sqlite

I have a database that is the output for a python script involving a basic game. When the code saves to the database, it saves it to a table called points with the data: name, account_name, time, score. What I want is for the data be saved into a second table when sorted by name, I will then do the same with account_name. Some of the points table:
name |account_name | time | score
oliver |Oliver | 10:29:14-01:04:2017 | 250
oliver |Oliver | 10:29:20-01:04:2017 | 500
dave |Oliver | 10:29:34-01:04:2017 | 250
What I want is for the data to be sorted into a table called name, where the score is totalled for all records with the same name and a column keeps track of how many entries have been merged(In this case, it will be equal to number of games played). For example:
name | totalpoints | totalgames
oliver| 750 | 2
dave | 250 | 1
I will use this format to do the same with account_name. I have found information on how to group and sum the data but not into a second table. Thank you in advance.

first, create your table by:
CREATE TABLE `stats` (
`name` TEXT PRIMARY KEY ON CONFLICT REPLACE,
`totalpoints` INTEGER,
`totalgames` INTEGER
);
then insert into your table with:
INSERT INTO stats
SELECT games.name, SUM(games.score) AS totalpoints, COUNT(*) AS totalgames
FROM games
GROUP BY games.name

Related

DynamoDB Table/Index Modeling + Querying

Basic requirements:
I have a table with a bunch of attributes (20-30), but only 3 are used in querying: User, Category, and Date, and would be structured something like this...
User | Category | Date | ...
1 | Red | 5/15
1 | Green | 5/15
1 | Red | 5/16
1 | Green | 5/16
2 | Red | 5/18
2 | Green | 5/18
I want to be able to query this table in the following 2 ways:
Most recent rows (based on Date) by User. e.g., User=1 returns the 2 rows from 5/16 (3rd and 4th row)
Most recent rows (based on Date) by User and Category. e.g., User=1, Category=Red returns the 5/16 row only (the 3rd row).
Is the best way to model this with a HASH on User, RANGE on Date, and a GSI with HASH on User+Category and RANGE on Date? Is there anything else that might be more efficient? If that's the path of least resistance, I'd still need to know how many rows to return, which would require doing a count against distinct categories or something?
I've decided that it's going to be easier to just change the way I'm storing the documents. I'll move the category and other attributes into a sub-document so I can easily query against User+Date and I'll do any User+Category+Date querying with some client-side code against the User+Date result set.

How to compare the columns of a datatable based on the column name and value?

I have a datatable like below one which is not fixed it can contain n number of columns. I need to compare the column values based on the column name and update the other row value
E.g
dtFinYearValues
dtColumnName | 2017AU | 2017CN | 2018AU | 2018CN | 2019CN | 2020CN
--------------------------------------------------------------------
Value | -1234 | -500 | -300 | 1000 | 1000 | -500
LatestValue | -1234 | -500 | -300 | 500 | 1000 | -500
LatestValue of 2018CN --> Sum of 2017CN Value (-500) and 2018CN Value(1000).
For the above datatable i need to compare the column name and update the value accordingly
Conditions :
1) If the value is -ve update the LatestValue with Same Value.
2) If the value is +ve check whether any -ve value exists for previous fin-year of same country like (In the above datatable 2018CN Value is +ve but 2017CN value is negative so the sum of 2017CN and 2018CN has to be updated for 2018CN-->Latest Value.)
I can't hard-code the column number as there can be different country fin year combination, i need to compare the value of one country with the same country only.
How can i code this in vb.net?
Can you bring back the data from the db another way? If so look at using SQL unpivot (the unpivot could also be done in your code using your original dataset but you would have to manually code this). This will give you 3 columns (name, value, latestvalue). This will be easier to process. Updates can still be done using your original dataset.

Filter on a Query Item

I am creating a report and have a field that has multiple values representing different data values. i.e 4-Completeness 5-accuracy etc... What I need to do is make multiple columns where that field is filtered down to one value. The problem is I get the error if I try and edit the query item in the report of 'Boolen value expression as query item is not supported' How do I fix?
example:
ID column | Data Value = 4 | Actual Data | Data Value = 5
EDIT:
I currently have a case when [Data value] = 4 then [percentage] for the different columns but I am still getting wrong output. I am getting
ID1 | 45% | | |
ID1 | | 35% | |
ID1 | | | 67% |
I need all of ID1 to be in one row.
You can fix this by totaling by ID which will combine all three rows in your example to one:
total([Measure] for [ID])
Change each of the three percentage columns to use this expression, substituting their respective data item for [Measure].
Normally, you don't want to total percentages, but this is an exception. Since only one row has actual data, the total will match that row and the other two null values will not be included in the total.
Simple way would be to do it for each data value in three queries and join them on ID1

SQLite combine values of similar records into one

In my SQLite database I have a table called Tracks which is made up of the following columns: artist, track, genre1, genre2, genre3.
The table contains many values which have the same artist and track values with different genre1, genre2, genre3 values. Like the example below:
artist | track | genre1 | genre2 | genre3
ABBA | Song 1 | Rock | Rock | Rock
U2 | Song 4 | Rock | Rock | Rock
ABBA | Song 1 | Pop | Pop | Pop
U2 | Song 4 | Pop | Pop | Pop
ABBA | Song 1 | 70s | 70s | 70s
U2 | Song 4 | 90s | 90s | 90s
I need to create an SQLite statement that will amalgamate all unique genre values where the artist and track are the same, like the example shown below:
artist | track | genre1 | genre2 | genre3
ABBA | Song 1 | Rock | Pop | 70s
U2 | Song 4 | Pop | Rock | 90s
Any help with this would be hugely appreciated.
Unfortunately it looks like you are suffering from poor database design. In the design above you are limited to only 3 genre per song, and when only one genre is applicable to a song you just repeat the same value across all genre fields. In a good design you should be able to have as many or as little genre entries per song, as well you can also reduce the amount of data required to store genre values.
So lets talk many-to-many relations and mapping tables. A many-to-many relationship in this situation would be many unique genre values belonging to many unique song entries. This should also be true for the reverse, because we will also have many unique songs belonging to many (or possibly none) genres.
So how do we achieve this... Well first lets make a table called Genres. I should have two fields: a integer primary key, and a string for the genre value, with a unique constraint on the latter. Next we will need a mapping table (for futer reference lets call it SongGenre), a table that just has two foreign key fields, one for referencing the song table and one for referencing the genre table. Each record will say this genre belongs to this song, and we can have multiple entries per song.
once you get your mapping table setup you can achieve what you want to do with something like this
SELECT Artist, Track, Group_Concat(Genre.GenreName)
FROM Song
JOIN SongGenre USING (SongID)
JOIN Genre USING (GenreID)
GROUP BY Artist, Track;

Sqlite: Query same table twice with two different "where"

How can I combine two select queries for the same table, which have two different where in Sqlite?
I have a table with more than 10k rows. Two example rows looks like this:
year | project | finance | flow
1990 | water | 300500 | grant
1999 | energy | 200500 | loan
My attempt does not work:
SELECT sum(finance), (select sum(finance) from table where flow = grant)
FROM table where flow = loan group by year
The result should have all results group by year and list the sum(budget) in a column for grants and a column for loans.
year | grant | loan
1990 | 62662 | 383983
1991 | 28928 | 278272
UPDATE: The first example did not correspond correctly to my use case. I had to change it.
In this case, use CASE:
SELECT year,
SUM(CASE WHEN flow = 'grant' THEN finance ELSE 0 END) AS grant_total,
SUM(CASE WHEN flow = 'loan' THEN finance ELSE 0 END) AS loan_total
FROM your_table
GROUP BY year
The logic is different than what you outlined (it's a single SELECT with a single WHERE clause) but it will effectively pivot the data you need from rows to columns.

Resources