When I execute the following script with hive:
select
a.keyno
from
(
select
keyno,
reportyear
from hive_ldtmp.tmp_kzz_company_report_people_count_grow_info
where yeartype=1
and keyno='00003d22be771b36f27a7be24431e407'
and reportyear=2019
) a
left join
(
select
keyno,
reportyear
from hive_ldtmp.tmp_kzz_company_report_people_count_grow_info
where yeartype=2
and keyno='00003d22be771b36f27a7be24431e407'
and reportyear=2019
) b on a.keyno=b.keyno
and a.reportyear=b.reportyear;
The return i get is None:
0 results.
However, I am sure that when I execute the two queries separately, they have results:
-- a
select
keyno,
reportyear
from hive_ldtmp.tmp_kzz_company_report_people_count_grow_info
where yeartype=1
and keyno='00003d22be771b36f27a7be24431e407'
and reportyear=2019;
-- b
select
keyno,
reportyear
from hive_ldtmp.tmp_kzz_company_report_people_count_grow_info
where yeartype=2
and keyno='00003d22be771b36f27a7be24431e407'
and reportyear=2019;
this is the result(the results of a and b are the same):
keyno
year
00003d22be771b36f27a7be24431e407
2019
Than,I changed the format of the table from ORC to Textfile,
I executed the entire code and it got the result I wanted.
so...?Where is the problem?
In the first query this filter is different where yeartype=2. In the query b which you executed separately it is where yeartype=1
BTW you can eliminate joining with the same table. Use aggregation and filtering, like in this query:
select keyno
from
(
select keyno,
max(case when reportyear = 1 then keyno else null end) as yr1_keyno,
max(case when reportyear = 2 then keyno else null end) as yr2_keyno,
reportyear
from hive_ldtmp.tmp_kzz_company_report_people_count_grow_info
where yeartype in ( 1, 2)
and keyno='00003d22be771b36f27a7be24431e407'
and reportyear=2019
group by keyno, reportyear
)s where yr1_keyno=yr2_keyno --the same as your INNER JOIN (if join does not duplicate rows)
Related
I would like it to create a NUMBER column where the records for each date will be counted. So, for example, how many NRBs are there in 2021-10. However, when I choose count it gets such a result, sum cannot be because these are not numbers but an identification number
Here is my result:
Here my code:
PROC SQL; <- FIRST QUERY
create table PolisyEnd as
select distinct
datepart(t1.data_danych) as DATA_DANYCH format yymmdd10.
,(t4.spr_NRB) as NRB
,datepart(t1.PRP_END_DATE) as PRP_END_DATE format yymmdd10.
,datepart(t1.PRP_END_DATE) as POLICY_VINTAGE format yymmd7.,
case
when datepart(t1.PRP_END_DATE) IS NOT NULL and datepart(t1.PRP_END_DATE) - &gv_date_dly. < 0 THEN 'WYGASLA'
when datepart(t1.PRP_END_DATE) IS NOT NULL and datepart(t1.PRP_END_DATE) - &gv_date_dly. >= 0 and datepart(t1.PRP_END_DATE) - &gv_date_dly. <=7 THEN 'UWAGA'
when datepart(t1.PRP_END_DATE) IS NOT NULL and datepart(t1.PRP_END_DATE) - &gv_date_dly. >= 30 THEN 'AKTYWNA'
when datepart(t1.PRP_END_DATE) IS NULL THEN 'BRAK INFORMACJI O POLISIE'
end as POLISA_INFORMACJA
from
cmz.WMDTZDP_BH t1
left join
(select distinct kontr_id,obj_oid from cmz.BH_D_ZAB_X_ALOK_&thismonth) t2
on t2.obj_oid = t1.obj_oid
left join
(select distinct data_danych, kontr_id, kre_nrb from dm.BH_WMDTKRE_&thismonth) t3
on t3.kontr_id = t2.kontr_id
left join
(select distinct spr_NRB, spr_STATUS from _mart.mart_kred) t4
on t4.spr_NRB = t3.kre_nrb
where datepart(t1.data_danych) between '5Aug2019'd and &gv_date_dly. and t1.Actual = "T"
and t4.spr_STATUS ="A"
; SECOND CAME FROM FIRST
create table PolisyEnd1 as
select distinct
DATE_
,(POLICY_VINTAGE)
,count(NRB) as NUMBER
,POLISA_INFORMACJA
from PolisyEnd
where INFORMATION ="U"
;
Quit;
EDIT 1 :
I got the result, but how to do so that for 2021-11 there is one result and summed up all records for this period
Rather than using a distinct here what you really want is a GROUP BY.
PROC SQL;
create table PolisyEnd1 as
select
DATE_
,(POLICY_VINTAGE)
,count(NRB) as NUMBER
,POLISA_INFORMACJA
from PolisyEnd
where INFORMATION ="U"
group by DATE_, (POLICY_VINTAGE), POLISA_INFORMACJA
;
Quit;
You can use group by
If you want to count just based on the DATE_ column here is an example
select DATE_, count(NRB) as NUMBER
from PolisyEnd
where INFORMATION ="U"
group by DATE_
Otherwise, you can add other columns also in the group by and select clause.
For Edit1:
For each month you can use this:
select POLICY_VINTAGE, SUM(NUMBER) as NUMBER
from Your_Table
group by POLICY_VINTAGE
I have two tables :
I want to use the chart_num value for two tables.
Table hospital_payment_data
id chart_num treatment_fees_difference treatment_fees_check_division
1 9 200000 test
2 9 -100000 test
3 10 200000 test
4 10 -100000 test
Table advenced_payment
id chart_num advenced_amount
1 9 100000
2 10 100000
I want result
if_treatment_fees_check_division sum_init_amount test COUNT
200000 200000 400000 4
However, when you send a query, the following results are printed.
SELECT
SUM(t_join.treatment_fees_difference) if_treatment_fees_check_division,
SUM(t_join.advenced_amount) sum_init_amount,
SUM(t_join.treatment_fees_difference) + SUM(t_join.advenced_amount) test,
COUNT(*) "count"
FROM
(
SELECT t_a.treatment_fees_difference , IFNULL(t_b.advenced_amount,0 ) AS advenced_amount
FROM hospital_payment_data t_a
LEFT OUTER JOIN advenced_payment t_b on t_a.chart_num = t_b.chart_num
WHERE t_a.treatment_fees_check_division = 'test'
) t_join
bad result
How do I fix my query to get the results I want?
The problem appears to be that you are joining the tables, which will give you two rows in the results, then you are summing the values with sum(t_join.advenced_amount) sum_init_amount which is giving double the value you want.
This is a quick and nasty fix:
SELECT
sum(t_join.treatment_fees_difference) if_treatment_fees_check_division,
MIN(t_join.advenced_amount) sum_init_amount ,
sum(t_join.treatment_fees_difference) + sum(t_join.advenced_amount) test,
COUNT(*) "count"
FROM
(
SELECT t_a.treatment_fees_difference , IFNULL(t_b.advenced_amount,0 ) AS advenced_amount
FROM hospital_payment_data t_a LEFT OUTER JOIN advenced_payment t_b on t_a.chart_num = t_b.chart_num
WHERE t_a.treatment_fees_check_division = 'test'
) t_join
But that will probably fail in a real world application.
Try it like this (untested):
SELECT
if_treatment_fees_check_division,
advenced_amount AS sum_init_amount ,
if_treatment_fees_check_division + advenced_amount AS test,
row_count AS "count"
FROM
(
SELECT
SUM(treatment_fees_difference) as if_treatment_fees_check_division,
count(*) as row_count,
chart_num
FROM hospital_payment_data
WHERE treatment_fees_check_division = 'test'
GROUP BY chart_num
) as TABLE1
LEFT OUTER JOIN advenced_payment AS TABLE2
ON TABLE1.chart_num = TABLE2.chart_num
I want to create a table using with clause:
For example :
with cte as
(SELECT B.STEUC "HSN",
CASE WHEN A.FORMAT_CD='520' THEN '2' ELSE A.FORMAT_CD END FORMAT_CD,
CASE WHEN A.FORMAT_CD='520' THEN 'DIGITAL' ELSE A.FORMAT_DESC END FORMAT_DESC,
A.ARTICLE,
A.REGION "STATE",
SUM(CASE WHEN BWART IN ('702','704','708','711','713','715','717','551','553','555','903','909','951','Z09') THEN DMBTR ELSE 0 END) -
SUM(CASE WHEN BWART IN ('701','703','707','712','714','716','718','552','554',' 556','904','910','952','Z10') THEN DMBTR ELSE 0 END) "LOSS_VALUE"
FROM "_SYS_BIC"."RRA.DnL/CV_STOCK_MOVEMENT" A INNER JOIN "P22"."MARA" B ON A.ARTICLE=B.MATNR
WHERE posting_date BETWEEN '20181101' AND '20181130' AND
BWART IN ('702','704',' 708',' 711','713','715','717','701','703','707','712','714','716','718','551',
'552','553','554','555','556','903','904','909','910','951','952','Z09','Z10') AND
A.COMPANY_CODE='9008' AND
A.LEVEL2 NOT IN ('10','99') AND
A.LEVEL5 NOT IN ('140601010') AND
A.FORMAT_CD NOT IN ('51','56','62','509')
GROUP BY B.STEUC,A.ARTICLE,A.REGION,A.COMPANY_CODE,A.FORMAT_CD,
A.FORMAT_DESC)
SELECT A.HSN,
A.STATE,
A.FORMAT_CD,
A.FORMAT_DESC,
A.ARTICLE,
A.LOSS_ART,
B.LOSS
FROM (
SELECT A.HSN,
A.STATE,
A.FORMAT_CD,
A.FORMAT_DESC,
A.ARTICLE,
A.LOSS LOSS_ART,
SUM(A.LOSS) OVER (PARTITION BY A.HSN,A.STATE,A.FORMAT_CD ORDER BY LOSS DESC) LOSS
FROM (SELECT A.HSN,A.STATE,A.FORMAT_CD,A.FORMAT_DESC,A.ARTICLE,SUM(LOSS_VALUE) LOSS FROM
--"RR_ANALYST"."REETIKA_LOSS_DATA_1"
cte A
INNER JOIN P22.MARA B ON A.ARTICLE=B.MATNR
WHERE B.ATTYP<>'11'
GROUP BY A.HSN,A.STATE,A.FORMAT_CD,A.FORMAT_DESC,A.ARTICLE
HAVING SUM(LOSS_VALUE)>0 ) A
) A ,
(SELECT A.HSN,A.STATE,A.FORMAT_CD,SUM(LOSS_VALUE) LOSS FROM
cte A
group by A.HSN,A.STATE,A.FORMAT_CD HAVING SUM(LOSS_VALUE)>0) B
WHERE A.HSN=B.HSN AND
A.STATE=B.STATE AND
A.FORMAT_CD=B.FORMAT_CD AND
A.LOSS<=B.LOSS*1
Which i guess is not supported in Hana.
What can be an alternative to the same ?
Does hana support spooling like oracle ?
I know i can export the result set and then create a table accordingly.
But is there any way to achieve and create the table dynamically ?
An alternative is the oldfashioned way - an inline view.
CREATE column TABLE t AS
SELECT *
FROM (SELECT 1 as some_value --> this is your WITH factoring clause
FROM dummy
UNION ALL
SELECT 2
FROM dummy
);
I have a question about creating a running total on an existing query in SQL Server 2008 R2.
Basically, I have a query that combines data from 3 separate tables and groups them to produce a single entry for each combination of ACCOUNT, PROFITCENTRE, TIMEID and DIVISION
However, I need to alter the values in SIGNEDDATA, such that each entry is the total of all previous months.
e.g.
TIMEID 20120300 (March 2012) would contain -35143.0000000000
TIMEID 20120400 (April 2012) should then contain -36000.0000000000 (March plus the -857 for April)
TIMEID 20120500 (May 2012) should be -36857.0000000000, etc.,
Rather than what I currently get, which is the sum of SIGNEDDATA for each month, grouped by ACCOUNT, PROFITCENTRE, DIVISION, etc, but not added to previous months.
How can I do this, as I have tried removing TIMEID from the GROUP BY clause, but I just get the usual error about not being able to retrieve the column as it is not contained in either an aggregate or Group by......?
My selection is as follows:
Insert INTO Data_Services.dbo.[NEW_TABLE]
Select
[Account],
[Category],
[DataSrc],
[ProfitCentre],
'MY_FLOW' as [Flow],
[RptCurrency],
[TimeID],
sum(round([SignedData],2,0)) as SignedData,
[Division],
#CurrentTime as CTimeID
FROM
(select
T1.[Account],
T1.[Category],
T1.[DataSrc],
T1.[ProfitCentre],
T1.[Flow],
T1.[RptCurrency],
T1.[TimeID],
sum(round(T1.[SignedData],2,0)) as SignedData,
mbrPC.[Division]
from
MY_DATABASE.dbo.TABLE1 T1
join
MY_DATABASE.dbo.mbrProfitCentre mbrPC on T1.ProfitCentre = mbrPC.[ID]
where
T1.Category = 'WForecast'
and T1.DataSrc in (SELECT [ID] from DSParent)
and T1.Flow = 'MOVEMENT'
and T1.Account in
(SELECT [ID] from AccIEParent)
and TimeID in
(select [TimeID] from MY_DATABASE.dbo.mbrTime
where [Period_Start] <> ''
and [Period_Start] is not null
and convert(date,[Period_Start],101) <= '2013-01-24'
and [CURRYEAR] = 'Y')
group by T1.[Account],
T1.[Category],
T1.[DataSrc],
T1.[ProfitCentre],
T1.[Flow],
T1.[RptCurrency],
T1.[TimeID],
mbrPC.[Division]
UNION
select
T2.[Account],
T2.[Category],
T2.[DataSrc],
T2.[ProfitCentre],
T2.[Flow],
T2.[RptCurrency],
T2.[TimeID],
sum(round(T2.[SignedData],2,0)) as SignedData,
mbrPC.[Division]
from MY_DATABASE.dbo.TABLE2 T2
join MY_DATABASE.dbo.mbrProfitCentre mbrPC
on T2.ProfitCentre = mbrPC.[ID]
where T2.Category = 'WForecast'
and T2.DataSrc in
(SELECT [ID] from DSParent)
and T2.Flow = 'MOVEMENT'
and T2.Account in
(SELECT [ID] from AccIEParent)
and TimeID in
(select [TimeID] from MY_DATABASE.dbo.mbrTime
where [Period_Start] <> ''
and [Period_Start] is not null
and convert(date,[Period_Start],101) <= '2013-01-24'
and [CURRYEAR] = 'Y')
group by T2.[Account],
T2.[Category],
T2.[DataSrc],
T2.[ProfitCentre],
T2.[Flow],
T2.[RptCurrency],
T2.[TimeID],
mbrPC.[Division]
UNION
select
T3.[Account],
T3.[Category],
T3.[DataSrc],
T3.[ProfitCentre],
T3.[Flow],
T3.[RptCurrency],
T3.[TimeID],
sum(round(T3.[SignedData],2,0)) as SignedData,
mbrPC.[Division]
from MY_DATABASE.dbo.TABLE3 T3
join MY_DATABASE.dbo.mbrProfitCentre mbrPC
on T3.ProfitCentre = mbrPC.[ID]
where T3.Category = 'WForecast'
and T3.DataSrc in
(SELECT [ID] from DSParent)
and T3.Flow = 'MOVEMENT'
and T3.Account in
(SELECT [ID] from AccIEParent)
and TimeID in
(select [TimeID] from MY_DATABASE.dbo.mbrTime
where [Period_Start] <> ''
and [Period_Start] is not null
and convert(date,[Period_Start],101) <= '2013-01-24'
and [CURRYEAR] = 'Y')
group by T3.[Account],
T3.[Category],
T3.[DataSrc],
T3.[ProfitCentre],
T3.[Flow],
T3.[RptCurrency],
T3.[TimeID],
mbrPC.[Division]
) a
group by [Account],
[Category],
[DataSrc],
[ProfitCentre],
[Flow],
[RptCurrency],
[TimeID],
[Division]
And the resulting data-set is:
What you're trying to do is slightly more complex than the examples in this ticket but there are answers here with various methods of calculating a cumulative sum.
how to get cumulative sum
I have the following query
SELECT ProgramDate, [CountVal]= COUNT(ProgramDate)
FROM ProgramsTbl
WHERE (Type = 'Type1' AND ProgramDate = '10/18/11' )
GROUP BY ProgramDate
What happens is that if there is no record that matches the Type and ProgramDate, I do not get any records returned.
What I like to have outputted in the above is something like the following if there is no values returned. Notice how for the CountVal we have 0 even if there are no records returned that fit the match condition:
ProgramDate CountVal
10/18/11 0
This is a little more complicated than you would like however, it is very possible. You will first have to create a temporary table of dates. For example, the query below creates a range of dates from 2011-10-11 to 2011-10-20
CREATE TEMPORARY TABLE date_stamps AS
SELECT (date '2011-10-10' + new_number) AS date_stamp
FROM generate_series(1, 10) AS new_number;
Using this temporary table, you can select from it and left join your table ProgramsTbl. For example
SELECT date_stamp,COUNT(ProgramDate)
FROM date_stamps
LEFT JOIN ProgramsTbl ON ProgramsTbl.ProgramDate = date_stamps.date_stamp
WHERE Type = 'Type1'
GROUP BY ProgramDate;
Select ProgramDate, [CountVal]= SUM(occur)
from
(
SELECT ProgramDate, 1 occur
FROM ProgramsTbl
WHERE (Type = 'Type1' AND ProgramDate = '10/18/11' )
UNION
SELECT '10/18/11', 0
)
GROUP BY ProgramDate
Because each SELECT statement is really building a table of records you can use a SELECT query to build a table with both the program count and a default count of zero. This would require two SELECT queries (one to get the actual count, one to get the default count) and using a UNION to combine the two SELECT results into a single table.
From there you can SELECT from the UNIONed table to sum the CountVals (if the programDate occurs in the ProgramTable the CountVal will be
CountVal of the first query if it exists(>0) + CountVal of the second query (=0)).
This way even if there are no records for the desired programDate in ProgramTable you will get a record back indicating a count of 0.
This would look like:
SELECT ProgramDate, SUM(CountVal)
FROM
(SELECT ProgramDate, COUNT(*) AS CountVal
FROM ProgramsTbl
WHERE (Type = 'Type1' AND ProgramDate = '10/18/11' )
UNION
SELECT '10/18/11' AS ProgramDate, 0 AS CountVal) T1
Here's a solution that works on SQL Server; not sure about other db platforms:
DECLARE #Type VARCHAR(5) = 'Type1'
, #ProgramDate DATE = '10/18/2011'
SELECT pt.ProgramDate
, COUNT(pt2.ProgramDate)
FROM ( SELECT #ProgramDate AS ProgramDate
, #Type AS Type
) pt
LEFT JOIN ProgramsTbl pt2 ON pt.Type = pt2.Type
AND pt.ProgramDate = pt2.ProgramDate
GROUP BY pt.ProgramDate
Grunge but simple and efficient
SELECT '10/18/11' as 'Program Date', count(*) as 'count'
FROM ProgramsTbl
WHERE Type = 'Type1' AND ProgramDate = '10/18/11'
Try something along these lines. This will establish a row with a date of 10/18/11 that will definitely return. Then you left join to your actual data to get your desired count (which can now return 0 if there are no corresponding rows).
To do this for more than 1 date, you'd want to build a Date table that holds a list of all dates you want to query (so substitute the "select '10/18/11'" with "select Date from DateTbl").
SELECT ProgDt.ProgDate, [CountVal]= COUNT(ProgramsTbl.ProgramDate)
FROM (SELECT '10/18/11' as 'ProgDate') ProgDt
LEFT JOIN ProgramsTbl
ON ProgDt.ProgDate = ProgramsTbl.ProgramDate
WHERE (Type = 'Type1')
GROUP BY ProgDt.ProgDate
To create a date table that you can use for querying, do this (assumes SQL Server 2005+):
create table Dates (MyDate datetime)
go
insert into Dates
select top 100000 row_number() over (order by s1.name)
from master..spt_values s1, master..spt_values s2
go