T-SQL Merge records based on criteria - tsql
In SQL server 2008, I have below table Score. I want to show Score1 and Score2 together for same student. Name and Email combined uniquely identifies a student (Name or Email may be missing too, like Jack and Maya#moon.com). The expected output shown as T_Combined.
Score
Name Email Score1 Score2
John 'John#pluto.com' 75
Peter 'Peter#pluto.com' 34
Nina 'Nina#pluto.com' 45
Joseph 'Joseph#pluto.com' 76
Tom 'Tom#pluto.com' 43
Sam 'Sam#pluto.com' 76
Nancy 'Nancy#pluto.com' 12
Tina 'Tina#pluto.com' 56
John 'John#mars.com' 98
Peter 'Peter#mars.com' 12
Nina 'Nina#mars.com' 45
Joseph 'Joseph#mars.com' 87
Tom 'Tom#mars.com' 67
Sam 'Sam#mars.com' 99
Nancy 'Nancy#mars.com' 33
Tina 'Tina#mars.com' 23
John 'John#pluto.com' 86
Peter 'Peter#pluto.com' 56
Nina 'Nina#pluto.com' 98
Joseph 'Joseph#pluto.com' 78
Tom 'Tom#pluto.com' 12
Sam 'Sam#pluto.com' 45
Nancy 'Nancy#pluto.com' 76
Tina 'Tina#pluto.com' 78
John 'John#mars.com' 98
Peter 'Peter#mars.com' 45
Nina 'Nina#mars.com' 76
Joseph 'Joseph#mars.com' 12
Tom 'Tom#mars.com' 84
Sam 'Sam#mars.com' 27
Nancy 'Nancy#mars.com' 54
Tina 'Tina#mars.com' 50
Jack 10
'Maya#moon.com' 20
T_Combined
Name Email Score1 Score2
John 'John#pluto.com' 86
Peter 'Peter#pluto.com' 56
Nina 'Nina#pluto.com' 98
Joseph 'Joseph#pluto.com' 78
Tom 'Tom#pluto.com' 43 12
Sam 'Sam#pluto.com' 76 45
Nancy 'Nancy#pluto.com' 12
Tina 'Tina#pluto.com' 56
John 'John#mars.com' 98
Peter 'Peter#mars.com' 12
Nina 'Nina#mars.com' 45 76
Joseph 'Joseph#mars.com' 87 12
Tom 'Tom#mars.com' 67 84
Sam 'Sam#mars.com' 99 27
Nancy 'Nancy#mars.com' 33 54
Tina 'Tina#mars.com' 23 50
Jack 10
'Maya#moon.com' 20
Many thanks
Create table Score (Name varchar(20),Email varchar(20),Score1 int,Score2 int)
insert into Score (Name,Email,Score1)values('John','John#pluto.com',75)
insert into Score (Name,Email,Score1)values('Peter','Peter#pluto.com',34)
insert into Score (Name,Email,Score1)values('Nina','Nina#pluto.com',45)
insert into Score (Name,Email,Score1)values('Joseph','Joseph#pluto.com',76)
insert into Score (Name,Email,Score1)values('Tom','Tom#pluto.com',43)
insert into Score (Name,Email,Score1)values('Sam','Sam#pluto.com',76)
insert into Score (Name,Email,Score1)values('Nancy','Nancy#pluto.com',12)
insert into Score (Name,Email,Score1)values('Tina','Tina#pluto.com',56)
insert into Score (Name,Email,Score1)values('John','John#mars.com',98)
insert into Score (Name,Email,Score1)values('Peter','Peter#mars.com',12)
insert into Score (Name,Email,Score1)values('Nina','Nina#mars.com',45)
insert into Score (Name,Email,Score1)values('Joseph','Joseph#mars.com',87)
insert into Score (Name,Email,Score1)values('Tom','Tom#mars.com',67)
insert into Score (Name,Email,Score1)values('Sam','Sam#mars.com',99)
insert into Score (Name,Email,Score1)values('Nancy','Nancy#mars.com',33)
insert into Score (Name,Email,Score1)values('Tina','Tina#mars.com',23)
insert into Score (Name,Email,Score2)values('John','John#pluto.com',86)
insert into Score (Name,Email,Score2)values('Peter','Peter#pluto.com',56)
insert into Score (Name,Email,Score2)values('Nina','Nina#pluto.com',98)
insert into Score (Name,Email,Score2)values('Joseph','Joseph#pluto.com',78)
insert into Score (Name,Email,Score2)values('Tom','Tom#pluto.com',12)
insert into Score (Name,Email,Score2)values('Sam','Sam#pluto.com',45)
insert into Score (Name,Email,Score2)values('Nancy','Nancy#pluto.com',76)
insert into Score (Name,Email,Score2)values('Tina','Tina#pluto.com',78)
insert into Score (Name,Email,Score2)values('John','John#mars.com',98)
insert into Score (Name,Email,Score2)values('Peter','Peter#mars.com',45)
insert into Score (Name,Email,Score2)values('Nina','Nina#mars.com',76)
insert into Score (Name,Email,Score2)values('Joseph','Joseph#mars.com',12)
insert into Score (Name,Email,Score2)values('Tom','Tom#mars.com',84)
insert into Score (Name,Email,Score2)values('Sam','Sam#mars.com',27)
insert into Score (Name,Email,Score2)values('Nancy','Nancy#mars.com',54)
insert into Score (Name,Email,Score2)values('Tina','Tina#mars.com',50)
insert into Score (Name,Score1)values('Jack',10)
insert into Score (Email,Score2)values('Maya#moon.com',20)
select Name, Email, isnull(SUM(Score1),'') as Score1, isnull(SUM(Score2),'') as Score2
from Score
group by Name, Email
Related
PostgreSQL Lag Function between of Two Tables
I am new to Postgresql/Python, so please bear with me! Assuming we have two tables: item table having a itemid, price, time. user table having colums userid, itemid, timecreated, quantity, firstprice, lastprice, difference. Table examples like : item table: itemid price time RBK 92 1546408800 LBV 51 1546408800 ZBT 49 1546408800 GLS 22 1546408800 DBC 17 1546408800 RBK 91 1546495200 LBV 55 1546495200 ZBT 51 1546495200 GLS 24 1546495200 DBC 28 1546581600 RBK 108 1546581600 LBV 46 1546581600 ZBT 49 1546581600 GLS 21 1546581600 DBC 107 1546581600 In item table all those values comes up with api. and user table: userid itemid timecreated quantitty firstprice currentprice difference 1 RBK 1546408800 20 2 RBK 1546408800 15 3 RBK 1546408800 35 3 GLS 1546408800 101 3 DBC 1546495200 140 1 RBV 1546495200 141 2 RBK 1546495200 25 2 RBV 1546581600 31 User table is djangobased table which is user can register\add new items to follow prices. My struggle access the item table to fetch first price which is having a same timestamp. In that example userid 1 RBK First price (1546408800) must be filling with 92 I did some trick with postgresql with (lag) But this does not seems to be working: update user set firstprice = tt.prev_price from (select item.*, lag(price) over (partition by itemid order by time) as prev_price from item ) tt where tt.id = item.id and tt.prev_close is distinct from item.price; I can call current price from the api but didnt find out the way to filling firstprice from the item table. I will be making for a trigger for this query. I searched on google and on stackoverflow but I couldn't find anything that could help me. Thanks in advance.
I can advice next approach (may be not fastest): update "user" set firstprice = ( select price from "item" i where i.itemid = "user".itemid and i.time >= "user".timecreated order by i.time limit 1 ); It calculate firstprice using sub-query. Test this SQL here
TSQL Select TOP and Distinct from one table into a TEMP table
I have the following table: Data nr1 nr2 nr3 nr4 nr5 nr6 2020-09-12 6 15 36 42 67 78 2020-09-10 46 48 67 78 80 87 2020-09-08 23 27 28 31 69 89 2020-09-05 7 14 27 56 72 83 2020-09-03 16 17 38 39 68 84 2020-09-01 10 22 28 45 48 71 2020-08-29 1 3 35 42 55 61 2020-08-27 37 49 52 53 75 87 2020-08-25 15 24 31 70 83 84 2020-08-22 7 12 45 47 73 87 2020-08-20 7 17 30 39 41 67 2020-08-18 13 22 28 58 65 77 2020-08-17 5 9 26 62 77 79 2020-08-13 4 5 49 57 66 75 2020-08-11 7 9 38 68 78 80 2020-08-08 6 16 22 55 58 83 2020-08-06 21 37 40 46 69 80 2020-08-04 5 19 21 25 45 82 2020-08-01 4 14 17 18 26 45 2020-07-30 4 15 19 26 28 55 2020-07-28 23 45 49 71 80 82 2020-07-25 18 30 42 70 78 80 2020-07-23 10 29 37 49 56 57 2020-07-21 4 34 46 54 55 62 2020-07-18 18 33 49 76 80 84 I have to do the following task: Select into a #TEMP table with only one column DistinctNumbers all distinct numbers of the above table because some numbers in the above table might be repeated across rows and columns. Select into another #TEMP table all numbers in the range from 1 to 99 which are not in the original table. What is the best way of accomplishing these two tasks?
You should unpivot original table first 1.Unpivot original table into #temp table 2.Now you have all numbers in one column 3.Use while between 1 and 99 and insert counter into #RESULT table where not in #temp(unpivoted table) SELECT DISTINCT(num) num INTO #TEMP_DISTINCT_NUMBERS FROM ORIGINAL_TABLE UNPIVOT ( num FOR PivotColumn IN (nr1,nr2,nr3,nr4,nr5,nr6) ) AS UNPIVOTE_TABLE CREATE TABLE #RESULT(NUM INT) DECLARE #COUNTER INT =1; WHILE(#COUNTER<=99) BEGIN INSERT INTO #RESULT SELECT #COUNTER WHERE #COUNTER NOT IN (SELECT num FROM #TEMP_DISTINCT_NUMBERS) SET #COUNTER=#COUNTER+1 END SELECT * FROM #RESULT
you can try this: ;WITH tally AS (SELECT 1 AS num UNION ALL SELECT num + 1 FROM tally WHERE num < 99) SELECT DISTINCT tally.num FROM tally LEFT JOIN ( SELECT num FROM #dataset --your dataset CROSS APPLY (VALUES (nr1),(nr2),(nr3),(nr4),(nr5),(nr6)) AS B (num) ) AS dataset ON tally.num = dataset.num WHERE dataset.num IS NULL Code above: Create [tally] recursive common table expression with sequence from 1 to 99 Left join tally with your unpivoted dataset ... test here: https://rextester.com/YEB57637
Update only matched records in keyed KDB+ table with Q
I have a keyed table (tableA), and I want to update it with changes from another keyed (tableB) table, but only with records where the key in tableB matches a key in tableA. In other words, I do not want any new records in tableA, I only want to update the ones that area already there. tableA will have some columns that don't exist in tableB, and I want to preserve the original values of those columns. For example, if tableA and tableB looked like this: tableA:([lname:`Dent`Beeblebrox`Prefect; fname:`Arthur`Zaphod`Ford]; iq:98 42 126; age: 23 49 78) lname fname | iq age -----------------| ------- Dent Arthur| 98 23 Beeblebrox Zaphod| 42 49 Prefect Ford | 126 78 tableB:([lname:`Dent`Dirk; fname:`Arthur`Gently]; iq:105 118) <-- no age column lname fname | iq ------------| --- Dent Arthur| 105 Dirk Gently| 118 Then the desired outcome of the update would be: lname fname | iq age -----------------| ------- Dent Arthur| 105 23 <-- iq updated, age left alone Beeblebrox Zaphod| 42 49 Prefect Ford | 126 78 * Dirk Gently NOT added because he didn't exist in tableA Very grateful if anyone can give me an efficient syntax for updating a table this way.
lj is what you want here: q)tableA lj tableB lname fname | iq age -----------------| ------- Dent Arthur| 105 23 Beeblebrox Zaphod| 42 49 Prefect Ford | 126 78
Tableau: How to perform "Summarize totals except top 3"?
I have data something like below for the name of person and the total sales he/she made: ABC1 34 ABC2 45 ABC3 78 ABC4 79 ABC5 23 ABC6 61 ABC7 34 ABC8 54 ABC9 90 I have to display the dashboard as below, top 3 sales guys and the overall total sales made by rest of the team as ROT which is 498 - (90 + 78 + 79) = 251 team: ABC9 90 ABC4 79 ABC3 78 ROT 251 For the top sales made, I gave a filter by sales person name, with Limit condition as "Top 3". But I am struggling to display the ROI even in a separate worksheet. Any help is appreciated.
Right click on your dimension [Sales Guy] and choose Create/Set Define the set by the Top N (either hard code it or use a parameter to change it easily) and call it [TopNSalesGuy] Create a calculated field [TopNSalesGuysPlusOther] with the formular: IF [TopNSalesGuy] THEN [Sales Guy] ELSE 'ROT' END Use [TopNSalesGuysPlusOther] in your table/graph and you should have the top N sales guys by name and everythign else as 'ROT
Derived Table(View) is required [closed]
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 8 years ago. Improve this question I have the below table in database: "ID Number" "Balance Amount Type" "Balance Amount" 234 20 94 234 21 102 234 22 100 212 20 40 212 21 50 212 22 60 I want to create a below derived table(which is just like views in database) from the above table Universe having below fields : "ID" "BalAmount if Amount Type=20" "BalAmount if Amount Type=21" "BalAmount if Amount Type=22" 234 94 102 100 212 40 50 60 Can you please help me in writing SQL for this derived table, (Database is DB2) ?
A derived table is nothing more than a regular SELECT: SELECT ID, MAX(CASE WHEN amount_type=20 THEN balamount END) type_20_amt, MAX(CASE WHEN amount_type=20 THEN balamount END) type_21_amt, MAX(CASE WHEN amount_type=20 THEN balamount END) type_22_amt FROM table GROUP BY ID EDIT added in response to comment: The max() function is required in order to put all values on one row. Using the sample data from the question, a query without max() would produce: id type_20_amt type_21_amt type_22_amt -- ----------- ----------- ----------- 234 94 234 102 234 100 212 40 212 50 212 60 Using max(), however, puts them on one row: id type_20_amt type_21_amt type_22_amt -- ----------- ----------- ----------- 234 94 102 100 212 40 50 60