Using SQL generate pivot table - sql

i'm trying to learn using SQL to generate pivot table. But no matter how i try i keep getting ORA-00936: missing expression error from oracle.
Here is my query:
SELECT * FROM (SELECT HOS_PAY_ID, AMOUNT FROM HOSPITAL_PAYMENT)
PIVOT (SUM (AMOUNT) FOR AMOUNT IN ([10000],[8000],[7000],[9000],[11000],[13000]) AS TEST
ORDER BY HOS_PAY_ID;
and this is my data:
Thank You.

Try this
SELECT * FROM
(
SELECT HOS_PAY_ID, AMOUNT
FROM HOSPITAL_PAYMENT
)
PIVOT (
SUM (AMOUNT) FOR AMOUNT IN (10000,8000,7000,9000,11000,13000)
)
ORDER BY HOS_PAY_ID;

Related

How to select all columns with sum function

Assuming there is a table with 100 columns, how can I select all columns with a sum without having to type out all the columns?
For example something like this:
select *, sum(price) as sales
from table
group by *
order by date
try this
select table.* , t.sales from table
inner join (select id, sum(price) as sales from table group by id ) t
on table.id=t.id
order by date
But in general it is not recommended to use an stare in a select statement,
for example dont use * in table-valued function

Transpose rows/columns in SQL result set

I have found many answers to this question, but all of them leave me stymied as to actually making it work. Apparently, I am just being dense today. Can someone just give me the code to make this work?
My T-SQL query returns the following rows:
Before
I need to transpose the rows and columns:
Like this
unpivot —> pivot
select p.*
from
(
--My T-SQL query returns the following rows:
select *
from
(
values
('A',1,2,3,4,5,6,7,8),('B',10,20,30,40,50,60,70,80),
('C',100,200,300,400,500,600,700,800), ('I',11,22,33,0,0,0,0,88)
) as t(product_code,week_1,week_2,week_3,week_4,week_5,week_6,week_7,week_8)
) as s
unpivot
(
thevalue for weekno in (week_1,week_2,week_3,week_4,week_5,week_6,week_7,week_8)
) as u
pivot
(
max(thevalue) for product_code in (A,B,C,D,E,F,G,H,I,J)
) as p;

Unable to use exact_count_distinct BigQuery aggregate function

I am getting Resources exceeded during query execution error when I execute following query:
SELECT EXACT_COUNT_DISTINCT( id ) FROM [bigquery-public-data:github_repos.contents]
I used EXACT_COUNT_DISTINCT aggregate function as COUNT([DISTINCT]) function gives only a statistical approximation.
try below
for BigQuery Legacy SQL
SELECT COUNT(1) AS cnt FROM (
SELECT id
FROM [bigquery-public-data:github_repos.contents]
GROUP BY id
)
for BigQuery Standard SQL
SELECT COUNT(DISTINCT id) as cnt
FROM `bigquery-public-data.github_repos.contents`

Convert query PIVOT from Oracle to SQL Server error

Currently I am converting the an Oracle trigger to SQL Server, but I run into a problem.
My query in Oracle is like this
SELECT
CUSTOMER_ID, NVL(MATRIX1,0) MATRIX1, NVL(MATRIX2,0) MATRIX2
FROM
(SELECT DISTINCT
CUSTOMER_ID, SCORE
FROM STABLE) A
PIVOT (SUM(1) AS SCORE_MATRIX FOR (SCORE) IN (
1 AS IND_1, 2 AS IND_2));
My T-SQL query in SQL Server looks like this
SELECT
CUSTOMER_ID, ISNULL(MATRIX1,0) AS MATRIX1, ISNULL(MATRIX2,0) AS MATRIX2
FROM
(SELECT DISTINCT
CUSTOMER_ID, SCORE
FROM STABLE) A
PIVOT (SUM(1) AS SCORE_MATRIX FOR (SCORE) IN (
1 AS IND_1, 2 AS IND_2)) PVT;
However I get an error:
Msg 102, Level 15, State 1, Line 18
Incorrect syntax near '1'.
I could not figure out why for this error, so could anyone help identify? Thank you very much!
The error comes from the SUM(1).
Since 1 is not a column in the source query A.
Also, on sql-server those alias names within the PIVOT declaration aren't allowed like on Oracle.
So with a few fixes the PIVOT will work on sql-server.
SELECT
CUSTOMER_ID, ISNULL([1],0) AS MATRIX1, ISNULL([2],0) AS MATRIX2
FROM
(SELECT DISTINCT CUSTOMER_ID, SCORE FROM STABLE) AS A
PIVOT (
COUNT(SCORE) FOR SCORE IN ([1], [2])
) AS PVT;
Notes:
SUM(1) was replaced by COUNT(SCORE).
And that COUNT(SCORE) will give either 1 or NULL in that PIVOT for SCORE.
So this will also result in a matrix filled with 0 a 1.

Pivot Table select all the data in my column & Extra Sum column

i got a question here.
I'm trying to select all the date inside my database (1-31), instead of using 1,[2],[3]... manually way is there any easier way to select them all?
I tried using some dumb way like:
SUM(TOTAL) FOR DATE IN (*)
SUM(TOTAL) FOR DATE IN ([*])
Here is my query:
SELECT * FROM
(
SELECT BRANCH.NAME,SALES.TOTAL AS TOTAL,TIME.DATE
FROM SALES
INNER JOIN BRANCH
ON SALES.BRANCH_ID=BRANCH.BRANCH_ID
INNER JOIN TIME
ON SALES.TIME_ID=TIME.TIME_ID
WHERE TIME.MONTH='APR'
)AS TABLE1
PIVOT (
SUM(TOTAL) FOR DATE IN ([1],[2],[3],[5],[6],[7],[8],[9],[10])
) PIVOTTABLE
Also is there possible to create an extra grand total column by SQL like excel do ?
SAMPLE: