in my table I have a column name "Status" and a RECORD type column name "Payment", which has another column name "Status" inside it. So I basically have 2 columns: "Status" and "Payment.Status". When I want to UNNEST the STRUCT data("Payment" column) and use the data, I will get the error message Column name STATUS is ambiguous
How do I solve this problem?
Thanks in advance
When you are trying to use 'STATUS', BigQuery does not know to which column you are referring to.
You can overcome this with giving your UNNEST() an alias and using it to reference the specific column you are using.
Example:
with example_data as (
select true as status,
[STRUCT( 123 as id, false as status)] as payment
)
select e.status,
p.id,
p.status as payment_status
from example_data e, unnest(payment) as p
Related
I'm not sure of the correct terminology, but this should be clear when you see what I have so far.
SELECT
(
-- WHAT GOES HERE?
) as "Type",
COUNT(*) AS pie
FROM "people"
GROUP BY "Type"
ORDER BY COUNT(*) DESC, "Type"
I'm trying to classify people based on whether or not they have a value in any of these columns:
employee_id
student_id
with these types being possible:
Employee
Student
Both Employee & Student
(As you might have guessed from the SQL, this is going to generate a pie graph, so instead of putting anyone in 2 categories, I have a category that includes the people who are both employees and students.)
I believe a CASE expression would be suitable
CASE
WHEN employee_id IS NOT NULL AND student_id IS NOT NULL THEN 'Both'
WHEN employee_id IS NOT NULL then 'Employee'
WHEN student_id IS NOT NULL then 'Student'
ELSE 'None'
END AS "Type"
You can say GROUP BY 1 to select the first expression.
From https://www.postgresql.org/docs/10/sql-select.html ...
The optional GROUP BY clause has the general form
GROUP BY grouping_element [, ...]
GROUP BY will condense into a single row all selected rows that share the same values for the grouped expressions. An expression used inside a grouping_element can be an input column name, or the name or ordinal number of an output column (SELECT list item), or an arbitrary expression formed from input-column values. In case of ambiguity, a GROUP BY name will be interpreted as an input-column name rather than an output column name.
Display id and name for salesmen along with id and category of products in a single table. Indicate the source of the row in result by adding an additional column TYPE with possible values as 'S' (Salesman) and 'P' (Product). Display all rows.
SELECT CONCAT('S','') "TYPE", SID "ID", SNAME "DETAILS" FROM Salesman UNION ALL SELECT CONCAT('P', '') "TYPE", PRODID "ID", CATEGORY "DETAILS" FROM Product
I got the answer, I have tried this above query, it is giving me the answer.
Is there any other way to get the same output?
We can use desired entries in the non-existing columns by enclosing it with quotes followed by as type(i.e 'S' as type)
select 'S' as type,sid id,sname details from salesman union all select 'P' as type,prodid id,category from Product
I have a SQL table with "name" as one column, date as another, and location as a third. The location column supports null values.
I am trying to write a query to determine the number of times a null value occurs in the location column for each distinct value in the name column.
Can someone please assist?
One method uses conditional aggregation:
select name, sum(case when location is null then 1 else 0 end)
from t
group by name;
Another method that involves slightly less typing is:
select name, count(*) - count(location)
from t
group by name;
use count along with filters, as you only requires Null occurrence
select name, count(*) occurances
from mytable
where location is null
group by name
From your question, you'll want to get a distinct list of all different 'name' rows, and then you would like a count of how many NULLs there are per each name.
The following will achieve this:
SELECT name, count(*) as null_counts
FROM table
WHERE location IS NULL
GROUP BY name
The WHERE clause will only retrieve records where the records have NULL as their location.
The GROUP BY will pivot the data based on NAME.
The SELECT will give you the name, and the COUNT(*) of the number of records, per name.
I have a table with four columns: NAME, AGE, PRIMARYWEIGHT and SECONDARYWEIGHT
Where NAME = 'Damian', I wish to select AGE and PRIMARYWEIGHT only if SECONDARYWEIGHT is NULL otherwise I'll take PRIMARYWEIGHT.
Ideally I'd like to give its an alias 'WEIGHT' regardless of whether it was PRIMARYWEIGHT or SECONDARYWEIGHT.
SELECT NAME, AGE, ISNULL(PRIMARYWEIGHT, SECONDARYWEIGH) As WEIGHT
msdn reference
SELECT AGE, COALESCE(SECONDARYWEIGH, PRIMARYWEIGHT) As WEIGHT
You can use COALESCE (as indicated in your tag)
Evaluates the arguments in order and returns the current value of the first expression that initially does not evaluate to NULL.
I have table with the following static columns
ID Sys_Date Name prop_name1 prop_name2 prop_name3 prop_value1 prop_value2 prop_value3
10 11/2/2011Java class method parameter Imanager getOrders orderNumber
I need to write SQL query which get an input property name like “method” and go over (prop_name1 prop_name2 prop_name3 ) and check which column
Is equal to “method” in case I found it I need to jump 3 columns to the proper value which is “getOrders” and get the value from there where
prop_name1 is mapping to prop_value1
prop_name2 is mapping to prop_value2
prop_name3 is mapping to prop_value3
how can I do it with sql query?
Thanks in advance
You could do something like this:
select name,value
from
(
select id, prop_name name, prop_name value
from table
union
select id, prop_name2 name, prop_name2 value
from table
union
select id, prop_name3 name, prop_name3 value
from table
)
where name = 'method'
...which is basically shoe-horning your data into a more easily queryable structure. You'd be better off changing the table structure, though..