I created partitions on one table. I have created the partitions using column year.
I want to retrieve the data from partition table using clause.
How can I write my select clause?
Something like this...
-- fetch all the rows on the 2012 partition
SELECT
t.name,
t.year
FROM
mytable AS t
WHERE
$PARTITION.<partition_function_name>(t.year) = $PARTITION.<partition_function_name>(2012)
For more info look here.
Related
I am creating view in Snowflake that has CTE on base table without any filters. I have other CTEs that depend on Parent CTE to fetch further information.
Everything is working fine when I query all records from base table that has 45K rows. But when I query view for one particular ID, explain plan shows Base CTE is picking up 45K rows, joining rest of CTE on 45K rows then finally applying my unique ID filter and returning one row.
I am not getting any difference in performance pulling data for all records or one record. Snowflake is not optimizing base CTE to apply the filter criteria I am looking for.
Any suggestions how can I resolve this issue? I used local variables in filter criteria of base CTE but it is not viable solution.
CREATE OR REPLACE VIEW test_v AS
WITH parent_cte as
(select document_id, time, ...
from audit_table
),
emp_cte as
(select employee_details, ...
from employee_tab,
parent_cte
where parent_cte.document_id = employee_tab.document_id),
dep_cte as
(select dep_details, ....
from dependent_tab,
emp_cte
where ..........)
select *
from dep_cte, emp_cte, base_cte;
Now when I query the view for one document_id, plan is fetching all data and joining then applying filter which is not efficient.
select * from test_v where document_id = '1001';
I can't use these tables in one select with join condition as I am using "LATERAL FLATTEN" which is cross multiplying each base table record so I am going with CTE approach.
Appreciate your ideas.
I am pretty familiar with the COUNT function in SQL, but I don't know how to use it to achieve the results I'm looking for.
I manage a data warehouse with routine loads and I would like to report the number of raw data points (defined by #records * #columns) each time I load records into a table.
I can use the COUNT function to get the #records in the formula, but how can I dynamically count columns in a table? From there I can do the math.
Thanks!
You can query the schema tables where table meta data is stored in sql server.
SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MyTable'
SELECT COUNT(*) FROM MyTable
I have a table with a couple hundred rows. I want to know the most common value of the data in one of the columns. How do I go about that?
I recommend you do it in your sql query with something like this :
select top 1 column, count(*) cnt
from table
group by column
order by count(*) desc
This syntax has to be adapted to your rdbms. For instance, in Oracle it would be something like this :
select column from (
select column, count(*)
from table
group by column
order by count(*) desc
) where rownum = 1
If you want to do it in Talend you can use :
Input -- tAggregateRow -- tSortRow -- tSampleRow -- Output
In tAggregateRow you use a count function to count the frequency of values in your column, then you sort them by descending order in tSortRow, then you get the first line with tSampleRow (just put "1")
I have SQL statement which takes really a lot of time to execute and I really had to improve it somehow.
select * from table where ID=1 and GROUP in
(select group from groupteam where
department= 'marketing' )
My question is if I should create index on columns ID and GROUP would it help?
Or if not should I create index on second table on column DEPARTMENT?
Or I should create two indexes for both tables?
First table has 249003.
Second table has in total 900 rows while query in that table returns only 2 rows.
That is why I am surprised that response is so slow.
Thank you
You can also use EXISTS, depending on your database like so:
select * from table t
where id = 1
and exists (
select 1 from groupteam
where department = 'marketing'
and group = t.group
)
Create a composite index on individual indexes on groupteam's department and group
Create a composite index or individual indexes on table's id and group
Do an explain/analyze depending on your database to review how indexes are being used by your database engine.
Try a join instead:
select * from table t
JOIN groupteam gt
ON d.group = gt.group
where ID=1 AND gt.department= 'marketing'
Index on table group and id column and table groupteam group column would help too.
I am importing the data from a table to another table with same structure only one changes in these two table i add a primary key in new table. So i want to find out the duplicate id my table . I am using below query for find out but this did not work . But the same query are working in mysql sybase . What i need to change in the blow query .
SELECT RPSID,COUNT(1) AS n
FROM Rpserv
GROUP BY RPSID
HAVING n>1 ORDER BY RPSID DESC
SELECT RPSID, COUNT(*) AS n
FROM Rpserv
GROUP BY RPSID
HAVING COUNT(*)>1
ORDER BY RPSID DESC