This question already has an answer here:
Sqlite query - order results with numbers last
(1 answer)
Closed 7 years ago.
I have table data like this:
ID name
----------
1 30
2 aaa
3 zzz
4 20
5 40
6 10
My result should be sorted data
ID name
5 40
1 30
4 20
6 10
3 zzz
2 aaa
Means numeric and alphabetic should be separate and it should be sorted in DESC or ASC.
If anyone have an idea to perform this operation in sqlite3 help me
SELECT * FROM yourTableName ORDER BY ID ASC;
If you want something more detailed than that, it would help to know what you are currently running or what you want the output to be.
Related
This question already has answers here:
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
Select First Row of Every Group in sql [duplicate]
(2 answers)
Return row with the max value of one column per group [duplicate]
(3 answers)
SQL: getting the max value of one column and the corresponding other columns [duplicate]
(2 answers)
Closed 6 days ago.
I am using Sql Developer and the database is Oracle 12c.
I have the following query that returns the first row that has an update_timestamp date that is equal to or less than a specified value:
SELECT j.ID, j.partsID, j.amount, j.update_timestamp
FROM junk j
WHERE j.update_timestamp <= '10-JAN-23'
ORDER BY j.update_timestamp desc
FETCH first 1 row only;
How can I modify the SQL above to return the first row for each partsID before a given update_timestamp?
Examples:
Here is the source table, "junk":
ID
partsID
amouunt
update_timestamp
1
11
500
14-JAN-23
2
12
300
12-JAN-23
3
13
300
12-JAN-23
4
11
300
11-JAN-23
5
12
300
11-JAN-23
6
13
300
11-JAN-23
7
11
300
09-JAN-23
8
12
300
08-JAN-23
9
13
300
07-JAN-23
Using the "junk" table above, I want the first rows with update_timestamp values that are less than or equal to '10-JAN-23' for each partsID. The results should be:
ID
partsID
amouunt
update_timestamp
7
11
300
09-JAN-23
8
12
300
08-JAN-23
9
13
300
07-JAN-23
Another example using "junk": When searching for the first rows with update_timestamp values that are less than or equal to '13-JAN-23' for each partsID, the query would return the following:
ID
partsID
amouunt
update_timestamp
2
12
300
12-JAN-23
3
13
300
12-JAN-23
4
11
300
11-JAN-23
This question already has answers here:
How to count number of occurrences for all different values in database column?
(2 answers)
Closed 3 years ago.
I have a table like the following:
ID X Y
5 2 0
5 1 1
5 3 3
4 -2 1
4 0 0
3 5 -3
I would like to count how the records for each ID
ID count
5 3
4 2
3 1
This is pretty much all
SELECT ID, COUNT(*) AS count FROM TABLE GROUP BY ID
I've Benchmarking table like this
BMID TestID BMTitle ConnectedTestID
---------------------------------------------------
1 5 My BM1 0
2 6 My BM2 5
3 7 My BM3 5,6
4 8 My BM4 10,12,8
5 9 My BM5 0
6 10 My BM6 3,6
7 5 My BM7 8,3,12,9
8 3 My BM8 7,10
9 8 My BM9 0
10 12 My BM10 9
---------------------------------------------
Explaining the table a little
Here the TestID and the connected TestID is playing the roles. If the user wants all the benchmarks for the TestID 3
It should return rows where testID=3 and also if any rows having connectedTestID column having that testID in it among the comma separated values
That means if the user specify the value 3 as the testID, it should return
---------------------------------------------
8 3 My BM8 7,10
7 5 My BM7 8,3,12,9
6 10 My BM6 3,6
--------------------------------------------
Hope its clear how those 3 rows returned. Means First row is because the testID 3 is there. the other two rows because 3 is in their connectedIDs cell
You should fix the data structure. Storing numeric ids in a comma-delimited list is a bad, bad, bad idea:
SQL Server doesn't have the best string manipulation functions.
Storing numberings as character strings is a bad idea.
Having undeclared foreign key relationships is a bad idea.
The resulting queries cannot make use of indexes.
While you are exploring what a junction table is so you can fix the problem with the data structure, you can use a query such as this:
where testid = 3 or
',' + ConnectedTestID + ',' like '%,3,%'
I would like to perform a special group by statement, in other programming languages I would use some kind of loop, but I have no idea on how to tackle this using sql. Hope that you guys can be of some help. Bit of sample data
NR date Code
2 1-1-2013 6
2 1-1-2013 6
2 3-1-2013 6
2 4-1-2013 7
2 5-1-2013 6
2 5-1-2013 5
3 1-1-2013 1
3 2-1-2013 1
3 2-1-2013 6
3 3-1-2013 7
I would like to do a group by on NR and Code. However I don't want to group non-succeeding Code's (they are sorted on NR and date). The desired output will make it clear i think:
NR Code #
2 6 3
2 7 1
2 6 1
2 5 1
3 1 2
3 6 1
3 7 1
After this I would like to cast it to this format (could be another question, but illustrates my need for a solution for the problem above):
NR Code_string #_string
2 6,7,6,5 3,1,1,1
3 1,6,7 2,1,1
If I did not provide a good example, please tell, this is my first question for sql (learned alot of R using SO)
select NR, Code, count(*) from yourdatabasename
group by NR, Code
order by NR, Code
This command should satisfy yor first desired output.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Oracle: how to “group by” over a range?
Let's say I have data that looks like this:
Item Count
======== ========
1 123
2 1
3 47
4 117
5 18
6 466
7 202
I want to create a query that gives me this:
Count Start Count End Occurrences
=========== =========== ===========
0 100 3
101 200 2
201 300 1
301 400 0
401 500 1
Basically, I want to take a bunch of counts and group them into ranges for statistical rollups. I don't think I'm using the right keywords to find the answer to this. I am going against Oracle, though if there is an ANSI SQL answer I'd love to have it.
select
a.mini,
a.maxi,
count(a.item)
from
(
select
table.item,
case (table.counter)
when counter>=0 and counter<=100 then 0
when counter>100 and counter<200 then 101
when ....
end as mini
table.item,
case (table.counter)
when counter>=0 and counter<=100 then 100
when counter>100 and counter<200 then 201
when ....
end as maxi
from
table
) a
group by
a.mini,
a.maxi
One way is using CASE statements. If you want it to be scalable, try having the range in a separate table and use JOIN to count the occurence.