This question already has answers here:
What is the best way to create and populate a numbers table?
(12 answers)
Closed 4 years ago.
I am wondering if it is possible to get a query that will take a range of numbers, in this case 8 to 17, compare it against a field in a table and remove the ones that do appear in the table and return the rest?
I assume the peusdo code would look something like
Select nums from range(8-17) where nums not in (select column from table)
Is this possible at all?
Edit
To clarify my question.
In table I might have the following:
Intnumber
9
10
16
I would like to have the numbers between 8-17 that do not appear in this table, so 8,11,12,13,14,15,17
Kind regards
Matt
select nums from table where nums not between 8 and 17;
Related
This question already has answers here:
How to use count and group by at the same select statement
(11 answers)
Closed 6 months ago.
I want to count a certain value in a column and output it as a number.
Here is an example:
id
job
1
police
2
police
3
ambulance
Now I want to count the value "police" in the "job" column and make the result in a number, so because there are two entries with "police" in the column it would be as output the number two. With the value "ambulance" it is only one entry so the result would be 1.
Can anyone tell me how to write this as code?
I have now searched a lot on the Internet and tried myself but I have found nothing that worked.
You're saying you want to count how many of each type of job there is, right?
SELECT COUNT(*), job
FROM tablename
GROUP BY job
This question already has answers here:
Group rows into sets of 5
(2 answers)
Closed 3 years ago.
as I realized that my project cannot be implemented with Excel I am now forced to learn working with SQL. Because I'm used to Excel VBA I am already failing at simple tasks.
In a table of my SQL Database I wanna create a new column with values counting up (ASC) but it shouldn't count up like 1,2,3,4,...it rather should increase the value by 1 every 9th value.
So if we start in row 1 with Value 1 than every row until row 9 in this column should countain value 1 followed by an increase in row 10 by 1. So value 2 should be shown until row 18.....in total this should end in row 306.
Is anybody able to help me? I have no clue at all..
THANK YOU FOR YOU HELP!
You can first simply add a column in your table and then update that column with a simple mathematics formula -
UPDATE YOUR_TABLE
SET NEW_COL = CEILING(((ID-1)/9)+1)
Hope this helps.
This question already has an answer here:
How can I get a number with a thounds separator on top?
(1 answer)
Closed 5 years ago.
SELECT count(id) FROM table a
it gives me a number lets say 36136, how can i make thousand separator on top?
Try (MSSQL)
SELECT REPLACE(REPLACE(
CONVERT(VARCHAR,CONVERT(MONEY, COUNT(ID)),1)
,'.00','') ,',','''')
FROM TABLE A
Example
SELECT REPLACE(REPLACE(CONVERT(VARCHAR,CONVERT(MONEY, 1000),1),'.00','') ,',','''') X
Output
X
----
1'000
Use TOP statement in SQL server :
SELECT TOP 1000 * FROM table a
This question already has an answer here:
BigQuery check for array overlap
(1 answer)
Closed 5 years ago.
I have an array of values I'd like to filter results for if an array column contains any of these values, is there an easy way to perform an intersect in BigQuery using the standard SQL language?
This should give you the general direction:
SELECT ...
WHERE EXISTS(SELECT 1 FROM UNNEST(array_column) a WHERE a IN UNNEST(array_values))
This question already has answers here:
Pad a string with leading zeros so it's 3 characters long in SQL Server 2008
(18 answers)
Closed 8 years ago.
I have two tables with product numbers. They both are limited to 12 characters (varchar(12)). One of them (product A) has a number structure like this:
Product No:
2345
568
89
And product B has the same exact numbers but with zeros to fill the 12 characters missing. It is something like this:
Product No:
000000002345
000000000568
000000000089
I just want to modify product A table to add the zeros at the beginning of the sequence. I had an idea with REPLACE() function but to add the zeros I might need another function. Thanks for reading and sorry for the time.
Try this, you can use this statement
RIGHT('000000000000'+ISNULL(ProductNo,''),12)
This should do it:
UPDATE tblA
SET ProductNo = REPLICATE('0', 12 - LEN(ProductNo)) + ProductNo
I just want to modify product A table to add the zeros at the beginning of the sequence.
Big hairy question for you: Are you absolutely certain that you want to store these values in the table with leading zeros, or just be able to display it as-needed with leading zeros?
Reason I ask is because the varchar(12) implies 13 bytes in memory, where an int only takes 4, which will make a big difference if this column participates in indexes and foreign key relationships with other tables.