Pandas Pivot Table Repeating 1 Level of Index with No Rows - pandas

So I'm generating a pivot table using pandas. I'm using index Location Name, Customer ID and Customer Name (SortName).
When I pivot it's returning a row for each Customer Name per CustomerID. You can see that all the customer names are zero except for the customer name actually associated with the ID. When I filter there isn't any data for the customerID with the other names. Why is it doing this? I really only want it to group for unique combinations of the 3 indexes that have data.

Related

SQL Query to get distinct results from one table and match those results to the another column in the table

I have an SQL table with product names in one column and a product category in another column. So each product belongs to a specific category. I am trying to figure out an sql command that will return distinct values from the product name column but will also display the product category each product belongs to. Any ideas?
You can try like this,
SELECT Distinct([ProductName]),[ProductCategeory] FROM [DB].[dbo].[tblProduct]

Sort Order Column Dynamically Created From Another Column (Postgres)

I am trying to output a sort order column on a Postgres select query to give each row an order integer based the values of it's columns - with this sort order derived from another column. Something like this:
select column_1_name, column_2_name, column_3_name, [a formula] AS sort_order
from table_name
The data in the column sort_order_column would be a comma separated list of column names in the order I would like them to show in the sort_order (starting from 1 again for each unique item in the item column).
In the example below, I want to sort the 'car' items first by column_2_name, then by column_3_name. - this means the green car comes before the red car.
For boats, these are sorted first by column_1_name, then column_3_name, then finally by column_2_name. This means the 10m boat comes before the 20m boat.
Data:
Desired output
Thanks in advance.

Assign an ID Value for Every Set of Duplicates

How can i generate an ID value for every set of duplicate records as seen in the second table with ID column? In other words, how can I let the first table to look like the second table using SQL query?
Assume that first name and last name in the first table can appear in duplicates.
Each first name and last name can have one or many purchase yr and cost.
The given image is just a sample. Total records in table 1 can reach thousands.
I'm using Oracle SQL.
Note: I'm working with one table only that is the first one. The second table is what I want.
You can use the DENSE_RANK analytic function to assign ID's as below:
EDIT:
Simplified query to generate ID's.
SELECT
DENSE_RANK() OVER (ORDER BY First_Name, Last_Name) ID,
t.*
FROM Table1 t;
Reference:
DENSE_RANK on Oracle Database SQL Reference

How to join a column from another table to a new table you wish to display

I am currently working on a database where I am trying to find all the transactional type tables (where the table name does not start with _Result or _ History) in the database and then display how many times each table is used in a calculation for the database model.
The purpose of finding out this is to determine the most important tables in the calculation, so that these certain tables will have a priority when updating statistics.
There are currently two tables I am working with.
1) The first table called tmpCalcSources shows the name for all the source tables in the database (Column called 'Source') along with the Calculation ID (Column called 'CalculationID') associated with it
2) The second table called tmpCalcSourceRows shows the source table name (Column called 'TableName') as well as the amount of rows associated with each source table (Column called 'RowNum')
I currently have this query:
SELECT Source,
COUNT(CalculationID) AS NumberOfUses
FROM tmpCalcSources
WHERE Source not like '%_Result%'
GROUP BY Source
ORDER BY NumberOfUses DESC;
The above query provides me with the following table:
plPeriods 292
plMeasures 10
Time 43
etc...
I am now trying to add one more thing to the above table. I want it to also show the number of rows contained in each table (so a another column). I would like to take the column 'RowNum' from tmpCalcSourceRows table and be able to display that in the table shown above.
Is this what you're looking for:
select
Source
, NumberOfUses
, numberofrows
from (
SELECT Source,COUNT(CalculationID) AS NumberOfUses
FROM tmpCalcSources
WHERE Source not like '%_Result%'
GROUP BY Source
) tableUses
join numRowsTable on numRowsTable.TableName=tableUses.Source
ORDER BY NumberOfUses DESC;

Append two rows into one (that differs on one colum)

I am trying to create a query that returns a single row for each unique ID in my oracle table.
The problem is that i have one column, Description, that isnt unique in each row (Description-column is the only coulmn that can differ for each ID row btw). This is what my table looks like:
ID Description Customer
==================================================
5119450733 Cost GOW_1
5119450733 Price GOW_1
1543512377 Cost GOW_2
Is there a way to query the table so that i append the results from Description so that i can have unique id rows? for example like this:
ID Description Customer
==================================================
5119450733 Cost,Price GOW_1
1543512377 Cost GOW_2
Use LISTAGG function if you are using Oracle 11g Release 2.
SELECT Id,
listagg(Description,',') WITHIN GROUP(ORDER BY description) AS Description,
Customer
FROM <table_name>
GROUP BY id, customer;
Refer the below link to know more about String Aggregation Techniques on different versions.