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.
Related
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.
I have a table holding information about contacts made to many different customers in the format
email_address | treatment_group | customer_id | contact_date |
I am trying to add a column that looks at each distinct customer and numbers the contacts they've received from longest ago to most recent. I'm using this code:
explain create table db.responses_with_rank
as
( select a.*,
rank () over (partition by customer_id order by contact_date asc)as xrank
from db.responses_with_rank
)
with data
primary index (email_address, treatment_group)
My query is spooling out. There is a primary index of email_address, treatment_group that leads to a skew factor of 1.1 and a secondary primary index on customer_id. I've collected statistics on both sets of indexes. The table is quite large - around 200M records. Is there something I can try to optimize this query?
There is not enough information to determine the cause of the error.
For start, please add the following to your question:
TD version (select * from dbc.dbcinfo)
Execution plan
The statistics collection commands you have used
customer_id top frequencies (select top 10 customer_id,count(*) from db.responses_with_rank group by 1 order by 2 desc)
Do you have wide text columns in your table?
P.s.
I strongly recommend to use create multiset table and not create table.
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.)
Alright, so we have a phonegap app with a table setup like
tblTest (actualid INTEGER PRIMARY KEY, id INTEGER, name TEXT)
The actualid is a unique id for the device and the id is maintained in a server side database. We've had issues with webservices returning duplicate records and while we're fixing that I wanted to add something to our sql that would prevent duplicates from being added (fixing bad data is a pain).
Our current insert statement is setup like
INSERT INTO tblTest (id, name) VALUES (101, 'Bob')
If you run that twice the database will end up looking like
actualid | id| name
1 | 101| Bob
2 | 101| Bob
And what I'd like for it to look like is
actualid | id| name
1 | 101| Bob
Insert or Replace would give me an actualid 2 in the example and any example I've found using a where clause was setup like
INSERT INTO tblTest SELECT ..... WHERE.....
Which doesn't work because none of the data is in a table yet (unless I'm making a newbie mistake, I'm not very good at sqlite or general sql).
Use INSERT OR IGNORE:
INSERT OR IGNORE INTO tblTest (id, name) VALUES (101, 'Bob')
(This requires a unique index on the id column, which you already have.)
You might want to try this:
INSERT INTO tblTest
(id, name)
SELECT 101 as id, 'Bob' as name
FROM tblTest
WHERE NOT EXISTS(SELECT * FROM tblTest WHERE id = 101 and name = 'Bob')
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.