I have a statament wrote for teradata by someone who don't work here anymore, so i can't ask him directly.
In this statament, the last clause in the where is : ... and Column_Name >= '' .I have no clue about what should that clause do.
If it matter Column_name is a [decimal](7,0) NULL.
Can someone explain to me which case are accepted and which are refused by that clause ?
to me it seams that should allow trough everything since everything is major or equal to null
When you compare a string to a numeric value the string is converted to a FLOAT, in your case the empty string '' is treated as 0, so this is just a stupid way to check for Column_Name >= 0 and filters negative values and NULL. You never know if this was actually the intention of the guy who wrote it :)
When >='' clause is applied on a character column then it will restrict the null records.
If the table has 2 columns one Region_cd & rank_nr and the data in the table is as below:-
select * from databasename.tablename;
*** Query completed. 5 rows found. Two column returned.
*** Total elapsed time was 1 second.
Region | rank_nr
---------- -----------
emea | 1
amr | 2
apac | 3
? | 4
| 5
? is represents NULL and space in region_cd column for rank=5 is not visible
If we query in this table with where clause as Region>='' then it will result below 4 rows:-
select * from databasename.tablename where Region_cd >='' ;
*** Query completed. 4 rows found. 2 columns returned.
*** Total elapsed time was 1 second.
col1 | rank_nr
---------- -----------
emea | 1
apac | 3
amr | 2
| 5
Related
I have a table with a large amount of data; moving forward, I would like to enforce uniqueness for a given column in this table. However, the table contains a large amount of rows where that column is non-unique. I am not able to delete or alter these rows.
Is it possible to enforce uniqueness over a given date range, or since a specific date, or based on the value of another column (or something else like that) in MariaDB?
You can create a UNIQUE index on multiple columns, where one column is nullable. MariaDB will see each column with NULL values as a different value regarding the UNIQUE index, even if the other column values of the UNIQUE index are the same. Check the MariaDB documentation Getting Started with Indexes - Unique Index:
The fact that a UNIQUE constraint can be NULL is often overlooked. In SQL any NULL is never equal to anything, not even to another NULL. Consequently, a UNIQUE constraint will not prevent one from storing duplicate rows if they contain null values:
CREATE TABLE t1 (a INT NOT NULL, b INT, UNIQUE (a,b));
INSERT INTO t1 values (3,NULL), (3, NULL);
SELECT * FROM t1;
+---+------+
| a | b |
+---+------+
| 1 | 1 |
| 2 | 1 |
| 2 | 2 |
| 3 | NULL |
| 3 | NULL |
+---+------+
You can create such a UNIQUE index on the date column you already have and a new column which indicates if the date value should be unique or not:
CREATE TABLE Foobar(
id INT AUTO_INCREMENT PRIMARY KEY NOT NULL,
createdAt DATE NOT NULL,
dateUniqueMarker BIT NULL DEFAULT 0,
UNIQUE KEY uq_createdAt(createdAt, dateUniqueMarker)
);
INSERT INTO Foobar(createdAt) VALUES ('2021-11-04'),('2021-11-05'),('2021-11-06');
SELECT * FROM Foobar;
+----+------------+------------------------------------+
| id | createdAt | dateUniqueMarker |
+----+------------+------------------------------------+
| 1 | 2021-11-04 | 0x00 |
| 2 | 2021-11-05 | 0x00 |
| 3 | 2021-11-06 | 0x00 |
+----+------------+------------------------------------+
INSERT INTO Foobar(createdAt) VALUES ('2021-11-05');
ERROR 1062 (23000): Duplicate entry '2021-11-05-\x00' for key 'Foobar.uq_createdAt'
UPDATE Foobar SET dateUniqueMarker = NULL WHERE createdAt = '2021-11-05';
INSERT INTO Foobar(createdAt, dateUniqueMarker) VALUES ('2021-11-05', NULL);
SELECT * FROM Foobar;
+----+------------+------------------------------------+
| id | createdAt | dateUniqueMarker |
+----+------------+------------------------------------+
| 1 | 2021-11-04 | 0x00 |
| 2 | 2021-11-05 | NULL |
| 5 | 2021-11-05 | NULL |
| 3 | 2021-11-06 | 0x00 |
+----+------------+------------------------------------+
Without any data example and scenario illustration, it's hard to know. If you can update your question with those information, please do.
"Is it possible to enforce uniqueness over a given date range, or since a specific date, or based on the value of another column (or something else like that) in MariaDB?"
If by "enforce" you mean to create a new column then populate it with unique identifier, then yes it is possible. If what you really mean is to generate a unique value based on other column, that's also possible. Question is, how unique do you want it to be?
Is it like this unique?
column1
column2
column3
unique_val
2021-02-02
ABC
DEF
1
2021-02-02
CBD
FEA
1
2021-02-03
BED
GER
2
2021-02-04
ART
TOY
3
2021-02-04
ZSE
KSL
3
Whereby if it's the same date (on column1), it should have the same unique value regardless of column2 & column3 data.
Or like this?
column1
column2
column3
unique_val
2021-02-02
ABC
DEF
1
2021-02-02
CBD
FEA
2
2021-02-03
BED
GER
3
2021-02-04
ART
TOY
4
2021-02-04
ZSE
KSL
5
Taking all (or certain) columns to consider the unique value.
Both of the scenario above can be achieved in query without the need to alter the table, adding and populate a new column but of course, the latter is also possible.
Env: Oracle APEX v5.1 with Oracle 12c Release 2
Firstly, I have created an Interactive Grid that isn't based off an underlying table as I will process this manually using PL/SQL.
I have been using the following as a guide:
https://apex.oracle.com/pls/apex/germancommunities/apexcommunity/tipp/6361/index-en.html
I basically have the following query:
select
level as id,
level as grid_row,
null as product,
null as product_item
from dual connect by level <= 1
Concentrating on just the product and product_item columns where the product_item column will be a readonly column and only the product number can be entered, I would like to achieve the following:
Product Product Item
---------- -------------
123456 123456-1
123456 123456-2
556677 556677-1
654321 654321-1
654321 654321-2
654321 654321-3
123456 123456-3
From the above, as the user types in the Product and then tabs out of the field, I would like a DA to fire that will add the sequence of "-1" to the end of that product number. Then is the user then adds another row within the IG and enters the same product number, I then want it to append "-2" to the end of it.
Only when the product changes number, I need the sequence to reset to "-1" for that new product as per 556677 and so forth.
Other scenarios that should also be taken into consideration are as follows:
From above IG, the user entered 123456 again but this should calculate that the next sequence for 123456 is "-3"
The same needs to be catered for, when a Product is removed from the IG but to always look at the max sequence number for that product.
I was thinking of possibly using APEX_COLLECTIONS as a means of storing what is currently in the grid, since no changes have been committed to the database.
Assuming you have a collection of product values (in this case, I am using the built-in SYS.ODCINUMBERLIST which is a VARRAY data type) then the SQL for your output would be:
SELECT id,
id AS grid_row,
product,
product || '-' || ROW_NUMBER() OVER ( PARTITION BY product ORDER BY id )
AS product_item
FROM (
SELECT ROWNUM AS id,
COLUMN_VALUE AS product
FROM TABLE(
SYS.ODCINUMBERLIST(
123456,
123456,
556677,
654321,
654321,
654321,
123456
)
)
)
ORDER BY id
Output:
ID | GRID_ROW | PRODUCT | PRODUCT_ITEM
-: | -------: | ------: | :-----------
1 | 1 | 123456 | 123456-1
2 | 2 | 123456 | 123456-2
3 | 3 | 556677 | 556677-1
4 | 4 | 654321 | 654321-1
5 | 5 | 654321 | 654321-2
6 | 6 | 654321 | 654321-3
7 | 7 | 123456 | 123456-3
db<>fiddle here
As you mentioned, the data you enter is not saved into the DB whilst you are inserting your products, so it is not in fact stored anywhere.
So you cannot go check if that value already exists and enter a -2 or other.
Some things to consider would be to maybe save the values into a temp table so you can then have a function go check how many product_item like 123456-% are in there and use that number +1 as your new product_item.
Or you could go the even harder way and do it all with javascript. For this you will need to somehow get all records in the IG, go through them all and see how many occurences of 123456 you have and then insert 123456-(no of occurences + 1).
I have a table users with this structure:
id | user | offset
0 | alvin | -1
1 | alvin | -2
2 | alvin | 5
3 | alvin | 6
4 | simon | -5
5 | simon | -4
6 | theodore | -1
7 | theodore | 1
I want a query that returns the distinct usernames which have at least 1 positive entry AND at least 1 negative entry in offset. In this example, the query would return "alvin" and "theodore" but not "simon".
I've looked at a number of other posts related to GROUP BY, HAVING, COUNT(), and combinations thereof, but none of them seem to address this idea of counting the number of true returns from a conditional statement. In particular, I think I'm misunderstanding how to incorporate aggregate functions like count() into the WHERE clause (or if that's even possible). When I try something like:
SELECT user FROM users WHERE count(offset > 0) > 0;
I get an error: misuse of aggregate function count()
(I know that statement doesn't full specify the query I want - it's meant more as an indication of how I've been trying to approach the problem.)
I'm coming from a Python/Pandas mindset here, and it may just be that this approach doesn't translate well to SQL.
Any advice much appreciated!
This could be done with a compound query:
SELECT user
FROM users
WHERE offset > 0
UNION
SELECT user
FROM users
WHERE offset < 0;
It is actually possible to use aggregation functions.
However, COUNT() counts rows; to count something else, it is a better idea to use a boolean expression (returning 0 or 1), and SUM() that:
SELECT user
FROM users
GROUP BY user
HAVING SUM(offset < 0) > 0
AND SUM(offset > 0) > 0;
I have a tablename contacts which has columns [ "id","name","age" ].I need to get all the contacts order by name in ascending order.I wrote the following query for it:
Select * from contacts order by name collate nocase asc;
The results I get from the above above query are following:
1. 11 | #ax Nene | 21
1. 21 | 123 Ray | 22
1. 33 | maxy Wel | 25
1. 41 | Max Vele | 23
1. 53 | Nam sing | 25
The above ordering is fine but I want names starting with special characters [#,$ or any other non-alphabet] to be at the bottom in the results rather than at top.What should I modify in my query to achieve the desired results.
NOTE: I am using sqlite.
You have to prepend some character like ~ (which is sorted after letters) to any such string.
When you do this only in the ORDER BY clause, it affects only the sorting and not the returned values:
SELECT *
FROM contacts
ORDER BY CASE WHEN name GLOB '[A-Za-z]*'
THEN name
ELSE '~' || name
END COLLATE NOCASE;
(COLLATE NOCASE makes the sorting case-insensitive.)
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;