I have the following table, with example data:
sample data table
Now, I want to buil a script, that based on a "value_1" provided in a section before (let's say for this sample data, value_1 = 44277), I wanted for find out what is the following absolute value in the table, and bring that as a result.
On example amount I gave here, I was expecting the result to be "45757".
Is below script ok?
Or really open to any suggestions please.
''''
SELECT MIN( value_1) AS next_salary_fx
FROM aprvalues
WHERE client = 'CY'
AND attribute_id = 'CS'
AND status = 'N'
AND value_id = 'C100'
AND value_1 >= '44277'
AND dim_value LIKE 'A10' + '%'
GROUP BY attribute_id, client''''
Related
I am using PSQL.
I have a table with a few columns, one column is event that can have 4 different values - X1, X2, Y1, Y2. I have another column that is the name of the service and I want to group by using this column.
My goal is to make a query that take an event and verify that for a specific service name I have count(X1) == count(X2) if not display a new column with "error"
Is this even possible? I am kinda new to SQL and not sure how to write this.
So far I tried something like this
select
service_name, event, count(service_name)
from
service_table st
group by
(service_name, event);
I am getting the count of each event for specific service_name but I would like to verify that count of event 1 == count of event 2 for each service_name.
I want to add that each service_name have a choice of 2 different event only.
You may not need a subquery/CTE for this, but it will work (and makes the logic easier to follow):
WITH event_counts_by_service AS (SELECT
service_name
, COUNT(CASE WHEN event='X1' THEN 1 END) AS count_x1
, COUNT(CASE WHEN event='X2' THEN 1 END) AS count_x2
FROM service_table
GROUP BY service_name)
SELECT service_name
, CASE WHEN count_x1=count_x2 THEN NULL ELSE 'Error' END AS are_counts_equal
FROM event_counts_by_service
I have a query which has a condition to eliminate certain rows from the output. I need this condition to run for only two organizations. For the rest it should by pass the condition and return all the rows. This is the query
SELECT lines.customer_trx_id customer_trx_id,
lines.unit_selling_price,
lines.customer_trx_line_id customer_trx_line_id
FROM mtl_units_of_measure uom,
ra_customer_trx_lines_all lines,
ra_customer_trx_all trx
WHERE trx.customer_trx_id = lines.customer_trx_id
AND trx.complete_flag = 'Y'
AND lines.uom_code = uom.uom_code(+)
AND lines.line_type = 'LINE'
AND trx.customer_Trx_id =1
AND lines.unit_selling_price <>0
AND lines.description NOT LIKE '%PROM%DISCOUNT%'
AND lines.description NOT LIKE '%TIER%DISCOUNT%'
The last two conditions should run for only orgs 1 and 2 but not for org 3. the field name for org is org_id.
i dont know which table org_id belongs to , so i just added it as org_id below. Just some extra pharantesis and OR condition can do the trick i think, just make sure that org_id doesnt contain null's :
SELECT lines.customer_trx_id customer_trx_id,
lines.unit_selling_price,
lines.customer_trx_line_id customer_trx_line_id
FROM mtl_units_of_measure uom,
ra_customer_trx_lines_all lines,
ra_customer_trx_all trx
WHERE trx.customer_trx_id = lines.customer_trx_id
AND trx.complete_flag = 'Y'
AND lines.uom_code = uom.uom_code(+)
AND lines.line_type = 'LINE'
AND trx.customer_Trx_id =1
AND lines.unit_selling_price <>0
AND
( (lines.description NOT LIKE '%PROM%DISCOUNT%'
AND lines.description NOT LIKE '%TIER%DISCOUNT%'
and org_id in (1,2)) or
org_id not in (1,2)
)
I have a problem with duplicate records in a SQL Server 2014 database.
Users get a small postcard with a parcel number printed on them.
The postcard also shows a link to a simple form that they can use, to register their parcel.
The form unfortunately does not have any type of validation, to ensure that the same parcel does not get submitted more than once.
I currently have no control on the web form, and I am not sure how long will take for the responsible team to implement validation on it.
So I have to come up with a routine to deactivate the duplicate records, and keep only one.
This has to be a query that process a bulk of records, no tokens passed to the routine.
When the web form gets submitted, it creates a record id in sequential order, and assigns an application status of "Registered'.
I think that the way to correct this, would be to take highest record id value per parcel, and that would be the one to keep, the rest, will have to be deactivated.
Deactivate the non most recent records putting a rec_status of "I"
Set APPLICATION_STATUS to 'Closed' to the non most recent records
The query I use, returns 4 columns: Record Id, Parcel Number, Record Status, and Application Status
SELECT
B.[RECORD_ID],
B.[PARCEL_NBR],
B.[RECORD_STATUS], -- The value of this column would be "I" for the duplicate records.
B.[APPLICATION_STATUS]
FROM
A_TABLE A
INNER JOIN B_TABLE B
ON A.PARCEL_NBR = B.PARCEL_NBR
AND (A.APPLICATION_STATUS IS NULL
OR B.APPLICATION_STATUS = 'Registered');
Initial Output:
RECORD_ID PARCEL_NBR RECORD_STATUS APPLICATION_STATUS
REC-00081 0608012098 A Registered
REC-00082 0608012098 A Registered
REC-00083 0608012098 A Registered
Expected Output:
RECORD_ID PARCEL_NBR RECORD_STATUS APPLICATION_STATUS
REC-00081 0608012098 I Closed - this record got updated
REC-00082 0608012098 I Closed - this record got updated
REC-00083 0608012098 A Registered
I think that perhaps a cursor might be part of the solution? Honestly I am not sure. I kindly ask for your help.
You can use window functions and case logic:
SELECT B.[RECORD_ID], B.[PARCEL_NBR],
(CASE WHEN ROW_NUMBER() OVER (PARTITION BY B.PARCEL_NBR ORDER BY B.RECORD_ID DESC) > 1
THEN 'I' ELSE B.[RECORD_STATUS]
END) as RECORD_STATUS,
(CASE WHEN ROW_NUMBER() OVER (PARTITION BY B.PARCEL_NBR ORDER BY B.RECORD_ID DESC) > 1
THEN Closed - this record got updated ELSE B.APPLICATION_STATUS
END) as APPLICATION_STATUS,
B.[]
FROM A_TABLE A JOIN
B_TABLE B
ON A.PARCEL_NBR = B.PARCEL_NBR AND
(A.APPLICATION_STATUS IS NULL OR B.APPLICATION_STATUS = 'Registered');
I'm not sure what role A_TABLE plays in this, but this may give you what you want:
update B_TABLE
set record_Status = 'I'
, application_status = 'Closed - this record got updated'
where record_status = 'A'
and application_status = 'Registered'
and record_id <> (select max(record_id)
from B_TABLE b
where b.parcel_nbr = B_TABLE.parcel_nbr
and b.record_status = 'A'
and b.application_status = 'Registered');
I am trying to be more specific on my query as you can see (link to image below) from the cupID column there is groups A to H 3 times. All I am trying to do is have 3 queries, first query to output all groups A-H only once, second query from the second and third from the third if that makes sense?
This is the query
SELECT cupID, date, matchno,
clan1,
clan2,
si
FROM ws_bi2_cup_matches
WHERE ladID='0'
AND matchno = '6'
AND TYPE = 'gs'
GROUP BY clan1
ORDER BY cupID ASC
which shows: (take a look at picture)
http://s13.postimg.org/6rufgywcn/image.png
so query 1/2/3 should output separately like (a,b,c,d etc) instead of 1 query showing multiples (aaa,bbb,ccc,ddd etc)
Many thanks for help
Based on the assumption that you are performing a union all on your 3 queries, please add a dummy column viz SortOrder and order by on it.
In the following sample query (SQL Server), I assumed all 3 queries as same, please do change them accordingly with the dummy sortorder:
-- 1st query
SELECT cupID, date, matchno,
clan1,
clan2,
si,
1 as SortOrder -- dummy sort column
FROM ws_bi2_cup_matches
WHERE ladID='0'
AND matchno = '6'
AND TYPE = 'gs'
GROUP BY clan1
union all
-- 2nd query
SELECT cupID, date, matchno,
clan1,
clan2,
si,
2 as SortOrder -- dummy sort column
FROM ws_bi2_cup_matches
WHERE ladID='0'
AND matchno = '6'
AND TYPE = 'gs'
GROUP BY clan1
union all
-- 3rd query
SELECT cupID, date, matchno,
clan1,
clan2,
si,
3 as SortOrder -- dummy sort column
FROM ws_bi2_cup_matches
WHERE ladID='0'
AND matchno = '6'
AND TYPE = 'gs'
GROUP BY clan1
order by 7 -- dummy sort order column
Easier to describe by showing a simplified view of the existing data structure and the desired result...
CURRENTLY...
Element Response ElementType ElementNumber
EntryVal.1 1234.56 EntryVal 1
EntryDes.1 'Current Value' EntryDes 1
EntryVal.2 4321.0 EntryVal 2
EntryDes.2 'Another Value' EntryDes 2
EntryVal.3 6543.21 EntryVal 3
EntryDes.3 'Final Value' EntryDes 3
DESIRED...
Name Value
Current Value 1234.56
Another Value 4321.0
Final Value 6543.21
(split element column into ElementType and ElementNumber column in the hopes
it might help)
Have tried various sub-selects but have not found the secret.
Could do some looping in PHP but hope there is a more elegant sole single MySQL query approach.
There is other columns like location involved so trying to keep it clean.
Here's how I'd do it:
SELECT des.Response AS Name, val.Response AS Value
FROM MyTable AS des JOIN MyTable AS val USING (ElementNumber)
WHERE des.ElementType = 'EntryDes' AND val.ElementType = 'EntryVal';
Use:
SELECT MAX(CASE WHEN t.elementtype = 'EntryDes' THEN t.response END) AS Name,
MAX(CASE WHEN t.elementtype = 'EntryVal' THEN t.response END) AS Value,
FROM YOUR_TABLE t
GROUP BY t.elementnumber
You might want to keep elementnumber as a column, in case you need to ensure order.