I am trying to delete the duplicate rows based on two columns. An example for table as below
Table Name as REFF_TABLE
LOCATIONID TICKER ROW_KEY
AB PA 201605
AB PA 201605
AB PA 201606
AB PA 201606
DA PB 201705
DA PB 201706
DA PB 201707
DE PC 201808
DE PC 201809
I want to remove duplicates rows by considering two columns - LOCATIONID, TICKER . Here need to take maximum value of ROW_KEY
Final output table as below
LOCATIONID TICKER ROW_KEY
AB PA 201606
DA PB 201707
DE PC 201809
Please help me to solve this
A typical SQL GROUP BY should be enough:
select locationid, ticker, max(row_key) row_key
from table
group by locationid, ticker
Related
I have two tables as follow
Table 1:
Product Type Location Sale Date
AB Retail California 6/13/2023
AB Sales Los Angels 7/13/2023
BC Retail Los Angels 8/13/2023
CD Sales California 9/13/2023
AB Retail Los Angels 9/13/2023
EF Retail Los Angels 10/13/2023
Table 2:
Product Type Location Sale Date
AB Retail California 6/13/2023
AB Sales California 7/13/2023
CD Sales California 9/13/2023
AB Retail Los Angels 9/13/2023
BC Retail Los Angels 9/13/2023
I need to check whether table 1 records are available in table 2 records and create a new column called Available with yes or no
It needs to be match all the columns by each row
Expected output:
Product Type Location Sale Date Available
AB Retail California 6/13/2023 Yes
AB Sales Los Angels 7/13/2023 No
BC Retail Los Angels 8/13/2023 No
CD Sales California 9/13/2023 Yes
AB Retail Los Angels 9/13/2023 No
EF Retail Los Angels 10/13/2023 No
You can use something like below
SELECT ProductType, Location, SaleDate, CASE WHEN B.MatchRow>0 'Yes' ELSE 'No' END as Available
FROM TABLEA A
OUTER APPLY(
SELECT COUNT(*) AS MatchRow
FROM TABLEB B
WHERE A.ProductType = B.ProductType AND A.Location = B.Location AND A.SaleDate = B.SaleDate
) B
The first step is to join staff and customer together. The second step is to count the distinct product_id. My target is to add the total(sum) field under the result table.
Thanks.
staff
staff_ID Name cust_id
1 Tom 101
1 Tom 101
1 Tom 105
2 Peter 102
2 Peter 104
3 Billy 103
customer
cust_id product_id
101 A1
102 A2
103 A3
104 A4
105 A5
My work:
SELECT a.staff_name,COUNT(DISTINCT a.product_id)
FROM (SELECT distinct a.staff_id, a.staff_name, a.cust_id
FROM staff)a
LEFT JOIN customer b ON a.cust_id=b.cust_id
GROUPBY a.staff_name
What I want is to add the total column below the count.
Name count
Tom 2
Peter 2
Billy 1
Total 5
Update:
Regarding the "Total", as #MatBailie correctly pointed out in the comments:
The aggregate of multiple COUNT(DISTINCT) rows CAN NOT be guaranteed to be summable. If two staff members share the same product_id the summary value will be LESS THAN the sum of its members.
So for this sample data set:
db<>fiddle here
cust_id
product_id
101
A1
102
A2
103
A3
104
A4 <== Same product
105
A5
105
A4 <== Same product
Using GROUP BY ROLLUP yields a "Total" value of 5:
SELECT COALESCE(a.staff_name, 'Total') AS Staff_Name
, COUNT(DISTINCT b.product_id) AS [Count]
FROM staff a LEFT JOIN customer b ON a.cust_id=b.cust_id
GROUP BY ROLLUP (a.staff_name);
Results:
Staff_Name
Count
Billy
1
Peter
2
Tom
3
Total
5 **
Whereas calculating a simple sum of the totals, yields a "Total" value of 6. So just be aware of the difference.
Staff_Name
Count
Billy
1
Peter
2
Tom
3
Total
6 **
Original (Wrong Answer):
Can't remember where I saw this answer, but ... assuming Staff_Name is never null, you could use GROUP BY ROLLUP to obtain the total. That said calculating grand totals is usually more of a front-end job.
SELECT COALESCE(a.staff_name, 'Total') AS Staff_Name
, COUNT(DISTINCT b.product_id) AS [Count]
FROM staff a LEFT JOIN customer b ON a.cust_id=b.cust_id
GROUP BY ROLLUP (a.staff_name);
Try this one:
SELECT s.staff_name, COUNT(DISTINCT b.product_id), SUM(count) Total
FROM staff s
INNER JOIN customer b ON b.cust_id = s.cust_id
GROUP BY s.staff_name
I have two tables, table1 has end products and product parts, table2 has a mapping between table1.productid and partsid. i.e.
table1
--------------------------------------------
productid descrip code cost ....etc
1235 product A 07 12.5 ......
789 labor 03 2.5 ....
839 part1 03 5 ....
and table2 looks like
table2
--------------------------------------------
productid partsID quantity
1235 789 1
1235 839 2
2341 2315 2
.....
I need to select the end product that meets specific code and go pull up the parts from the part table and display like below:
Resuls
--------------------------------------------
productid descrip code partsID cost ....etc
1235 product A 07 789 2.5 ......
1235 product A 03 839 5 ....
.......
Basically for each end product, line up the parts to the right and cost and all other details associated with the parts and not the end product.
Appreciate any help with this.
Could you not join the Two tables across ProductID field:
SELECT * FROM
dbo.TABLE1 o INNER JOIN
dbo.TABLE2 t ON o.ProductId = t.ProductId
WHERE o.code = 7
OR o.code = 3
etc ... whatever additional criteria you need
If you also need to Join the partsID you could say
AND o.PartId = t.partsId
It looks like you want to get something like this query, but I'm not sure about the code=07, you've specified in the example. Probably the code must be 03 for both parts.
SELECT
prd.productid,
prd.descrip,
prt.code,
prdprt.partsID,
prt.cost,
....etc
FROM table1 prd
JOIN table2 prdprt ON prd.productid=prdprt.productid
JOIN table1 prt ON prdprt.partsID=prt.productid
WHERE prd.code in ('07', ...othecodes) --if you filter by product's code
OR prt.code in ('03', ...othecodes) --if you filter by parts's code
I am trying to Create a SQL View by joining two SQL tables and return only the lowest value from second table and all the rows from first table similar to left join.
My problem can be clearly explained with the below example.
Table1
Id Product Grade Term Bid Offer
100 ABC A Q1 10 20
101 ABC A Q1 5 25
102 XYZ A Q2 25 30
103 XYZ B Q2 20 30
Table2
Id Product Grade Term TradeValue
1 ABC A Q1 100
2 ABC A Q1 95
3 XYZ B Q2 100
In the above data I want to join Table1 and Table2 when ever the columns Product,Grade and Term from both the tables are equal and return all the rows from Table1 while joining the lowest Value of the column TradeValue from Table2 to the first record of the match and making TradeValue as NULL for other rows of the resultant View and the resultant View should have the Id of Table2 as LTID
So the resultant SQL View should be
RESULT
Id Product Grade Term Bid Offer TradeValue LTID
100 ABC A Q1 10 20 95 2
101 ABC A Q1 5 25 NULL 2
102 XYZ A Q2 25 30 NULL NULL
103 XYZ B Q2 20 30 100 3
I tried using the following query
CREATE VIEW [dbo].[ViewCC]
AS
SELECT
a.Id,a.Product,a.Grade,a.Term,a.Bid,a.Offer,
b.TradeValue
FROM Table1 AS a
left JOIN (SELECT Product,Grade,Term,MIN(TradeValue) TradeValue from Table2 Group by Product,Grade,Term,) AS b
ON b.Product=a.Product
and b.Grade=a.Grade
and b.Term=a.Term
GO
The above Query returned the following data which is apt to the query I wrote but that is not what I was trying to get
Id Product Grade Term Bid Offer TradeValue
100 ABC A Q1 10 20 95
101 ABC A Q1 5 25 95 --This should be null
102 XYZ A Q2 25 30 NULL
103 XYZ B Q2 20 30 100
As we can see minimum value of TradeValue being assigned to all matching rows in Table1 and also I was not able to return Id As LTID from Table2 as I have issues with group by clause as I cannot group it by b.Id as it returns too many rows.
May I know a better way to deal with this?
You need a row number attached to each record from Table1, so that the requirement of only joining the first record from each group of Table1 can be fulfilled:
CREATE VIEW [dbo].[ViewCC]
AS
SELECT a.Id, a.Product, a.Grade, a.Term, a.Bid, a.Offer,
b.TradeValue, b.Id AS LTID
FROM (
SELECT *, ROW_NUMBER() OVER(PARTITION BY Product, Grade, Term ORDER BY Id) AS rn
FROM Table1
) a
OUTER APPLY (
SELECT TOP 1 CASE WHEN rn = 1 THEN TradeValue
ELSE NULL
END AS TradeValue, Id
FROM Table2
WHERE Product=a.Product AND Grade=a.Grade AND Term=a.Term
ORDER BY TradeValue) b
GO
OUTER APPLY returns a table expression containing either the matching record from Table2 with the lowest TradeValue, or NULL if no matching record exists.
I have the query to solve out the following problem...
Excel Sheet1:
Empid EmpName Des
001 Samar eng
002 kalyan eng
Excel Sheet2:
Empid EmpName AccNo Emp.MobNo Emp.Address Empwork
001 Samar 1001 44545455646 ctc tttt
002 kalyan 1002 65464656654 bbs ppp
003 barak 1003 54654564564 polp ppp
Final DB Table:
Empid EmpName Des AccNo Emp.MobNo Emp.Address Empwork
001 Samar eng 1001 44545455646 ctc tttt
002 kalyan eng 1002 65464656654 bbs ppp
After you've imported your Excel sheets in a SQL Server instance, you can use the INNER JOIN syntax to select only rows that are present in both tables.
SELECT s1.Empid
, s1.EmpName
, s1.Des
, s2.AccNo
, s2.[Emp.MobNo]
, s2.[Emp.Address]
, s2.[Empwork]
FROM sheet1 s1
INNER JOIN sheet2 s2 ON s2.Empid = s1.Empid
If you want to make a select from Sheet1 and Sheet2 that gives final table, it would be like this:
INSERT INTO finalDBtable
Select s1.*, s2.AccNo, s2.[Emp.MobNo] , s2.[Emp.Address], s2.Empwork
from Sheet1 s1
inner join Sheet2 s2 on s1.Empid = s2.Empid
So, first put Sheet1 and Sheet2 in database tables and then make the select previous select to insert in the final table.