I'm trying to return the MINIMUM VALUE and the MAXIMUM VALUE in my PRICE table(the field is the val_price).
What is the problem with this query?
select des_price, val_price from price
WHERE val_price IN
(SELECT MIN(val_price), MAX(val_price) from price);
The error message is:
00913. 00000 - "too many values"*Cause:
*Action:
If I try with just one value in the inner select everything works fine, but with multiple values I don't know why is not working.
This example works fine with 2 values in the follow inner select:
UPDATE price SET val_price = val_price * 1.05
WHERE des_price in('NORMAL','RELEASE');
You cannot select two columns from sub-query when you are using only one column in Where clause
Try this way
SELECT des_price,
val_price
FROM price
WHERE val_price IN (SELECT Min(val_price)
FROM price
UNION ALL
SELECT Max(val_price)
FROM price);
or
SELECT des_price,
val_price
FROM price
WHERE ( val_price, val_price ) IN (SELECT Min(val_price),
Max(val_price)
FROM price)
Related
I have a BQ table that looks look this:
and I want to add a column that divides the shipping_handling based on the number of rows under the same invoice, in this case since there are 5 entries under invoice J513183, it will be 461.71/5 = 92.34
which would look like this:
tried adding this code "shipping_handling/count(invoice) as shipping" but it's not working. I hope anyone can help me find a solution on this google big query sql
Below is for BigQuery Standard SQL
#standardSQL
SELECT *,
ROUND(shipping_handling / (COUNT(*) OVER(PARTITION BY invoice)), 2) AS shipping
FROM `project.dataset.table`
You can use a window function:
select invoice_date,
invoice,
description,
shipping_handling,
avg_line_total,
shipping_handling / count(*) partition by (invoice) as shipping
from the_table;
Depending on your DBMS, you might need to cast one of the values in the division to a numeric data type otherwise it might use integer division which does not yield decimal digits.
One possible solution would be to create temp table with count of groups and than use it in your query
-- DROP TABLE IF EXISTS
IF OBJECT_ID(N'tempdb..#tmpInvoice') IS NOT NULL
BEGIN
DROP TABLE #tmpInvoice
END
-- GROUP INVOICES AND INSERT INTO TEMP TABLE
SELECT invoice as invoiceId, COUNT(invoice) invoiceCount
INTO #tmpInvoice
FROM BQ GROUP BY invoice
-- GET SHIPPING WITH ADDITIONAL SELECT
SELECT invoice_date,
invoice,
description,
shipping_handling,
avg_line_total,
cast((shipping_handling/(SELECT invoiceCount from #tmpInvoice where invoiceId = invoice)) as numeric(10,2)) Shipping
FROM BQ
-- GET SHIPPING WITH INNER JOIN
SELECT bq.invoice_date,
bq.invoice,
bq.description,
bq.shipping_handling,
bq.avg_line_total,
cast((bq.shipping_handling/ti.invoiceCount) as numeric(10,2)) Shipping
FROM BQ bq
INNER JOIN #tmpInvoice ti on bq.invoice = ti.invoiceId
I get the following error
Scalar subquery contains more than one row; SQL statement: select * from trip where price= ( select price from hiking_trip ); [90053-193]
From the SQL
SELECT *
FROM trip
WHERE price = (
SELECT price
FROM hiking_trip
);
I know the error disappears if I add ANY to my code after =. but I don't understand why it does not work? Shouldn't it give me the price equal to the given condition? and why ANY will make it work?
UPDATE: i got the point you told me but then
select *
from country
where exists
(
select *
from mountain
where
mountain.country_id=country.id
);
in this query won't the select *statement in subquery return more than one row or column yet it is working here??
It's because the subquery SELECT price FROM hiking_trip returns multiple rows.
You would need to use IN instead of = for it to work.
SELECT * FROM trip WHERE price IN ( SELECT price FROM hiking_trip );
This is because your subquery is returning more than one row.
You can use IN instead of =
SELECT * FROM trip WHERE price IN ( SELECT price FROM hiking_trip );
or you can use LIMIT
SELECT * FROM trip WHERE price = (SELECT price FROM hiking_trip LIMIT 1)
which will filter the subquery result with first row
This fails because = is expecting a single value for the comparison, not a list of values which is what the select returns. Technically, it wants a scalar subquery -- a subquery that returns 1 column and 0 or 1 rows.
You can fix the syntax problem in many ways.
Use aggregation to return one value:
WHERE trip.price = ( SELECT MAX(price) FROM hiking_trip )
Use limit to return one value:
WHERE trip.price = (SELECT price FROM hiking_trip LIMIT 1)
Use in to match any value:
WHERE trip.price IN (SELECT price FROM hiking_trip)
Use any:
WHERE trip.price = ANY (SELECT price FROM hiking_trip)
Need help with active record query, I have customers table with name and balance fields.
How can I make query that will return 6 values, first 5 is top customers by balance and 6th one is sum of all others?
need to select name and balance, for others it will custom name ''Other
Divide et impera: make two view then put results together
select *
from ( select * from view_top_5
union
select * from view_sum )
where view_top_5 is view (or a subquery) that gives you the top 5 customers, view_sum is a query giving you the sum. To get "all others" you can build your query from this skeleton:
-- skeleton for view "view_sum"
select <what you need>
from mytable
where customer_id not in (select client_id
from view_top_5)
I have one table and I want to calculate the percentage of one column
I tried to do so in two ways.
but I am actually face with error.
The error is 'syntax error at or near "select"'
This is my code in below:
WITH total AS
(
select krs_name,count(fclass) as cluster
FROM residentioal
GROUP BY krs_name
)
SELECT total.cluster,
total.krs_name
(select count(fclass)
FROM residentioal
where fclass='village'
or fclass='hamlet'
or fclass='suburb'
or fclass='island'
AND krs_name = total.krs_name
)::float / count(fclass) * 100 as percentageofonline
FROM residentioal, total
WHERE residentioal.krs_name = total.krs_name
GROUP BY total.krs_name total.krs_name
My table has 5437 rows in which there is 8 group of krs_name and in the other column namely fclass, there is 6 group. Therefore I want to calculate the percentage of 4 groups from fclass for each krs_name . thus, i have to first query the count(fclass) group by krs_name and then query the count of fclass where fclass is equal to my condition group by krs_name and finally count(fclass) "with condition" / count(fclass) "total fclass" * 100 goup by krs_name?
I'm using Postgresql 9.1.
The problem is in this line:
SELECT total.cluster, total.krs_name (
The open paren makes no sense.
But, this seems to do what you want and it is much simpler:
SELECT r.krs_name, COUNT(*) as total,
AVG( (fclass in ('village', 'hamlet', 'suburb', 'island'))::int ) * 100 as percentageofonline
FROM residentioal r
GROUP BY r.krs_name
i have a table with two column:
Name Values
----------------
Target 20
v1 10
v2 20
v3 10
Total 30
percent ?
i want to calculate the percentage with the single column value to get the formula as
--> Target/Total * 100.
i.e for ex: Total = SUM (Values) in that way......
by using two rows in the column in sql query, how to do calculation in single column with diff row values by implementing a formula in it.
i dont want to calculate total. i want to calculate percent:
formula:Target/Total * 100 . that gives me the value in percent row?
how? to query it?
You want an additional record to show up in your query output? That seems like you're trying to make it look & work like an Excel spreadsheet, but anyways, here's how you'd go about doing it:
SELECT Name, Values FROM table
UNION (Select function(inputs) AS Name, function(inputs) AS Values FROM table)
If you want to this in SQL...
;WITH
cTarget AS (SELECT Values AS Target FROM MyTable WHERE name = 'Target'),
cTotals AS (SELECT SUM(Values) AS Total FROM MyTable WHERE name <> 'Target')
SELECT * FROM MyTable
UNION ALL
SELECT 'Total', Total FROM cTotals
UNION ALL
SELECT
'Percentage', 1.0 * Target / Total * 100
FROM
cTarget
CROSS JOIN
cTotals