Count Employee's Departmentwise - inner-join

I have two table employee and Native leave I want count employee’s Department wise and count employee in native
Employee Table:-
Empno Name Depart Addre Age Exisisting_No
-----------------------------------------
1 Abc Acc Kkk 20 1
2 Efg Hr Hhh 22 2
3 ijk Acc Yyy 21 3
4 Mno Hr Zzz 20 4
Native Leave Table:-
Exisisting_No Name From To Reason
---------------------------------
2 Efg 30/01/14 04/02/14 personal
Want Output Like:-
Depart EmpNo NL
---------------
Hr 2 1
Acc 2 0
So please any one can help me and sorry for my English

SELECT e.Dept
, count(Empno)
, count(NL.Exisisting_No)
FROM Employee e LEFT JOIN NativeLeave NL ON e.Exisisting_No = NL.Exisisting_No
GROUP BY e.Dept

Related

Are these tables in 3rd Normal form?

users
id
first_name
last_name
email
1
John
Doe
john.doe#john.doe.com
2
Mary
Doe
mary.doe#mary.doe.com
user_details
id
user_id
ethnicity_id
education_id
location_id
1
John
1
1
1
2
Mary
4
3
2
locations
id
location
lat
long
1
London, UK
53.2321
-1.2321
2
Berlin, Germany
52.2321
-1.5345

Sum values from multiple tables grouping by a common column

I have three tables in MS SQL Server 2014. Each of them holds a couple of numeric values, a description and a date. For the sake of brevety, let's assume the following tables:
table "beverages"
day beverage amount
---------- -------- ------
2018-12-01 water 2
2018-12-01 tea 1
2018-12-01 coffee 7
2018-12-02 water 4
2018-12-02 tea 2
table "meals"
day meal amount
---------- ------ ------
2018-12-01 burger 1
2018-12-01 bread 2
2018-12-02 steak 1
table "fruit"
day fruit amount
---------- ------ ------
2018-12-01 apple 4
2018-12-01 banana 1
2018-12-02 apple 2
Then I have another table holding only a list of dates.
table "dates"
day
----------
2018-12-01
2018-12-02
What I need is a query that returns one row for each of the rows in the dates table, and in each row has the date, the total amount of beverages, the total amount of meals and the total amount of fruit for that day. I do not care for the different types of beverages, meals and fruit, just the sum. The result should be:
expected result
day beverages meals fruit
---------- ----------- ----------- -----------
2018-12-01 10 3 5
2018-12-02 6 1 2
But instead I receive
received result
day beverages meals fruit
---------- ----------- ----------- -----------
2018-12-01 40 18 30
2018-12-02 6 2 4
I already know what the problem is, just not how to fix it. Even worse, I'm sure that I knew the answer once, but now I can't even figure the right search terms to make Google tell me...
When I do the query like this (I used table variables for testing)
SELECT
[d].[day]
,SUM([b].[amount]) AS [beverages]
,SUM([m].[amount]) AS [meals]
,SUM([f].[amount]) AS [fruit]
FROM #dates AS [d]
LEFT OUTER JOIN #beverages AS [b]
ON [d].[day] = [b].[day]
LEFT OUTER JOIN #meals AS [m]
ON [d].[day] = [m].[day]
LEFT OUTER JOIN #fruit AS [f]
ON [d].[day] = [f].[day]
GROUP BY [d].[day]
it sums each row from the different tables more than once, because it returns every possible combination of the three tables. Removing the SUM() and GROUP BY proves that:
day beverages meals fruit
---------- ----------- ----------- -----------
2018-12-01 2 1 4
2018-12-01 2 1 1
2018-12-01 2 2 4
2018-12-01 2 2 1
2018-12-01 1 1 4
2018-12-01 1 1 1
2018-12-01 1 2 4
2018-12-01 1 2 1
2018-12-01 7 1 4
2018-12-01 7 1 1
2018-12-01 7 2 4
2018-12-01 7 2 1
2018-12-02 4 1 2
2018-12-02 2 1 2
So, what do I need to change in the query to make it sum the values for each of the three tables without multiplying it with the number of the rows in the other tables?
Group the Tables before joining like so:
SELECT
[d].[day]
,[b].[amount] AS [beverages]
,[m].[amount] AS [meals]
,[f].[amount] AS [fruit]
FROM #dates AS [d]
LEFT OUTER JOIN (SELECT day, SUM(amount) as amount FROM #beverages GROUP BY day) AS [b]
ON [d].[day] = [b].[day]
LEFT OUTER JOIN (SELECT day, SUM(amount) as amount FROM #meals GROUP BY day) AS [m]
ON [d].[day] = [m].[day]
LEFT OUTER JOIN (SELECT day, SUM(amount) as amount FROM #fruit GROUP BY day) AS [f]
ON [d].[day] = [f].[day]
How about a PIVOT instead?
Example
Select *
From (
Select day,Item='beverage',amount from beverages
Union All
Select day,Item='meals' ,amount from meals
Union All
Select day,Item='fruit' ,amount from fruit
) src
Pivot ( sum(amount) for Item in ([beverages],[meals],[fruit]) ) pvt

Assign name to other rows upon group condition

I have a dataset which is of following nature. I would like to replace the names in "MAKE" column if the column contains "PQR" per unique country.
country MAKE
1 USA PQR
2 USA ABC
3 UK PQR
4 UK DEF
5 JPN DEF
6 JPN LMN
Desired Output:
country MAKE
1 USA PQR
2 USA PQR
3 UK PQR
4 UK PQR
5 JPN OTHERS
5 JPN OTHERS
One option is conditional aggregation with analytic functions:
SELECT
country,
CASE WHEN SUM(CASE WHEN MAKE = 'PQR' THEN 1 ELSE 0 END) OVER (PARTITION BY country) > 0
THEN 'PQR' ELSE 'OTHERS' END AS MAKE
FROM yourTable
ORDER BY
country;
Demo

compare two tables and add result in second table

I have been working on comparing two almost identical tables and update/add the result in second table. Here are the two tables i am using. Person1(ID,Name,PHNumber) and Person2(ID,Name,PHNumber,IsActive).
Note: person1 has always a more/less/same rows than person2.Person1 is reference table we need to add/delete the rows which does/doesnot present in person1 int to person2. I am using microsoft SQL server management studio.
Case1: If the compared result has more rows(Let say person1 has 10, person2 has 8rows) then we need to add those(2rows) to person2 and keep IsActive-1 for those rows.
Case2: If the compared result has differnce(Let say person1 has 10 but person2 has 20rows) then we need to find those delta 10rows and keep IsActive-0 for those 10rows.
Hope the query is clear and expecting your valuable solutions. Thank you.
Here Person1 does not contain newdata2 and contains newdata,newdata1 which are
new values after comparing, so we are giving IsActive-0 and IsActive-1
respectively.
Person1:
ID Name PHNumber
1 missouri 123
2 kansas 111
3 stlouise 234
4 california 456
5 india 888
6 srilanka 780
7 dallas 890
8 texas 1111
9 mario 1112
10 sister 7878
11 pontiac 8765
12 newdata 1234
13 newdata1 2345
Person2:
ID Name PHNumber IsActive
1 missouri 123 1
2 kansas 111 1
3 stlouise 234 1
4 california 456 1
5 india 888 1
6 srilanka 780 1
7 dallas 890 1
8 texas 1111 1
9 mario 1112 1
10 sister 7878 1
11 pontiac 8765 1
12 newdata2 987 1
RESULT: Person2:
ID Name PHNumber IsActive
1 missouri 123 1
2 kansas 111 1
3 stlouise 234 1
4 california 456 1
5 india 888 1
6 srilanka 780 1
7 dallas 890 1
8 texas 1111 1
9 mario 1112 1
10 sister 7878 1
11 pontiac 8765 1
12 newdata2 987 0
13 newdata 1234 1
14 newdata1 2345 1
If you are still looking or answer, you can try like this...
;WITH cte
AS (SELECT
COALESCE(p1.id, p2.id) AS id,
COALESCE(p1.name, p2.name) AS name,
COALESCE(p1.phnumber, p2.phnumber) AS phnumber,
CASE WHEN p1.name IS NULL THEN 0 ELSE 1 END AS IsActive
FROM Person1 p1
FULL JOIN person2 p2
ON p1.name = p2.name
AND p1.phnumber = p2.phnumber)
SELECT
ROW_NUMBER() OVER (ORDER BY id, phnumber) AS id,
name, phnumber, IsActive
FROM cte

need result of two table sql query

I have a two sql server tables that contains data like this
Table a
catId | catname | Isdeleted
-------------------------------------------------
1 ABC 0
2 DEF 0
3 GHI 0
and another table is
Table B
id | Name | Name1 | Catid
--------------------------------------------------
1 abc aaaa 1
2 def bbbb 1
3 ghi gggg 2
4 jkl jjjj 2
5 xyz xxxxx 3
Now I want result in this format
catname from table a and all the fields from table b according to
catid of table a and catname should be distinct.
Please help me
Write your query like this :
SELECT DISTINCT a.catname, b.* FROM a INNER JOIN b
ON a.catid = b.catid WHERE catid = [catid]
If you have multiple records in table b for each catid or catname, you will see multiple records with same catname in result. there is no other choice unless catname be unique in both tables a and b.

Resources