How can i do this query in laravel - laravel-5

Query details
SELECT userfbimages_id, count( * ) FROM votes GROUP BY userfbimages_id
HAVING count( * ) >= ALL (SELECT count( * ) FROM votes GROUP BY userfbimages_id)

DB::statement('SELECT userfbimages_id, count() FROM votes
GROUP BY userfbimages_id
HAVING count() >= ALL (SELECT count(*) FROM votes GROUP BY userfbimages_id)');

Related

how to write this sql query to codeigniter query

SELECT 1 + (SELECT count( * ) FROM student_rank a WHERE a.obtained_total_mark > b.obtained_total_mark ) AS rank FROM student_rank b WHERE student_id = '5' ORDER BY rank LIMIT 1 ;
convert search query
use
$this->db->query("SELECT 1 + (SELECT count( * ) FROM student_rank a WHERE a.obtained_total_mark > b.obtained_total_mark ) AS rank FROM student_rank b WHERE student_id = '5' ORDER BY rank LIMIT 1")

How to get count by using UNION operator

i'm trying to get total count by using UNION operator but it gives wrong count.
select count(*) as companyRatings from (
select count(*) hrs from (
select distinct hrs from companyA
)
union
select count(*) financehrs from (
select distinct finance_hrs from companyB
)
union
select count(*) hrids from (
select regexp_substr(hr_id,'[^/]+',1,3) hrid from companyZ
)
union
select count(*) cities from (
select regexp_substr(city,'[^/]+',1,3) city from companyY
)
);
individual query's working fine but total count not matching.
individual results here: 12 19 3 6
present total count: 31
Actual total count:40.
so there is any alternate solution without UNION operator?
To add values you'd use +. UNION is to add data sets.
select
(select count(distinct hrs) from companyA)
+
(select count(distinct finance_hrs) from companyB)
+
(select count(regexp_substr(hr_id,'[^/]+',1,3)) from companyZ)
+
(select count(regexp_substr(city,'[^/]+',1,3)) from companyY)
as total
from dual;
But I agree with juergen d; you should not have separate tables per company in the first place.
Edit. Updated query using Sum
select sum(cnt) as companyRatings from
(
select count(*) as cnt from (select distinct hrs from companyA)
union all
select count(*) as cnt from (select distinct finance_hrs from companyB)
union all
select count(*) as cnt from (select regexp_substr(hr_id,'[^/]+',1,3) hrid from companyZ)
union all
select count(*) as cnt from (select regexp_substr(city,'[^/]+',1,3) city from companyY)
)
Previous answer:
Try this
SELECT (
SELECT count(*) hrs
FROM (
SELECT DISTINCT hrs
FROM companyA
)
)
+
(
SELECT count(*) financehrs
FROM (
SELECT DISTINCT finance_hrs
FROM companyB
)
)
+
(
SELECT count(*) hrids
FROM (
SELECT regexp_substr(hr_id, '[^/]+', 1, 3) hrid
FROM companyZ
)
)
+
(
SELECT count(*) cities
FROM (
SELECT regexp_substr(city, '[^/]+', 1, 3) city
FROM companyY
)
)
AS total_count
FROM dual

Get first three rows : with clause

I have the following Query:
with TEMPRESULT AS
(
select CONTACTS.line_id , count(*) totalcount
from CONTACTS
where (((E_DATE-S_DATE)*24*60*60)<=60 and to_char(S_DATE,'YYYY-MM-DD')='2015-12-12')
group by CONTACTS.line_id
order by totalcount DESC
)
I want TEMPRESULT holds only the first three rows returned by select, how to do that ?
You can specify a condition on rownum:
with TEMPRESULT AS
(
select * from
(
select CONTACTS.line_id , count(*) totalcount
from CONTACTS
where (((E_DATE-S_DATE)*24*60*60)<=60 and to_char(S_DATE,'YYYY-MM-DD')='2015-12-12')
group by CONTACTS.line_id
order by totalcount DESC
) x
where rownum < 3
)
if you have oracle 12c you can use the fetch first
WITH TEMPRESULT AS
(SELECT CONTACTS.line_id ,
COUNT(*) totalcount
FROM CONTACTS
WHERE (((E_DATE-S_DATE)*24*60*60)<=60
AND TO_CHAR(S_DATE,'YYYY-MM-DD') ='2015-12-12')
GROUP BY CONTACTS.line_id
ORDER BY 2 DESC
FETCH FIRST 3 rows only
)
SELECT * FROM tempresult

Oracle Quarter SQL Query with top three products

I am working on a small project in Oracle. I need to get the top three best-selling products and the total amounts taken on these for the year and for each of the four quarters April-June, July-September, October-December and January-March. I have already found the first part, I simply need help getting the 4 quarter totals for each product. Hope someone can help, thank you
This is the SQL commands used so far:
select * from (
select "FACTQUANTITY"."PRODUCTID" as "PRODUCTID",
"DIMPRODUCT"."PRODUCTNAME" as "PRODUCTNAME",
sum(FACTQUANTITY.QUANTITY) as "QUANTITY"
from "FACTQUANTITY" "FACTQUANTITY",
"DIMPRODUCT" "DIMPRODUCT"
where "DIMPRODUCT"."PRODUCTID"="FACTQUANTITY"."PRODUCTID"
group by FACTQUANTITY.PRODUCTID,
DIMPRODUCT.PRODUCTNAME
order by sum(FACTQUANTITY.QUANTITY) desc
)
WHERE ROWNUM <= 3;
You can start from here:
select
trunc(fact_table.date_column,'Q') as quarter,
"FACTQUANTITY"."PRODUCTID" ,
"DIMPRODUCT"."PRODUCTNAME",
sum(FACTQUANTITY.QUANTITY) as "QUANTITY"
from "FACTQUANTITY" JOIN "DIMPRODUCT"
ON "DIMPRODUCT"."PRODUCTID"="FACTQUANTITY"."PRODUCTID"
group by
FACTQUANTITY.PRODUCTID, DIMPRODUCT.PRODUCTNAME, trunc(fact_table.date_column,'Q')
;
Subsequently, you can:
with a as (<previous query>)
select *
from (
select
quarter,
productid,
productname,
quantity,
row_number() over (partition by quarter order by quantity desc) rnk
from a
)
where rnk <= 3;
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER ( PARTITION BY quarter
ORDER BY quantity DESC, PRODUCTNAME ASC ) AS rn
FROM (
SELECT TRUNC( f.datetime, 'Q' ) AS quarter,
f.PRODUCTID,
d.PRODUCTNAME,
sum(f.QUANTITY) AS QUANTITY
FROM FACTQUANTITY f
INNER JOIN
DIMPRODUCT d
ON ( d.PRODUCTID = f.PRODUCTID )
GROUP BY TRUNC( f.datetime, 'Q' ),
FACTQUANTITY.PRODUCTID,
DIMPRODUCT.PRODUCTNAME
)
)
WHERE rn <= 3;

Oracle query with rownum and orderBy return wrong records

I got an confusing result when working with rownum of Oracle10g.
first query
select * from A where name like '%test' order by name asc
==> return 1 record
second query with rownum
select * from (
select * from A where name like '%test'
order by name asc
)
where rownum <= 2
==> return 2 records
If I remove 'order by' then it will return 1 record.
Any one can help me to explain this behavior?
maybe you wanted this functionality:
select * from (
select X.*, rownum r from (
select * from A where name like '%test'
order by name asc
) X
)
where r <= 2
or alternatively
select * from (
select A.*, ROW_NUMBER() OVER (ORDER BY name) r
from A where name like '%test'
)
where r <= 2

Resources