Postgresql Select Query solution - sql

Can anyone help me with this scenario ?
Actual Table
rrno | filename | type | amount | element
--------------------------------------------------------
000000000001 | 00dww | 0200 | 500 | 45
000000000001 | d00dww | 0200 | 700 | 456
000000000001 | addww | 0100 | 250 | 7236
000000000001 | qc5gdw | 0400 | 431 | 173
012600003598 | q979wa | 0110 | 050 | --
Current Query
select rrno,filename,type,amoumt
from table
where type in ('0220)
and amount in ('500','700');
Result for Current query
rrno | filename | type | amount | element
--------------------------------------------------------
000000000001 | 00dww | 0200 | 500 | 45
000000000001 | d00dww | 0200 | 700 | 456
after getting the above results i want to check whether
rrno field 000000000001 has type '0100' with amount 050.
How to create it in single query ?

I suspect that you want exists:
select rrno, filename, type, amoumt
from mytable t
where type = '0220' and amount in ('500', '700') and exists (
select 1
from mytable t1
where t1.rrno = t.rrno and t1.type = '0100' and t1.amount = '050'
)
Starting from your existing query, this filters the resultset on rows for which another row exists in the table with the same rrno, type '0100' and amount '050'.
I find quite suprising that a column called amount would be of a string datatype. If it's a number, then remove the single quotes around the values.

Related

SQL: Format Numbers to Display as 4-Digits

Working with multiple data tables in GBQ that all display the common ID_NUM, which are all four-digit numbers. However in one table, they do not include leading 0's, meaning that my tables look like this:
---------------------------------------
| TABLE A | TABLE B | TABLE C |
---------------------------------------
| 0111 | 0111 | 111 |
----------------------------------------
| 0112 | 0112 | 112 |
----------------------------------------
| 0234 | 0234 | 234 |
----------------------------------------
| 1215 | 1215 | 1215 |
----------------------------------------
| 5665 | 5665 | 5665 |
----------------------------------------
When I'm trying to combine the data and filter on specific ID_NUM values, filtering on '0111' will not pull back results where '111' is the ID_NUM in Table C.
I've attempted to use FORMAT(ID_NUM, 0000) but it results in too many arguments, and I'm not sure what is triggering that error.
You can use safe_cast():
where safe_cast(id_num as int64) = 0
You can use below options:
Integer Value:
SELECT FORMAT("%04d", 111);
String Value:
SELECT LPAD('111', 4, '0');

SQL GROUPING with conditional

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
);

how to get last processed rows in cursor

I am having table data like this below
+---------------+----------+-----------+----------+--------+-------+
| BilliedAmount | paidamnt | AccAmount | addAmunt | 1stBC | 2ndBC |
+---------------+----------+-----------+----------+--------+-------+
| 10358.00 | 1523.55 | 8725.41 | 460 | | |
+---------------+----------+-----------+----------+--------+-------+
| 222.00 | 103.84 | 118.16 | 73.76 | 222.00 | 99203 |
+---------------+----------+-----------+----------+--------+-------+
| 10358.00 | 1523.55 | 8725.41 | 460 | | |
+---------------+----------+-----------+----------+--------+-------+
| 222.00 | 103.84 | 118.16 | 73.76 | | |
+---------------+----------+-----------+----------+--------+-------+
I am using cursor to process row by row in this kind of data
Condition 1 :
If 1st BC and 2nd BC are not there i need insert this values into one table (BilliedAmount paidamnt AccAmount addAmunt)
Condition 2 :
If 1st BC and and 2nd Bc are there i need to insert 1st BC and 2nd Bc into other table
I am using cursor so i need to check for previous value(check for 1st BC and 2nd Bc vaues) when cursor is processing for the current row and access those values and store it in a table..
Would any one please help on this, that would be very grateful to me.
Many thanks in advance..
Don't need to use cursor just use INSERT statement with SELECT statement
insert into newtable1(BilliedAmount, paidamnt, AccAmount, addAmunt)
select BilliedAmount, paidamnt, AccAmount, addAmunt
from table
where 1stBC is null and 2ndBC is null -- 1STBC and 2ndBC are not there
insert into newtable2(1stBC, 2ndBC)
select 1stBC , 2ndBC
from table
where 1stBC is not null and 2ndBC is not null -- 1STBC and 2ndBC are there

Pick a record based on a given value in postgres

I have a table in postgres like below,
alg_campaignid | alg_score | cp | sum
----------------+-----------+---------+----------
9829 | 30.44056 | 12.4000 | 12.4000
9880 | 29.59280 | 12.0600 | 24.4600
9882 | 29.59280 | 12.0600 | 36.5200
9827 | 29.27504 | 11.9300 | 48.4500
9821 | 29.14840 | 11.8800 | 60.3300
9881 | 29.14840 | 11.8800 | 72.2100
9883 | 29.14840 | 11.8800 | 84.0900
10026 | 28.79280 | 11.7300 | 95.8200
10680 | 10.31504 | 4.1800 | 100.0000
From which i have to select a record based on randomly generated number from 0 to 100.i.e first record should be returned if random number picked is between 0 and 12.4000,second if rendom is between 12.4000 and 24.4600,and likewise last if random no is between 95.8200 and 100.0000.
For Example
if the random number picked is 8 then the first record should be returned
or
if the random number picked is 48 then the fourth record should be returned
Is it possible to do this postgres if so kindly recommend a solution for this..
Yes, you can do this in Postgres. If you want to generate the number in the database:
with r as (
select random() * 100 as r
)
select t.*
from table t cross join r
where t.sum <= r.r
order by t.sum desc
limit 1;

How to check date in postgresql

my table name is tbl1. The fileds are id,name,txdate.
| ID | NAME | TXDATE |
| 1 | RAJ | 1-1-2013 |
| 2 | RAVI | |
| 3 | PRABHU | 25-3-2013 |
| 4 | SAT | |
Now i want to use select query for check txdate < 2-2-2013 in which rows have txdate not empty and the select also retrun which rows have txdate empty.
The Result is like this
| ID | NAME | TXDATE |
| 1 | RAJ | 1-1-2013 |
| 2 | RAVI | |
| 4 | SAT | |
Any feasible solution is there?.
With out using union it is possible?.
Assuming that the TXDATE is of data type DATE then you can use WHERE "TXDATE" < '2013-2-2' OR "TXDATE" IS NULL. Something like:
SELECT *
FROM table1
WHERE "TXDATE" < '2013-2-2'
OR "TXDATE" IS NULL;
See it in action:
SQL Fiddle Demo
I don't now what database your are using and what data type the TXDATE is.
I just tried on my postgreSQL 9.2, with a field "timestamp without time zone".
I have three rows in the table , like:
ac_device_name | ac_last_heartbeat_time
----------------+-------------------------
Nest-Test1 |
Nest-Test3 |
Nest-Test2 | 2013-04-10 15:06:18.287
Then use below statement
select ac_device_name,ac_last_heartbeat_time
from at_device
where ac_last_heartbeat_time<'2013-04-11';
It is ok to return only one record:
ac_device_name | ac_last_heartbeat_time
----------------+-------------------------
Nest-Test2 | 2013-04-10 15:06:18.287
I think you can try statement like:
select * from tbl1 where TXDATE<'2-2-2013' and TXDATE is not NULL
this statement also works in my environment.