Splitting one column from same table into multiple columns in MS-Access - ms-access-2010

For example I have a DB which has following entries:
Item_Name |TransactionType |Qty
A |Purchase |50
A |Sale | 1
A |Sale | 2
B |Purchase |25
B | Sale | 1
B |Sale | 1
Above table is named as Entries, Now I want to write a query in MS-Access which will give following output?
Item_Name |Purchased_QTY |Sale_QTY
A |50 | 3
B |25 | 2
I want to check for each item what is the sales and purchase
Can someone help here

You can use crosstab query. SQL text of it below. Also this can be done using IIF() functions, but crosstab is faster in design and execution.
TRANSFORM Sum([DataTable].Qty) AS SumOfQty
SELECT [DataTable].Item_Name
FROM DataTable
GROUP BY [DataTable].Item_Name
PIVOT [DataTable].TransactionType;

Related

How to scan DynamoDB table for retrieving only one item in each partition key

Let say I have a table with partition key "ID" and range key "Time" with the following items:
ID | Time | Data
------------------
A | 1 | abc
A | 2 | def
B | 2 | ghi
B | 3 | jkl
And I want to scan only one item in each partition that has the highest time value in each partition. So the outcome of the scan should look like:
ID | Time | Data
------------------
A | 2 | def
B | 3 | jkl
Is this possible with the DynamoDB's scan feature?
(I want to avoid scan all and do such filtering by myself).
If you want to fetch just a few IDs along with their highest Time, you can query with reverse index, so for every ID, you will have only 1 item read. But for this you need an existing list of IDs.
So for each ID, there will be:
1 query
1 item read
Otherwise, the only way is to scan everything unfortunately.

How to rank rows in a table in sqlite?

How can I create a column that has ranked the information of the table based on two or three keys?
For example, in this table the rank variable is based on Department and Name:
Dep | Name | Rank
----+------+------
1 | Jeff | 1
1 | Jeff | 2
1 | Paul | 1
2 | Nick | 1
2 | Nick | 2
I have found this solution but it's in SQL and I don't think it applies to my case as all information is in one table and the responses seem to SELECT and JOIN combine information from different tables.
Thank you in advance
You can count how many rows come before the current row in the current group:
UPDATE MyTable
SET Rank = (SELECT COUNT(*)
FROM MyTable AS T2
WHERE T2.Dep = MyTable.Dep
AND T2.Name = MyTable.Name
AND T2.rowid <= MyTable.rowid);
(The rowid column is used to differentiate between otherwise identical rows. Use the primary key, if you have one.)

SQLite select query to get one row from multiple rows have same id

I'm trying to get one row from table that have an id column that duplicate in several rows of a SQlite database, but I am getting all rows in table.
This is my table creation and selection query.
CREATE TABLE IMAGES ( MID INTEGER , IMAGE BLOB, ID INTEGER );
SELECT DISTINCT ID, IMAGE FROM IMAGES;
This what I get
mid | image | id
1 | m1 | 1
2 | m2 | 1
3 | m3 | 1
4 | m4 | 2
5 | m5 | 3
6 | m6 | 3
And this what i want
mid | image | id
1 | m1 | 1
4 | m4 | 2
5 | m5 | 3
DISTINCT applies to all the SELECTed columns, i.e., you get each unique combination of id and image in the data.
To control which columns get deduplicated, use GROUP BY.
You also have to specify which image you want for each ID; if you don't use MIN(), you get a random image:
SELECT id, MIN(image)
FROM images
GROUP BY id;
You need to choose the correct columns
SELECT min(mid) FROM images GROUP BY id;
Then you need to use that to select the correct rows.
SELECT DISTINCT ID, IMAGE FROM IMAGES WHERE mid in (SELECT min(mid) FROM images GROUP BY id);
Otherwise the records from the GROUP BY, could be made up from any (or last) of the rows.

Join a query result set with an existing table in sqlite?

I have a query result set like this:
value | id
500 | 1
400 | 1
300 | 2
and a product table:
product_name | id
product_1 | 1
product_2 | 2
product_5 | 3
product_6 | 4
product_9 | 5
product_0 | 6
Now I want to find the product_name from product table for every row in query result set and join the result to look like following:
The result after joining should be following:
product_name | id | value
product_1 | 1 | 500
product_1 | 1 | 400
product_2 | 2 | 300
I am getting confused how to use left join to achieve the result.
You'd join based on the value that's common between the two tables; in this case, it's id.
It doesn't seem like you want any NULL values for value, so you wouldn't use a LEFT JOIN; you'd want an INNER JOIN instead.
This would get you pretty much what you want; bear in mind that this is untested.
SELECT p.product_name, qr.id, value
FROM product p
INNER JOIN query_result_set qr ON qr.id = p.id
ORDER BY value DESC;
Also, your expectations are wrong - product_5 has an id of 3, so it wouldn't appear in this join result.

selecting a row based on a number of column values in SQLite

I have a table with this structure:
id | IDs | Name | Type
1 | 10 | A | 1
2 | 11 | B | 1
3 | 12 | C | 2
4 | 13 | D | 3
except id nothing else is a FOREIGN or PRIMARY KEY. I want to select a row based on it's column values that are not PRIMARY KEY. I have tried the following syntax but it yields no results.
SELECT * FROM MyTable WHERE Name = 'A', Type = 1;
what am I doing wrong? What is exactly returned by a SELECT statement? I'm totally new to Data Base and I'm currently experimenting and trying to learn it. so far my search has not yield any results regarding this case.
Use and to add multiple conditions to your query
SELECT *
FROM MyTable
WHERE Name = 'A'
AND Type = 1;

Resources