Assign name to other rows upon group condition - sql-server

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

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

SQL Server query for Total of hours across multiple rows

I've tried to resolve this a few ways and wanting some extra help.
I'm wanting to return the same number of rows but trying to calculate the number of total hours delivered by each Employee for each service on each day.
I've added a duplicate flag but that doesn't help me to work out the max hours by the 1 employee in 1 day.
Emp Service Date Start End Hrs Duplicate Flag Flag hrs
Fred xyz 14/09/2017 8:45 15:00 6.25 1 1 6.25
Fred xyz 14/09/2017 9:00 14:15 5.25 1 0 0
Fred xyz 14/09/2017 9:00 14:15 5.25 2 0 0
Fred xyz 14/09/2017 9:00 15:00 6 1 0 0
John xyz 15/09/2017 10:00 12:00 2 1 1 2
John xyz 15/09/2017 10:00 13:00 3 1 0 0
John xyz 15/09/2017 11:00 15:00 4 1 0 0
John xyz 15/09/2017 12:00 16:00 4 1 1 4
the last 2 columns are the ones I can't quite work out how to add. I've tried Overlaps and other ANDing methods.
thanks, Dave
I think you are looking for an OVER clause. Not sure what the duplicate flag is for though? If you ignore your last three columns, assuming they are computed columns in a query, you could use...
Select
*,
sum(Hrs) over (partition by Emp, Date, Service order by Date)
From (select distinct * from your table) x
If the last three columns are actual columns in your table just replace select * in the derived table with the column names, except those three.

SQL to sort the data on multiple columns and add a column to assign an incremental counter for each sort combination

I have this as input data
CustomerId Customer_name PurchaseId Purchase_Date
----------------------------------------------------
1234 Robert Benson ABC123 12/07/2012
1218 Gary Thomas PP122 26/01/2013
1218 Gary Thomas PP122 28/01/2013
1234 Robert Benson ABC123 28/01/2013
1234 Robert Benson ABC123 29/01/2013
1254 Robert Sharma PML563 29/04/2012
1218 Gary Thomas PR124 06/03/2013
1234 Benson Cruiso LML123 14/07/2012
1234 Martha Cruiso FMPL123 15/07/2012
I want to sort the data on multiple columns i.e CustomerId and PurchaseId and add an order of the sorted data as
GroupID CustomerId Customer_name PurchaseId Purchase_Date
-------------------------------------------------------------
1 1218 Gary Thomas PR124 06/03/2013
1 1218 Gary Thomas PP122 26/01/2013
2 1218 Gary Thomas PP122 28/01/2013
1 1234 Robert Benson ABC123 12/07/2012
2 1234 Robert Benson ABC123 28/01/2013
2 1234 Robert Benson PP122 29/01/2013
1 1234 Benson Cruiso LML123 14/07/2012
1 1234 Martha Cruiso FMPL123 15/07/2012
1 1254 Robert Sharma PML563 27/04/2012
If I understand your question correctly, this can be achieved with a simple row_number or rank, depending on how you want your query to handle ties.
Select Row_Number() over
(partition by CustomerID, PurchaseID
order by PurchaseDate) as GroupID, *
from MyTable
This will assign sequential numbers to rows that have the same CustomerID and PurchaseID, assigning 1 to the first PurchaseDate, 2 to the next, and so on, and starting the numbering over again when it reaches a new CustomerID/PurchaseID combination. In the event of two rows with the same CustomerID, PurchaseID, and PurchaseDate, it will arbitrarily assign different numbers. If you want those cases to be treated as ties and assigned the same number, use rank or dense_rank instead of row_number.

Count Employee's Departmentwise

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

How to get column data in column names in query?

If I have the following table:
Cust Prod Qty
===== ==== ===
Bob Apple 3
Bob Orange 2
Bob Banana 4
Rob Apple 2
Rob Orange 1
Bob Apple 2
How can I get the following result with the table data as the column names:
Prod Bob Rob
====== === ===
Apple 5 2
Orange 2 1
Banana 4 null
You can use PIVOT in MSSQL or the following way:
SELECT
PROD,
SUM(CASE WHEN Cust='Bob' THEN Qty ELSE 0 END) as Bob,
SUM(CASE WHEN Cust='Rob' THEN Qty ELSE 0 END) as Rob
FROM T
GROUP BY PROD
SQLFiddle demo
SELECT * from t
pivot
(
sum(Qty)
for [Cust] in ([Bob],[Rob])
)as p;
GROUP BY PROD
fiddle demo

Resources