Getting output of duplicates and not the same as another column - sqlite

Example database:
column1 | column2
--------+---------
1 | 1
2 | 1
3 | 3
4 | 3
5 | 5
6 | 7
7 | 3
I would like to get an output of
2 | 1
4 | 3
7 | 3
This is the query I use:
SELECT
column2, COUNT(*) c
FROM
table
GROUP BY
column2
HAVING
c > 1
which returns:
1 | 2
3 | 3
I would like to take it a step further:
SELECT *
FROM table
WHERE (column1 != column2 AND having count(*) > 1)

The first SQL
SELECT column2, COUNT(*) c
FROM table
GROUP BY column2
HAVING c > 1
returns a nice list of the column2 values you want. You will need a self-JOIN and get the column1's that go with. That will require using table aliases. You could try it this way
SELECT T1.column1, T1.column2
from table T1
JOIN (SELECT column2, count(*) c
FROM table
GROUP BY column2
HAVING c > 1) T2 ON T2.column2 = T1.column2
WHERE T1.column1 <> T2.column2
[Author's note] This is not the answer as originally written. The stack help re: editing says:
Edits are expected to be substantial and to leave the post better than
you found it. Common reasons for edits include:
To fix grammar and spelling mistakes
To clarify the meaning of the post (without changing that meaning)
To include additional information only found in comments, so all of the information relevant to the post is contained in one place
To correct minor mistakes or add updates as the post ages
To add related resources or hyperlinks
The edit does not meet any of the criteria. However, on the same page it says:
If you are not comfortable with the idea of your contributions being
collaboratively edited by other trusted users, this may not be the
site for you.
So be it.

Related

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: Number rows of query output

I am learning SQLite and I am currently posing the question whether there is a simple way of adding a sequential numbering to the output of a query. Underneath, I provide an example of what I am trying to achieve.
For instance, I have the following query:
SELECT
splTicker AS 'Ticker',
count(splTicker) AS '# of Splits'
FROM Splits
GROUP BY splTicker
ORDER BY count(splTicker) DESC, splTicker ASC;
The output of this query is as follows:
bash-3.2$ sqlite3 myShares < Queries/Split.sql
Ticker # of Splits
---------- -----------
AI.PA 7
ASML.AS 3
BN.PA 3
ALTR.LS 2
BOKA.AS 2
DG.PA 2
...
SON.LS 1
SU.PA 1
SW.PA 1
TEC.PA 1
UMI.BR 1
VIV.PA 1
VPK.AS 1
I am trying to add a sequential number to the rows to obtain the following output:
# Ticker # of Splits
-- ---------- -----------
1 AI.PA 7
2 ASML.AS 3
3 BN.PA 3
4 ALTR.LS 2
5 BOKA.AS 2
6 DG.PA 2
...
Currently, I use a workaround and add the row numbers post-query in Perl. I am posing the question whether I could do this directly in SQLite. The idea seems simple, but I have not found a solution yet. Any help would be appreciated.
Best regards,
GAM
Try this:
SELECT
(SELECT COUNT(*)
FROM Splits AS s2
WHERE s2.splTicker <= s1.splTicker) AS '#',
splTicker AS 'Ticker',
count(splTicker) AS '# of Splits'
FROM Splits s1
GROUP BY s1.splTicker
ORDER BY count(s1.splTicker) DESC, s1.splTicker ASC;

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

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;

UPDATE multiple rows using SELECT

I have A table and two rows with id=1 and id=2 and their x parameter is 1. I also have B table and two rows with same id 1 and 2. I am trying to update all of the data(column)on B table which has same id with A table whose x parameter is 1.
A table
id | x |
1 | 1 |
2 | 1 |
B table
id | Y |
1 | yes|
2 | yes|
My query is
UPDATE B SET y='No' WHERE B.id=(SELECT A.id FROM A WHERE A.x=1);
The problem is select returns mutliple data and i can only update the first data.
I tried to use JOIN but sqlite gives syntax error near INNER i couldn't find the problem.
UPDATE B SET B.y='No' INNER JOIN A ON B.id=A.id WHERE A.x=1;
Use this:
UPDATE ... WHERE B.id IN (SELECT A.id ...);

Filtering in Oracle based on a group of values contained in a list of values

I have following two tables:
ID_PERSON NAME
-----------------
1 John
2 Joe
3 Peter
ID_PERSON ID_SPECIALIZATION
------------------------------
1 5
1 6
1 7
2 5
2 1
3 6
3 10
I need to filter data based on group of ids ID_SPECIALIZATION that will be provided. For example
I want to display only those persons who has specialization in 5 and 6 so it will return only first person. In ASP.NET Web form there will be two listboxes, left and right button, in first LB there will be all possible specializations and user will choose some of them to second LB as filtering options. I have no idea how to put this filtering condition in sql query. Thanks for help.
You could use the following:
SQL> SELECT p.id_person, p.NAME
2 FROM person p
3 JOIN person_spe s ON p.id_person = s.id_person
4 WHERE id_specialization IN (5, 6)
5 GROUP BY p.id_person, p.NAME
6 HAVING COUNT(*) = 2;
ID_PERSON NAME
---------- -----
1 John
One way to do it:
SELECT
ID_PERSON
, NAME
FROM
Person AS p
WHERE EXISTS
( SELECT *
FROM
PersonSpecialization AS ps
WHERE ps.ID_PERSON = p.ID_PERSON
AND ps.ID_SPECIALIZATION = 5
)
AND EXISTS
( SELECT *
FROM
PersonSpecialization AS ps
WHERE ps.ID_PERSON = p.ID_PERSON
AND ps.ID_SPECIALIZATION = 6
)
SELECT d1.id_person, d1.name FROM tbl_table1 d1
INNER JOIN tbl_table2 d1
ON d1.ID_PERSON=d2.ID_PERSON
WHERE ID_SPECILIZATION = ?
Theres the query but I'm not sure how asp.net works and passing in the value. It might be work looking up bind variables which allows you to use place holders in the sql which oracle then caches the query and just uses the values that you pass in at run tuime using EXECUTE IMMEDIATE.

Resources