I have two tables
table1
c1t1 c2t1
1 saanu
3 abc
table2
c1t2 c2t2
2 val2
4 val4
I have to find out the values of c2t1 and c2t2 for the minimum and maximum value of c1t1 and c1t2 with one line command.
For the above example I have to find saanu and val4
I had a very similar problem and solved it with UNION ALL. The minimum of the aColumn column in tables aTable1, ... , aTableN can be computed as:
SELECT Min(aColumn)
FROM (
SELECT aColumn FROM aTable1 UNION ALL
SELECT aColumn FROM aTable2 UNION ALL
...
SELECT aColumn FROM aTableN) t;
You should be able to do Min in each of the inner selects, but I haven't found out how to do that!
One approach:
select max(case c1 when min1 then c2 end) c2_first,
max(case c1 when max1 then c2 end) c2_last
from (select c1t1 c1, c2t1 c2 from table1
union all
select c1t2 c1, c2t2 c2 from table2) u
cross join
(select min(min11, min12) min1, max(max11, max12) max1 from
(select min(c1t1) min11, max(c1t1) max11 from table1) t1
cross join
(select min(c1t2) min12, max(c1t2) max12 from table2) t2) m
SQLFiddle here.
1)
SELECT c2t1
FROM table1
ORDER BY c1t1 ASC LIMIT 1
2)
SELECT c2t2
FROM talbe2
ORDER BY c1t2 DESC LIMIT 1
Related
I have 4 offices in the table OFFICE_DETAILS which are a,b,c,d.
The below query outputs offices which are having any lab and rad count, so it is including all offices except for c
office_name lab_count rad_count
a 5 0
b 1 2
d 3 1
I want the output to be as follows:
office_name lab_count rad_count
a 5 0
b 1 2
c 0 0
d 3 1
what is the change required in the following code
SELECT d.OFFICE_name AS "OFFICE_NAME" ,
count(CASE
WHEN c.LAB_TYPE LIKE 'L' THEN 1
END) AS "LAB_TEST_COUNT",
count(CASE
WHEN c.LAB_TYPE in ('X','O') THEN 1
END) AS "RAD_TEST_COUNT"
FROM
DOCTOR_CONSULT a
INNER JOIN consult_labtest b
on(a.CONSULT_ID=b.CONSULT_ID)
INNER JOIN test_setup c
on(b.LABTEST_ID=c.TEST_ID)
INNER JOIN OFFICE_DETAILS d
on(a.OFFICE_ID=D.OFFICE_ID)
INNER JOIN USER_SETUP e
on(a.DOCTORS_ID = e.USER_ID)
INNER JOIN DEPARTMENT_SETUP f
ON(a.DEPARTMENT_ID = f.DEPARTMENT_ID)
INNER JOIN TEST_CATEGORY g
ON (c.CATEGORY_ID=g.CATEGORY_ID)
WHERE
c.LAB_TYPE IN ('L','X','O') --'L'-> Laboratory, 'X'-> Radiology
AND c.ACTIVE_STATUS='Y'
AND d.ACTIVE_STATUS='Y'
AND g.ACTIVE_STATUS='Y'
AND
a.CONSULT_DATE BETWEEN CURRENT_DATE AND CURRENT_DATE
AND
d.ACTIVE_STATUS='Y'
AND
a.EMPLOYEE_ID NOT IN ('NEW RECRUITMENT 380', '0000', 'army', 'undefined')
GROUP BY d.OFFICE_NAME
ORDER BY d.OFFICE_NAME ASC;
To retrieve result rows from joined tables that do not have matching rows (e.g. no matching entries in OFFICE_DETAILS table) SQL provides OUTER JOINS.
Join the details tables as outer joins and handle the resulting NULLs in the projection.
This is the most common approach to address this requirement.
Alternatively, the details tables could contain a special “no match” record that is used to match in an OR branch of the join condition. Some data warehouses do this to avoid NULLs.
I did by using LEFT JOIN. I am getting the required output
CREATE VIEW ECLINIC_KNG.VIEW_LABRADTESTS_OFFICE_COUNT_TODAY AS
SELECT ROW_NUMBER() OVER() AS row_num,
v1.OFFICE_NAME,
v1.LAB_TEST_COUNT,
v1.RAD_TEST_COUNT
FROM
(
SELECT od.OFFICE_NAME as "OFFICE_NAME",
(CASE WHEN v.LAB_TEST_COUNT IS NULL THEN 0 ELSE v.LAB_TEST_COUNT END) AS
"LAB_TEST_COUNT",
(CASE WHEN v.RAD_TEST_COUNT IS NULL THEN 0 ELSE v.RAD_TEST_COUNT END) AS
"RAD_TEST_COUNT"
FROM
OFFICE_DETAILS od
LEFT JOIN
(
SELECT d.OFFICE_NAME ,
count(CASE WHEN c.LAB_TYPE LIKE 'L' THEN 1 END) AS "LAB_TEST_COUNT",
count(CASE WHEN c.LAB_TYPE in ('X','O') THEN 1 END) AS "RAD_TEST_COUNT"
FROM
ECLINIC_KNG.DOCTOR_CONSULT a
INNER JOIN ECLINIC_KNG.consult_labtest b
on(a.CONSULT_ID=b.CONSULT_ID)
INNER JOIN ECLINIC_KNG.test_setup c
on(b.LABTEST_ID=c.TEST_ID)
INNER JOIN ECLINIC_KNG.OFFICE_DETAILS d
on(a.OFFICE_ID=D.OFFICE_ID)
INNER JOIN ECLINIC_KNG.USER_SETUP e
on(a.DOCTORS_ID = e.USER_ID)
INNER JOIN ECLINIC_KNG.DEPARTMENT_SETUP f
ON(a.DEPARTMENT_ID = f.DEPARTMENT_ID)
INNER JOIN ECLINIC_KNG.TEST_CATEGORY g
ON (c.CATEGORY_ID=g.CATEGORY_ID)
WHERE
c.LAB_TYPE IN ('L','X','O') --'L'-> Laboratory, 'X'-> Radiology
AND c.ACTIVE_STATUS='Y'
AND d.ACTIVE_STATUS='Y'
AND g.ACTIVE_STATUS='Y'
AND a.CONSULT_DATE BETWEEN CURRENT_DATE AND CURRENT_DATE
AND d.ACTIVE_STATUS='Y'
AND a.EMPLOYEE_ID NOT IN ('NEW RECRUITMENT 380', '0000', 'army', 'undefined')
GROUP BY d.OFFICE_NAME
ORDER BY d.OFFICE_NAME ASC
)AS v
on(od.OFFICE_NAME=v.OFFICE_NAME)
WHERE od.ACTIVE_STATUS='Y'
ORDER BY od.OFFICE_NAME
)AS v1
Table three columns id, numers1 and numbers2. We need to summarize numers1 and numbers2 but the first row to the second row numers1 numers2 the second with the third and forth etc.:
CREATE TABLE tb1 (id INTEGER PRIMARY KEY AUTOINCREMENT,numbers1,numbers2);
INSERT INTO tb1 (numbers1,numbers2) values(1,10);
INSERT INTO tb1 (numbers1,numbers2) values(2,20);
INSERT INTO tb1 (numbers1,numbers2) values(3,30);
INSERT INTO tb1 (numbers1,numbers2) values(4,40);
INSERT INTO tb1 (numbers1,numbers2) values(5,50);
I want to get as:
21
32
43
54
with the reference of getting the correct row index per record here:
How to use ROW_NUMBER in sqlite
I was able to create the required result with the following query:
SELECT
num1 + coalesce(b_num2, 0)
FROM(
SELECT
num1,
(select count(*) from test as b where a.id >= b.id) as cnt
FROM test as a) as a
LEFT JOIN
(SELECT num2 as b_num2,
(select count(*) from test as b where a.id >= b.id) as cnt
FROM test as a
) as b
ON b.cnt = a.cnt + 1
Explanation:
by joining two same table of similar record index, then merge the next record with the current record and then sum num1 of current record with num2 of next record, I do not know how you want to deal with the last row as it does not have a next row so I assume it to add nothing to have a result of just the value of num1
Result:
For one row with a specific ID x, you can get values from the next row by searching for ID values larger than x, and taking the first such row:
SELECT ...
FROM tb1
WHERE id > x
ORDER BY id
LIMIT 1;
You can then use this as a correlated subquery to get that value for each row:
SELECT numbers1 + (SELECT T2.numbers2
FROM tb1 AS T2
WHERE T2.id > T1.id
ORDER BY T2.id
LIMIT 1) AS sum
FROM tb1 AS T1
WHERE sum IS NOT NULL; -- this omits the last row, where the subquery returns NULL
I am trying to use union but i get this vague syntax error. I Can get each query to run seperately, but when i try to use union i get this error.
(SELECT s.sname, COUNT(s.sname) AS number
FROM sailor s, boat b
WHERE b.rating <= s.rating
GROUP BY s.sname)
UNION
(SELECT s.sname, 0 AS number
FROM sailor s
WHERE NOT EXISTS(
SELECT * FROM boat b
WHERE s.rating >= b.rating));
In SQL, only subqueries use parentheses. In a compound query, the individual SELECTs must be written without them:
SELECT s.sname, COUNT(s.sname) AS number
FROM sailor s, boat b
WHERE b.rating <= s.rating
GROUP BY s.sname
UNION
SELECT s.sname, 0 AS number
FROM sailor s
WHERE NOT EXISTS(
SELECT * FROM boat b
WHERE s.rating >= b.rating);
And in this specific case, you might just want to use an outer join:
SELECT s.sname,
IFNULL(COUNT(b.rating), 0) AS number
FROM sailor AS s
LEFT JOIN boat AS b
ON b.rating <= s.rating;
I want to do the following in LINQ to SQL:
Select count(*) as count_1,
(select count(*) from tableName2) as count_2 FROM tableName
Where x = y
The result should be
Column 1 | column 2
--------------------
50 34
What you need to do is something like this:
select
(select count(*)
from tableName
where x = y) as count_1,
(select count(*)
from tableName2) as count_2
I have a query in SQLite where I group by a certain column and use an aggregate function MAX on another column in the select statement. Now I also want the rowid of the row which holds the value that is displayed by the MAX aggregate. I know that this must be a unique row because of the primary key constraint. I can't figure out how to write the query. See the following example:
create table t1 (c1, c2, constraint t1_pk primary key (c1, c2));
insert into t1 values ('boys', 1);
insert into t1 values ('boys', 2);
insert into t1 values ('girls', 1);
insert into t1 values ('girls', 2);
Now I have the table with the primary constraint over both columns. A SELECT query for the table gives the following output:
sqlite> select rowid, * from t1;
rowid|c1|c2
1|boys|1
2|boys|2
3|girls|1
4|girls|2
Now I want to group by c1 and select the MAX of c2. Then I want the rowid of the row which holds the values displayed now. See the following queries:
sqlite> select rowid, c1, max(c2) from t1 group by c1;
rowid|c1|max(c2)
2|boys|2
4|girls|2
sqlite> select rowid, c1, min(c2) from t1 group by c1;
rowid|c1|min(c2)
2|boys|1
4|girls|1
The second query with the MIN aggregate should return the rowids of the rows holding the MIN values, this is what I want to achieve:
rowid|c1|min(c2)
1|boys|1
3|girls|1
Now I've tried the following subselect, which doesn't work either because it gives an error:
sqlite> select (select rowid from t1 b where b.c1 = a.c1 and b.c2 = max(a.c2)), a.c1, max(a.c2) from t1 a group by a.c1;
Error: misuse of aggregate function max()
sqlite> select (select rowid from t1 b where b.c1 = a.c1 and b.c2 = min(a.c2)), a.c1, min(a.c2) from t1 a group by a.c1;
Error: misuse of aggregate function min()
The last thing I've tried is a subquery in the FROM clause, which also doesn't work:
sqlite> select
...> (select rowid from t1 b where b.c1 = c.c1 and b.c2 = c.c2),
...> c1,
...> c2
...> from
...> (select a.c1, max(a.c2) as c2 from t1 a group by a.c1) c;
Error: misuse of aggregate: max()
sqlite> select
...> (select rowid from t1 b where b.c1 = c.c1 and b.c2 = max(c.c2)),
...> c.c1,
...> max(c.c2)
...> from
...> (select a.c1, a.c2 from t1 a group by a.c1) c;
Error: misuse of aggregate function max()
Is there any solution for my problem? I really don't know what else I could try.
If I understood your question correctly, try like this:
select rowid, c1, min(c2) from t1 a
where c2=(select min(c2) from t1 b where b.c1=a.c1)
group by rowid,c1;
check the FIDDLE
Indeed, following the answer by #Pradeeshnarayan, I have been obliged to improve it to make it work in Oracle.
"Group by" clause is useless
select rowid, c1, c2 from t1 a
where c2=(select min(c2) from t1 b where b.c1=a.c1);