Here are the tables
TAGS :
tag_id
tag_name
1
SENT
2
AIRTIME
3
RECEIVED
4
KOPOKOPO
5
RESTAURANT
6
PAID
7
UBER
TRANS_TAG
tag_id
trans_id
1
1
2
1
3
1
4
1
5
1
6
1
1
2
2
2
3
2
4
2
5
2
6
2
The following are default tags (SENT, AIRTIME, RECEIVED, PAID), the rest are not default tags.
The TRANS_TAG table tracks a transaction id and the tags attached to it.
I would like to write a select SQL statement that selects from these two tables and achieves the following result. - it returns a list of all the tags that have been attached to at least one transaction. it then orders them alphabetically but grouped with the default tags first.
This is the desired result
tag_name
AIRTIME
PAID
RECIVED
SENT
KOPOKOPO
RESTAURANT
This is the code that I have so far
String SQL = "SELECT "
+ KEY_TAG_TITLE +
" FROM " + TABLE_TRANS_TAG
+ " INNER JOIN " + TABLE_TAG + " " + "ON " + TABLE_TRANS_TAG + "." + KEY_TRANS_TAG_TAG_ID
+ " = " + TABLE_TAG + "." + KEY_TAG_ID
+ " GROUP BY " + KEY_TAG_TITLE
+ " ORDER BY " + TABLE_TAG + "." + KEY_TAG_TITLE + " ASC, " + TABLE_TAG + "." + KEY_ROW_ID + " ASC";
You can use:
SELECT T.KEY_TAG_TITLE
FROM TABLE_TRANS_TAG TT INNER JOIN
TABLE_TAG T
ON TT.KEY_TRANS_TAG_TAG_ID = T.KEY_TAG_ID
GROUP BY T.KEY_TAG_TITLE
ORDER BY (CASE WHEN T.KEY_TAG_TITLE IN ('SENT', 'AIRTIME', 'RECEIVED', 'PAID') THEN 1 ELSE 2 END),
T.KEY_TAG_TITLE;
Don't use join and aggregation for such a simple requirement.
It would result in poor performance.
Use EXISTS and conditional sorting:
String SQL = "SELECT tg." + KEY_TAG_TITLE + " " +
"FROM " + TABLE_TAG + " AS tg " +
"WHERE EXISTS (SELECT 1 FROM " + TABLE_TRANS_TAG + " AS tr WHERE tr." + KEY_TRANS_TAG_TAG_ID + " = tg." + KEY_TAG_ID + ") " +
"ORDER BY tg." + KEY_TAG_TITLE + " IN ('SENT', 'AIRTIME', 'RECEIVED', 'PAID') DESC, tg." + KEY_TAG_TITLE;
Related
select
ic.item_name,
lh.locn_brcd from_locn,
lh2.locn_brcd to_locn,
wl.from_container,
wl.to_container,
wl.units,
wl.prev_from_container_status prev_from_lpn_status,
wl.curr_from_container_status curr_from_lpn_status,
wl.prev_to_container_status prev_to_lpn_status,
wl.curr_to_container_status curr_to_lpn_status,
wl.work_batch_number,
wl.transaction_name,
wl.action,
wl.work_id,
wl.date_updated,
wl.source_updated,
wl.tote_number,
wl.chute
from m_work_log wl
LEFT join item_cbo ic on wl.item_id=ic.item_id
left join locn_hdr lh on wl.from_location_id = lh.locn_id
left join locn_hdr lh2 on wl.to_location_id = lh2.locn_id
where wl.action in (:action)
and trunc(wl.date_updated) between :start_date and :end_date
and (ic.item_name in (:list) OR
wl.source_updated = :username OR
wl.to_container in (:LPNList) OR
(:list is null and :username is null and :LPNList is null)
)
order by date_updated desc
Hi everyone,
when I run this code through Oracle SQL Developer and I add two items to the :list parameter and two items to the :action parameter it works fine. But when I run this through SSRS (report builder ) it fails to run and I get an "ORA-00920: invalid relational operator". I'm new to SQL and i'm not sure what I am doing incorrectly here. Any help is greatly appreciated. Thanks!
There are 2 ways to do this:
Multiple Value Parameter:
First of all, you must use Oracle provider, multiple value paramaters does not work with neither ODBC nor OLEDB connections (reference).
Here is an external link explaining in detail here.
Using an expression as the query by putting the whole thing like this ="query_here"
="select "
+ " ic.item_name,"
+ " lh.locn_brcd from_locn,"
+ " lh2.locn_brcd to_locn,"
+ " wl.from_container,"
+ " wl.to_container,"
+ " wl.units,"
+ " wl.prev_from_container_status prev_from_lpn_status,"
+ " wl.curr_from_container_status curr_from_lpn_status,"
+ " wl.prev_to_container_status prev_to_lpn_status,"
+ " wl.curr_to_container_status curr_to_lpn_status,"
+ " wl.work_batch_number,"
+ " wl.transaction_name,"
+ " wl.action,"
+ " wl.work_id,"
+ " wl.date_updated,"
+ " wl.source_updated,"
+ " wl.tote_number,"
+ " wl.chute"
+ "from m_work_log wl"
+ " LEFT join item_cbo ic on wl.item_id=ic.item_id"
+ " left join locn_hdr lh on wl.from_location_id = lh.locn_id"
+ " left join locn_hdr lh2 on wl.to_location_id = lh2.locn_id"
+ "where wl.action in (:action)"
+ " and trunc(wl.date_updated) between :start_date and :end_date"
+ " and (ic.item_name in ('" + Join(Parameters!list.Value , "', '")" + ') OR"
+ " wl.source_updated = :username OR"
+ " wl.to_container in ('" + Join(Parameters!LPNList.Value , "', '")" + ') OR"
+ " (:list = '_N/A_' and :username is null and :LPNList = '_N/A_')"
+ " )"
+ " order by date_updated desc"
In this case you will need to provide default empty values to your lists. I used 'N/A' in my example.
Hi guys I´m doing a native query for my project using JPA, but i don´t know how can I do a type of if and else
in a where clause, my ?0 can be 4 values: 10,20,30 and 40, when ?0 is 10, 20, 30 I should use CI.QTD <=
and when ?0 is 40, the condition should be >= . How can I do this?
Follow my code with one condition:
SELECT rownum AS id,
x.*
FROM (
SELECT ci.text,
ci.lenght
FROM ci_table CI
WHERE (?0 IS NULL OR ci.qtd <= ?0)
GROUP BY ci.text,
ci.lenght
ORDER BY ci.text) x ,
nativequery = true);
ADD THE QUERY AFTER THE ADJUSTMENT:
#Query(value =
"SELECT rownum as ID, X.* FROM (SELECT " +
" CI.PARAM1 " +
" CI.PARAM2, " +
" CI.PARAM3, " +
" CI.PARAM4, " +
" CI.PARAM5" +
" FROM " +
" CM_PARAMS CI " +
" WHERE " +
" (?1 is null or CI.PARAM6 = ?1) " +
" AND (?2 is null or CI.PARAM7 = ?2) " +
" AND (?3 is null or CI.PARAM8 = ?3) " +
" AND (?4 is null or CI.PARAM9 = ?4) " +
" AND (?5 is null or CI.PARAM10 = ?5) " +
" AND (?6 is null or CI.PARAM11 = ?6) " +
" AND (?7 is null or CI.PARAM12 = ?7) " +
" AND (?8 is null or CI.PARAM13 = ?8) " +
" AND (CASE WHEN ?9 >30 THEN CI.PARAM14 > 30 " +
" CASE WHEN ?9 <31 THEN CI.PARAM15 <= ?9 END) = 1 " +
" GROUP BY " +
" CI.PARAM1, " +
" CI.PARAM2, " +
" CI.PARAM3, " +
" CI.PARAM4, " +
" CI.PARAM5" +
" ORDER BY " +
" CI.PARAM1 ASC) X " , nativeQuery = true)
Try:
SELECT rownum AS id,
x.*
FROM (
SELECT ci.text,
ci.lenght
FROM ci_table CI
WHERE (CASE WHEN ?0 IN (10,20,30) THEN CI.QTD <= ?0
WHEN ?0 = 40 THEN CI.QTD > ?0 END) = 1
GROUP BY ci.text,
ci.lenght
ORDER BY ci.text) x ,
nativequery = true);
First of all clarify that I am quite bad with databases, so please do not be to mean with my code :P
I have a problem with a query on hibernate using join and restrictions. I have a huge list of Assignments and some of them have an Asr object.
queryString.append("select new commons.bo.assignment.AssignmentAssociateExtract("
+ " assignment.id, assignment.contract.assignmentStatus,"
+ " assignment.contract.beginDate, assignment.contract.endDate,"
+ " assignment.contract.contractType, assignment.organizationalData.homeCountryKey,"
+ " assignment.organizationalData.hostCountryKey,"
+ " assignment.organizationalData.homeOrgUnitKey,"
+ " assignment.associate.globalIdAssociate,"
+ " assignment.associate.localIdHome,"
+ " assignment.associate.firstName,"
+ " assignment.associate.lastName)"
+ " from Assignment assignment left join assignment.asr asr"
+ " where assignment.contract.assignmentStatus.code = 5"
+ " and asr.homeAsrElegibility is not 'X'"
+ " or asr.homeAsrElegibility is null"
);
I was creating this query step by step. Before I created it without the join clause left join assignment.asr asr and it was working well but of course it was not showing the Assignments that did not have a Asr object. After I added the join clause, now it shows every single Assignment (10.000 records when those who have an assignmentStatus = 5 are just 4.000) and the restrictions like
where assignment.contract.assignmentStatus.code = 5
are not reflected in the result anymore.
So to sum up: I need a list with all assignments with assignmentStatus = 5 and asr.homeAsrElegibility != 'X'. But it needs to include also all assignments with assignmentStatus = 5 even if they do not have an Asr object.
Any ideas?? Thanks!
Parenthesis helps to clarify the situation.
queryString.append("select new commons.bo.assignment.AssignmentAssociateExtract("
+ " assignment.id, assignment.contract.assignmentStatus,"
+ " assignment.contract.beginDate, assignment.contract.endDate,"
+ " assignment.contract.contractType, assignment.organizationalData.homeCountryKey,"
+ " assignment.organizationalData.hostCountryKey,"
+ " assignment.organizationalData.homeOrgUnitKey,"
+ " assignment.associate.globalIdAssociate,"
+ " assignment.associate.localIdHome,"
+ " assignment.associate.firstName,"
+ " assignment.associate.lastName)"
+ " from Assignment assignment left join assignment.asr asr"
+ " where assignment.contract.assignmentStatus.code = 5"
+ " and ((asr.homeAsrElegibility is not null and asr.homeAsrElegibility is not 'X')"
+ " or (asr.homeAsrElegibility is null))"
);
I'm trying to join 3 tables in a view; here is my situation:
I have a table that contains Sale it Contain Sale Details For per Item
Another Sale Master A All Sale Details of All Item..
And Another Inventory Details
String query = "SELECT SALE.ITEM_CODE, SALE.ITEM_NAME, SALE.UNIT, "
+ "SALE.QNTY, SALE.AMOUNT, SALE_MASTER.LONGDATE, SALE_MASTER.BILL_NO, "
+ "SALE_MASTER.LEDGER_CODE, SALE_MASTER.LEDGER_NAME FROM SALE "
+ "INNER JOIN SALE_MASTER"
+ " ON SALE.BILL_NO = SALE_MASTER.BILL_NO SALE"
+ "INNER JOIN INVENTORY ON SALE.ITEM_CODE = INVENTORY.ITEM_CODE"
+ "WHERE "+CATORINORG+" LIKE '%"+LIKE+"%' "
+ "AND (SALE_MASTER.LONGDATE >= " + From + " AND SALE_MASTER.LONGDATE <= " + To + ")";
is it right way.. thanks adv
SELECT SALE.ITEM_CODE, SALE.ITEM_NAME, SALE.UNIT, "
+ "SALE.QNTY, SALE.AMOUNT, SALE_MASTER.LONGDATE, SALE_MASTER.BILL_NO, "
+ "SALE_MASTER.LEDGER_CODE, SALE_MASTER.LEDGER_NAME FROM SALE "
+ "INNER JOIN SALE_MASTER"
+ " ON SALE.BILL_NO = SALE_MASTER.BILL_NO"
+ " INNER JOIN INVENTORY ON INVENTORY.ITEM_CODE = SALE.ITEM_CODE"
+ " WHERE "+CATORINORG+" LIKE '%"+LIKE+"%' "
+ "AND (SALE_MASTER.LONGDATE >= " + From + " AND SALE_MASTER.LONGDATE <= " + To + ")
its worked for me
I have a report that looks like this:
Query Name Attempts Successes Failures SuccessProportion
First Query 1 0 1 0/1
Second Query 1 0 1 0/1
Third Query 2 1 1 1/2
Fourth Query 1 1 0 1/1
Fifth Query 1 0 1 0/1
Sixth Query 1 0 1 0/1
this is my code that creates the report:
l_strsql = "SELECT Query_Name, " +
" Attempts, " +
" Successes, " +
" Failures, " +
" CAST([Successes] AS NVARCHAR(10)) + ' / ' + CAST([Attempts] AS NVARCHAR(10)) AS Proportion " +
" FROM ( " +
"SELECT generate_query AS Query_Name, " +
" sum(case when RecNum is NOT NULL then 1 else 0 end) AS Attempts, " +
" sum(case when export_TIR = 'Yes' then 1 else 0 end) AS Successes, " +
" sum(case when export_TIR = 'No' then 1 else 0 end) AS Failures " +
" FROM tbl_TPF " +
" WHERE export_type = 'Excel' " +
" GROUP BY generate_query " +
" ) Q ";
How can I get the SuccessProportion to show as a decimal with 2 places after the decimal? - is it possible?
" CAST([Successes] AS NVARCHAR(10)) + ' / ' + CAST([Attempts] AS NVARCHAR(10)) AS Proportion
That line is setting it to a varchar, which is the 1/2 format you are seeing.
" CAST([Successes] AS numeric(8,2)) / CAST([Attempts] AS numeric(8,2)) AS Proportion
that should give a numeric value as a success/attempts. There is no 0 handling (attempts = 0 means a div by 0 error), but I suspect from your query, that won't happen
edit - one final cast to get to the final format you want:
" cast(CAST([Successes] AS numeric(8,2)) / CAST([Attempts] AS numeric(8,2)) as numeric(8,2))"