Order By Problem T-Sql - tsql

I have a sql statement I am using a simple sort such as the following
Select numbers
From theTable
Order By numbers
What I get in return is the following
1
11
12
14
2
21
22
23
3
35
37
etc...
I want it to be ordered in normal order
1
2
3
4
5
6
etc...

The column you are selecting isn't stored as a numeric value. You need to cast it to a some kind of number before orderby will behave the way you want.
It should be as easy as:
select numbers from order orderby cast(numbers as int)
As long as long as all the values in that column cast properly.

what's the datatype of the column storing the numbers? Convert/cast it to an int and you should get what you expect.

Related

PostgreSQL - Is it possible to write a PostgreSQL query that will limits the amount of results it returns based on specific criteria?

I know the wording of my title is vague so I will try to clarify my question.
tasks_table
user_id
completed_date
task_type
task_id
1
11/14/2021
A
34
1
11/13/2021
B
35
1
11/11/2021
A
36
1
11/09/2021
B
37
2
11/12/2021
A
38
2
11/02/2021
A
39
2
11/14/2021
B
40
2
10/14/2021
B
41
The table I am working with has more fields than this, but, these are the ones that are pertinent to the question. The task type can be either A or B.
What I am currently trying to do is get a result set that contains, at max, two tasks per user_id, one of task type A and one of task type B, that have been completed in the past 7 days. For example, the set the query should generate the following result set:
user_id
completed_date
task_type
task_id
1
11/14/2021
A
34
1
11/13/2021
B
35
2
11/12/2021
A
38
2
11/14/2021
B
40
There is a possibility that a user may have only done tasks of one type in that time period, but, it is guaranteed that any given user will have done atleast one task within that time. My question is it possible to create a query that can return such a result or would I have to query for a more generalized result and then trim the set down through logic in my JPA?
To select the most recent task for a given user_id and task_type within the last 7 days from now, if exists, you can try this :
SELECT DISTINCT ON (t.user_id, t.task_type) t.*
FROM tasks_table AS t
WHERE t.completed_date >= current_date - interval '7 days'
ORDER BY t.user_id, t.task_type, t.completed_date DESC

I am trying to combine 6 columns into two, I cant use concatenate, Google Sheets is constantly updating the rows

So I am trying to combine 6 columns
Number
Date
Number
Date
Number
Date
1
1/12/21
2
2/20/21
3
3/5/21
2
2/12/21
3
2/27/21
4
4/1/21
3
1/15/20
4
1/20/21
1
3/30/21
4
1/4/21
1
2/25/21
2
4/2/21
So what I am trying to accomplish is that these rows would combine into two with the latest date being displayed:
Number
Date
1
3/30/21
2
4/2/21
3
3/5/21
4
4/1/21
To get the latest date, I have tried to use
=query('scba fill practise'!A:G,"select A, max(G) group by A")
To get all my numbers to constantly update, I've used
=UNIQUE({A3:A;C3:C;E3:E})
Maybe something like this?
=QUERY({'scba fill practise'!A2:B4;'scba fill practise'!C2:D4;'scba fill practise'!E2:F4}, "SELECT Col1, MAX(Col2) GROUP BY Col1")

Spotfire data difference: same column

I have the following table:
Id Claim_id Date
4 111 10/08/2017
5 333 27/08/2017
2 111 07/08/2017
3 222 08/08/2017
1 444 03/07/2017
7 333 02/09/2017
6 333 28/08/2017
there are more rows (dates) associated to the same Claim_id; column "Id" is based on column "Date" (more recent dates have a greater Id).
I need to create a Calculated Column given by the date difference over claim_id, with the following output:
Id Claim_id Date Days
3 111 10/08/2017 3
1 333 27/08/2017
2 111 07/08/2017
4 222 08/08/2017
7 444 03/07/2017
6 333 02/09/2017 5
5 333 28/08/2017 1
I have tried to use the code given here: Spotfire date difference using over function but it doesn't work (it produces wrong values).
I think that, maybe, it's because my table is not sorted, but I can't order it because I have no access to the source database.
How can I modify that expression?
Thank you!
Valentina
#V.Ang- One way to do this is by adding a column 'decreasing_count'.
What this does is, it counts the number of instances of ID by date. Meaning - ID with highest date would be counted first and then followed by next instance of the same ID with date lower than the previous date and so on. Advantage of this column is, your data need not be sorted for this solution to work.
Now, using this 'decreasing_count' column calculate the difference of dates.
decreasing_count column expression:
Count([Claim_id]) over (Intersect([Claim_id],AllNext([Date])))
Note: This column works in the background. You need not display it in the table
Days calculated column expression:
Days([Date] - Min([Date]) over (Intersect([Claim_id],Next([decreasing_count]))))
Final Output:
Hope this helps!

Add values from multiple columns in pivot table

I have a created a pivot table from an Excel spreadsheet which has many columns and many rows. Here is my requirement.
The Pivot Table has
Row Labels --> Individual Names
Column Labels --> Types of Products
Now I have 4 regions like AP, EMEA, CALA, & US in the Excel spreadsheet.
I need to get the value of = Sum of (AP + EMEA + CALA + AP), for each type of Product for the respective individual name.
For example,
Clarke would have sold Type 1 Product, 4 Nos in AP, 10 in EMEA, 4 in CALA, 7 in US
would have sold Type 2 product, 12 Nos in AP, 16 in EMEA, 8 in CALA, 5 in US
I need pivot table, which looks like
Type 1 Type 2 Type 3 Type 4
Clarke 25 41 0 0
Marsh 11 20 12 6
How do I get this?
Like this:
Grand Total for rows and columns is optional and other Types/Names etc may be added to suit into the source data without other configuration changes except (i) possibly the range will need extending (PivotTable Tools > Data - Change Data Source) and (ii) the PT will need to be refreshed (right-click on the data area and left-click Refresh).

TSQL - reorganize fields

Suppose i have the following data in a table:
ID Type Preis
1 1 10
1 2 20
1 3 30
and now i want it reorganized like this:
ID Preis_A Preis_B Preis_C
1 10 20 30
How can i do this in t-sql?
Which version of SQL Server are you using? Search here for Cross-Tab and/or Pivot.