Pivot Table In BigQuery Using Standard SQL - google-bigquery

I have a set of data (referenced the attached Excel sheet) that is unpivoted, which I am trying to pivot and convert the rows to columns.
In this case, the yellow headers are duplicated data. The green headers are what I want to convert from rows to columns, which will remove the duplication and thus pivot the data.
The top of the Excel sheet is what I have currently, the bottom is what I am wanting to get to.
I am using BigQuery standard SQL.
Any help is appreciated.
Excel Screenshot
Here is a short sample of what I have.
catalog_number
manufacturer
region
region_price
region_catalog_number
123
nike
north
10
123-n
123
nike
south
8
123-s
Here is a sample of what I want.
catalog_number
manufacturer
region_north
region_north_price
region_north_catalog_number
region_south
region_south_price
region_south_catalog_number
123
nike
north
10
123-n
south
8
123-s

Consider below simple approach
select *
from your_table
pivot (
any_value(region) region,
any_value(region_price) region_price,
any_value(region_catalog_number) region_catalog_number
for region in ('north', 'south')
)
if applied to sample data in your question - output is

Related

Issue with PIVOTING two columns at the same time and one includes a datetime column in SQL Server

I have a table used for attendance tracking, so I can track the arrival time of a person and the date in which they have arrived. I'm now trying to get a report based on that data and have created a view for it below. I have used the PIVOT function to split up the dates and it works without the column highlighted in bold below. The only issue is that I'm trying to PIVOT on a second column called DeviceFirstScannedTime which returns a datetime value, once that gets added the original PIVOT query doesn't group correctly.
Using SQL Server 2012
Current Query for PIVOTING
SELECT *
FROM (SELECT [Code] AS [CB Code], [Lastname], [Firstname], CompanyName AS [Company], DeviceScannedDateOnly, [Code], **DeviceFirstScannedTime**
FROM [dbo].[vLDN23_DailyReportForPivot]) AS SourceTable
PIVOT (Count([Code]) FOR DeviceScannedDateOnly IN ([06/02/2023], [07/02/2023])) AS PivotTable;
What it shows without the DeviceFirstScannedTime column added, shows it will pivot on DeviceScannedDateOnly and Split the dates correctly (it groups the two scans in the original source table and pivots them);
CB Code
Lastname
Firstname
Company
06/02/23
07/02/23
WSSPS24HX6
Smith
Bob
Stan
1
1
With the datetime column added I get the data separated;
CB Code
Lastname
Firstname
Company
DeviceFirstScannedTime
06/02/23
07/02/23
WSSPS24HX6
Smith
Bob
Stan
2023-02-06 10:12:0060
1
0
WSSPS24HX6
Smith
Bob
Stan
2023-02-07 13:87:0000
0
1
Data is now split which isn't ideal for reporting..
What I'm trying to do is this
Firstname
Company
06 Scanned time
06/02/23
07 Scanned Time
07/02/23
Bob
Stan
2023-02-06 10:12:0060
1
2023-02-07 13:87:0000
1
I've removed the first couple of columns to make it more readable but essentially I'm trying to PIVOT on two columns and split the table accordingly all into one line.
Is that possible with a datatime column?

PostgreSQL dynamic query comparing row from table to column name of another table

I work with QGIS and PostgreSQL with PostGIS. I need help with dynamic queries for PostgreSQL.
Information is structured in tables that contain votes for parties, and other types of information like geographic area or election date.
I need to work with “tidy” data in plotly for pie charts. The desired end table must have one row per observation.
So for example for the given table “Election Results”:
Country
PartyA
PartyB
PartyC
Argentina
100
10
20
Uruguay
3
5
1
Chile
40
200
50
Values for columns to be treated like parties, are stored in table “Parties”:
Party
PartyA
PartyB
PartyC
PartyD
PartyE
I need to separate one observation of voting results per row as follows (“Ending Table”):
Country
Party
Votes
Argentina
PartyA
100
Argentina
PartyB
10
Argentina
PartyC
20
Uruguay
PartyA
3
Uruguay
PartyB
5
Uruguay
PartyC
1
Chile
PartyA
40
Chile
PartyB
200
Chile
PartyC
50
The query should work with any number of parties stored in the “Parties” table. The “parties” table could include some rows not present in the election results table, but all parties in the election results table will exist in the “parties” table.
I understand it should be done iterating over the columns in the “Election results” table. If the name of a column matches the value of a row in “party table”, then we “untidy” data as in the “ending table”.
You can "iterate" over the columns using Postgres's JSON functions:
Something like this:
select er."Country",
pv.*
from election_results er
cross join jsonb_each_text(to_jsonb(er) - 'Country') as pv(party, votes)
join parties p on p.party = pv.party
;
Online example

DAX for Grouping

I have following data set as data Model.
Country City AssetCount
USA Newyork 50
USA Washington 40
USA California 30
India Bangalore 100
India Delhi 50
India Bombay 30
I want to show one row showing sum of Assetcount at country level & city level on the same row.
There are two slicers for slicing City & Country as below:
USA Newyork
India Washington
California
Bangalore
Delhi
Bombay
So when I select country as India it should show sum of Asset-Count at country(India) level.
In the same way when I select City as Delhi it should show Asset-Count at City(Delhi) level.
India Delhi
180 50
Is it possible using PowerPivot using DAX?
Related content from their question on MSDN
Actually your solution is not working. I have created the hierarchy as Country-->City & kept that in Rows. So when I select a particular Country & City it showing like this:
Row Labels AssetCount
USA 40
Washington 40
Grand Total 40
But I want
USA Washington
120 40
or may be like
USA 120
Washington 40
I have tried some aggregate functions like below:
=SUMX(VALUES(Query[City]),CALCULATE(SUM(Query[AssetCount])))
=CALCULATE(SUM(Query[AssetCount]),SUMMARIZE('Query',Query[City]))
Here Query is table for Data Model & City can be replaced by Country.
but not working.
So showing such counts on same row is possible or not?
Sounds like you are just getting started with Power Pivot. You might browse through the links on this page for more help.
I took the data you provided and pasted it into Excel.
Selected the data and clicked Add to Data Model and checked the box for My Data Has Headers.
I made sure the AssetCount Column had a data type of whole number. Then clicked the Pivot Table button and created a pivot table on my existing spreadsheet.
I put AssetCount in the values and made sure it was set to Sum in the Field Value Settings.
I selected my pivot table and then went to the Analyze tab under PivotTable Tools and clicked the Insert Slicer button.
I selected both Country and City as slicers.
This gives your desired result.
If you want the two numbers in a row, that's pretty straightforward. Keep in mind, that all those slicers do is putting filters on the pivot table.
Therefore to get your city result, you could use either an implicit measure or explicit measure that simply sums up AssetCount.
For the country result, you'd wish to overload the city filter like this:
=calculate(SUM(Query[AssetCount]),ALL(Query[City]))
If you also need the country and city names there, it gets a bit tricky.

How can I merge rows (of a particular column) having the same value in SQL Server Reports RDL?

I'm new to working with SQL Server 2005 Reporting Servives using RDLs in BIDS.
I need to modify an existing report so that I can merge cells in adjacent rows which would have the same value for that particular column.
Ex: consider this is the table returned from the stored procedure used by the reports RDL.
_________________________________________________
Id SubCategory Field1 Field2 Total
_________________________________________________
1 a Bob US 17
1 b John UK 17
2 a Mary AUS 12
3 d Ram IND 19
4 b Alex UK 09
4 c Abby FR 09
5 e Tim IT 03
_________________________________________________
Table Example - Couldn' Format Text :( Image here : http_://i.stack.imgur.com/gWEH5.png_
What I need to do is I want the cells merged into one where two adjacent rows in the same column have the same value.
Like Id 1 is repeated twice, so the cells for these must be merged. (Also 4)
Similarly for the last column Total for cells with Ids 1 and 4 must be merged.
The RDL has "TextBox" for columns, I saw some other questions in this forum but were related to Tablix or Matrix, so I thought it would be better if I mentioned it.
I need this merging to be done in the RDL, and this should also be present when exported to Excel.
Hoping someone will be able to help soon.
Change the SQL query to use a group by on ID.
in reporting services drag Id On Row group Of Tablix Upaer than Detail Group
and drag other Fields(except Total) Besid ID and befor vertical dash line In the table.
and drag Total to first cell afte vertical dash line

Rows to Dynamic columns in Access

I need a setup in Access where some rows in a table are converted to columns...for example, lets say I have this table:
Team Employee DaysWorked
Sales John 23
Sales Mark 3
Sales James 5
And then through the use of a query/form/something else, I would like the following display:
Team John Mark James
Sales 23 3 5
This conversion of rows to columns would have to be dynamic as a team could have any number of Employees and the Employees could change etc. Could anyone please guide me on the best way to achieve this?
You want to create a CrossTab query. Here's the SQL that you can use.
TRANSFORM SUM(YourTable.DaysWorked) AS DaysWorked
SELECT YourTable.Team
FROM YourTable
GROUP BY YourTable.Team
PIVOT YourTable.Employee
Of course the output is slightly different in that the columns are in alphabetical order.
Team James John Mark
Sales 5 23 3
For more detail see Make summary data easier to read by using a crosstab query at office.microsoft.com