Want to sort the data in column after 3rd position - sql-order-by

I have a table named City and I want the output in a way like
first 2 rows should have values as "Chennai" then after that from the 3rd position it should sort remaining rows in ascending order starting from a-z.
Please help me with this.

Assuming you have two entries in City for Chennai you could do:
ORDER BY
CASE WHEN city = 'Chennai' THEN 0 ELSE 1 END,
city

Related

Oracle counting distinct rows using two columns merged as one

I have one table where each row has three columns. The first two columns are a prefix and a value. The third column is what I'm trying to get a distinct count for columns one/two.
Basically I'm trying to get to this.
Account
Totals
prefix & value1
101
prefix & value2
102
prefix & value3
103
I've tried a lot of different versions but I'm basically noodling around this.
select prefix||value as Account, count(distinct thirdcolumn) as Totals from Transactions
It sounds like you want
SELECT
prefix||value Account,
count(distinct thirdcolumn) Totals
FROM Transactions
GROUP BY prefix, value
The count(distinct thirdcolumn) says you want a count of the distinct values in the third column. The GROUP BY prefix, value says you want a row returned for each unique prefix/value combination and that the count applies to that combination.
Note that "thirdcolumn" is a placeholder for the name of your third column, not a magic keyword, since I didn't see the actual name in the post.
If you want the number of rows for each prefix/value pair then you can use:
SELECT prefix || value AS account,
COUNT(*) AS totals
FROM Transactions
GROUP BY prefix, value
You do not want to count the DISTINCT values for prefix/value as if you GROUP BY those values then different values for the pairs will be in different groups so the COUNT of DISTINCT prefix/value pairs would always be one.

Count rows since last occurrence of value , excluding blank cells from the count

I want to count the number of rows since the last occurrence of a specific value, while excluding rows where the cell is a blank or empty value. I have provided a sample of fake data to try to explain what I'm asking. So, with the sample data, on every occurrence of "Apple", I would like to show the number of valued rows since the last occurrence of "Apple". I currently have a formula like this:
=ROW()-MAX(FILTER(ROW(INDIRECT("B1:B"&ROW()-1)),INDIRECT("B1:B"&ROW()-1)=INDIRECT("B"&ROW())))
The formula gives me the number of rows since the last occurrence, but does not exclude the blank cells, and I'm wanting to figure out a way to get this result without counting the rows with blank cells in the column.
Sample Data
Assuming values are in range A2:A and we want the results in column B, use this formula in cell B2 and then manually extend down:
=IF(A2="Apple",IFERROR(MATCH("Apple",QUERY({A$2:A2,SEQUENCE(ROWS(A$2:A2))},"select Col1 where Col1 is not null order by Col2 desc offset 1"),0),0),)
Explanation
The QUERY retrieves all values above the current one (e.g. for row 7 it will be A2:A6) in reverse order (A6, A5, ..., A2) and excluding blanks.
MATCH returns the position of the first occurrence of the value in interest in the result of the QUERY.
I was not able easily to make this formula auto-extend using ARRAYFORMULA or MAP(LAMBDA), so it has to be manually extended.

Complex count row etl requirement

I have a requirement related as below
1-If there is employee record then count the number of rows
a-if there are four rows then follow the layout 1,
and populate the column1 and column 2 with values in report and ltrimrtrim
b- if there are three rows, then follow the layout 2,
and hardcode the column 1 and column 2 with NULL
Otherwise, look for the employee record.
Couldn't get the logic, I used the router with as if column 1 and two null the send to layout two else 1. But the requirement is different.
router transformation, if null, layout one else 2
Step 1 - Use SRT>AGG>JNR to calculate count. create new column as count_all and set to COUNT(*). Please group by proper key columns.
Step 2 - Use RTR next to split data based on your condition.
group 1- count_all =4 then follow the layout 1 and...
group 2- count_all =3 then follow the layout 2 and...
group 3 - if count <3 then do employee record.

Split a range of numbers in equal parts in hive

I have a table with 40 lac rows with a column 'playcount' which has min value 1 and max value of around 17,000.
I would like to split this table into 15 groups by adding a column which will have values 1 to 15 based on 'playcount' column.
Hive has a function NTILE which allows to do something similar. Here if I did NTILE(15) OVER (ORDER BY playcount) AS mygroup, it does break it up but based on count of playcount values and as the lower values are lot lot more( more than 50% have values less than 5), the grouping is such that values over 35 have group value of 15(maximum).
I would like to do the grouping based on the playcount and not on count of playcount values.
Is something similar possible in hive.
Thanks
One possibility I can think of is playcount%15 as mygroup.

Sort entire Pivot table in Excel 2013 by column

I have a Pivot Table with many columns.
I want the Pivot the ability to sort one of the columns in a way that the whole column is sorted and not the relative position in the hierarchy.
Example:
NAME PRODUCT SUM
Joe A 400
Joe B 200
Joe B 300
Alice A 500
Alice A 200
Alice C 300
If I use the regular sort on the Sum column, I will get the data sorted partially.
Alice A 500
Alice A 300
Alice C 200
Joe A 400
Joe B 300
Joe B 200
As you can see, the Sum column is sorted only relevant to the Name column.
I want the whole column to be sorted.
Expected result should look something like this:
Alice A 500
Joe A 400
Alice C 300
Joe B 300
Alice A 200
Joe B 200
If you have more than one field in the row area of the pivot table, you cannot create a sort purely by value. The hierarchy cannot be ignored. That's how a pivot table works. Don't shoot the messenger.
So, essentially, you want to sort the SUM columns first and then the NAME column and last the PRODUCT column.
Have a look in the MSDN Sort data in a PivotTable, I think that should do the trick
Add a dummy column in the data set and create an index (1 through the number of rows). If you pull that into the left-most column in your pivot, you'll be able to sort by the field you want. You can hide that column for visual purposes.

Resources