sql query first rows with specific column values - sql

i want to start by saying that i know there are a couple of questions regarding similar problems but they either dont answer my question fully or seem to be incompatible with SQLite.
I want to query all rows with value -1 and the first rows with values other than -1.
And by "first rows" i mean the group of rows that are first with a certain value. the first row is the row that is first stumbled upon depending on the SORT BY clause
An example of the data and outcome:
Data:
a b -1
c d 1
e f 2
g h 2
i j 2
k l -1
Result:
a b -1
c d 1
e f 2
k l -1
And as said above, i am using a SQLite database

Do this as two separate queries, one of them containing an inline view; UNION the two
select blah, blah from T where ...
UNION
select * from
(
select blah, blah from T where something else order by somecolumn limit 1
)

With simple example:
> select * from ex1;
+------+----------+
| id | name |
+------+----------+
| 1 | Pirate |
| 2 | Monkey |
| 3 | Ninja |
| 4 | Spagheti |
| 5 | kumar |
| 6 | siva |
+------+----------+
> select * from ex1 union select "1", "sing" order by case name when 'sing' then 1 else 2 end, name;
+------+----------+
| id | name |
+------+----------+
| 1 | sing |
| 5 | kumar |
| 2 | Monkey |
| 3 | Ninja |
| 1 | Pirate |
| 6 | siva |
| 4 | Spagheti |
+------+----------+

Related

Count the number of appearances of char given a ID

I have to perform a query where I can count the number of distinct codes per Id.
|Id | Code
------------
| 1 | C
| 1 | I
| 2 | I
| 2 | C
| 2 | D
| 2 | D
| 3 | C
| 3 | I
| 3 | D
| 4 | I
| 4 | C
| 4 | C
The output should be something like:
|Id | Count | #Code C | #Code I | #Code D
-------------------------------------------
| 1 | 2 | 1 | 1 | 0
| 2 | 3 | 1 | 0 | 2
| 3 | 3 | 1 | 1 | 1
| 4 | 2 | 2 | 1 | 0
Can you give me some advise on this?
This answers the original version of the question.
You are looking for count(distinct):
select id, count(distinct code)
from t
group by id;
If the codes are only to the provided ones, the following query can provide the desired result.
select
pvt.Id,
codes.total As [Count],
COALESCE(C, 0) AS [#Code C],
COALESCE(I, 0) AS [#Code I],
COALESCE(D, 0) AS [#Code D]
from
( select Id, Code, Count(code) cnt
from t
Group by Id, Code) s
PIVOT(MAX(cnt) FOR Code IN ([C], [I], [D])) pvt
join (select Id, count(distinct Code) total from t group by Id) codes on pvt.Id = codes.Id ;
Note: as I can see from sample input data, code 'I' is found in all of Ids. Its count is zero for Id = 3 in the expected output (in the question).
Here is the correct output:
DB Fiddle

Over Partition to find duplicates and remove them based on criteria SQL

I hope everyone is doing well. I have a dilemma that i can not quite figure out.
I am trying to find a unique value for a field that is not a duplicate.
For example:
Table 1
|Col1 | Col2| Col3 |
| 123 | A | 1 |
| 123 | A | 2 |
| 12 | B | 1 |
| 12 | B | 2 |
| 12 | C | 3 |
| 12 | D | 4 |
| 1 | A | 1 |
| 2 | D | 1 |
| 3 | D | 1 |
Col 1 is the field that would have the duplicate values. Col2 would be the owner of the value in Col 1. Col 3 uses the row number() Over Partition syntax to get the numbers in ascending order.
The goal i am trying to accomplish is to remove the value in col 1 if it is not truly unique when looking at col2.
Example:
Col1 has the value 123, Col2 has the value A. Although there are two instances of 123 being owned by A, i can determine that it is indeed unique.
Now look at Col1 that has the value 12 with values in Col2 of B,C,D.
Value 12 is associated with three different owners thus eliminating 12 from our result list.
So in the end i would like to see a result table such as this :
|Col1 | Col2|
| 123 | A |
| 1 | A |
| 2 | D |
| 3 | D |
To summarize, i would like to first use the partition numbers to identify if the value in col1 is repeated. From there i want to verify that the values in col 2 are the same. If so the value in col 1 and col 2 remains as one single entry. However if the values in col 2 do not match, all records for the col1 value are removed.
I will provide the syntax code for my query if needed.
Update**
I failed to mention that table 1 is the result of inner joining two tables.
So Col1 comes from table a and Col2 comes from table b.
The values in table a for col2 are hard to interpret so i had to make sense of them and assigned it proper name values.
The join query i used to combine the two are:
Select a.Col1, B.Col2 FROM Table a INNER JOIN Table b on a.Colx = b.Colx
Update**
Table a:
|Col1 | Colx| Col3 |
| 123 | SMS | 1 |
| 123 | S9W | 2 |
| 12 | NAV | 1 |
| 12 | NFR | 2 |
| 12 | ABC | 3 |
| 12 | DEF | 4 |
| 1 | SMS | 1 |
| 2 | DEF | 1 |
| 3 | DES | 1 |
Table b:
|Colx | Col2|
| SMS | A |
| S9W | A |
| DEF | D |
| DES | D |
| NAV | B |
| NFR | B |
| ABC | C |
Above are sample data for both tables that get joined in order to create the first table displayed in this body.
Thank you all so much!
NOT EXISTS operator can be used to do this task:
SELECT distinct Col1 , Col2
FROM table t
WHERE NOT EXISTS(
SELECT 1 FROM table t1
WHERE t.col1=t1.col1 AND t.col2 <> t1.col2
)
If I understand correctly, you want:
select col1, min(col2)
from t
group by col1
where min(col2) <> max(col2);
I think the third column is confusing you. It doesn't seem to play any role in the logic you want.

Need sql help: For each record in table A (has more columns than table B), insert into Table B

I know my subject is a little sparse, but for the life of me I cannot figure out how to do this. I could accomplish this in C# but I am getting confused by the SQL syntax. I searched and searched and I can't seem to find what I am looking for probably because I don't understand some of the SQL that I am looking at.
TABLE 1
-----------
| CustNo | Catalog1 | Catalog2 | Catalog3 | Catalog4 |
| 1 | A | B | C | NULL |
| 2 | B | C | NULL | D |
| 3 | A | C | E | F |
TABLE 2 (empty)
COLUMNS: CustNo|Catalog
So Basically for each record in Table 1, I want to insert the catalogs into table 2.
So the desired output would look like the following.
TABLE 2
CustNo|Catalog
| 1 | A
| 1 | B
| 1 | C
| 2 | B
| 2 | C
| 2 | D
| 3 | A
| 3 | C
| 3 | E
| 3 | F
Thank you all for any help!
Just unpivot. I like to do this using apply;
insert into table2 (CustNo, Catalog)
select t1.CustNo, v.Catalog
from table1 t1 cross apply
(values (t1.Catalog1), (t1.Catalog2), (t1.Catalog3), (t1.Catalog4)
) v(catalog)
where v.Catalog is not null;

SQL - Select where record-set does not contain a value

I am not sure the title reflects what I am searching for accurately.
I have a header table, and a lines table. For each header, there can be multiple line records.
If the lines table doesn't contain the line value for a header record, I want to return that header record.
Example:
Header Table -
| ID | xyz...|
| 1 | abc |
| 2 | abc |
Lines Table -
| LineID | HeaderID | xyz...|
| 1 | 1 | abc |
| 2 | 1 | abc |
| 3 | 1 | abc |
| 3 | 2 | abc |
| 4 | 2 | abc |
| 5 | 2 | abc |
In this case, I am looking to return Header Record 2 if I search for the record that doesn't contain a line ID of 1 or 2.
I can't think of the query for the life of me, even though I know it's staring me right in the face.
When I think of queries to try, I will post them to show my workings, but I am hoping someone has an answer for this.
A simple NOT EXISTS will do the trick:
SELECT *
FROM Header h
WHERE NOT EXISTS(
SELECT 1
FROM Lines l
WHERE
l.HeaderID = h.ID
AND l.LineID IN(1, 2)
)
ONLINE DEMO
Query:
LINK
SELECT h.*
FROM Header h
LEFT JOIN Lines l
ON l.HeaderID = h.ID
AND l.LineID IN(1, 2)
WHERE l.HeaderID is null

How to count rows that have the same values in two columns (SQL)?

I am sure there must be a relatively straightforward way to do this, but it is escaping me at the moment. Suppose I have a SQL table like this:
+-----+-----+-----+-----+-----+
| A | B | C | D | E |
+=====+=====+=====+=====+=====+
| 1 | 2 | 3 | foo | bar | << 1,2
+-----+-----+-----+-----+-----+
| 1 | 3 | 3 | biz | bar | << 1,3
+-----+-----+-----+-----+-----+
| 1 | 2 | 4 | x | y | << 1,2
+-----+-----+-----+-----+-----+
| 1 | 2 | 5 | foo | bar | << 1,2
+-----+-----+-----+-----+-----+
| 4 | 2 | 3 | foo | bar | << 4,2
+-----+-----+-----+-----+-----+
| 1 | 3 | 3 | foo | bar | << 1,3
+-----+-----+-----+-----+-----+
Now, I want to know how many times each combination of values for columns A and B appear, regardless of the other columns. So, in this example, I want an output something like this:
+-----+-----+-----+
| A | B |count|
+=====+=====+=====+
| 1 | 2 | 3 |
+-----+-----+-----+
| 1 | 3 | 2 |
+-----+-----+-----+
| 4 | 2 | 1 |
+-----+-----+-----+
What would be the SQL to determine that? I feel like this must not be a very uncommon thing to want to do.
Thanks!
SELECT A,B,COUNT(*)
FROM the-table
GROUP BY A,B
TRY:
SELECT
A, B , COUNT(*)
FROM YourTable
GROUP BY A, B
This should do it:
SELECT A, B, COUNT(*)
FROM TableName
GROUP BY A, B;
SELECT A,B,COUNT(1) As COUNT_OF
FROM YourTable
GROUP BY A,B
SELECT A,B,COUNT(*)
FROM table
GROUP BY A,B
SELECT A, B, COUNT(*)
FROM MyTable
GROUP BY A, B
This could be the answer:
SELECT a, b, COUNT(*)
FROM <your table name here>
GROUP BY a,b
ORDER BY 3 DESC;