I am trying to make a database to manage expenses between people. I want a table where I can save how many euros has spend every person.
I have a table called people which only have two columns, _id and name.
Then I am trying to create a table with _id, total amount and save how many euros has spend everyone. I was thinking in made a column for each person with his _id and put how many euros has spend, but I don't know how to do it.
For example:
Or maybe I could use some columns to store the id and the amount, like this:
(it is the same example)
Thank you in advance!
You propose two separate SQL antipatterns there. One is having columns that are named by another table, and the other is having a blah_1, blah_2, … series of columns. In each case they indicate that you're thinking about this wrong; a database is not a spreadsheet.
You would be better off having a table that records the unique mapping between transaction ID, person ID and how much they spent (omitting the _id for clarity):
txID | personID | spend
-----+----------+-------
1 | 1 | 10
2 | 1 | 5
2 | 2 | 10
3 | 2 | 10
3 | 3 | 10
4 | 1 | 4
You'll want to specify that the combination of txID and personID is unique, but not that either column is unique in itself.
Now that's not to say that you've lost the amount that anyone's spent or other basic aggregate info like that. You can then derive the total amount spent in a transaction using a query like:
SELECT SUM(spend) AS "Total amount" FROM spendTable WHERE txID = 2
However, you can also do things like finding out how much someone has spent in total:
SELECT SUM(spend) FROM spendTable WHERE personID = 1
Such a query is entirely sensible, and would be much more difficult with the second table design you proposed. (The first design would be better, except then you can't actually explicitly link with the PEOPLE table via a foreign key relation, which would make things much trickier as soon as you start doing anything more complex.)
Related
We are experiencing a sudden performance drop with a query structured like this:
table(tablename)
| where MeasurementName in ('ActiveJobId')
and MachineId == machineId
and SourceTimestamp <= from
and isnotnull( Value)
| order by SourceTimestamp desc
| distinct SourceTimestamp, MeasurementName, tostring(Value), SourceTimestampUtc
| take rows
tablename, machineId, from, rows are all query parameters. rows is typically "20". Value column is of type "dynamic"
The table contains 240 Million entries, with about 64,000 matching the WHERE criteria. The goal of the query is to get the last 20 UNIQUE, non-empty entries for a given machine and data point, starting after a specific date.
The query runs smooth in the Staging database system, but started to degrade in performance on the Dev system. Possibly because of increased data amount.
If we remove the distinct clause, or move it behind the TAKE clause, the query completes very fast. (<1s). The data contains about 5-10% duplicate entries.
To our understanding the query should be performed like this:
Prepare a filter for the source table, start at a specific datetime range
Order desc: walk backwards
Walk down the table and stop when you got 20 distinct rows
From the time it sometimes takes it looks almost as if ADX walks down the whole table, performs a distinct, and then only takes the topmost 20 rows.
The problem persists if we swap | order and | distinct around.
The problem disappears if we move | distinct to the end of the query, but then we often receive 1-2 items less than required.
Is there a logical error we make, can this query be rewritten, or are there better options at hand?
The goal of the query is to get the last 20 UNIQUE, non-empty entries for a given machine and data point, starting after a specific date.
This part of the description doesn't match the filter in your query: and SourceTimestamp <= from - did you mean to use >= instead of <= ?
Is there a logical error we make, can this query be rewritten, or are there better options at hand?
If you can't eliminate the duplicates upstream, you can consider setting a materialized view that performs the deduplication, then query the view directly instead of the raw data. Also see Handle duplicate data
I have a table with 2 columns, user id & book id.
userId | bookId |
-----------------------
12 | 3
23 | 4
34 | 2
56 | 1
45 | 4
345 | 1
Is there a way to get only the unique values of bookId? like GROUP BY in sql.
Meaning query and get - [1,2,3,4]
Thanks.
DynamoDB doesn't have "columns" like a SQL table. Instead, it has documents (called items in DynamoDB terminology) which are indexed by a key (either simple or composite). And these items have attributes, but for the purposes of retrieval it's useful to imagine the items as being arbitrary payloads.
As such, there are no aggregate query APIs for DynamoDB tables. So you can't ask Dynamo to compute aggregations over multiple items.
If you need to identify unique items in a table you'll have to scan and perform the aggregation in your application. It's useful to think about how you might need to query the data upfront and use secondary indexes, or precompute aggregations as you update the data in your table.
My SQLite table name is Invoices, having columns Part Number and Manufacturer.
My problem is to query the table in such a manner that it shows only records where part number have at least 2 different unique manufacturers.
I researched the stack over flow and I tried this solution
QString Filter = "PART_NUMBER in (select PART_NUMBER FROM Invoices GROUP BY "
"PART_NUMBER HAVING count(PART_NUMBER)>1)";
model->setFilter(Filter);
model->select();
But this solution's problem is it shows part number having same manufacturer also.
Edit:
In this example it should return part 2 only
You need to count Manufacturer:
select PART_NUMBER FROM Invoices GROUP BY "
"PART_NUMBER HAVING count(MANUFACTURER)>1
Ok, so you're saying that your data looks like this:
PART_NUMBER | MANUFACTURER
1 | A
2 | A
2 | A (duplicate entry)
3 | A
3 | B
4 | A
4 | B
Then you'd need to select HAVING COUNT(DISTINCT(MANUFACTURER)).
In sqlite, this looks a bit more complex:
SELECT COUNT(MANUFACTURER) FROM (SELECT DISTINCT MANUFACTURER FROM Table WHERE ...);
See this blog post.
But that's more than QSqlQueryModel can do with setFilter(...).
This problem looks like a database design issue. Do you know about database normalization?
When you've normalized your tables, the problem becomes significantly simplified.
I am a web developer who's working on an Exam Generator project. Now I am stuck at one point.
I have one database with different tables. Four of these tables are somehow similar with their columns. I want to know what is the best practice for such thing.
The similar tables I have are:
Exam (Used to store the exam name and the number of questions
included in the exam).
ID | ExamName | NumberofQuestions
UserExam (Used to store the the exams availble for a user with his
grade in each exam he took).
ID | MemberID | ExamID | Grade
QuestionExam (Used to store Question IDs included in each exam).
ID | ExamID | QuestionID
UserSolution (Used to store the user's answers for each exam he
took).
ID | MemberID | ExamID | QuestionID | UserAnswer
In the beginning, I wanted to merge the "Exam" table with the "QuestionExam" table, but then I asked myself if I merged them how would I have one ID for each exam? So I kept it as it is.
Everything is correct with the tables. IF you want to be more desciriptive some practices say to value like Exam_ID, UserExam_ID, or UserSolution_ID that way you can distinguish between the two in a join. It all depends on personal preference. It is a little more writing but saves you from a headache in the long run.
Here is the table structure:
Id | Description
1 | Test1
2 | Test2
In asp.net forms, I have two fields for inserting values for this table.
As I can't add a new column for memorizing the position of rows, do you have any ideas for accomplishing this functionality?
Edit:
I don't know whether stored procedure can do this or not.
Well you could just append the order value as a suffix in the description, and when you read out the value you would remove the suffix when displaying it.
If your order is "Test2","Test1","Test3":
Id | Description
1 | Test1-2
2 | Test2-1
3 | Test3-3
Of course this is a horrible hack and you should rather add another column in your db table. But if you are unable to do this, this is the only solution I see.