I could really use some help creating a pivot table. I have data in some rows that instead need to be appear in columns, juxtaposed next to values in other records.
The data is currently in the following format:
Region | Location | Customer | CustomerKey |Status
North | New York | John | 111 |Active
North | New York | Mary | 112 |Active
North | Delaware | Bob | 113 |Idle
North | New Jersey| Bob | 113 |Active
West | California| Bob | 113 |Inactive
West | Washington| Greg | 114 |Inactive
West | Utah | Tim | 115 |Active
North | All States | Bob | 113 |VIP Customer
North | All States | Mary | 112 |Regular Customer
West | All States | Bob | 113 |Regular Customer
West | All States | Tim | 115 |Regular Customer
West | All States | Greg | 114 |VIP Customer
North | All States | John | 111 |Regular Customer
The issue is with the 'Status' column, which can have one group of values (Inactive/Active/Idle) and another (VIP Customer and Regular Customer). When the 'Location' column is 'All States', it uses the VIP/Regular values. I would like to add a column, to have the data appear along the lines of:
Region | Location | Customer | CustomerKey |Status | VIPStatus
North | New York | John | 111 |Active | No
North | New York | Mary | 112 |Active | No
North | Delaware | Bob | 113 |Idle | Yes
North | New Jersey| Bob | 113 |Active | Yes
West | California| Bob | 113 |Inactive | No
West | Washington| Greg | 114 |Inactive | Yes
West | Utah | Tim | 115 |Active | No
Basically, if the Customer has a record with the Status of a 'VIP Customer', under a combination of a Region and a corresponding Location value of 'All States', then it will show a 'VIPStatus' of 'Yes' or 'No' under any record of that customer under that given Region (regardless of the Location state). Is there a simple solution for this? Any help on rearranging this data would in T-SQL would be greatly appreciated.
You should be able to join on the table multiple times to get the result that you need:
select t1.region,
t1.location,
t1.customer,
t1.customerkey,
t1.status,
case when t2.status is not null then 'Yes' else 'No' end VIPStatus
from yourtable t1
left join yourtable t2
on t1.CustomerKey = t2.CustomerKey
and t2.Location = 'All States'
and t2.status = 'VIP Customer'
where t1.Location <> 'All States'
See SQL Fiddle with Demo
The result is:
| REGION | LOCATION | CUSTOMER | CUSTOMERKEY | STATUS | VIPSTATUS |
-----------------------------------------------------------------------
| North | New York | John | 111 | Active | No |
| North | New York | Mary | 112 | Active | No |
| North | Delaware | Bob | 113 | Idle | Yes |
| North | New Jersey | Bob | 113 | Active | Yes |
| West | California | Bob | 113 | Inactive | Yes |
| West | Washington | Greg | 114 | Inactive | Yes |
| West | Utah | Tim | 115 | Active | No |
Related
select CITY_CALLING
sum(DISTANCE_KM)
from REAL_TRIP join
SOURCE_CITY
on SOURCE_CITY.city_id = REAL_TRIP.city_id
group by 1
city_CALLING | sum |
Visakhapatnam | 14.5920725980000014 |
Hyderabad | 2759.24699709970082 |
San Diego | 87.3699351497999999 |
Moscow | 984.947118170600447 |
Alexandria | 8.96134862429999934 |
Prague | 86.0471747345999916 |
Recife | 20.7398930000000021 |
Leeds | 140.606494992300014 |
Copenhagen | 14.7657918324999997 |
Fresno | 29.6572209023999989 |
Tijuana | 61.7240377603999946 |
Baton Rouge | 7.05829104329999968 |
Krasnodar | 296.730780097399986 |
Sochi | 237.51827971039998 |
Cincinnati | 116.423747349400003 |
Guwahati | 1057.34938192379968 |
Champaign | 6.8250736618000003 |
Vienna | 1180.11211812669899 |
Charlotte | 150.293475570500021 |
Raleigh-Durham | 152.720579113999946 |
select CITY_CALLING
sum(DISTANCE_KM)
from REAL_TRIP join
SOURCE_CITY
on SOURCE_CITY.city_id = REAL_TRIP.city_id
group by 1
HAVING SUM(DISTANCE_KM) > 10000;
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 2 years ago.
Improve this question
I am working on this dataset
https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=447a5d2c33b04346e70dab0a8d098655
Custom logic:
Grouped by name, testcentre, coursename, testtype.
If a retest is taken, then scores are compared - if higher the highest one precedes or original score.
Lag:
Based on above selected row, if a lag window exists between the remaining set of records example within 4 days , then the higest score record shall be picked up.
Any suggestions is appreciated.
Sample data
+----------+-------+------------+------------+----------+-----------+------------+------------+------------------+-----------------------------------------------+
| recordid | Name | testcentre | coursename | testtype | testscore | testdate | retestflag | Preferred_Output | RejectReason |
+----------+-------+------------+------------+----------+-----------+------------+------------+------------------+-----------------------------------------------+
| 1 | Sam | Paris | English | IELTS | 90 | 01/02/2019 | 0 | 0 | |
| 3 | Sam | Paris | English | IELTS | 95 | 02/02/2019 | 1 | 1 | Better score in retest |
| 4 | Sam | Paris | English | TOEFL | 80 | 04/02/2019 | 0 | 0 | Within 4 days of previous test |
| 21 | Sam | Paris | English | IELTS | 95 | 02/02/2018 | 1 | 1 | marked as retest without base.needs inclusion |
| 5 | Jack | London | English | IELTS | 90 | 01/02/2019 | 0 | 1 | Same or bad score in retest |
| 8 | Jack | London | English | IELTS | 90 | 02/02/2019 | 1 | 0 | Same or bad score in retest |
| 7 | Louis | Brazil | English | IELTS | 70 | 01/02/2019 | 0 | 1 | Same score in retest |
| 11 | Louis | Brazil | English | IELTS | 70 | 02/02/2019 | 1 | 0 | Same score in retest |
| 13 | Louis | Brazil | English | TOEFL | 100 | 04/02/2019 | 0 | 0 | Within 4 days of previous test |
| 55 | Sam | Paris | English | IELTS | 90 | 01/02/2016 | 0 | 1 | Older test, no follow on |
| 60 | Sam | Paris | English | IELTS | 95 | 01/08/2019 | 0 | 1 | same score in retest |
| 61 | Sam | Paris | English | IELTS | 95 | 02/08/2019 | 1 | 0 | |
| 62 | Sam | Paris | English | TOEFL | 80 | 04/01/2020 | 0 | 1 | More than 4 days, included |
+----------+-------+------------+------------+----------+-----------+------------+------------+------------------+-----------------------------------------------+
Desired Output
+----------+-------+------------+------------+----------+-----------+------------+------------+------------------+
| recordid | Name | testcentre | coursename | testtype | testscore | testdate | retestflag | Preferred_Output |
+----------+-------+------------+------------+----------+-----------+------------+------------+------------------+
| 3 | Sam | Paris | English | IELTS | 95 | 02/02/2019 | 1 | 1 |
| 21 | Sam | Paris | English | IELTS | 95 | 02/02/2018 | 1 | 1 |
| 5 | Jack | London | English | IELTS | 90 | 01/02/2019 | 0 | 1 |
| 7 | Louis | Brazil | English | IELTS | 70 | 01/02/2019 | 0 | 1 |
| 55 | Sam | Paris | English | IELTS | 90 | 01/02/2016 | 0 | 1 |
| 60 | Sam | Paris | English | IELTS | 95 | 01/08/2019 | 0 | 1 |
| 62 | Sam | Paris | English | TOEFL | 80 | 04/01/2020 | 0 | 1 |
+----------+-------+------------+------------+----------+-----------+------------+------------+------------------+
Based on your sample data and description, this seems to do what you want:
select t.*
from (select t.*,
row_number() over (partition by name, testcentre, coursename, testtype order by testscore desc) as seqnum,
count(*) over (partition by name, testcentre, coursename, testtype) as cnt
from test t
) t
where seqnum = 1 and cnt >= 2;
This does not include the condition on "within 4 days", because that condition is not clearly explained. What happens if there are a series of 5 tests, each 3 days apart, for instance?
select * from (
select *, row_number() over (partition by name order by testscore desc) rn
from test
) t
where rn = 1
Fiddle
I have three tables:
Salesman Table
+-----+---------+----------+
| SID | SNAME | LOCATION |
+-----+---------+----------+
| 1 | Peter | London |
| 2 | Michael | Paris |
| 3 | John | Mumbai |
| 4 | Harry | Chicago |
| 5 | Kevin | London |
| 6 | Alex | Chicago |
+-----+---------+----------+
Sale Table
+--------+-----+-----------+
| SALEID | SID | SLDATE |
+--------+-----+-----------+
| 1001 | 1 | 01-JAN-14 |
| 1002 | 5 | 02-JAN-14 |
| 1003 | 4 | 01-FEB-14 |
| 1004 | 1 | 01-MAR-14 |
| 1005 | 2 | 01-FEB-14 |
| 1006 | 1 | 01-JUN-15 |
+--------+-----+-----------+
Expected Result
+--------+-----------+
| SALEID | SLDATE |
+--------+-----------+
| 1001 | 01-JAN-14 |
| 1002 | 02-JAN-14 |
| 1004 | 01-MAR-14 |
| 1006 | 01-JUN-15 |
+--------+-----------+
I am using Oracle SQLDeveloper. I run the code below:
SELECT S.SALEID, S.SLDATE
FROM Salesman SA
INNER JOIN Sale S ON SA.SID = S.SID
WHERE SA.LOCATION = 'London';
but I get error:
Error: Your result did not match the Expected result.
If anyone can find the errors please answer.
OKAY, I got it, it was an database error, Refreshed the database and the same Query worked.
The query mentioned is correct tho.
HI I am trying to query the distinct localities in my table.
Here is my query.
select distinct city,locality, avg_sqft from real_estate.re_search where city = 'bangalore' AND locality != 'jayanagar';
Result
+-----------+--------------+----------+
| city | locality | avg_sqft |
+-----------+--------------+----------+
| bangalore | bannerghatta | 13500 |
| bangalore | kormangala | 18000 |
| bangalore | kodipur | 7000 |
| bangalore | kormangala | 16000 |
| bangalore | horamavu | 9000 |
| bangalore | bellandur | 15500 |
| bangalore | kodipur | 9000 |
| bangalore | madivala | 12000 |
| bangalore | varthur | 12000 |
| bangalore | kormangala | 13500 |
| bangalore | bellandur | 13000 |
| bangalore | kodipur | 11500 |
| bangalore | kormangala | 14000 |
the problem is I need to display the distinct locality in result.any help will be appreciated.
You should be able to get a list of distinct members of the locality column in your table, where the city is Bangalore by using the COUNT and GROUP BY operators:
SELECT city
,locality
,COUNT(locality)
FROM database.table
WHERE city = 'Bangalore'
GROUP BY city
,locality;
I have table Persons:
----------------------------------------
id | name | phone | house_id |
----------------------------------------
1 | Sarah | 1234567 | 101 |
2 | Joseph | 7654321 | 102 |
3 | David | 1231231 | null |
Ans second table Houses:
----------------------------------------
id | street | number |
----------------------------------------
101 | Evergreen Terrace | 742 |
102 | Baker Street | 223B |
103 | Oxford Street | 23A |
I need such output table as following:
--------------------------------------------------------------------------------
id(person)| name | phone | house_id | id(house) | street | number |
--------------------------------------------------------------------------------
1 | Sarah | 1234567 | 101 | 101 | Evergreen T...| 742 |
2 | Joseph | 7654321 | 102 | 102 | Baker Street | 223B |
3 | David | 1231231 | null | null | null | null |
4 | null | null | null | 103 | Oxford Street | 23A |
What kind of join do I need to use to achieve such result?
SELECT
A.id AS 'Person',
A.name,
A.phone,
A.house_id,
B.id AS 'House',
B.street,
B.number
FROM
Persons AS A
FULL OUTER JOIN Houses AS B
ON A.house_id = B.id