I am getting different results to the query, the only difference is the order of the where conditions.
Can this be because something is wrong with the source table.
SELECT *
FROM source
WHERE ((submission <> 'Y' or submission is NULL) and payment >0.01)
OR (submission='Y' and payment_2 > 0.01);
Result 1:
+----------+-------------+-------------+------------+----------+
| Name | entry | submission | payment_2 | payment |
+------------------------------+-------------+-----------------+
| abc | 1 | NULL | 0.0 | 790.5 |
+----------+-------------+-------------+------------+----------+
Query 2:
SELECT *
FROM source
WHERE (submission='Y' and payment_2 > 0.01)
OR ((submission <> 'Y' or submission is NULL) and payment >0.01);
Result 2:
+----------+-------------+-------------+------------+----------+
| Name | entry | submission | payment_2 | payment |
+------------------------------+-------------+-----------------+
+----------+-------------+-------------+------------+----------+```
Related
I am using two filers from a table to get the data, i am getting two rows out of which i need to select the row with the max date. Anyone can suggest better way to get it as i am not getting any result from below
SELECT inv,account,activity,seq,st_date,open_amt,State,activity_date
FROM table
WHERE inv = 'test_inv'
AND State = 'issued'
AND activity_date = (select MAX (activity_date) FROM table)
Added Table for more context, i need data based below two conditions
1. Max (activity_date) with Open_amt <> 0 and
2. Exclude rows if Max (activity_date) with Open_amt = '0' and State = 'closed'
| Inv #| Account #|Activity|State |Open_Amt|Last Activity Dt|Seq|St_date |
| Inv #| Account #|Activity|State |Open_Amt|Last Activity Dt|Seq|St_date |
| -----| ---------|--------|------|--------|----------------|---|----------|
| 123 | Customer1| Act-1 |Issued|12.50 |2022-02-18 |455|2022-01-04|
| 123 | Customer1| Act-2 |Closed|0.00 |2022-03-05 |567|2022-01-04|
| 345 | Customer2| Act-1 |Issued|15.00 |2022-02-18 |467|2022-01-12|
| 345 | Customer2| Act-2 |Issued|09.35 |2022-02-25 |488|2022-01-12|
| 678 | Customer3| Act-1 |Issued|30.50 |2022-03-20 |589|2022-01-23|
| 678 | Customer3| Act-2 |Closed|00.00 |2022-03-30 |623|2022-01-23|
| 678 | Customer3| Act-3 |Issued|30.50 |2022-04-02 |788|2022-01-23|
| 678 | Customer3| Act-4 |Issued|05.50 |2022-04-10 |988|2022-01-23|
for above table below is the output
| Inv #| Account #|Activity|State |Open_Amt|Last Activity Dt|Seq|St_date |
| -----| ---------|--------|------|--------|----------------|---|----------|
| 345 | Customer2| Act-2 |Issued|09.35 |2022-02-25 |488|2022-01-12|
| 678 | Customer3| Act-4 |Issued|05.50 |2022-04-10 |988|2022-01-23|
You want the max date from only the qualifying rows, you need to correlate the subquery:
SELECT inv, account, activity, seq, st_date ,open_amt, State, activity_date
FROM table t
WHERE inv = 'test_inv'
AND State = 'issued'
AND activity_date = (
SELECT MAX (activity_date)
FROM table t2
WHERE t2.inv = t.inv AND t2.State = t.State
);
I need help getting the User which has an 'IN' and 'Out' in Column isIN. If the user has an IN and OUT do not select them in the list. I need to select the user who has only had an IN. Please I need help. Thanks in advance.
This is the table:
| Users | IsIN |
|:------------------:|:-----:|
| MHYHDC61TMJ907867 | IN |
| MHYHDC61TMJ907867 | OUT |
| MHYHDC61TMJ907922 | IN |
| MHYHDC61TMJ907922 | OUT |
| MHYHDC61TMJ907923 | IN |
| MHYHDC61TMJ907923 | OUT |
| MHYHDC61TMJ907924 | IN | - I need to get only this row
| MHYHDC61TMJ907925 | IN |
| MHYHDC61TMJ907925 | OUT |
| MHYHDC61TMJ908054 | IN | - I need to get only this row
| MHYHDC61TMJ908096 | IN | - I need to get only this row
| MHYHDC61TMJ908109 | IN | - I need to get only this row
Need to get the result like
| Users | IsIN |
|:------------------:|:-----:|
| MHYHDC61TMJ907924 | IN |
| MHYHDC61TMJ908054 | IN |
| MHYHDC61TMJ908096 | IN |
| MHYHDC61TMJ908109 | IN |
I tried using this query and sample query below but it doesn't work.
select s.[Users], s.[isIn] [dbo].[tblIO] s
where not exists (
select 1
from [dbWBS].[dbo].[tblIO] s2
where s2.[Users] = s.[Users] and s2.isIn = 'IN'
);
You can use not exists:
select s.*
from sample s
where not exists (select 1
from sample s2
where s2.user = s.user and s2.inout = 'OUT'
);
If you want only users that meet the condition (and not the full rows):
select user
from sample s
group by user
having min(inout) = max(inout) and min(inout) = 'IN';
Bearing in mind that an 'OUT' IsIn must be always preceded by an 'IN' record, you could use a query like this:
select s.Users, 'IN' as IsIn
from sample s
group by s.Users
having count(distinct s.IsIn) = 1
I am sure this is easy to accomplish but after spending the whole day trying I had to give up and ask for your help.
I have a table that looks like this
| PatientID | VisitId | DateOfVisit | FollowUp(Y/N) | FollowUpWks |
----------------------------------------------------------------------
| 123456789 | 2222222 | 20180802 | Y | 2 |
| 123456789 | 3333333 | 20180902 | Y | 4 |
| 234453656 | 4443232 | 20180506 | N | NULL |
| 455344243 | 2446364 | 20180618 | Y | 12 |
----------------------------------------------------------------------
Basically I have a list of PatientIDs, each patient can have multiple visits (VisitID and DateOfVisit). FollowUp(Y/N) specifies whether the patients has to be seen again and in how many weeks (FollowUpWks).
Now, what I need is a query that extracts PatientsID, DateOfVisit (the most recent one and only if FollowUp is YES) and the FollowUpWks field.
Final result should look like this
| PatientID | VisitId | DateOfVisit | FollowUp(Y/N) | FollowUpWks |
----------------------------------------------------------------------
| 123456789 | 3333333 | 20180902 | Y | 4 |
| 455344243 | 2446364 | 20180618 | Y | 12 |
----------------------------------------------------------------------
The closest I could get was with this code
SELECT PatientID,
Max(DateOfVisit) AS LastVisit
FROM mytable
WHERE FollowUp = True
GROUP BY PatientID;
The problem is that when I try adding the FollowUpWks field to the SELECT I get the following error: "The query does not include the specified expression as part of an aggregate function." However, if I add FollowUpWks to the GROUP BY statement than I get all visits, not just the most recent ones.
You need to match back to the most recent visit. One method uses a correlated subquery:
SELECT t.*
FROM mytable as t
WHERE t.FollowUp = True AND
t.DateOfVisit = (SELECT MAX(t2.DateOfVisit)
FROM mytable as t2
WHERE t2.PatientID = t.PatientID
);
I have three tables :
BookingNode , Booking AirTrip
AirTrip :
+----+------------+
| ID | Name |
+----+------------+
| 0 | One way |
| 1 | Round trip |
| 2 | Circle |
| 3 | Other |
+----+------------+
When ever we make a booking we store the data as :
BookingNode table
+--------+-------------------+------------+----------------------+
| ID | CustomerGivenName | IPAddress | Email |
+--------+-------------------+------------+----------------------+
| 177022 | xfghfh | 2130706473 | mikehussey#gmail.com |
| 177021 | cfggjfj | 2130706473 | mikehussey#gmail.com |
+--------+-------------------+------------+----------------------+
Booking Table :
+--------+---------------+-----------+------------+------------+
| ID | BookingNodeID | AirTripID | AirLineId | Provider |
+--------+---------------+-----------+------------+------------+
| 181251 | 177020 | 1 | 978 | Jet |
| 181252 | 177021 | 0 | 982 | Go |
| 181253 | 177021 | 0 | 978 | Jet |
+--------+---------------+-----------+------------+------------+
If round trip flight is booked and ProviderID is same then a single entry is done in Booking Table with AirTripID value as 1.(Booking ID : 181251 and Provider Jet )
But if providers are different for both the legs then two entries are done in Booking Table with AirTripID for both entries are one(Booking ID : 181252 and 181253 Provider Go,Jet ).In this case BookingNodeID value being same.
Prob : I have to write a query to get different type of Bookings.(Oneway, RoundTrip,Circle).But when I apply join on AirTripID , it is giving me incorrect results.
How can I write my query to give correct results knowing that BookingNodeID is going to be the same for roundtrip (both entries in Booking Table)
Sample Output
+-------------+---------------+-------------------+------------+
| AirTripName | BookingNodeID | CustomerGivenName | IPAddress |
+-------------+---------------+-------------------+------------+
| TwoWay | 177020 | xfghfh | 2130706473 |
| TwoWay | 177021 | cfggjfj | 2130706473 |
+-------------+---------------+-------------------+------------+
Basically, this code might have an error due to my laziness syntom of data entry. But, the logic of the query is, if b.AirTripID is 0, add extra condition which group by Booking. if result return more than 1 row, is actually 2 way. so AirTripType will become 1, otherwise, remain the same as b.AirTripID. You may copy below on and try fix if theres any error. i believe the logic should work based on your expected result.
select
bd.ID,
bd.CustomerGivenName,
case b.AirTripID
when 1 then 1
when 2 then 2
when 3 then 3
when 0 then
case select BookingNodeID
from Booking
where Booking.BookingNodeID = bd.ID group by BookingNodeID having Count(BookingNodeID)
when 1 then 1
else 0 end as AirTripType,
bd.IPAddress
from BookingNode bd
inner join (select BookingNodeID ,AirTripID from Booking group by BookingNodeID ,AirTripID) as b on b.BookingNodeID = bd.ID
where id=177021
Try This
WITH CTE
AS
(
SELECT
SeqNo = ROW_NUMBER() OVER(PARTITION BY BN.ID ORDER BY B.ID),
B.BookingNodeID,
BN.CustomerGivenName,
BN.IPAddress,
AirTripId = A.ID,
AirTripNm = A.Name
FROM Booking B
INNER JOIN AirTrip A
ON A.ID = B.AirTripID
LEFT JOIN BookingNode BN
ON B.BookingNodeID = BN.id
)
SELECT
C1.SeqNo,
AirTripName = CASE WHEN C2.SeqNo IS NOT NULL
THEN 'Round trip'
ELSE C1.AirTripNm END,
C1.BookingNodeID,
C1.CustomerGivenName,
C1.IPAddress
FROM CTE C1
LEFT JOIN CTE C2
ON C1.BookingNodeID = C2.BookingNodeID
AND C2.SeqNo = 2
WHERE c1.SeqNo = 1
SQL Fiddle Link Here
Select distinct bk.bookingnodeid,cst.customername,ipaddress,
case when count(airtripid)over(partition by bookingnodeid order by bookingnodeid)=2 then 'RoundTrip' else name end As AirTripName
from booking bk
inner join airlinetrip at
on bk.airtripid=at.id
inner join customer cst
on cst.id=bk.bookingnodeid
Lets say I have this table :
--------------------------------------------------------------------------
| id | allowed_to_play_pokemon_go_after | last_played_pokemon_go | name |
--------------------------------------------------------------------------
| 1 | 20-JUL-16 | 19-JUL-16 | Jhon |
--------------------------------------------------------------------------
| 2 | 19-JUL-16 | 21-JUL-16 | Bill |
--------------------------------------------------------------------------
Now I want to make a select like that :
SELECT name, (last_played_pokemon_go > allowed_to_play_pokemon_go_after) as must_punish
FROM myTable;
Where 'must_punish' has to bo a boolean (1/0).
You can use case:
SELECT name,
(case when last_played_pokemon_go > allowed_to_play_pokemon_go_after then 1 else 0
end) as must_punish
FROM myTable;
Oracle (the database) doesn't have a boolean data type. A number should be fine.