This question already has answers here:
Simple way to transpose columns and rows in SQL?
(9 answers)
Pivot Dynamic Columns, no Aggregation
(1 answer)
Closed 7 years ago.
I have a table Dev with the data below
YYYMMDD Atest BTest CTest
20150525 100 200 300
20150526 110 210 310
20150527 120 220 320
I need output like below
xyz 20150525 201050526 20150527
Atest 100 110 120
BTest 200 210 220
CTest 300 310 320
How can i achieve above result set. My table Dev will grow and i need the result set table to build columns dynamically and display the data as required.
Any help is appreciated. If you suggest pivot, may i know what field should i use for aggregation and how to use it. Thanks.
First you will need to unpivot your table and after that pivot again. This is the key idea:
select * from TableName
unpivot(v for xyz in([Atest],[Btest],[Ctest]))u
pivot(max(v) for yyyymmdd in([20150525],[20150526],[20150527]))p
Fiddle http://sqlfiddle.com/#!3/a675c/1
As for dynamic sql, you can find many example of how you can use STUFF function to concatenate distinct dates in one string and construct dynamic query. For instance T-SQL dynamic pivot
Related
First of I've got a table like this:
vID
bID
date
type
value
1
100
22.01.2021
o
250.00
1
110
25.01.2021
c
100.00
2
120
13.02.2021
o
400.00
3
130
20.02.2021
o
475.00
3
140
11.03.2022
c
75.00
1
150
15.03.2022
o
560.00
To show which values were ordered(o) and charged(c) per Month, I have to like 'generate' columns for each month both ordered and charged in a MSSQL SELECT query.
Here is an example table of what I want to get:
vID
JAN2021O
JAN2021C
FEB2021O
FEB2021C
…
MAR2022O
MAR2022C
1
250.00
100.00
560.00
2
400.00
3
475.00
75.00
I need a posibility to join it in a SQL SELECT in addition to some other columns I already have.
Does anyone has an idea and could help me please?
The SQL language has a very strict requirement to know the number of columns in the results and the type of each column at query compile time, before looking at any data in the tables. This applies even to SELECT * and PIVOT queries, where the columns are still determined at query compile time via the table definition (not data) or SQL statement.
Therefore, what you want to do is only possible in a single query if you want to show a specific, known number of months from a base date. In that case, you can accomplish this by specifying each column in the SQL and using date math with conditional aggregation to figure the value for each of the months from your starting point. The PIVOT keyword can help reduce the code, but you're still specifying every column by hand, and the query will still be far from trivial.
If you do not have a specific, known number of months to evaluate, you must do this over several steps:
Run a query to find out how many months you have.
Use the result from step 1 to dynamically construct a new statement
Run the statement constructed in step 2.
There is no other way.
Even then, this kind of pivot is usually better handled in the client code or reporting tool (at the presentation level) than via SQL itself.
It's not as likely to come up for this specific query, but you should also be aware there are certain security issues that can be raised from this kind of dynamic SQL, because some of the normal mechanisms to protect against injection issues aren't available (you can't parameterize the names of the source columns, which are dependent on data that might be user-generated) as you build the new query in step 2.
This question already has answers here:
Oracle SQL pivot query
(4 answers)
Closed 3 years ago.
select * from (select REGS_CRSE_TITLE as CRSE_TITLE, REGS_BILL_HR as BILL_HOURS, FCR_TO_DATE, FCR_TUIT_REFUND
from REGS, FCR
where REGS_ID = 123456
and REGS_CODE = FCR_CODE
and FCR_TERM_CODE = 2019
and FCR_RSTS_CODE in ('CD','CW')
order by REGS_CRSE_TITLE
)
pivot
(
max (FCR_TO_DATE)
for FCR_TUIT_REFUND IN (100, 50)
);
Which produces a result similar to the following:
CRSE_TITLE BILL_HOURS 100 50
English II 3 28-Aug-19 9-Sep-19
Sculpture 1 3 28-Aug-19 9-Sep-19
Intro to Business 3 28-Aug-19 9-Sep-19
Graphic Design 3 28-Aug-19 9-Sep-19
Senior Project 0.5 28-Aug-19 9-Sep-19
Web Application Dev 3 28-Aug-19 9-Sep-19
I need to rename the columns that get created by the pivot to something else. So the 100 and 50 column headers would be renamed to something more meaningful. I can't seem to do it with a simple 'AS' like the first two columns. Also the 'Order By' does not seem to work in this context either, but that is not as important to me as getting the columns renamed. Any help would be greatly appreciated!
You could use AS and ORDER BY should be the last clause:
...
pivot
(
max (FCR_TO_DATE)
for FCR_TUIT_REFUND IN (100 AS col1, 50 AS col2)
)
order by CRSE_TITLE
;
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;
Currently I have arround 250 clients with their 5 years datas and the tables structure were splited up based on their years (Eg),A client named as XX.
T00_XX_2011,T00_XX_2012,T00_XX_2013,T00_XX_2014 each table contains 220 column with more or less 10 millions records in which 12 column already has indexes
The issue was for a single select query it get arround 5 to 10 min Can anyone help to tweek the performance
This question already has answers here:
Efficiently convert rows to columns in sql server
(5 answers)
Closed 6 years ago.
I have this table:
Value | Name
300 | moshe
400 | yoni
500 | niv
And i would like to convert it into this:
nameColumn: moshe yoni niv
value: 300 400 500
The value is float type and name is nchar(20).
anyone?
thanks
Most databases have a PIVOT relational operator (link for SQL Server) to turn the unique values of a specified column from multiple rows into multiple column values in the output (cross-tab), effectively rotating a table.