how set value depending on a column value select statement t-sql. Like if 0 set 3, if less than 4 set 2 and so on - sql

I have a column named photos, the photos column is of type tinyint and vary from 0 to 8. I want to apply a rank to the column when selecting data from the table. the values that want to get are: if the record has 4 or more photos, it receives a rank of 1. Because it is a fair amount of photos like 5, 6, 7 and 8. if the record has between 1 and 3 set a rank of 2, a medium rank, and if it has no photos set 3. with this and can sort the records based on the number of photos and make my appliation consistent.

Sure, you would just use a CASE statement in your ORDER BY. Something like this:
SELECT *
FROM Table
ORDER BY
CASE
WHEN photos >= 4 THEN 1
WHEN photos >= 1 THEN 2
ELSE 3
END
Or if you wanted it to be an actual column on the table, you could add it as a computed column:
ALTER TABLE mytable ADD rank AS
CASE
WHEN photos >= 4 THEN 1
WHEN photos >= 1 THEN 2
ELSE 3
END

Related

BigQuery INSERT SELECT results in random order of records?

I used standard SQL to insert data form one table to another in BigQuery using Jupyter Notebook.
For example I have two tables:
table1
ID Product
0 1 book1
1 2 book2
2 3 book3
table2
ID Product Price
0 5 book5 8.0
1 6 book6 9.0
2 4 book4 3.0
I used the following codes
INSERT test_data.table1
SELECT *
FROM test_data.table2
ORDER BY Price;
SELECT *
FROM test_data.table1
I got
ID Product
0 1 book1
1 3 book3
2 2 book2
3 5 book5
4 6 book6
5 4 book4
I expected it appears in the order of ID 1 2 3 4 5 6 which 4,5,6 are ordered by Price
It also seems that the data INSERT and/or SELECT FROM display records in a random order in different run.
How do I control the SELECT FROM output without including the 'Price' column in the output table in order to sort them?
And this happened when I import a csv file to create a new table, the record order is random when using SELECT FROM to display them.
The ORDER BY clause specifies a column or expression as the sort criterion for the result set.
If an ORDER BY clause is not present, the order of the results of a query is not defined.
Column aliases from a FROM clause or SELECT list are allowed. If a query contains aliases in the SELECT clause, those aliases override names in the corresponding FROM clause.
So, you most likely wanted something like below
SELECT *
FROM test_data.table1
ORDER BY Price DESC
LIMIT 100
Note the use of LIMIT - it is important part - If you are sorting a very large number of values, use a LIMIT clause to avoid resource exceeded type of error

How do I display grouped ID's as a list with their respective values?

How do I not get all the ID's grouped, but instead listed from first to last; with all their respective values in the columns next to them?
Instead of grouping it, it should show ID 1 and its value, ID 2 and its value. EVEN if the values for the ID is the same. I tried removing the GROUP_CONCAT, but then it's only showing one ID per customfield_value?
SELECT GROUP_CONCAT(virtuemart_product_id), customfield_value, COUNT(*) c
FROM jos_virtuemart_product_customfields
WHERE virtuemart_custom_id = 6
GROUP BY customfield_value
HAVING c > 1
It's working currently, but grouping the ID's and spacing them with a comma. Should just display as in a normal table/list format.
Currently it shows like this(as you can see, it's ALL the same ICOS number, but different ID's. I ONLY need to display the values WHERE the ICOS NUMBER is "duplicate"):
ID ICOS Count
1,2,3 775896 3
It should be displaying like this:
ID ICOS Count
1 775896 1
2 775896 1
3 775896 1
All rows where the customfield_value is not unique:
-- Assuming MySQL
SELECT virtuemart_product_id, customfield_value
, COUNT(*) c -- maybe not needed
FROM jos_virtuemart_product_customfields
WHERE virtuemart_custom_id = 6
AND customfield_value IN
( SELECT customfield_value
FROM jos_virtuemart_product_customfields
WHERE virtuemart_custom_id = 6
GROUP BY customfield_value
HAVING COUNT(*) > 1 -- more than one row exists
)
GROUP BY virtuemart_product_id, customfield_value -- maybe not needed
If the virtuemart_product_id is unique you don't need the outer count/group by as it will always be 1.

MonetDB: Enumerate groups of rows based on a given "boundary" condition

Consider the following table:
id gap groupID
0 0 1
2 3 1
3 7 2
4 1 2
5 5 2
6 7 3
7 3 3
8 8 4
9 2 4
Where groupID is the desired, computed column, such as its value is incremented whenever the gap column is greater than a threshold (in this case 6). The id column defines the sequential order of appearance of the rows (and it's already given).
Can you please help me figure out how to dynamically fill out the appropriate values for groupID?
I have looked in several other entries here in StackOverflow, and I've seen the usage of sum as an aggregate for a window function. I can't use sum because it's not supported in MonetDB window functions (only rank, dense_rank, and row_num). I can't use triggers (to modify the record insertion before it takes place) either because I need to keep the data mentioned above within a stored function in a local temporary table -- and trigger declarations are not supported in MonetDB function definitions.
I have also tried filling out the groupID column value by reading the previous table (id and gap) into another temporary table (id, gap, groupID), with the hope that this would force a row-by-row operation. But this has failed as well because it gives the groupID 0 to all records:
declare threshold int;
set threshold = 6;
insert into newTable( id, gap, groupID )
select A.id, A.gap,
case when A.gap > threshold then
(select case when max(groupID) is null then 0 else max(groupID)+1 end from newTable)
else
(select case when max(groupID) is null then 0 else max(groupID) end from newTable)
end
from A
order by A.id asc;
Any help, tip, or reference is greatly appreciated. It's been a long time already trying to figure this out.
BTW: Cursors are not supported in MonetDB either --
You can assign the group using a correlated subquery. Simply count the number of previous values that exceed 6:
select id, gap,
(select 1 + count(*)
from t as t2
where t2.id <= t.id and t2.gap > 6
) as Groupid
from t;

Updating values with conditions

Let's assume I have a table like that
END
id rank degree
1 4 3
2 3 3
**rank 4 has 4 degrees.
**rank 3 has 4 degrees.
And we will assume that each rank has some number of degrees. I will assume that rank 3 has 4 degrees.
And I want that, when I increase a degree that is the maximum for the current rank, the rank increases by 1 and the degree resets back to 1. For example, I want to increase the degree of the id 2 in the above table by 1. As a result, the rank should be 4 and the degree should be 1.
How can I make that update efficiently in SQL Server 2008?
I assume you want to update all rows with the same rank of the one with id=3.
UPDATE t SET t.rank = t.rank + 1, t.degree = 1
FROM tableName t
WHERE rank = (SELECT rank FROM tableName t2 WHERE id=#id)
Is this what you want?
update t
set degree = (case when degree = 5 then 1 else degree + 1 end),
rank = (case when degree = 5 then rank + 1 else rank end)
where id = 3;
If there is a reference table containing information about the maximum degree for every supported rank (let's call it dbo.ranks), something like this:
rank maxdegree
---- ---------
1 3
2 4
3 5
4 4
... ...
where maxdegree is assumed to be an integer greater than 0, then here's how you could use it:
UPDATE t
SET
t.rank += t.degree / r.maxdegree,
t.degree += t.degree % r.maxdegree + 1
FROM dbo.atable AS t
INNER JOIN dbo.ranks AS r
ON t.rank = r.rank
;
where dbo.atable is assumed to be the name of the table to update.
Note that this query is only intended for increasing by 1. If you want it to be able to increase degree by an arbitrary number, you will need to make more substantial changes than just replacing 1 in the + 1 bit.
Also, this query will not work correctly with some cases of invalid data in your table (like degrees greater than the corresponding maximums), so make sure you've removed any anomalies in your data before trying to use this.

SQL Access db - select every third row from database

How can I select every thrid row from the table?
if a table has
1
2
3
4
5
6
7
8
9
records
it should pick up 3, 6,9 record. regards less what their data is.
Modulo is what you want...
Assuming contiguous values:
SELECT *
FROM Mytable
WHERE [TheColumn] Mod 3 = 0
And with gaps
SELECT *
FROM Mytable
WHERE DCount("TheColumn", "table", "TheColumn <= " & [TheColumn]) Mod 3 = 0
Edit: To exclude every 3rd record, ...Mod 3 <> 0
If its SQL you could use the row_number and over commands. see this, then where rownumvar % 3 =0 but not sure if that works in access.
Or you could put the table into a recordset and iterate through checking the index for % 3=0 if your using any kind of code.
How about a Count() on a field that has unique members. (id?) then % 3 on that.