Convert Column to row (pivot) [duplicate] - sql

This question already has answers here:
Simple way to transpose columns and rows in SQL?
(9 answers)
Closed 6 years ago.
i need to make the data in the column to be column head
this is the current result from my query .. :
select employee_id,reimbursement_type,SUM(amount) as [total amount],reimbursement_status from md_reimbursement
group by employee_id,reimbursement_type,reimbursement_status
and i want it to be like this :
*just ignore the Status field
so the data reimbursement_type to become a column head and it SUM each amount.
i already tried using pivot but didnt get what i expected.
thx

Prepared a sample according to your requirement. As you have mentioned to ignore Status, I am not considering it into the query.
select * from
(
select employee_id,reimbursement_type,amount from md_reimbursement
)src
pivot
(
sum(amount) for reimbursement_type in ([Biaya Dinas],[Other],[Transport],[Uang Makan])
)pvt

Related

Datetiime column needs to show only the time [duplicate]

This question already has answers here:
How to get Time from DateTime format in SQL?
(19 answers)
Closed 5 years ago.
I have a datetime column that has data as this:
Appt_DateTime (datetime, not null)`
12/30/1899 7:50:00PM
I want to display only the time in this case the 7:50:pm. it can be with or without the seconds, better without them.
How can I do this in a select?
You may try this by using something like
Select cast(<your column name> as time) [time] from <your table>

Mean vs Median that is the? [duplicate]

This question already has answers here:
Function to Calculate Median in SQL Server
(37 answers)
Closed 5 years ago.
I have a number of select statements which calculate fields such as sums, division and also averages. However I am now needing to include a median based on the query, as well as a mean(avg). The table contains 50,000 rows in the MSSQL database, so in the same query return I need to have the results return for each line.
I know there is not a Median formula in SQl, well not that I am aware of. I am using SQL 2012. So if anyone has an idea, I would welcome your thoughts, as I can not be the only person to come up against this.
Example would be something like this
Select
Round (AVG ([LENGTH]),2) as Length_X,
Round (Median ([LENGTH]),2) as Length_median,
I understand Median is not an accepted sql statement, so just for demo purposes to get my point across
Cheers
If you have numeric data, then there is a "median" function. It is just spelled differently:
select percentile_cont(0.5) within group (order by ??) over ()
or
select percentile_disc(0.5) within group (order by ??) over ()
(The difference between the two is subtle and probably doesn't matter for most purposes.)
It is, unfortunately, not an aggregation function, so it is often used in a subquery.

Pivot data in BigQuery SQL? [duplicate]

This question already has answers here:
How to Pivot table in BigQuery
(7 answers)
Closed 2 years ago.
I am working with BigQuery. I have two tables:
organisations:
org_code STRING
name STRING
spending:
org STRING
month DATE
quantity INTEGER
code STRING
And then quite a complicated query to get results by each organisation, by month:
SELECT
organisations.org_code AS org,
num.month AS month,
(num.quantity / denom.quantity) AS ratio_quantity
FROM (
SELECT
org_code, name
FROM
[mytable.organisations]) AS organisations
LEFT OUTER JOIN EACH (
SELECT
org,
month,
SUM(quantity) AS quantity
FROM
[mytable.spending]
GROUP BY
org,
month) AS denom
ON
denom.org = organisations.org_code
LEFT OUTER JOIN EACH (
SELECT
org,
month,
SUM(quantity) AS quantity
FROM
[hscic.spending]
WHERE
code LIKE 'XXXX%'
GROUP BY
org,
month) AS num
ON
denom.month = num.month
AND denom.org = num.org
ORDER BY org, month
My final results look like this, with a row per org/month combination:
org,month,ratio_quantity
A81001,2015-10-01 00:00:00 UTC,28
A82001,2015-11-01 00:00:00 UTC,43
A82002,2015-10-01 00:00:00 UTC,16
Now I would like to pivot the results to look like this, with one row per month, and one column per organisation:
month,items.A81001,items.A82002...
2015-10-01 00:00:00 UTC,28,16
2015-11-01 00:00:00 UTC,43,...
Is this possible in the same BigQuery call? Or should I create a new table and pivot it from there? Or should I just do the reshaping in Python?
UPDATE: There are about 500,000 results, fyi.
Q. Is this possible in the same BigQuery call? Or should I create a new
table and pivot it from there?
In general, you can use that “complicated query” as a subquery for extra logic to be applied to your current result.
So, it is definitely doable. But code can quickly become un-manageable or hard to manage – so you can consider writing this result into new table and then pivot it from there
If you stuck with direction of doing pivot (the way you described in your question) - check below link to see detailed intro on how you can implement pivot within BigQuery.
How to scale Pivoting in BigQuery?
Please note – there is a limitation of 10K columns per table - so you are limited with 10K organizations.
You can also see below as simplified examples (if above one is too complex/verbose):
How to transpose rows to columns with large amount of the data in BigQuery/SQL?
How to create dummy variable columns for thousands of categories in Google BigQuery?
Pivot Repeated fields in BigQuery
Q. Or should I just do the reshaping in Python?
If above will not work for you – pivoting on client is always an option but now you should consider client side limitations
Hope this helped!

Logic of SQL Query kindly help me out [duplicate]

This question already has answers here:
How to count the number of occurrences of a character in an Oracle varchar value?
(9 answers)
Counting the number of occurrences of a character in Oracle SQL [closed]
(2 answers)
Closed 8 years ago.
If a column contains a certain value (for example, SEEMA), what method might one to use to determine how many times the letter E occurs in that value?
regexp_count should do the trick:
SELECT col, REGEXP_COUNT(col, 'E')
FROM some_table
WHERE col LIKE '%SEEMA%'

SQL - Rounding off numbers [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Accuracy in rounding numbers
I have the following requirement-
Get the A_MINUTES column value from TableA for all rows
Sum the A_MINUTES.
Convert the summed minutes values to hours - divide by 60
Round off the final hours value to 2 decimal places.
This needs to be written in SQL. Do you think the following query will have any rounding errors?
SELECT ROUND ( (SUM(A_MINUTES)/60.0) , 2) FROM TABLEA
If point 3.Sum the hours values to be considered you are missing one SUM function.
SELECT ROUND ( SUM(SUM(A_MINUTES)/60.0) , 2) FROM TABLEA