I have 3 tables
-----------table1-------------
id code name quantity
1 001 car1 1
2 002 car2 2
3 003 car3 3
-----------table2-------------
id code name quantity
1 001 car1 1
2 002 car2 2
-----------table3-------------
id code name quantity
1 001 car1 1
2 002 car2 2
3 004 car4 4
I want to join the three tables and take the total from quantity in SQL Server:
---------table-------
code name total
001 car1 3
002 car2 6
003 car3 3
004 car4 4
In MySql I try this and worked, but in SQL Server, I get me error :(
select
ID, CODE, NAME, sum(QUANTITY) as total
from
(select ID, CODE, NAME, QUANTITY from AP1
union all
select ID, CODE, NAME, QUANTITY from AP2
union all
select ID, CODE, NAME, QUANTITY from AP3) x
group by ID;
Your method would work in any database, if your group by fields match the unaggregated columns:
select CODE, NAME, sum(QUANTITY) as total
from (select ID, CODE, NAME, QUANTITY from AP1 union all
select ID, CODE, NAME, QUANTITY from AP2 union all
select ID, CODE, NAME, QUANTITY from AP3
) x
group by CODE, NAME;
The fact that it works in MySQL is due to a (mis)feature that allows any unaggregated columns in the select for an aggregation query. In general, other databases do not support this non-standard syntax.
Related
table 1 is maintable_KQPPJ : contains GroupID, Year, Name, VendorID. This table contains multiple records with the same GroupID
table 2 is cb_vendorinformation: contains GroupID and CompanyName
I would like to join both tables on GroupID. The output should only have GroupID, CompanyName, and Count. The Count is the distinct count of GroupID in maintable_KQPPJ.
I have the following code but it doesn't really give me the output I'm looking for.
SELECT maintable_KQPPJ.GROUPID, cb_vendorinformation.CompanyName, count(distinct maintable_KQPPJ.GROUPID)
FROM maintable_KQPPJ
JOIN cb_vendorinformation ON maintable_KQPPJ.GROUPID=cb_vendorinformation.GROUPID
maintable_KQPPJ:
GroupID Year VendorID Name
26 2019 9999 John
26 2020 2345 Jane
6 2018 3244 Jack
36 2021 3245 Jill
cb_vendorinformation:
GroupID CompanyName
26 Walmart
6 Target
36 Kroger
The output should look like
GroupID CompanyName Count
26 Walmart 2
6 Target 1
36 Kroger 1
You need group by and count(*)
SELECT maintable_KQPPJ.GROUPID
, cb_vendorinformation.CompanyName
, count(*)
FROM maintable_KQPPJ
JOIN cb_vendorinformation ON maintable_KQPPJ.GROUPID=cb_vendorinformation.GROUPID
GROUP BY maintable_KQPPJ.GROUPID
, cb_vendorinformation.CompanyName
I am hoping someone can help me with my query.
I have a table with the columns, 'Date', 'ID_Num and 'Name'. What I want to do is add a column at the end to show the total amount of times each ID_Num is within the data but based on the date. So although 'ID_Num' 1001 shows 4 times in total, it is twice on the 20/04/2018 and once on both the 21/04/2018 and 22/04/2018.
EDIT: I should have stipulated that I will be pulling several other columns with information, which I cant use a group by on everything.
Date ID_Num Name Count
20/04/2018 1001 John 2
20/04/2018 1001 John 2
20/04/2018 1002 Paul 2
20/04/2018 1002 Paul 2
20/04/2018 1003 David 2
20/04/2018 1003 David 2
20/04/2018 1004 Stephen 1
21/04/2018 1001 John 1
21/04/2018 1002 Paul 3
21/04/2018 1002 Paul 3
21/04/2018 1002 Paul 3
21/04/2018 1004 Stephen 1
22/04/2018 1001 John 1
22/04/2018 1002 Paul 1
22/04/2018 1003 David 1
22/04/2018 1004 Stephen 1
Thanks
Unless I'm missing something here, a simple group by and count should do it:
SELECT Date, ID_Num, Name, Count(*)
FROM TableName
GROUP BY Date, ID_Num, Name
(That is, assuming there can only be one Name for each ID_Num)
Update
Assuming your rdbms supports it, you can use count with an over clause:
SELECT Date, ID_Num, Name, Count(*) OVER(PARTITION BY Date, Id_Num)
FROM TableName
If not, you can use a sub query:
SELECT Date,
ID_Num,
Name,
(SELECT Count(*)
FROM TableName As t1
WHERE t1.Date = t0.Date
AND t1.ID_NUM = t0.ID_NUM)
FROM TableName As t0
Try this:
SELECT
Date,
Id_num,
count(*) count
FROM
tabel_name
GROUP BY
Date,
Id_num
If you want name as well:
SELECT
Date,
Id_num,
Name
count(*) count
FROM
tabel_name
GROUP BY
Date,
Id_num,
Name
You can use a normal select query and then add a sub query to do a group and show the total. Simple example below
SELECT Date, ID_Num, Name,
(SELECT Count(ID_Num) FROM TableName AS CHILD WHERE CHILD.Id_Num = Parent.Id_Num) AS Total
FROM TableName AS Parent
I'm trying to sum values from duplicates rows (with the same ID, Month and Person) in multiple columns to the first or the last duplicate row. Then delete the duplicate rows exept the one with the total value.
The biggest problem is that sometimes I need to sum values in two different columns.
PrimaryTable:
ID Month Person Value1 Value2
**123 1 Smith** 10 20
**123 1 Smith** 5 NULL
**123 1 Smith** NULL 5
123 2 Smith 10 5
**189 3 Murphy** NULL 15
**189 3 Murphy** NULL 10
190 2 Brown 25 25
**345 2 Lee** 25 20
**345 2 Lee** 25 20
Result1 (expected result after sum duplicates values to the first one):
ID Month Person Value1 Value2
123 1 Smith **15** **25**
123 1 Smith 5 NULL
123 1 Smith NULL 5
123 2 Smith 10 5
189 3 Murphy NULL **25**
189 3 Murphy NULL 10
190 2 Brown 25 25
345 2 Lee **50** **40**
345 2 Lee 25 20
FinalTable (expected result after deleting duplicates, except the first one):
ID Month Person Value1 Value2
123 1 Smith **15** **25**
123 2 Smith 10 5
189 3 Murphy NULL **25**
190 2 Brown 25 25
345 2 Lee **50** **40**
I'm trying with this code:
SELECT ID, Month, Person, SUM(Value1), SumValue2
FROM
(
SELECT ID, Month, Person, Value1, SUM(Value2) AS SumValue2
FROM db.Hours
GROUP BY ID, Month, Person, Value1
)
GROUP BY ID, Month, Person, SumValue2
But sometimes it makes double sum of total of Value2.
SELECT ID, Month, Person, SUM(Value1) as SumValue1, SUM(Value2) AS SumValue2
FROM db.Hours
GROUP BY ID, Month, Person
I am not sure why you are looking at this as two steps etc. There is no removal of duplicates etc. this is a scenario for Group By Aggregation. Where you group like rows and summarize the value columns. The only reason you would need to make this a multi step operation would be if one of your value columns will be considered within your grouping e.g. ID, Month, Person, and Value1. In your case you simply need to group by ID, Month, Person and do the aggregation for Value1 and Value2.
I'm trying to sum values from duplicates rows (with the same ID, Month and Person) in multiple columns to the first or the last duplicate row. Then delete the duplicate rows exept the one with the total value.
The biggest problem is that sometimes I need to sum values in two different columns.
PrimaryTable:
ID Month Person Value1 Value2
**123 1 Smith** 10 20
**123 1 Smith** 5 NULL
**123 1 Smith** NULL 5
123 2 Smith 10 5
**189 3 Murphy** NULL 15
**189 3 Murphy** NULL 10
190 2 Brown 25 25
**345 2 Lee** 25 20
**345 2 Lee** 25 20
Result1 (expected result after sum duplicates values to the first one):
ID Month Person Value1 Value2
123 1 Smith **15** **25**
123 1 Smith 5 NULL
123 1 Smith NULL 5
123 2 Smith 10 5
189 3 Murphy NULL **25**
189 3 Murphy NULL 10
190 2 Brown 25 25
345 2 Lee **50** **40**
345 2 Lee 25 20
FinalTable (expected result after deleting duplicates, except the first one):
ID Month Person Value1 Value2
123 1 Smith **15** **25**
123 2 Smith 10 5
189 3 Murphy NULL **25**
190 2 Brown 25 25
345 2 Lee **50** **40**
I'm trying with this code:
SELECT ID, Month, Person, SUM(Value1), SumValue2
FROM
(
SELECT ID, Month, Person, Value1, SUM(Value2) AS SumValue2
FROM db.Hours
GROUP BY ID, Month, Person, Value1
)
GROUP BY ID, Month, Person, SumValue2
But sometimes it makes double sum of total of Value2.
SELECT ID, Month, Person, SUM(Value1) as SumValue1, SUM(Value2) AS SumValue2
FROM db.Hours
GROUP BY ID, Month, Person
I am not sure why you are looking at this as two steps etc. There is no removal of duplicates etc. this is a scenario for Group By Aggregation. Where you group like rows and summarize the value columns. The only reason you would need to make this a multi step operation would be if one of your value columns will be considered within your grouping e.g. ID, Month, Person, and Value1. In your case you simply need to group by ID, Month, Person and do the aggregation for Value1 and Value2.
Using SQL Server 2008
table1
id name
001 rahim
002 vijay
table2
id name amount
003 vijayan 08.00
004 suresh 12.00
I want to combine table1 & table2 using union
Query
Select id, name, '' from table1 union Select id, name amount from table2
Output
id name amount
001 rahim 0 -- 0 should not appear, should be null
002 vijay 0 -- 0 should not apperar, should be null
003 vijayan 08.00
004 suresh 12.00
0 is display instead of null, because of table2 amount column is numeric.
How to handle this. Need SQL Query help
select id, name, amount from table2
union all
select id, name, null from table1
order by id
As per Microsoft's msdn site regarding the UNION operator link: msdn UNION
The number and order of columns must match and be type compatible.
User: podiluska uses the correct approach.