Sybase round not working as expected - rounding

Below is my scenario. We are retrieving data from Sybase. We are using putty to achieve this since we do not have direct access to Sybase. From the inner query, i am getting value as 6.500000 for a metric. But in the outer query, when i apply Round function to this metric , it is becoming 6 instead of 7. Please help on this issue!
Below is my Inner query and the complete query. Have attached the data also for reference.
Inner Query
SELECT D.Col1,A.Col2,C.Col3,
SUM ( C.net_td_cnt ) AS td_cnt
FROM Tab1 A,Tab2 B,Tab3 C,Tab4 D
WHERE (A.Tab1 =B.Tab1 AND B.Tab2=C.yld_loc_cd AND D.Tab1=A.Tab1)
AND (D.Col1 IN ('Fil1') and A.Col2='Fil2'
AND ((C.Col3 BETWEEN 'Jul 6 2016' AND 'Jul 6 2016') OR
(C.Col3 BETWEEN 'Jul 6 2016' AND 'Jul 6 2016')))
GROUP BY D.Col1,A.Col2,C.Col3
UNION
SELECT C.Col1,A.Col2,D.Col3,
SUM ( D.net_td_cnt ) AS td_cnt
FROM Tab1 A,Tab2 B,Tab4 C,Tab5 D
WHERE ( A.Tab1 =B.Tab1 AND C.Tab1=A.Tab1 AND B.Tab2 =D.yld_loc_cd )
AND ( C.Col1 IN ('Fil1') and A.Col2='Fil2'
AND (D.Col3 BETWEEN 'Jul 6 2016' AND 'Jul 6 2016') )
GROUP BY C.Col1,A.Col2,D.Col3
Output:
Col1 |Col2 |Col3 |td_cnt
Fil1 |Fil2 |Jul 6 2016 12:00AM |6.500000
Full Query
SELECT A.Col1,A.Col2,A.Col3,
ROUND(A.td_cnt,0) AS td_cnt
FROM
(SELECT D.Col1,A.Col2,C.Col3,
SUM ( C.net_td_cnt ) AS td_cnt
FROM Tab1 A,Tab2 B,Tab3 C,Tab4 D
WHERE (A.Tab1 =B.Tab1 AND B.Tab2=C.yld_loc_cd AND D.Tab1=A.Tab1)
AND (D.Col1 IN ('Fil1') and A.Col2='Fil2'
AND ((C.Col3 BETWEEN 'Jul 6 2016' AND 'Jul 6 2016') OR
(C.Col3 BETWEEN 'Jul 6 2016' AND 'Jul 6 2016')))
GROUP BY D.Col1,A.Col2,C.Col3
UNION
SELECT C.Col1,A.Col2,D.Col3,
SUM ( D.net_td_cnt ) AS td_cnt
FROM Tab1 A,Tab2 B,Tab4 C,Tab5 D
WHERE ( A.Tab1 =B.Tab1 AND C.Tab1=A.Tab1 AND B.Tab2 =D.yld_loc_cd )
AND ( C.Col1 IN ('Cent') and A.Col2='Fil2'
AND (D.Col3 BETWEEN 'Jul 6 2016' AND 'Jul 6 2016') )
GROUP BY C.Col1,A.Col2,D.Col3
) A ORDER BY A.Col3
Output:
Col1 |Col2 |Col3 |td_cnt
Fil1 |Fil2 |Jul 6 2016 12:00AM |6

select round(6.500000,0) returns 7 in various versions of sybase ASE and IQ, what is your product version? Please run
select ##version
Kimon

Related

Unnest from Table in Snowflake

I have the following table:
PersonID CW_MilesRun PW_MilesRun CM_MilesRun PM_MilesRun
1 15 25 35 45
2 10 20 30 40
3 5 10 15 20
...
I need to split this table into a vertical table with an id for each field (i.e CD_MilesRun =1, CW_MilesRun = 2, etc) So that my table looks similar to this:
PersonID TimeID Description C_MilesRun P_MilesRun
1 1 Week 15 25
1 2 Month 35 45
2 1 Week 10 20
2 2 Month 30 40
3 1 Week 5 10
3 2 Month 15 20
In postgres, I would use something similar to:
SELECT
PersonID
, unnest(array[1,2]) AS TimeID
, unnest(array['Week','Month']) AS "Description"
, unnest(array["CW_MilesRun","CM_MilesRun"]) C_MilesRun
, unnest(array["PW_MilesRun","PM_MilesRun"]) P_MilesRun
FROM myTableHere
;
However, I cannot get a similar function in snowflake to work. Any ideas?
You can use FLATTEN() with LATERAL to get the result you want, although the query is quite different.
with tbl as (select $1 PersonID, $2 CW_MilesRun, $3 PW_MilesRun, $4 CM_MilesRun, $5 PM_MilesRun from values (1, 15, 25, 35, 45),(2, 10, 20, 30, 40),(3, 5, 10, 15, 20))
select
PersonID,
t.value[0] TimeID,
t.value[1] Description,
iff(t.index=0,CW_MilesRun,CM_MilesRun) C_MilesRun,
iff(t.index=1,PW_MilesRun,PM_MilesRun) P_MilesRun
from tbl, lateral flatten(parse_json('[[1, "Week"],[2, "Month"]]')) t;
PERSONID TIMEID DESCRIPTION C_MILESRUN P_MILESRUN
1 1 "Week" 15 25
1 2 "Month" 35 45
2 1 "Week" 10 20
2 2 "Month" 30 40
3 1 "Week" 5 10
3 2 "Month" 15 20
P.S. Use t.* to see what's available after flattening (perhaps that is obvious.)
You could alternatively use UNPIVOT and NATURAL JOIN.
Above answer is great ... just like thinking about alternative ways of doing things ... you never know when it might suit your needs - plus exposes you to a couple new cool functions.
with cte as (
select
1 PersonID,
15 CW_MilesRun,
25 PW_MilesRun,
35 CM_MilesRun,
45 PM_MilesRun
union
select
2 PersonID,
10 CW_MilesRun,
20 PW_MilesRun,
30 CM_MilesRun,
40 PM_MilesRun
union
select
3 PersonID,
5 CW_MilesRun,
10 PW_MilesRun,
15 CM_MilesRun,
20 PM_MilesRun
)
select * from
(select
PersonID,
CW_MilesRun weekly,
CM_MilesRun monthly
from
cte
) unpivot (C_MilesRun for description in (weekly, monthly))
natural join
(select * from
(select
PersonID,
PW_MilesRun weekly,
PM_MilesRun monthly
from
cte
) unpivot (P_MilesRun for description in (weekly, monthly))) f

Identify cells with Repeated strings

Sometimes a person would Type 9999999999999999 or 0000000000 8999999998888888, ...
instead of typing their identification number. I want a column to identify those cases, meaning the cases in which a single string (In this case a number string) is typed more than 7 times.
I have no idea on how to do it.. My best guess would be to count each number using len, but that would be at least 10 IF statements opened... Any suggestions?
We can find the maximum digit count as follows (as a calculated column):
MaxDigitCount =
VAR n = LEN ( [ID] )
VAR Digits =
ADDCOLUMNS ( GENERATESERIES ( 1, n ), "#D", MID ( [ID], [Value], 1 ) )
VAR Frequencies =
GROUPBY ( Digits, [#D], "#Freq", COUNTX ( CURRENTGROUP (), [Value] ) )
RETURN
MAXX ( Frequencies, [#Freq] )
Suppose [ID] = "899999999777". Then n = 12 and Digits is the table generated by creating a list from 1 to 12 and adding the column of digits corresponding to each of those positions.
Digits =
Value
#D
1
8
2
9
3
9
4
9
5
9
6
9
7
9
8
9
9
9
10
7
11
7
12
7
Then Frequencies summarizes this table by grouping on #D and counting the number of occurrences of each distinct digit.
Frequencies =
#D
#Freq
8
1
9
8
7
3
Finally, return the maximum value in the #Freq column.
Using this, it's easy to check if the value is greater than 7. You can modify the final line to
IF ( MAXX ( Frequencies, [#Freq] ) > 7, ">7", "<=7" )

Find the total score. for each student

Hi I have a query to retrive students score in this format
Id subject class_score total_marks rank subject_type
1 MATLAB 33.80 73.30 11 Core
1 SCIENCE 39.50 83.00 4 Core
1 ENGLISH 37.60 77.60 8 Elective
1 WQP 43.90 77.40 9 Core
1 BDT 42.00 71.50 12 Elective
1 ART 47.10 84.60 2 Elective
1 COMPUTING 26.00 65.50 13 Elective
2 MATLAB 0.00 0.00 14 Core
2 SCIENCE 38.60 73.60 10 Core
2 ENGLISH 43.80 83.30 3 Elective
2 WQP 45.00 89.00 1 Core
2 BDT 41.00 79.50 6 Elective
2 ART 38.00 78.00 7 Elective
2 COMPUTING 40.80 80.80 5 Elective
I wish to calculate the overall score of each student by add the total_marks of all (Core subject + the top 3 score of the student's elective subjects) and rank them from first to last.Please can anyone assist ?Thanks
My current
use ods_sms;
SELECT student_id,sum(t.total_marks)score
FROM
(select
student_id,total_marks from tab_exam_tracker
where (subject_type='Core')
union all select t.student_id,t.total_marks from
(select student_id,total_marks from tab_exam_tracker
where subject_type = 'Elective' order by total_marks desc
limit 3) t
) t
GROUP BY student_id
But am getting wrong results
SELECT
t.id,
SUM(total_marks) AS Core_Marks,
Elective_Marks
FROM tab_exam_tracker t
INNER JOIN (
SELECT id, SUM(total_marks) as Elective_Marks
FROM (
SELECT
id,
total_marks,
IF(#id <> id, #rank:= 1, #rank:= #rank + 1) as rank,
#id:= id
FROM tab_exam_tracker,
(SELECT #id:= 0 r_id, #rank:= 0 AS r) AS a
WHERE subject_type = 'Elective'
ORDER BY id, total_marks DESC
) as b
WHERE rank <= 3
GROUP BY id
) AS e_marks ON t.id = e_marks.id
GROUP BY t.id, Elective_Marks;

Expand ( transform ) some of the hive columns as row (record)

Is there a efficient way to transform the below hive table with shown target transformation. The column count in the source table is ~ 1500.
Using spark 2.0, source and target as dataframes.
(id, dt , source1_ColA, source1_ColB, source2_ColA, source2_ColB)
------------------------------------------------------------
(10,"2018-06-01", 10, 9, 5, 8 )
(20,"2018-06-01", 20, 12, 16, 11 )
The columns A,B are transformed as shown below
Target table
(id, dt , element_name, source1, source2 )
---------------------------------------
(10,"2018-06-01", ColA , 10 , 5 )
(10,"2018-06-01", ColB , 9 , 8 )
(20,"2018-06-01", ColA , 20 , 16 )
(20,"2018-06-01", ColB , 12 , 11 )

Powerpivot DAX Rank products by quarterly sales with monthly data

I'm trying to create a calculated column that assigns a rank to each Product based on its sales in each combination of Country, Store, and Quarter.
However, the data in the table is monthly.
I was thinking of using SUMMARIZE() to create the quarterly view and then use RANKX() on that, but the value for the sum of sales cannot be evaluated for each column if I use summarize.
In order to get the rank for the monthly view I have been using the following formula:
=
RANKX (
FILTER (
Table,
Table[Country] = EARLIER ( Table[Country])
&& Table[StoreType] = EARLIER ( Table[StoreType])
),
[Sales]
)
Simplified the data looks like this with the calculated column for monthly rank and the quarterly one I'm trying to figure out
Country StoreType Month Product Sales MonthlyRank QuarterlyRank
USA A Jan-15 P1 10 1 2
USA A Jan-15 P2 15 2 1
USA B Jan-15 P1 5 1 2
USA A Feb-15 P1 5 3 2
USA A Feb-15 P2 20 1 1
USA A Feb-15 P3 10 2 3
USA A Mar-15 P1 10 2 2
USA A Mar-15 P2 25 1 1
USA B Mar-15 P3 15 1 1
How could I be able to achieve this?
You need to add a column to your date dimension that uniquely identifies each quarter. Why don't we call this unique column something like QuarterKey or QuarterIndex.
Then you can do the following in your SUMMARIZE():
ADDCOLUMNS(
SUMMARIZE(
FactSales
,DimGeography[Country]
,DimStore[Store]
,DimDate[QuarterKey]
)
,"SumSales"
,CALCULATE( SUM( FactSales[Net Amount] ) )
)

Resources