SUM function in SQL - sql

There are three tables,we have to select data from these table using one primary key and foreign key. But in the one table there is lot of data in the third table. We have to sum the data on the base of the primary key.
BAl = Balance, met = Method, amo = amount, cst_id, cut_id, cut_i = customer_id
Now we have to sum the on the basis of method and sum for 10 cust id in the same query. Can anyone help me on this?

;WITH cte
AS
(
SELECT
*,
ROW_NUMBER() OVER (ORDER BY t1.cst_id) RowNum
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.cst_id = t2.cut_id
INNER JOIN Table3 t3
ON t2.cut_id = t3.customer_id
AND t2.BAL = t3.Balance
AND t2.amo = t3.amount
)
SELECT SUM(*)
FROM cte
WHERE RowNum Between 1 AND 10
-- You can add a GROUP BY here

If you give some sample data it will be easier to write queries to help you.
But if your MET field is numerical and you want to sum it then you need.
select
t1.cst_n, t2.bal,
sum(t3.met) as met,
sum(t3.amo) as amo
from table1 as t1
inner join table2 as t2 on t2.cut_id = t1.cst_id
inner join table3 as t3 on t3.cut_i = t1.cst_id
group by t1.cst_n, t2.bal
well if you want to sum data for all 10 customers into one number, may be you just need
select
sum(t3.met) as met,
sum(t3.amo) as amo
from table3 as t3
where t3.cut_i in (select t.customerid from #<your variable table with cust. ids> as t)

Related

SQL Query to Join Two Tables where data in one table is transposed

I'm trying to join two tables but not getting the desired results. Im new to SQL and will need help to joining these two tables.
The data in Table 2 is transposed and the table will only be populated only when the "value" field is populated with random values.
Details of Table 1 and Table 2 and expected output
You could use CTEs to split up the impact from outcome. Also, your screenshot had an error for task_id 6.
with t_impact as (
select task_id, value
from table2
where name = 'task_impact'
),
t_outcome as (
select task_id, value
from table2
where name = 'task_outcome'
)
select distinct t1.id,
t1.title,
i.value as task_impact,
o.value as task_outcome
from table1 t1
left join t_impact i
on t1.id = i.task_id
left join t_outcome o
on t1.id = o.task_id
order by t1.id
DB-fiddle found here.

merge multiple tables in Hive SQL

I have two original tables: product table and component tables
Firstly, I need to do an inner merge between these two tables, then create a new table called T1:
select a.Product, a.Plant, b.component, b.position, b.valid_date from Product a
inner join component b on a.Product=b.Product
where a.Plant='A'
The result T1 looks like in the following way:
Furthermore, I need to create a new table named T2 based on T1
select T1.Product, T1.Plant,T1.position,max(T1.valid_date) as valid_date from T1
Where T1.Plant='A'
Group by T1.Product, T1.Plant,T1.position
The result T2 is:
Finally, I want to merge T1 and T2 based on Product, Plant, position, and valid_date for a final table:
select T2.Product, T2.Plant,T1.Component, T2.position, T2.valid_date from T1
INNER JOIN T2 on T1.Product=T2.Product and T1.Plant=T2.Plant and T1.position=T2.position and T1.valid_date=T2.valid_date
where T1.Plant='A'
The final table:
I know this whole process can be done in one hive SQL script. I am confused with multiple tables in one query. I appreciate someone can help me for that. Thank you
isn't it that you just want to do this?
select
a.Plant,b.Product,b.position,b.component,
max(valid_date) as valid_date
from Product a
inner join component b on a.Product = b.Product
where a.Plant = 'A'
group by a.Plant,b.Product,b.position,b.component
db<>fiddle here
the query above produces what you show in output , but based on your comment , I think this is what you are looking for :
select t.Plant,t.Product,t.position,t.component, max(t.valid_date) valid_date
(
select
b.Product, a.Plant,b.component,b.position,max(valid_date) over (partition by a.Plant,b.Product,b.position) as valid_date
from Product a
join component b on a.Product = b.Product
where a.Plant = 'A'
) t
group by t.Plant,t.Product,t.position,t.component;

Sum Column in Joined Table and add as column SQL

So say I have two tables in Oracle SQL (not actually data but for ease should highlight my question)
Table1 that contains only Order_id and Order_quantity and Table2 that contains only Order_id and Order_price
Then I join them as follows
Select T1.Order_id,
T1.Order_quantity,
T2.Order_price,
T1.Order_quantity*T2.Order_price As "Order_amount",
Sum(Order_amount) As "Total_Sales"
from Table1 T1
inner join Table2 T2
on T1.Order_id = T2.Order_id
So essentially I want to have two extra columns, one as the product of columns from the two tables, and another as the sum of that column in my joined table(so every entry will be the same). However as you need to
SUM(variable_name) From Table_Name
Can I assign a variable name to my new table and then refer to that. I tried the following but I'm getting a SQL command not properly ended error
Select T1.Order_id,
T1.Order_quantity,
T2.Order_price,
T1.Order_quantity*T2.Order_price As "Order_amount",
Sum(Order_amount) from New_Table As "Total_Sales"
from (Table1 T1
inner join Table2 T2
on T1.Order_id = T2.Order_id) As New_Table
Thanks for any assistance, apologies as I have a pretty naive understanding of SQL at present
I think you just want a window function:
select T1.Order_id, T1.Order_quantity, T2.Order_price,
T1.Order_quantity*T2.Order_price As order_amount,
sum(T1.Order_quantity*T2.Order_price) over () As Total_Sales
from Table1 T1 inner join
Table2 T2
on T1.Order_id = T2.Order_id
You cannot re-use the alias order_amount in the select. You need to repeat the expression -- or use a subquery or CTE to define it.
If your DBMS doesn't have a window function supports then you can use subquery instead
select order_id, Order_quantity,
(select t1.Order_quantity * t2.Order_price
from table2 t2
where t2.Order_id = t1.Order_id) as Order_amount,
(select sum(t1.Order_quantity * t2.Order_price)
from table2 t2
where t2.Order_id = t1.Order_id) as Total_Sales
from table1 t1;

SQL Tables need to be appended, but not like JOINS

I've a scenario as below.
I've two tables, and a Common column/Key between the two.
I need Table2 data to be just appended to Table1 without repetition like JOINs.
If there are more rows in one table, other table rows can be NULL.
As shown in figure, Result Table has NULLs when there is no corresponding row count from Table2.
I tried using Joins, but I'm getting a result of 45 rows. But I should get 9 rows.
Thanks in advance.
Edit: Added my queries
SELECT DISTINCT
APPT.PRSN_ID
,APPT.SCHEDULEDAPPOINTMENTS
,APPT.OVERDUEAPPOINTMENTS
,Visit.ENCOUNTER_CATEGORY
,Visit.ENCOUNTER_TYPE
,Visit.ENCOUNTER_DATE
,Visit.ENCOUNTER_FOLLOWUP_DATE
FROM
APPOINTMENTS APPT
OUTER APPLY dbo.fn_GetVisitsOfAPerson(PROV.PRSN_ID) AS Visit
/***********************************************************/
--IN THE ABOVE QUERY, THE FUNCTION IS DEFINED AS BELOW
CREATE FUNCTION dbo.fn_GetVisitsOfAPerson(#PrsnID AS bigint)
RETURNS TABLE
AS
RETURN
(
SELECT
VISITS.PVISITS_PRSN_KEY
,DATA.HE_Category_Description AS 'ENCOUNTER_CATEGORY'
,DATA.HE_Type_Description AS 'ENCOUNTER_TYPE'
,VISITS.PVISITS_DATE AS 'ENCOUNTER_DATE'
,VISITS.PVISITS_FOLLOWUP_DATE AS 'ENCOUNTER_FOLLOWUP_DATE'
FROM
[HS_PRSN_HEALTH_VISITS] VISITS INNER JOIN
[HS_HealthEncounter_Table] DATA ON
ENCOUNTER.PVISITS_CATEGORY = DATA.HE_Category_Code AND
ENCOUNTER.PVISITS_TYPE = DATA.HE_Type_Code
WHERE
PVISITS_PRSN_KEY = #PrsnID
AND PVISITS_VOID = 0
)
GO
I cannot read your data tables, but I think you just need to introduce a row number.
It would be something like this:
select . . .
from (select t1.*, row_number() over (partition by key order by ??) as seqnum
from table1 t1
) full join
(select t2.*, row_number() over (partition by key order by ??) as seqnum
from table1 t2
) t2
on t1.key = t2.key and t1.seqnum = t2.seqnum;
Based on you wanting the 9 rows present in the Visit table it looks like you need an OUTER JOIN, something like:
SELECT DISTINCT
APPT.PRSN_ID
,APPT.SCHEDULEDAPPOINTMENTS
,APPT.OVERDUEAPPOINTMENTS
,Visit.ENCOUNTER_CATEGORY
,Visit.ENCOUNTER_TYPE
,Visit.ENCOUNTER_DATE
,Visit.ENCOUNTER_FOLLOWUP_DATE
FROM Visit
LEFT OUTER JOIN
APPOINTMENTS APPT
ON Visit.key = APP.key

Left Join with duplicate keys in the right table

I'm trying merging 2 tables as follow
SELECT * FROM T1
LEFT JOIN T2 ON T1.EMPnum = T2.EMPnum
The above works well but I need the joining to use only the first appearance of the common key EMPnum record in table T2 so that the query returns exactly the same number of rows as T1
Thanks Avi
SQL tables are inherently unordered, so there is no such thing as a "first" key. In most databases, you can do something like this:
with t2 as (
select t2.*, row_number() over (partition by EMPnum order by id) as seqnum
from t2
)
select *
from t1 left join
t2
on t1.EMPnum = t2.EMPnum and t2.seqnum = 1;
Here id is just any column that specifies the ordering. If none exist, you can use EMPnum to get an arbitrary row.