Good day
I have problem with my table and counting
TABLE1
COLUMN1 COLUMN2
3 jjd
5 jd
3 jjd
4 kg
5 jd
48 gjh
446 djj
… …
I need
TABLE1
COLUMN1 COLUMN2 COLUMN3
3 jj 2
5 jd 2
4 kg 1
48 gjh 1
446 djj 1
... ... …
Iam doing but not working well.
SELECT * , COUNT(Column1) as column3 FROM TABLE1
Thanks for help withh my counting
Use a GROUP BY and ORDER BY with DESC to put them in COUNT total order.
SELECT COLUMN1, COLUMN2, COUNT(Column1) AS COLUMN3
FROM Table1
GROUP BY COLUMN1, COLUMN2
ORDER BY COUNT(Column1) DESC
Output
COLUMN1 COLUMN2 COLUMN3
5 jd 2
3 jjd 2
4 kg 1
446 djj 1
48 gjh 1
SQL Fiddle: http://sqlfiddle.com/#!6/89f49/4/0
Try by using group by
SELECT COLUMN1 ,
COLUMN2 ,
COUNT(Column1) As COLUMN3 FROM cte_TABLE1
Group by COLUMN1 ,COLUMN2
Order by COLUMN1
By using Window function
SELECT DISTINCT COLUMN1 ,
COLUMN2 ,
COUNT(Column1)OVER(Partition by COLUMN1,COLUMN2 ORder by COLUMN1 ) As COLUMN3 FROM cte_TABLE1
Result
COLUMN1 COLUMN2 column3
-----------------------
3 jjd 2
4 kg 1
5 jd 2
48 gjh 1
446 djj 1
Using OVER we can achieve it easily
SELECT COLUMN1 ,
COLUMN2 ,
COUNT(Column1)OVER(Partition by COLUMN1,COLUMN2 ORder by COLUMN1 ) As COLUMN3 FROM cte_TABLE1
Group By COLUMN1,COLUMN2
Related
using BigQuery, I would like to be able to divide one column, column1, into two separate columns, column2, and column3 with 50% of all records in column1 in column2 and 50% of all records in column1 in column 3. Ex column1 has 8 records of the number 2. I'd like to create a column2 with 4 records of the number 2 and column3 with 4 records of the number 2.
Is there a query to write this in BigQuery?
Column1
2
2
2
2
2
2
2
2
Column2
2
2
2
2
Column3
2
2
2
2
try:
SELECT
Column1 AS Column2
FROM `my-project.my-dataset.my-table`
WHERE 1=1
QUALIFY ROW_NUMBER() OVER (ORDER BY Column1) <= (
SELECT COUNT(*)/2
FROM `my-project.my-dataset.my-table`
);
SELECT
Column1 AS Column3
FROM `my-project.my-dataset.my-table`
WHERE 1=1
QUALIFY ROW_NUMBER() OVER (ORDER BY Column1) > (
SELECT COUNT(*)/2
FROM `my-project.my-dataset.my-table`
);
This will give you 2 results: One for each Column2 and Column3 with the first and second half of the data respectively order by Column1 (to use analytical functions you always have to specify an ORDER BY inside an OVER clause)
For random order try:
CREATE TEMP TABLE a AS (
SELECT Column1 as Column2
FROM `my-project.my-dataset.my-table`
WHERE 1=1
QUALIFY
ROW_NUMBER() OVER (ORDER BY RAND()) <= (SELECT COUNT(*)/2 FROM `my-project.my-dataset.my-table`)
);
SELECT Column1 as Column3
FROM `my-project.my-dataset.my-table`
WHERE Column1 NOT IN (SELECT * FROM a);
SELECT * FROM a
In this case you'll get 3 results: first one is the temporary table creation and the other 2 are the columns 2 and 3.
I need to separate two columns into two rows in sql
I have this:
Column1 Column2 Column3
Car 2 5
Boat 4
Truck 6
And I want this:
Column1 Column2
Car 2
Car 5
Boat 4
Truck 6
How can I do this in SQL?
This operation is unpivoting. I would recommend apply:
select t.column1, v.col
from t cross apply
(values (col1), (col2)) v(col)
where v.col is not null;
This should do it
select Column1, Column2
from tbl where Column2 is not null and Column2 <> ''
union
select Column1, Column3
from tbl where Column3 is not null and Column3 <> ''
I want to pull data from a table where i sum column3 as Total and then i want to show the orignal value of column3 as well for all the rows that have column1 = 1
Example
colum1 column2 column3
1 2456 20.00
2 2456 -5.00
1 2457 30.00
2 2457 -5.00
I did a
select a.column1,a.column2, sum(a.column3), b.column3 as total
from table A
inner join table B on a.column2 = b.column2
group by a.column1
but it wont all alow to put b.column3 as part of my select.
colum1 column2 column3 total
1 2456 20.00 15.00
1 2457 30.00 25.00
Use Aggregate Function
SELECT column1,column2, MAX(column3), SUM(column3) AS Total
FROM Table1
GROUP BY column1,column2
SQL FIDDLE
I think you want conditional aggregation:
SELECT ROW_NUMBER() OVER (ORDER BY column2) as id,
column2,
MAX(CASE WHEN column1 = 1 THEN column3 END) as column3,
SUM(column3) AS Total
FROM t
GROUP BY column2
I don't know how the first column in the result set is calculated. I'm assuming it is essentially a row number.
I have 3 columns as
Column1 column2 column3
GU1 1 a
GU1 2 a
GU1 3 a
GU2 4 b
GU3 5 c
GU4 6 a
GU4 7 b
I would like to filter out the column1 where the values are having multiple column3 values
In this example, I want to pull GU4 where it is having both a & b in the column3.
Use distinct in your count
select column1
from your_table
group by column1
having count(distinct column3) = 1
Using oracle developer, I've run a query that results in the following table. But I only want the results where column1 matches all the values columns 2 (3,4,8). So the output would be 2, 3, but not 4. I'm sure there is a way to bring this result about without hard coding it? I'm thinking its some sort of self-join?
select column1, column2
from table1
where column1 in (
select column1
from table2
where depth >= 100)
order by column2;
Output:
column1 column2
3 2
8 2
4 2
3 3
4 3
8 3
4 4
Table2
Column1 Area_Name Depth
1 Lake 40
2 River 50
3 Ocean 150
4 Cliff 150
5 Mountain 90
6 Construction 60
7 Building 50
8 Random 100
9 Also Random 50
10 Another one 80
Needed output:
column2
2
3
Ok, this is what I was looking for:
SELECT table1.column1
FROM table1
INNER JOIN table2
ON table1.column2 = table2.column2
WHERE table2.depth >= 100
GROUP BY boat_id
HAVING COUNT(*) >= (
select count(*)
from table2
where depth >= 100);
UPDATED
WITH qry AS (
SELECT column1, column2
FROM table1
WHERE column1 IN (
SELECT column1
FROM table2
WHERE depth >= 100)
)
SELECT t1.column2
FROM qry t1 LEFT JOIN qry t2
ON t1.column1 = t2.column1 AND t1.column2 = t2.column2
GROUP BY t1.column2
HAVING COUNT(*) = (SELECT COUNT(DISTINCT column1) FROM qry)
ORDER BY t1.column2
Output:
| COLUMN2 |
-----------
| 2 |
| 3 |
SQLFiddle