SQL SUM function inquiry - sql

I'm having a hard time summing up a column on two tables. The scenario is something like this (refer to the image below)
Table 1 may have a lot of rows per Date. But Table 2 may only consists of two rows of data per Date. What I wanted to do is to sum up all Item/Price (Table1) according to their Date and ADD them with another SUM of Item/Price of Table2. The category of SUM is by Date.
I tried any joins statement (left, right or inner) but it does not produce the result that I am expecting to. My expected result is the Result table. But on my query, it produces a very high value.
Thanks.

Use a UNION clause like this:
WITH t(d, p) AS (
SELECT [Date], Price FROM Table1
UNION ALL
SELECT [Date], Price FROM Table2
)
SELECT d, SUM(p) FROM t GROUP BY d

You can do this with UNION ALL in either a subquery or a cte, cte shown here:
;WITH cte AS (SELECT [Date], Price
FROM Table1
UNION ALL
SELECT [Date], Price
FROM Table2
)
SELECT [Date], SUM(Price) AS Total_Price
FROM cte
GROUP BY [Date]
Demo: SQL Fiddle

Try This,
with cte (C_Date,C_Price)
as
(
SELECT date,SUM(price) FROM table_1
group by date
union
SELECT date,SUM(price) FROM table_2
group by date
)
select c_date,SUM(c_price) from cte
group by C_Date

Try this
Select t.date,P1+P2
from(
Select Date,sum(Price) P1
from table1 t
group by Date
) t
left join
(
Select Date,sum(Price) P2
from table t2
group by date
) t1 on t.date = t1.date
group by date

Related

SQL - Combining two queries into one query

I have 2 queries.
For table1,
SELECT codesTable.code_id,COUNT(*) FROM table1,codesTable
WHERE table1.codeid=CodeTable.code_id
AND table1.type_no IN (1,2,3,4,5,6,7,8,9,10)
AND table1.recoverd_value IN (0,1)
GROUP BY table1.code_id
For Table2
SELECT codesTable.code_id,COUNT(*) FROM table2,codesTable
WHERE table2.codeid=CodeTable.code_id
AND table2.type_no IN (1,2,3,4,5,6,7,8,9,10)
AND table2.recoverd_value IN (0,1)
GROUP BY table2.code_id
Need to combine this as one query.
Note:
Result for 1st query: code_id and count of Table1 and
Result for 2nd query: code_id, count of table2.
Current output like code_id,count of table(1,2)
SELECT code_id, SUM(counted)
FROM (
SELECT code_id,COUNT(*) AS counted
FROM table1,codesTable
WHERE table1.codeid=CodeTable.code_id
AND table1.type_no IN (1,2,3,4,5,6,7,8,9,10)
AND table1.recoverd_value IN (0,1)
GROUP BY table1.code_id
UNION ALL
SELECT code_id,COUNT(*) AS counted
FROM table2,codesTable
WHERE table2.codeid=CodeTable.code_id
AND table2.type_no IN (1,2,3,4,5,6,7,8,9,10)
AND table2.recoverd_value IN (0,1)
GROUP BY table2.code_id
) AS subq
GROUP BY code_id
;
Don't forget to use UNION ALL instead of UNION because UNION ALL saves duplicates from both tables.
You can just use union for this operation where two tables will be combined excluding the duplicates. Code is Shown below.
(SELECT code_id,COUNT(*) FROM table1,codesTable
WHERE table1.codeid=CodeTable.code_id
AND table1.type_no IN (1,2,3,4,5,6,7,8,9,10)
AND table1.recoverd_value IN (0,1)
GROUP BY table1.code_id)
UNION
(SELECT code_id,COUNT(*) FROM table2,codesTable
WHERE table2.codeid=CodeTable.code_id
AND table2.type_no IN (1,2,3,4,5,6,7,8,9,10)
AND table2.recoverd_value IN (0,1)
GROUP BY table2.code_id)
Hope this clarified your doubt.
You may union all the required results from table1 and table2 then join this unioned query to codesTable as the following:
SELECT D.codeid, COUNT(*) CN
FROM
(
SELECT codeid, type_no, recoverd_value
FROM table1
UNION ALL
SELECT codeid, type_no, recoverd_value
FROM table2
) D
JOIN codesTable T
ON D.codeid = T.code_id
WHERE D.type_no IN (1,2,3,4,5,6,7,8,9,10) AND D.recoverd_value IN (0,1)
GROUP BY D.codeid
ORDER BY D.codeid
See a demo.

How to get Full Record with MAX as aggregate function

I have a table with schema (id, date, value, source, ticker). I wanted to get record having highest ID group by date in sql server
Example Data
ID|date|value|source|ticker
3|10-Dec-2017|10|a|b
1|10-Dec-2017|11|p|q
Below query works in Sqlite. Do we know if I can do same with SqlServer
select max(id), date, value, source, ticker from table group by date
Expected return:-
ID|date|value|source|ticker
3|10-Dec-2017|10|a|b
Also how I can do same operation on UNION of 2 tables with same schema.
You can use subquery :
select t.*
from table t
where id = (select max(t1.id) from table t1 where t1.date = t.date);
However, you can also use row_number() function :
select top (1) with ties *
from table t
order by row_number() over (partition by [date] order by id desc);
You can also do it like below :
select t1.* from table1 t1
join (
select max(id) as id, [date] from table1
group by [date]
) as t2 on t1.id = t2.id
SQL HERE

On the union step add a group by and take the max shipment date

Can someone please help me as where to add the group by in the below query and get the max shipment date so that i get one headered record so it does not double up later in the proc?
SELECT HEADER_ID,Actual_Shipment_Date
FROM HEADERINFO
UNION
SELECT HEADER_ID,Actual_Shipment_Date
FROM HEADERINFO2
select HEADER_ID, max(Actual_Shipment_Date) as Actual_Shipment_Date FROM
(SELECT HEADER_ID,Actual_Shipment_Date
FROM HEADERINFO
UNION
SELECT HEADER_ID,Actual_Shipment_Date
FROM HEADERINFO2) qry
group by HEADER_ID
Do you mean this?
The query given below will give you the max shipment date, grouped by the header id.
SELECT HEADER_ID,Max(Actual_Shipment_Date) as 'Max Shipment Date'
FROM dbo.HEADERINFO t1
group by t1.HEADER_ID
UNION
SELECT HEADER_ID,Max(Actual_Shipment_Date) as 'Max Shipment Date'
FROM dbo.HEADERINFO2 t2
group by t2.HEADER_ID
note: t1, t2 referes to table 1 and table 2 respectively.
try this,
SELECT HEADER_ID
,max(Actual_Shipment_Date) Shipment_Date
FROM (
SELECT HEADER_ID
,Actual_Shipment_Date
FROM HEADERINFO
UNION ALL
SELECT HEADER_ID
,Actual_Shipment_Date
FROM HEADERINFO2
) t4
GROUP BY HEADER_ID

Join based on min

I have two tables.
Table1:
id, date
Table2:
id,date
Both the table contain information about id. Table1 and Table2 can have some extra rows which are not present in another table.
Example:
Table1:
1,15-Jun
2,16-Jun
4,17-Jun
Table2
1,14-Jun
2,17-Jun
3,18-Jun
I need a summarize result which give minimum date for each row.
Expected result:
1,14-Jun
2,16-Jun
3,18-Jun
4,17-Jun
select id, min(date_) from (
select id, date_ from table1
union all
select id, date_ from table12
) group by id;
SELECT id, MIN(date)
FROM (SELECT id, date
FROM Table1
UNION
SELECT id, date
FROM Table2)
GROUP BY id
with a as(select t.i_id,t.dt_date from t
union
select b.i_id,b.dt_date from b)
select a.i_id,min(a.dt_date) from a group by a.i_id order by a.i_id;
You can check this link

Select and sums from another table. Whats wrong with this SQL?

Whats wrong with this SQL?
SELECT Id, (select SUM(VALUE) from SomeTable) AS SumValue, GETDATE()
FROM MyTable
WHERE SumValue > 0
You cannot use aliased columns in the SELECT clause in the same query, except in ORDER BY.
It needs to be subqueried
SELECT Id, SumValue, GETDATE()
FROM (
SELECT Id, (select SUM(VALUE) from TABLE) AS SumValue
FROM MyTable
) X
WHERE SumValue > 0
That is the general case. For your specific query, it doesn't make sense because the subquery is not correlated to the outer query, so either NO rows show, or ALL rows show (with the same SumValue). I will simply assume you have simplified the query a lot since a table name of "table" doesn't really work.
I would probably rewrite like this:
SELECT a.Id, b.SumValue, GETDATE() as [now]
FROM MyTable a
Join
(
select id, SUM(VALUE) as [SumValue]
from [TABLE]
Group by id
)b on a.Id = b.Id
WHERE b.SumValue > 0
This is assuming that the value you are totalling relates to the ID in your table?
right way is
SELECT Id, (select SUM(VALUE) from TABLE) AS SumValue, GETDATE()
FROM MyTable
WHERE (select SUM(VALUE) from TABLE) > 0