How can I change row information in a Query? - sql

I'm using Postgres and I'd like to know how to change row information within a query, Let's say I have a column called Numbers and it's got rows going 1,2,3,4,5 how could I edit the information in those rows? let's say I want the query to display 1,1,1,1,5 how would I write in a query that each row should be changed to 1 unless it's 5? Again it's only to change it within the Query, I'm not trying to do an UPDATE I realize how newbish this is on my part but I couldn't find this on google.

SELECT
CASE WHEN Numbers <> 5 THEN 1 ELSE Numbers END
FROM table
See 9.12. Conditional Expressions

Related

Bigquery - remove duplicates of certain columns, but not all

I have two tables I am left joining together. The first tables has transnational level detail, causing the key I join to the second table to duplicate. When I left join the second table, the measure "company_spend" is highly inflated.
I need a way to keep only a single value of the duplicated data, and my thought was to run a distinct function on only those columns, but I am not seeing that Bigquery supports distinct functions on only a few columns, but not all.
SELECT UPPER(cwnextt.Current_Contract_Number) AS Current_Contract_Number,
UPPER(cwnextt.Replacement_Contract_Number) AS Replacement_Contract_Number,
UPPER(cwnextt.Current_Contract_Name) AS Current_Contract_Name,
UPPER(cwnextt.Supplier_Top_Parent_Entity_Code) AS Supplier_Top_Parent_Entity_Code,
UPPER(cwnextt.Supplier_Top_Parent_Name) AS Supplier_Top_Parent_Name,
UPPER(cwnextt.company_Entity_Code) AS company_Entity_Code,
UPPER(cwnextt.Facility_Name) AS Facility_Name,
smart.company_Spend AS companySpend
FROM `test_etl_field.contracts_with_member_entity_codes_test_view_2` cwnextt
--this table is what is causing the below table to duplicate,
--but I need all of this data AS well in its current format.
LEFT JOIN `test.trans_analysis` tsa
ON TRIM(UPPER(cwnextt.company_entity_code)) = TRIM(UPPER(tsa.company_entity_code))
AND TRIM(UPPER(cwnextt.Supplier_Top_Parent_Entity_Code)) = TRIM(UPPER(tsa.manufacturer_top_parent_entity_code))
AND TRIM(UPPER(cwnextt.Current_Contract_Name)) = TRIM(UPPER(tsa.contract_category))
AND cwnextt.spend_period_yyyyqmm = tsa.spend_period_yyyyqmm
--this table contains "company_spend" which is now duplicated
LEFT JOIN `test_etl_field.ecr_smart_data` smart
ON smart.company_entity_code = cwnextt.company_entity_code
AND (smart.contract_number = cwnextt.current_contract_number
OR smart.contract_number = cwnextt.replacement_contract_number)
AND smart.month_key = cwnextt.spend_period_yyyyqmm
If something can be created that will keep company_spend from duplicating on the second left join, that is what I am after.
Not sure to understand all the details of your problem but here's a fact from BigQuery doc :
SELECT DISTINCT
A SELECT DISTINCT statement discards duplicate rows
and returns only the remaining rows.
You can't apply DISTINCT on specific columns because it doesn't make sense. Let's say you have 4 columns and call DISTINCT on 3 columns, what is SQL supposed to do with the last one ?
You must tell SQL which value to keep for the remaining column and GROUP BY is the right solution here.
So if you want to:
Remove a column that has been duplicated : Just adjust your SELECT to get only the columns you want
Remove lines that have the same value in specific columns : I would suggest a GROUP BY on the targeted column and taking the aggregation you want (first, avg, sum or whatever) for the remaining ones.
Remove the value from a row if another row has the same : You may not want to do that. A row has to keep its value and you won't get it back. Besides, same problem, which row do you want to keep ?
Hope this helps ! Feel free to give clarification on your problem if you want more specific answers.
While I couldn't resolve this issue in SQL, I used Tableau via a FIXED LOD to aggregate the data passed duplicates so the end user could visualize the output with accuracy. Not ideal, but the SQL route wasn't make sense.

I use name data to result code,Sql update question

Update x1 a set a.dept_cd=(select distinct dept_cd from x2 b a.nm=b.nm)
It's my sql
Distinct make data unique, but it result in an error message,
row subquery returns more than one row
My data is string
So i use name to return code(dept_cd)
Can you help me?
If this query return that error, it means that you have more than one dept_cd where nm is equal to the one you are looking for.
The goal of distinct is to avoid having twice the same value of dept_cd.
If you need one the first one no matter what the value is, you can add limit 0,1 ad the end of your subquery.
If the value you need is a specific one, you need to find a way to update your query to isolate it but without having the full context, we cannot help you on that.

How can I group a set of similar results and add up values for one final result in SQL?

I know there has to be a simple answer to this question, but I am a total SQL noob. In my result set I have multiple results in one column that have one specific value that is repeated several times. In another column there is another value that needs to be added. Please see the attached photo for clarification.
I want to be able to find all of the values in Column B that correspond to 'A' and add them up for one result like shown. I want to be sure to delete the duplicates in Column A. Any help is greatly appreciated.
You're trying to SUM the values in column B and you're trying to group them by (GROUP BY) the value in column A when you do that. This is accomplished like so:
SELECT
col_a,
SUM(col_b)
FROM
My_Table
GROUP BY
col_a

Can anyone explain me about this SQL Query

Select Null as Empty from (select * from TblMetaData)
Looks like, it is trying to get null rows for the same number of rows in tblMetaData.
EDIT: This could be written as
SELECT Null AS Empty FROM tblMetaData
It will yield a result set with one column named Empty which only contains NULL values. The number of rows will be equal to the number of rows available in TblMetaData.
It looks like the result of one of two possible situations:
The developer was getting paid per line, and threw in that query. It was probably originally structured to take more than one line.
The developer was incompetent and this was the only way they could think of to generate a bunch of null values.
The query returns a null value from each line of the table, so the only real information in the result is the number of records in the table.
This can of course be found out a lot more efficently using:
select count(*) as Count from TblMetaData
It's possible that the developer was not at all aware of the count aggregate (or how to search the web) and tried to get the number of records while making the result as small as possible.
It often used in this expression
select * from TableA where exists
(select null from TableB where TableB.Col1=TableA.Col1)
it can be used to give the number of rows in the table TblMetaData with the column's name denoting the first letter of empty(in this case only).
like suppose you gave
Select Null as Empty from (select * from TblMetaData)
so it will give
E
n rows selected
here n is the number of rows in the table.
suppose you gave
Select Null as XYZ from (select * from TblMetaData)
then it would be same but the column's name would change like
X
n rows selected

How to count the number of times a character appears in a SQL column?

For a user logging table I have in a SQL database, I track the some of the parameters off of a report request. The report allows multiple ID's to be passed to it and I store all of those in a single column in the database column. If this were to be a normalized set of data, there would definitely be an additional table setup for this, but this is what was inherited...
I've now been asked to give a quick count of the number of times a report was run with more than 2 ID's passed to it. I can easily get the number of records that have more than 1 report requested because they all include a comma.
What I need to do next is count the number of times a comma appears in a column. How do you do this in SQL?
--count the number of times more than 1 report was requested in the record
select
count(*) as cnt
from
[table]
where
RequestedReportParams Like '%,%'
SELECT LEN(RequestedReportParams) - LEN(REPLACE(RequestedReportParams, ',', ''))
FROM YourTable
WHERE .....
This is simply comparing the length of the column with the commas, with the length of the value with the commas removed, to give you the difference (i.e. the number of commas)
It seems the quick and dirty way to answer the question you've been asked would be to do this:
select
count(*) as cnt
FROM
[table]
WHERE
RequestedReportParams Like '%,%,%'