Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all rows and ignore the first row

I'm currently using the following SQL query which is returning 25 rows. How can I modify it to ignore the first row:

SELECT fiscal_year, SUM(total_sales) as sum_of_year, AVG(SUM(total_sales)) 
OVER () as avg_sum 
FROM sales_report 
GROUP BY fiscal_year 
ORDER BY fiscal_year ASC

I'm using SQL Server 2008.

Thanks.

like image 380
Brasciole Avatar asked May 01 '16 22:05

Brasciole


People also ask

How to select entire column but not first row in Excel?

Select entire column (including or excluding blank cells) except header with Kutools for Excel If your list does not contain any blank cells, you can use the shortcut to select entire column but the first row.

How to select rows where the column is null or not?

Selecting rows where the column is null or not. Let’s select rows where the 'Dept' column has null values and also filtering a dataframe where null values are excluded. First, we did a value count of the column ‘Dept’ column. The method .value_counts () returns a panda series listing all the values of the designated column and their frequency.

How to skip first 2 rows after header in CSV file?

The most simple one is by builing a list of rows which to be skipped: As you can see read_csv method keep the header and skip first 2 rows after the header. Parameter skiprows is defined as:

How to select entire column (including blank cells except header) except header?

Select entire column (including blank cells) except header with VBA. Also, besides the Define Name function, you can use VBA to select entire column but first row. 1. Select a cell of the column you want to select and press Alt + F11 to open the Microsoft Visual Basic for Applications window.


2 Answers

You can use EXCEPT in SQL Server 2008.

SELECT fiscal_year, SUM(total_sales) as sum_of_year, AVG(SUM(total_sales)) 
OVER () as avg_sum 
FROM sales_report 
GROUP BY fiscal_year

EXCEPT

SELECT TOP 1 fiscal_year, SUM(total_sales) as sum_of_year, AVG(SUM(total_sales)) 
OVER () as avg_sum 
FROM sales_report 
GROUP BY fiscal_year 
ORDER BY fiscal_year ASC

For SQL Server 2012 and above, you can use FETCH OFFSET

like image 74
sqluser Avatar answered Nov 15 '22 07:11

sqluser


assuming this is exactly how you'd query it, then: SELECT fiscal_year, SUM(total_sales) as sum_of_year, AVG(SUM(total_sales)) OVER () as avg_sum FROM sales_report WHERE fiscal year <> (SELECT MIN(Fiscal_year) FROM sales_report)) GROUP BY fiscal_year ORDER BY fiscal_year ASC

And then you can remove the "order by".
Works on all versions

like image 27
Hila DG Avatar answered Nov 15 '22 08:11

Hila DG