DimGroupCustomer Table
╔═══════════════╦═══════════╦══════════════╗
║ CustomerGroup ║ Customer ║ GroupCardKey ║
╠═══════════════╬═══════════╬══════════════╣
║ A ║ Customer1 ║ ACustomer1 ║
║ A ║ Customer2 ║ ACustomer2 ║
║ B ║ Customer1 ║ BCustomer1 ║
║ B ║ Customer3 ║ BCustomer3 ║
║ C ║ Customer4 ║ CCustomer4 ║
║ C ║ Customer5 ║ CCustomer5 ║
╚═══════════════╩═══════════╩══════════════╝
FactSale Table
╔═════════╦══════════════╦════════╗
║ Invoice ║ GroupCardKey ║ Amount ║
╠═════════╬══════════════╬════════╣
║ 1 ║ ACustomer1 ║ 100 ║
║ 2 ║ BCustomer3 ║ 200 ║
╚═════════╩══════════════╩════════╝
I write a "BoughtSomething?" measure and the result looks like
╔══════════════╦═════════════════╗
║ GroupCardKey ║ BoughtSomething ║
╠══════════════╬═════════════════╣
║ ACustomer1 ║ YES ║
║ ACustomer2 ║ NO ║
║ BCustomer1 ║ NO ║
║ BCustomer3 ║ YES ║
║ CCustomer4 ║ NO ║
║ CCustomer5 ║ NO ║
╚══════════════╩═════════════════╝
But I don't want to see no effective customer group at all. I tried but failed. Please help me to get the report like this one.
╔══════════════╦═════════════════╗
║ GroupCardKey ║ BoughtSomething ║
╠══════════════╬═════════════════╣
║ ACustomer1 ║ YES ║
║ ACustomer2 ║ NO ║
║ BCustomer1 ║ NO ║
║ BCustomer3 ║ YES ║
╚══════════════╩═════════════════╝
Try this. Basically check if the customer group bought something in the current date filter context and if so check the individual customer:
Bought Something:=IF(
CALCULATE(COUNTROWS(FactSale), ALLEXCEPT(DimGroupCustomer[CustomerGroup])) >0,
IF(COUNTROWS(FactSale)>0,"YES",NO")
)
Related
all
I've been trying to do, with data following the structure:
╔══════════════╦═══════════╦══╗
║ Alphabetical ║ Numerical ║ ║
╠══════════════╬═══════════╬══╣
║ A ║ 15 ║ ║
║ A ║ 30 ║ ║
║ E ║ 100 ║ ║
║ C ║ 45 ║ ║
║ F ║ 25 ║ ║
║ C ║ 65 ║ ║
║ B ║ 25 ║ ║
║ F ║ 35 ║ ║
║ C ║ 100 ║ ║
║ A ║ 10 ║ ║
║ C ║ 20 ║ ║
║ B ║ 5 ║ ║
║ E ║ 10 ║ ║
║ F ║ 85 ║ ║
║ D ║ 30 ║ ║
║ F ║ 1 ║ ║
╚══════════════╩═══════════╩══╝
To get the following:
╔══════════════╦══════╦═════════╗
║ Alphabetical ║ Rank ║ Numeric ║
╠══════════════╬══════╬═════════╣
║ A ║ 1 ║ 30 ║
║ A ║ 2 ║ 15 ║
║ A ║ 3 ║ 10 ║
║ B ║ 1 ║ 25 ║
║ B ║ 2 ║ 5 ║
║ C ║ 1 ║ 100 ║
║ C ║ 2 ║ 65 ║
║ C ║ 3 ║ 45 ║
║ C ║ 4 ║ 20 ║
║ D ║ 1 ║ 30 ║
║ E ║ 1 ║ 100 ║
║ E ║ 2 ║ 10 ║
║ F ║ 1 ║ 85 ║
║ F ║ 2 ║ 35 ║
║ F ║ 3 ║ 25 ║
║ F ║ 4 ║ 1 ║
╚══════════════╩══════╩═════════╝
Basically, to order the alphabetical field in ascending order, the numerical field in descending order and get the order or rank by using the order used for the numerical field, grouped by the alphabetical field.
I have only achieved it if I limit it to one specific value in the Alphabetical column, by using something like:
select ordered_src.*, ROWNUM Rank from (select src.* from Source src where alphabetical = 'A' order by Numeric desc) ordered_src;
But I have no idea how to get the result shown above. Any idea? Also, is there any alternative that will work also in mysql/mssql/etc?
Thanks!
Use row_number():
select s.*,
row_number() over (partition by alphabetical order by numerical desc) as rank
from source s
order by alphabetical, rank;
Hi i have postgres database and four tables
vehicles -> trips
vehicles -> component_values -> component_types
vehicles:
╔════╦══════════════════════════╦════════════════════════╦════════════════╦═════════╗
║ id ║ slug ║ name ║ manufacturer ║ model ║
╠════╬══════════════════════════╬════════════════════════╬════════════════╬═════════╣
║ 1 ║ manufacturer-x-model-3 ║ Manufacturer X Model 3 ║ Manufacturer X ║ Model 3 ║
║ 2 ║ manufacturer-x-model-1 ║ Manufacturer X Model 1 ║ Manufacturer X ║ Model 1 ║
║ 3 ║ manufacturer-x-model-1-1 ║ Manufacturer X Model 1 ║ Manufacturer X ║ Model 1 ║
╚════╩══════════════════════════╩════════════════════════╩════════════════╩═════════╝
trips:
╔═════╦════════════╦═════════════╦═════════════╦═════════════════╗
║ id ║ vehicle_id ║ name ║ mileage ║ recorded_at ║
╠═════╬════════════╬═════════════╬═════════════╬═════════════════╣
║ 1 ║ 1 ║ 10386735 ║ 386734.997 ║ 2/25/2014 13:56 ║
║ 2 ║ 1 ║ 11771530.14 ║ 771530.14 ║ 3/1/2014 19:41 ║
║ 3 ║ 1 ║ 121112028.4 ║ 1112028.39 ║ 3/5/2014 3:23 ║
║ 4 ║ 1 ║ 131406814.9 ║ 1406814.892 ║ 3/8/2014 20:56 ║
║ 5 ║ 1 ║ 141933528.7 ║ 1933528.711 ║ 3/13/2014 0:19 ║
║ 6 ║ 1 ║ 152256488.6 ║ 2256488.579 ║ 3/16/2014 21:15 ║
╚═════╩════════════╩═════════════╩═════════════╩═════════════════╝
component_values:
╔════╦═══════════════════╦═════════╦════════════╦════════════╦═════════════╦═════════════╗
║ id ║ component_type_id ║ trip_id ║ vehicle_id ║ mileage ║ damage ║ damage_eff ║
╠════╬═══════════════════╬═════════╬════════════╬════════════╬═════════════╬═════════════╣
║ 1 ║ 1 ║ 1 ║ 1 ║ 386734.997 ║ 0.002260565 ║ 0.002225831 ║
║ 2 ║ 2 ║ 1 ║ 1 ║ 386734.997 ║ 0.002260306 ║ 0.002238006 ║
║ 3 ║ 3 ║ 1 ║ 1 ║ 386734.997 ║ 0.002261288 ║ 0.002266295 ║
║ 4 ║ 4 ║ 1 ║ 1 ║ 386734.997 ║ 0.002054489 ║ 0.002060029 ║
║ 5 ║ 5 ║ 1 ║ 1 ║ 386734.997 ║ 0.002052669 ║ 0.002107272 ║
║ 6 ║ 6 ║ 1 ║ 1 ║ 386734.997 ║ NULL ║ NULL ║
║ 7 ║ 7 ║ 1 ║ 1 ║ 386734.997 ║ NULL ║ NULL ║
║ 8 ║ 1 ║ 2 ║ 1 ║ 771530.14 ║ 0.004792952 ║ 0.0048514 ║
║ 9 ║ 2 ║ 2 ║ 1 ║ 771530.14 ║ 0.004792404 ║ 0.004710451 ║
║ 10 ║ 3 ║ 2 ║ 1 ║ 771530.14 ║ 0.004794486 ║ 0.004805461 ║
╚════╩═══════════════════╩═════════╩════════════╩════════════╩═════════════╩═════════════╝
component_types:
╔════╦═════════════════════════════════════╦════════════════╦══════════════════════╗
║ id ║ slug ║ manufacturer ║ name ║
╠════╬═════════════════════════════════════╬════════════════╬══════════════════════╣
║ 6 ║ manufacturer-d-battery ║ Manufacturer D ║ Battery ║
║ 2 ║ manufacturer-b-differential-1 ║ Manufacturer B ║ Differential 1 ║
║ 3 ║ manufacturer-c-driveshaft-1 ║ Manufacturer C ║ Driveshaft 1 ║
║ 5 ║ manufacturer-c-gearbox-output-shaft ║ Manufacturer C ║ Gearbox output shaft ║
║ 1 ║ manufacturer-a-motor-1 ║ Manufacturer A ║ Motor 1 ║
║ 4 ║ manufacturer-c-gearbox-input-shaft ║ Manufacturer C ║ Gearbox input shaft ║
║ 7 ║ usage-profile ║ ║ Usage profile ║
╚════╩═════════════════════════════════════╩════════════════╩══════════════════════╝
and i'm trying to get the vehicles with the latest trip dates and mileage and also the heights and lowest damaged component for each vehicle
so the result will be like:
╔════════════╦══════════════════╦══════════════════════════╦═════════════════════════════════╦════════════════════════════════╦════════════════════════════════╦═══════════════════════════════╗
║ vehicle_id ║ latest_trip_date ║ latest_trip_date_mileage ║ heights_damaged_component_value ║ heights_damaged_component_name ║ lowest_damaged_component_value ║ lowest_damaged_component_name ║
╠════════════╬══════════════════╬══════════════════════════╬═════════════════════════════════╬════════════════════════════════╬════════════════════════════════╬═══════════════════════════════╣
║ 1 ║ 4/19/2014 3:27 ║ 4844305.912 ║ 0.029372972 ║ Gearbox input shaft ║ 0.002052669 ║ Gearbox output shaft ║
║ 2 ║ 5/19/2014 16:13 ║ 5567945.164 ║ 0.029405924 ║ Driveshaft 1 ║ 0.001864137 ║ Gearbox output shaft ║
║ 3 ║ 4/28/2014 12:55 ║ 5286175.763 ║ 0.030745029 ║ Driveshaft 1 ║ 0.001957685 ║ Differential 1 ║
║ 4 ║ 2/25/2014 3:32 ║ 5398006.007 ║ 0.030495792 ║ Driveshaft 1 ║ 0.001814434 ║ Differential 1 ║
║ 5 ║ 4/25/2014 9:51 ║ 5179558.475 ║ 0.032060074 ║ Gearbox input shaft ║ 0.001936431 ║ Differential 1 ║
║ 6 ║ 5/9/2014 7:43 ║ 5234355.804 ║ 0.030576454 ║ Gearbox input shaft ║ 0.002254191 ║ Gearbox output shaft ║
║ 7 ║ 6/21/2014 18:09 ║ 5705722.416 ║ 0.029957374 ║ Driveshaft 1 ║ 0.001653441 ║ Gearbox output shaft ║
║ 8 ║ 4/23/2014 5:25 ║ 5590470.028 ║ 0.031900163 ║ Driveshaft 1 ║ 0.002083733 ║ Gearbox output shaft ║
║ 9 ║ 3/28/2014 20:37 ║ 5598159.883 ║ 0.031208918 ║ Driveshaft 1 ║ 0.00162805 ║ Differential 1 ║
║ 10 ║ 5/24/2014 19:27 ║ 5020795.001 ║ 0.02962505 ║ Gearbox input shaft ║ 0.001729646 ║ Differential 1 ║
╚════════════╩══════════════════╩══════════════════════════╩═════════════════════════════════╩════════════════════════════════╩════════════════════════════════╩═══════════════════════════════╝
i already tried this query
select
vehicles.id as vehicle_id,
latest_trips.recorded_at as latest_trip_date,
latest_trips.mileage as latest_trip_date_mileage,
heights_damaged_components.damage as heights_damaged_component_value,
heights_damaged_components.name as heights_damaged_component_name,
lowest_damaged_components.damage as lowest_damaged_component_value,
lowest_damaged_components.name as lowest_damaged_component_name
from vehicles
left join (
SELECT t.id, t.vehicle_id, t.mileage, t.recorded_at
FROM public.trips t
inner JOIN (SELECT vehicle_id, MAX(recorded_at) as latest_trip_date FROM public.trips GROUP BY vehicle_id)
tm ON t.vehicle_id = tm.vehicle_id AND t.recorded_at = tm.latest_trip_date
)
as latest_trips on latest_trips.vehicle_id = vehicles.id
left join (
select ct.name, hd.component_type_id, hd.vehicle_id, hd.damage
from public.component_values as hd
INNER JOIN (
SELECT vehicle_id,
MAX(damage) as heights_damaged_component
FROM public.component_values
GROUP BY vehicle_id
)
hdm ON hd.vehicle_id = hdm.vehicle_id AND hd.damage = hdm.heights_damaged_component
join public.component_types as ct on ct.id = hd.component_type_id
)
as heights_damaged_components on heights_damaged_components.vehicle_id = vehicles.id
left join (
select ct.name, ld.component_type_id, ld.vehicle_id, ld.damage
from public.component_values as ld
INNER JOIN (
SELECT vehicle_id, MIN(damage) as lowest_damaged_component
FROM public.component_values
GROUP BY vehicle_id
)
ldm ON ld.vehicle_id = ldm.vehicle_id AND ld.damage = ldm.lowest_damaged_component
join public.component_types as ct on ct.id = ld.component_type_id
) as lowest_damaged_components on lowest_damaged_components.vehicle_id = vehicles.id
but i have like 10000 vehicles and big trips and component_values and this query give me the result in like 3 to 6 seconds, is their a batter way to do this with better performance and time?
can i use GENERATED columns in my case and how ?
please any help and many many thanks in advance.
I'm trying to get the first transaction in the history of all accounts belonging to an user_id. For example, in the case of the user_id = 12656 the older transaction of the account_id = 1553 could be the 064d1777-3783 but in the case of the account 1554, the oldest tx is 0cb4cfb2-44a2
This is a fragment of the table "order"
╔═════════╦════════════╦══════════╦══════════╦═══════╗
║ user_id ║ account_id ║ order_id ║ status ║ test ║
╠═════════╬════════════╬══════════╬══════════╬═══════╣
║ 12656 ║ 1553 ║ 1234 ║ CAPTURED ║ false ║
╠═════════╬════════════╬══════════╬══════════╬═══════╣
║ 12656 ║ 1554 ║ 5678 ║ CAPTURED ║ false ║
╠═════════╬════════════╬══════════╬══════════╬═══════╣
║ 12656 ║ 1554 ║ 91011 ║ PENDING ║ false ║
╠═════════╬════════════╬══════════╬══════════╬═══════╣
║ 12659 ║ 1556 ║ 176344 ║ PENDING ║ true ║
╠═════════╬════════════╬══════════╬══════════╬═══════╣
║ 12660 ║ 1557 ║ 261677 ║ CAPTURED ║ false ║
╠═════════╬════════════╬══════════╬══════════╬═══════╣
║ 12661 ║ 1558 ║ 347010 ║ CAPTURED ║ true ║
╠═════════╬════════════╬══════════╬══════════╬═══════╣
║ 12662 ║ 1559 ║ 432343 ║ CAPTURED ║ false ║
╠═════════╬════════════╬══════════╬══════════╬═══════╣
║ 12663 ║ 1560 ║ 517676 ║ CAPTURED ║ false ║
╚═════════╩════════════╩══════════╩══════════╩═══════╝
the same for table "transaction"
╔═════════╦════════════╦══════════╦════════════════╦═════════════════════════╗
║ user_id ║ account_id ║ order_id ║ transaction_id ║ creation_date ║
╠═════════╬════════════╬══════════╬════════════════╬═════════════════════════╣
║ 12656 ║ 1553 ║ 1234 ║ 064d1777-3783 ║ 2012-10-03 00:54:35.042 ║
╠═════════╬════════════╬══════════╬════════════════╬═════════════════════════╣
║ 12656 ║ 1554 ║ 5678 ║ 0cb4cfb2-44a2 ║ 2012-05-06 06:33:13.171 ║
╠═════════╬════════════╬══════════╬════════════════╬═════════════════════════╣
║ 12656 ║ 1554 ║ 91011 ║ 164494f1-3226 ║ 2012-05-29 00:49:27.611 ║
╠═════════╬════════════╬══════════╬════════════════╬═════════════════════════╣
║ 12659 ║ 1556 ║ 176344 ║ 064d1777-3784 ║ 2012-07-25 05:32:48.056 ║
╠═════════╬════════════╬══════════╬════════════════╬═════════════════════════╣
║ 12660 ║ 1557 ║ 261677 ║ 0cb4cfb2-44a3 ║ 2012-04-11 00:42:17.176 ║
╠═════════╬════════════╬══════════╬════════════════╬═════════════════════════╣
║ 12661 ║ 1558 ║ 347010 ║ 164494f1-3227 ║ 2012-05-16 08:01:38.666 ║
╠═════════╬════════════╬══════════╬════════════════╬═════════════════════════╣
║ 12662 ║ 1559 ║ 432343 ║ 064d1777-3785 ║ 2012-04-11 00:48:38.499 ║
╠═════════╬════════════╬══════════╬════════════════╬═════════════════════════╣
║ 12663 ║ 1560 ║ 517676 ║ 0cb4cfb2-44a4 ║ 2012-05-06 06:39:33.44 ║
╚═════════╩════════════╩══════════╩════════════════╩═════════════════════════╝
and I coded the following query:
SELECT order.account_id, order.user_id, tx.transaction_id, MIN(tx.creation_date) AS first_transaction
FROM transaction AS tx
INNER JOIN order AS order ON tx.order_id = order.order_id
WHERE order.test = 'false'
AND order.status = 'CAPTURED'
GROUP BY order.account_id, order.user_id, tx.transaction_id
But this isn't working and I want to know why. Thank you for your help.
Use distinct on:
SELECT DISTINCT ON (o.account_id, o.user_id) o.account_id, o.user_id,
tx.transaction_id, tx.creation_date AS first_transaction
FROM transaction tx INNER JOIN
order o
ON tx.order_id = o.order_id
WHERE o.test = 'false' AND o.status = 'CAPTURED'
ORDER BY o.account_id, o.user_id, tx.creation_date ;
I have 2 tables, and I am trying to match up the data but all the answers have suggested right joins or full joins, which are not available on SQLite.
Table 1:
╔═════════╦═════════╦══════╗
║ Company ║ Product ║ Cost ║
╠═════════╬═════════╬══════╣
║ A ║ abc ║ 100 ║
║ B ║ abc ║ 150 ║
║ F ║ xyz ║ 250 ║
║ G ║ xyz ║ 300 ║
╚═════════╩═════════╩══════╝
However I have a list of more companies (with same products)
Table 2:
╔═════════╦═════════╗
║ Product ║ Company ║
╠═════════╬═════════╣
║ abc ║ A ║
║ abc ║ B ║
║ abc ║ C ║
║ abc ║ D ║
║ abc ║ E ║
║ abc ║ F ║
║ abc ║ G ║
║ xyz ║ A ║
║ xyz ║ B ║
║ xyz ║ C ║
║ xyz ║ D ║
║ xyz ║ E ║
║ xyz ║ F ║
║ xyz ║ G ║
╚═════════╩═════════╝
How do I match them up so they look like this?
Table 3:
╔═════════╦═════════╦══════╗
║ Product ║ Company ║ Cost ║
╠═════════╬═════════╬══════╣
║ abc ║ A ║ 100 ║
║ abc ║ B ║ 150 ║
║ abc ║ C ║ null ║
║ abc ║ D ║ null ║
║ abc ║ E ║ null ║
║ abc ║ F ║ null ║
║ abc ║ G ║ null ║
║ xyz ║ A ║ null ║
║ xyz ║ B ║ null ║
║ xyz ║ C ║ null ║
║ xyz ║ D ║ null ║
║ xyz ║ E ║ null ║
║ xyz ║ F ║ 250 ║
║ xyz ║ G ║ 300 ║
╚═════════╩═════════╩══════╝
When I use this code,
SELECT Company, t.Product, Cost
FROM table1 as t INNER JOIN table2 as f ON t.product = f.product
WHERE t.company = f.company
it only returns [Company] with an associated [Product] and [Cost], but does not return [Company] with null values.
When I use
SELECT Company, t.Product, Cost
FROM table1 as t INNER JOIN table2 as f ON t.company = f.company
then my output looks like
╔═══════════╦═══════════╦═════════╗
║ t.Company ║ f.Company ║ Product ║
╠═══════════╬═══════════╬═════════╣
║ A ║ A ║ abc ║
║ B ║ A ║ abc ║
║ F ║ A ║ abc ║
║ G ║ A ║ abc ║
║ A ║ B ║ abc ║
║ B ║ B ║ abc ║
║ F ║ B ║ abc ║
║ G ║ B ║ abc ║
║ A ║ C ║ abc ║
║ B ║ C ║ abc ║
║ F ║ C ║ abc ║
║ G ║ C ║ abc ║
╚═══════════╩═══════════╩═════════╝
Any help will be much appreciated. Thank you!
SQLite does support LEFT OUTER JOIN, which should do the job just fine:
select two.product, two.company, one.cost from two
left outer join one on
((one.company = two.company) and (one.product = two.product));
(where two is your "table 2" and one is your "table 1")
Running this in SQLite with the above data:
abc|A|100
abc|B|150
abc|C|
abc|D|
abc|E|
abc|F|
abc|G|
xyz|A|
xyz|B|
xyz|C|
xyz|D|
xyz|E|
xyz|F|250
xyz|G|300
My main problem is that I'm not sure what I'm trying to do, so I don't know how to research it. If anyone could give me some terminology or direction or answer I would greatly appreciate it.
I know what my end goal is, I just don't know what the proper terms are.
I have a query that returns this:
select *
from details
where EventId in (1,2,3)
╔═════════╦═════════╦═════╦═══════════════════════╦══════╗
║ EventId ║ Name ║ Col ║ StrValue ║ Col6 ║
╠═════════╬═════════╬═════╬═══════════════════════╬══════╣
║ 1 ║ Person ║ 0 ║ Alice.Anderson ║ NULL ║
║ 1 ║ Machine ║ 0 ║ SPOT-001 ║ NULL ║
║ 1 ║ Address ║ 1 ║ 10.0.0.14 ║ NULL ║
║ 1 ║ Pool ║ 0 ║ GT2 ║ NULL ║
║ 2 ║ Person ║ 0 ║ Bob.Barker ║ NULL ║
║ 2 ║ Machine ║ 0 ║ SPOT-006 ║ NULL ║
║ 2 ║ Address ║ 1 ║ 10.0.0.19 ║ NULL ║
║ 2 ║ Pool ║ 0 ║ GT2 ║ NULL ║
║ 3 ║ Person ║ 0 ║ Christine.Christensen ║ NULL ║
║ 3 ║ Machine ║ 0 ║ SPOT-003 ║ NULL ║
║ 3 ║ Address ║ 1 ║ 10.0.0.34 ║ NULL ║
║ 3 ║ Pool ║ 0 ║ GO1 ║ NULL ║
╚═════════╩═════════╩═════╩═══════════════════════╩══════╝
I would like a query that returns this, where column 2 and 3 names are based on the fields from the previous table:
╔═════════╦══════════╦═══════════════════════╗
║ EventId ║ Machine ║ Person ║
╠═════════╬══════════╬═══════════════════════╣
║ 1 ║ SPOT-001 ║ Alice.Anderson ║
║ 2 ║ SPOT-006 ║ Bob.Barker ║
║ 3 ║ SPOT-003 ║ Christine.Christensen ║
╚═════════╩══════════╩═══════════════════════╝
I've been able to get close with a collection of case when statements, but there's always so many nulls and it doesn't collapse like I need it to.
I have tried something like this, and gotten the following:
select
case when Name in ('Person','Machine') then EventId end,
case Name when 'Machine' then StrValue end,
case Name when 'Person' then StrValue end
from details
where EventId = 1
╔═════════╦══════════╦════════════════╗
║ EventId ║ Machine ║ Person ║
╠═════════╬══════════╬════════════════╣
║ NULL ║ NULL ║ NULL ║
║ 1 ║ Spot-001 ║ NULL ║
║ 1 ║ NULL ║ Alice.Anderson ║
║ NULL ║ NULL ║ NULL ║
║ NULL ║ NULL ║ NULL ║
║ NULL ║ NULL ║ NULL ║
║ NULL ║ NULL ║ NULL ║
╚═════════╩══════════╩════════════════╝
But I don't know how to collapse those two rows and ignore all the nulls, or if there's a more efficient way to do this on large tables.
I'm pretty sure this would give you the output you're expecting:
select
EventId,
max(case Name when 'Machine' then StrValue end) as machine,
max(case Name when 'Person' then StrValue end) as person
from details
where EventId = 1
and name in ('Person','Machine')
group by EventId