Check Multiple Rows for a Value - sql

I need to check the column IsSubmitted in multiple rows in a table for a a certain user id.
If any of the rows has a 1 for is submitted I return 0 else I return another value.
How do I check the results of this query to see if any of the rows has a 1 for IsSubmitted if it returns multiple rows?
SELECT IsSubmitted FROM [Application] WHERE ID = #id
EXAMPLE this query may return
IsSubmitted
0
0
0
1
0
or
0
0
0
0
it could be any number of rows or only one row. I need to know if any of them contain a 1. We have an application where they only have to pay one time and they can submit as many apps as they want, so I need to check and see if they have already paid(submitted).
If they have submitted then i need to take one action, if they haven't then i need to take another action.

SELECT CASE
WHEN EXISTS(SELECT * FROM Application WHERE ID=#id AND IsSubmitted=1) THEN 0
ELSE 1 --or some other value
END
This can be inserted into the context of a larger query if needed.

Related

Trying to understand simple SQL query with case statement

I am trying to understand this query:
SELECT *
FROM servers
ORDER BY
CASE
WHEN status = "ACTIVE" THEN 1
WHEN status = "INACTIVE" THEN 2
ELSE 3
END
I know this is selecting all rows from the server table and ordering them first with where column status = "ACTIVE" and then where status = "INACTIVE."
What is the syntax THEN 1...THEN 2 ELSE 3 END mean? I know END is to close the case statement, but what are 1, 2, and 3?
Your CASE clause is in the ORDER BY section - it doesn't become part of the output, it's just used by the SQL engine for sorting.
The 1,2,3 are sortable values.
Basically it' saying to put the ACTIVE rows first (1), then the INACTIVE rows (2), then any rows that are neither (3) at the end.
Given that ACTIVE and INACTIVE sort the same way, I guess there are other values in the table that don't sort in that order (maybe CLOSED or DORMANT which would come before INACTIVE

How to set flag based on values in previous columns in same table ? (Oracle)

I'm creating a new table and carrying over several columns from a previous table. One of the new fields that I need to create is a flag that will have values 0 or 1 and value needs to be determined based on 6 previous fields in the table.
The 6 previous columns have preexisting values of 0 or 1 stored for each one. This new field needs to check whether any of the 6 columns have 1 and if so set the flag to 0. If there is 0 in all 6 fields then set itself to 1.
Hopefully this makes sense. How can I get this done in oracle? I assume a case statement and some sort of forloop?
You can use greatest() function: GREATEST
create table t_new
as
select
case when greatest(c1,c2,c3,c4,c5,c6)=1 -- at least one of them contains 1
then 0
else 1
end c_new
from t_old;
Or even shorter:
create table t_new
as
select
1-greatest(c1,c2,c3,c4,c5,c6) as c_new
from t_old;
In case of greatest = 1, (1-1)=0, otherwise (1-0)=1
You can use a virtual column with a case expression; something like:
flag number generated always as (
case when val_1 + val_2 + val_3 + val_4 + val_5 + val_6 = 0 then 1 else 0 end
) virtual
db<>fiddle
or the same thing with greatest() as #Sayan suggested.
Using a virtual column means the flag will be right for newly-inserted rows, and if any of the other values are updated; you won't have to recalculate or update the flag column manually.
I've assumed the other six columns can't be null and are constrained to only be 0 or 1, as the question suggests. If they can be null you can add nvl() or coalesce() to each term in the calculation.

Updating columns based on a combine rows value on the same table

Please assist if possible, I have used Stuff to combine rows into a single row based on other columns. However I want to turn each of the unique items into it's own column with a number showing if it exists, e.g. 1 or 0 and then doing the same for all subsequent rows?
I have been able to create the columns but I can't get them to update per whats in the one column.
But I want it to be dynamic so matter how many different names appear in categories it creates a new column and adds 1 or 0 if it appears or not
How about something like this for SQL Server?
strSQL = "SELECT Category, CASE WHEN Category IS NOT NULL THEN 1 ELSE 0 END AS IsCategoryExist FROM MyTable"
Sample data (the 2nd column shows as 1 if the first column is non-blank):
Cars, 1
[Blank], 0
Airplanes, 1
Radios, 1

Why does <> operator not work as expected on my varbinary(20) sql data?

I have a MySQL database, it contains a table that contains some data in columns of type varbinary(20)
Don't ask me why - it just is and i need to deal with it.
This data represents the number of modules a student has completed as part of a course. There is also another column called criterias that represents the number of modules that must be completed in order for the course to be marked complete. When I am doing a count of the number of completed courses, I want to count the rows where
completed = criteria AND completed != 0 ( ...oh and where the student is enrolled)
(because in some cases the criteria is 0 so I want to skip any courses where a student has completed 0 of 0 modules).
Additionally for some reason the value of 0 in the varbinary column is 30 (if anyone can offer an explanation for this then great) and so where there are 3 modules that need to be completed the value in the criteria column will be 33
So my SQL query looks like this -
count(CASE WHEN (completed <> 30 AND (completed = criterias) AND enrolled = 'Yes') THEN 1 END) AS Total_Complete
However this count seems to include courses where student has completed 0 of 0 courses so it would appear that my completed <> 30 is not working.
As I was writing this I answered my own question but I thought id leave it up here incase it helped anyone else.
The solution was to ignore the fact that for whatever reason 0 was represented by 30 and change my sql to -
count(CASE WHEN (completed <> 0 AND (completed = criterias) AND enrolled = 'Yes') THEN 1 END) AS Total_Complete
This worked

Get specific "version" of a column

I have an app that uses SQLite to store data locally. There may be more than one file storing the data. All the files contain a list of items, but only one of them has the "correct" status for the current user (basically, there is a "user" db in $HOME and a "system-wide" one in /etc).
Usually, both files will contain the same list of items, and only the status column will differ. If, however, either contains items not in both, I want that data as well.
SQLite does not have FULL OUTER JOIN.
A solution I have come up with is this:
SELECT item, group_concat(status) AS status FROM (SELECT item,status FROM items UNION SELECT item,status FROM otherdb.items) GROUP BY item;
And then parsing the comma-separated "status" output to get the "right" status. I would like a pure SQL solution, however.
The values I want for status are:
If any = 1, status = 1
elif any = -1, status = -1
elif any = 2, status = 2
elif any = -2, status = -2
else status = 0 or NULL
status may only (in the db) be -2,-1,0,NULL,1,2 so this covers all data.
If there is a solution that only gives whichever one is non-zero and non-null, that could work too, although I would prefer the above.
I would sugest you one approach:
1. create a temp table, one adittional column for a flag "otherbd";
2. throw everything from the 2 tables in there;
3. delete the lines you don't want;
Create a status priority table with the following values
status priority
1 5
-1 4
2 3
-2 2
0 1
NULL 0
The concept is to join your two tables against this StatusPriorty table, Group the records and use MAX(Priority) to get your results