Insert two select queries in a single row - sql

I have two select statements, both having some common fields.
This is my table structure :
Id, year, valueA, ValueB
Here is my two select statements
SELECT
id, [year],
SUM(Total_Volume) / (count(distinct(month)) * 7)
FROM
TableA
GROUP BY
id, [year]
and
SELECT
id, [year],
SUM(Total_volume) / (count(distinct(month)) * 5)
FROM
TableA
WHERE
weekday NOT IN ('Friday','Saturday')
GROUP BY
station_id, [year]
I have two different conditions for two statements. Id and year is common for both the statements.
Result from first select statement should be stored in column valueA and result from second select statement should be stored in ValueB .
Any possible way to combine these queries and insert them into table as a single statement?

SELECT a.id ,a.[year],valA,valB
from(
SELECT id ,[year],SUM(Total_Volume)/ (count(distinct(month))*7) valA from TableA
group by id,[year]) a INNER JOIN
(select id,[year],SUM(Total_volume)/(count(distinct(month))*5) valB
from TableA
WHERE weekday NOT IN ('Friday','Saturday')
group by station_id,[year]) b
on a.id=b.id and a.[year]=b.[year]

You could do both the calculation in a single query
SELECT id,
[year],
Sum(total_volume) / ( Count(DISTINCT( Month)) * 7 ),
Sum(CASE
WHEN weekday NOT IN ('Friday', 'Saturday' ) THEN
total_volume
ELSE 0
END) / (Count(DISTINCT( Month)) * 5 )
FROM tableA
GROUP BY id,
[year];

Related

I want to get difference of sum of a column from two tables

I have 2 tables: transactions and transactions_archive. Each of them has fields accountno,drcr(which has values either as C or D) and field amount. I want to get difference of sum of all 'C' in both transactions and transactions_archive and sum of all 'D' in both transactions and transactions_archive.
What query can I use to get this answer.
I tried this unsuccessfully:
select (
select accountno,drcr,sum(amount)as total from
(
select accountno,drcr,amount
from ebank.tbtransactions
where drcr='C'
union all
select accountno,drcr,amount
from ebank.tbtransactions_archive
where drcr='C'
)
)
-
(select accountno,drcr,sum(amount)as total
from (
select accountno,drcr,amount
from ebank.tbtransactions
where drcr='D'
union all
select accountno,drcr,amount
from ebank.tbtransactions_archive
where drcr='D'
)
)
group by accountno,drcr;
If I understand correctly, you want to subtract all the "D"s from the "C"s. Combine the tables using UNION ALL and use conditional aggregation:
select accountno,
sum(case when drcr = 'C' then amount else - amount end)as total
from ((select accountno, drcr, amount
from ebank.tbtransactions
) union all
(select accountno, drcr, amount
from ebank.tbtransactions_archive
)
) t
where drcr in ('D', 'C')
group by accountno;
SELECT top 1 (amount - nextamount) as diff from(
SELECT
amount,LEAD(amount, 1,0) OVER (ORDER BY YEAR(drcr)) AS nextamount FROM(
SELECT drcr, sum(amount) as amount from transactions JOIN transactions_archive on transactions.drcr and transactions_archive.drcr GROUP BY drcr))

How to define unique value in union if two rows are not same data

I'm creating a simple SQL query with union, the result is returned correctly, but how to set a default value in a dummy column if the union result has two rows for one value?
If the result returned two values for one employee, then the dummy column is 'N' for the first value and 'Y' for the second value.
And if the result returned only one value for the employee, then the dummy column is 'Y'
How to achieve that?
This is the query that I'm using
select
dbo.employee,
dbo.starting_date
from
table_1
union
select
dbo.employee,
dbo.hiring_date
from
table_2
With a CTE:
with cte as (
select dbo.employee, dbo.starting_date date from table_1
union all
select dbo.employee, dbo.hiring_date date from table_2
)
select
t.*,
case when exists (
select 1 from cte
where employee = t.employee and date > t.date
) then 'N' else 'Y' end dummycolumn
from cte t
You can use window functions for this:
select t.employee, t.date,
(case when 1 = row_number() over (partition by t.employee order by t.date)
then 'Y' else 'N'
end) as dummy
from ((select t1.employee, t1.starting_date as date
from table_1 t1
) union all
(select t2.employee, t2.starting_date as date
from table_2 t2
)
) t

SAP HANA | With Clause performance

We are using SAP HANA 1.0 SPS12.
We have daywise table like below -
select trans_date,article,measure1,measure2 from table_1
Volume of table ~ 5 millions rows
we need to see data like -
select 'day-1',sum(measure1),sum(meaure2) from table1 where trans_date=add_days(current_date,-1) group by 'day-1'
union all
select 'day-2',sum(measure1),sum(meaure2) from table1 where trans_date>=add_days(current_date,-2) group by 'day-2'
union all
select 'WTD',sum(measure1),sum(meaure2) from table1 where trans_date>=add_days(current_date,-7) group by 'WTD'
union all
select 'WTD-1',sum(measure1),sum(meaure2) from table1 where trans_date>=add_days(current_date,-15) and trans_Date <= add_days(current_date,-7) group by 'WTD-1'
so on and so forth for MTD,MTD-1,MTD-2,YTD.
Performance wise is it better to use WITH CLAUSE and hold data for one year and then split according to timeframe? OR Is it better to use separate aggregation for each timeframe as shown above.
As far as I understand, in RDBMSs like Oracle, WITH CLAUSE materializes results and use it from the memory. SAP HANA is In Memory database itself. Does using WITH CLAUSE in SAP HANA gives distinctive performance edge?
Query using WITH CLAUSE -
WITH t1 as
(
select trans_date,sum(measure1),sum(meaure2) from table1 where trans_date>=add_days(current_date,-365)
)
select 'day-1',sum(measure1),sum(meaure2) from t1 where trans_date=add_days(current_date,-1) group by 'day-1'
union all
select 'day-2',sum(measure1),sum(meaure2) from t1 where trans_date>=add_days(current_date,-2) group by 'day-2'
union all
select 'WTD',sum(measure1),sum(meaure2) from t1 where trans_date>=add_days(current_date,-7) group by 'WTD'
union all
select 'WTD-1',sum(measure1),sum(meaure2) from t1 where trans_date>=add_days(current_date,-15)
and trans_Date <= add_days(current_date,-7)
group by 'WTD-1'
If you care about performance, putting the data in a single row should be much better:
select sum(case when trans_date = add_days(current_date, -1) then measure1 end) as measure1_day1,
sum(case when trans_date = add_days(current_date, -1) then measure2 end) as measure2_day1,
sum(case when trans_date = add_days(current_date, -2) then measure1 end) as measure1_day2,
sum(case when trans_date = add_days(current_date, -2) then measure2 end) as measure2_day2,
. . .
from table1
where trans_date >= add_days(current_date, -15);
You can unpivot the results afterwards, if you really need the values in separate rows.
Alternatively, you can do:
select days, sum(measure1), sum(measure2)
from (select 1 as days from dummy union all
select 2 from dummy union all
select 7 from dummy union all
select 15 from dummy
) d left join
table1 t
on t.trans_date = add_days(current_date, - d.days)
group by days
order by days;

how to find all column records are same or not in group by column in SQL

How to find all column values are same in Group by of rows in table
CREATE TABLE #Temp (ID int,Value char(1))
insert into #Temp (ID ,Value ) ( Select 1 ,'A' union all Select 1 ,'W' union all Select 1 ,'I' union all Select 2 ,'I' union all Select 2 ,'I' union all Select 3 ,'A' union all Select 3 ,'B' union all Select 3 ,'1' )
select * from #Temp
Sample Table:
How to find all column value of 'Value' column are same or not if group by 'ID' Column.
Ex: select ID from #Temp group by ID
For ID 1 - Value column records are A, W, I - Not Same
For ID 2 - Value column records are I, I - Same
For ID 3 - Value column records are A, B, 1 - Not Same
I want the query to get a result like below
When all items in the group are the same, COUNT(DISTINCT Value) would be 1:
SELECT Id
, CASE WHEN COUNT(DISTINCT Value)=1 THEN 'Same' ELSE 'Not Same' END AS Result
FROM MyTable
GROUP BY Id
If you're using T-SQL, perhaps this will work for you:
SELECT t.ID,
CASE WHEN MAX(t.RN) > 1 THEN 'Same' ELSE 'Not Same' END AS GroupResults
FROM(
SELECT *, ROW_NUMBER() OVER(PARTITION BY ID, VALUE ORDER BY ID) RN
FROM #Temp
) t
GROUP BY t.ID
Usally that's rather easy: Aggregate per ID and count distinct values or compare minimum and maximum value.
However, neither COUNT(DISTINCT value) nor MIN(value) nor MAX(value) take nulls into consideration. So for an ID having value 'A' and null, these would detect uniqueness. Maybe this is what you want or nulls don't even occur in your data.
But if you want nulls to count as a value, then select distinct values first (where null gets a row too) and count then:
select id, case when count(*) = 1 then 'same' else 'not same' end as result
from (select distinct id, value from #temp) dist
group by id
order by id;
Rextester demo: http://rextester.com/KCZD88697

DB2 how to sum two column from two different table

Select sum(amt) as totalA from tableA where id>10;
Select sum(amount) as totalB from tableB where people = 'JOSH';
What is the best way if the objective is to have sum(totalA + totalB)?
select sum(total) from
(
select sum(amt) as total from tableA where id>10
union all
select sum(amount) from tableB where people = 'JOSH'
) as q