TSQL - reorganize fields - tsql

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.

Related

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")

Postgres funnel analysis (time spent)

I have a table like this:
id visited_time page visitor_id
1 2019-04-29T10:44:53.847014+02:00 1 1
2 2019-04-29T10:46:53.174894+02:00 1 3
3 2019-04-29T10:49:44.000390+02:00 2 1
18 2019-04-29T10:52:46.574140+02:00 2 3
19 2019-04-29T10:52:58.158146+02:00 3 1
20 2019-04-29T10:53:27.402038+02:00 1 9
25 2019-04-29T10:55:18.275441+02:00 2 9
54 2019-04-29T11:10:01.818343+02:00 1 13
72 2019-04-29T11:40:28.056813+02:00 2 13
A visitor will also be going from page 1 to 2 to 3 and so forth (but can dropout along the way). I want to find the average time spent on each page. Logically this is the difference between the a unique visitor_id visited page 1 and then page 2 etc.
Is there a smart way to do this in postgres?
Here you go:
SELECT
page,
avg(visited_time_next - visited_time)
FROM
(
SELECT
page,
visited_time,
-- the time of the next page view by a certain visitor...
lead(visited_time) OVER (PARTITION BY visitor_id ORDER BY visited_time) AS visited_time_next
FROM visits_so_56097366
) AS tmp
GROUP BY page
ORDER BY page;
Online example: https://dbfiddle.uk/?rdbms=postgres_11&fiddle=e64dd8862350b9357d9a4384937868c9
Please also make sure that you have an index over visitor_id and visited_time, otherwise you'll end up with very expensive sorts for larger number of intermediate rows:

"Inserting" Records into Fields from a Database Feed

So the background to this is I'm trying to create a survival curve based on a database feed from the directions here.
What I have so far is three calculated fields per below. Patient ID is not a calculated field or necessary for the survival analysis, but I believe it could be useful for this question. For reference, there are about 20,000 unique patients.
Patient ID | Time | Censor | Group
Id1 3 0 1
Id2 8 0 2
Id3 1 1 1
Id4 3 1 1
Id5 11 0 1
Id5 7 1 2
What I would like to do is insert two records (one for each group) such:
Patient ID | Time | Censor | Group | Link
0 1
0 2
Id1 3 0 1 link
Id2 8 0 2 link
Id3 1 1 1 link
Id4 3 1 1 link
Id5 11 0 1 link
Id5 7 1 2 link
I unsuccessfully tried to create an excel spreadsheet with these base attributes to union with the columns, however, an excel spreadsheet does not appear to be able to union with a database.
My next idea is to find 2 of the 20,000 patients where I can create a calculated field along these lines (not sure this is feasible in Tableau, please excuse my syntax):
IF [Patient ID] = Id3 THEN [TIME] = 0 AND [CENSOR] IS NULL
END
and then a [Link] calculated formula:
IF [Patient ID] = Id3 THEN NULL
ELSE "link"
END
Any help would be appreciated. Would like to avoid inserting these records in the database.
The best / easiest option is to use an outer join to your excel workbook -- this is a new feature in Tableau version 10 (Cross database joins)
Then, once the dataset is combined, you can build business logic through a filter or calculated field based on the absence or presence of the Excel data.
http://www.tableau.com/about/blog/2016/7/integrate-your-data-cross-database-joins-56724

Crystal Crosstab - access summarized values

I am trying to access the summarized fields in a crosstab in order to determine which is the greatest value in a row.
My data displayed is as follows:
Jan Feb Mar Quarter
clerk 1 shoes 0 3 1 4
clerk 1 pants 5 10 10 25
What I need to display on the report is that the major item sold by Clerk 1 is pants.
I am table to do this on a monthly basis but not summary level. Any ideas?
Thanks, Holdfast
You need to insert Embeeded Summary at grand total level and write maximum or minimum formula to retrive, but one issue here is when you insert embeeded summary then you will get extra row at each level of the cross tab.
Note: This is possible in CR 2008 and I have tested in CR 2008

Order By Problem T-Sql

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.