Search code examples
sqloracle-sqldevelopertableau-api

How do I add a column that marks one of each value?


I'm joining two tables in SQL. I currently have the SQL as:

SELECT table1.ProjectName AS "Project Name",
  table1.ProjectCost AS "Project Cost",
  table2.ExpenseName AS "Expense Name",
  table2.ExpenseCost AS "Expense Cost"
  FROM TABLE1 table1
  INNER JOIN TABLE2 table2
  ON table1.ProjectName = table2.ProjectName;

The result looks like:

Project Name    | Project Cost  | Expense Name  | Expense Cost
------------------------------------------------------------
Project 1   | 123456        | Labor     | 12365  
Project 1   | 123456        | Rent      | 120000  
Project 2   | 8421              | (null)    | (null)  
Project 3   | 987654        | Paper     | 1023  
Project 3   | 987654        | Pens      | 546  

I want to add a row that marks one of each Project Name so that I can filter over it in Tableau and sum the projects costs.

EX:

Project Name    | Project Cost  | Expense Name  | Expense Cost  | Unique Value
----------------------------------------------------------------------------
Project 1   | 123456        | Labor     | 12365     | Y  
Project 1   | 123456        | Rent      | 12000     | N  
Project 2   | 8421      | (null)    | (null)        | Y  
Project 3   | 987654        | Paper     | 1023      | Y  
Project 3   | 987654        | Pens      | 546       | N  
Project 3   | 987654        | Party     | 9856      | N

Solution

  • I suposse you can use the lag function, I actually asked this not a long time ago, I can share my question, maybe it helps you. But instead of selecting the value you could create a table temporal table and populate that column based on the lag function:

    SELECT Only one value per ID - SQL Server