Sqlite SUM sub query issue - sqlite

I am trying this query for my android app, this works fine with mysql but doesnt give proper results with sqlite
SELECT cust.Name,cust.ID,billNo,billTime,
(SELECT SUM(b.qty*b.Price*(SELECT (1+(s.tax/100))FROM tbl_stock s
WHERE b.itemCode=s.itemCode))
FROM tbl_billitems b WHERE a.billNo=b.billNo AND a.terminal=b.terminal)AS total ,
(SELECT COUNT(*) FROM tbl_billitems c WHERE a.billNo=c.billNo
AND a.terminal=c.terminal),terminal FROM tbl_bills a, tbl_customers cust
WHERE a.ID=cust.ID AND a.billNo IN (SELECT billNo from tbl_billitems)
it gives correct value with mysql but in sqlite is only giving the sum of (b.qty*b.Price)
Please guide me. thanks in advance

This is my app code..hope u have got some idea..
SELECT t.*, ((summeJ-summeVJ)/summeVJ*100) AS variance
FROM (SELECT k.ZKUNDENNR, k.ZNAME1,
(SELECT SUM(vk.ZNETTO) FROM ZPBSROW r LEFT JOIN ZWARENGRUPPEVK vk ON vk.ZPBSROW=r.Z_PK WHERE r.ZKUNDE=k.Z_PK AND ZJAHR=2013 AND ZMONAT>=1 AND ZMONAT<=6) AS summeJ,
(SELECT SUM(vk.ZNETTO) FROM ZPBSROW r LEFT JOIN ZWARENGRUPPEVK vk ON vk.ZPBSROW=r.Z_PK WHERE r.ZKUNDE=k.Z_PK AND ZJAHR=2012 AND ZMONAT>=1 AND ZMONAT<=6) as summeVJ,
(SELECT SUM(vk.ZDB_BASIS) FROM ZPBSROW r LEFT JOIN ZWARENGRUPPEVK vk ON vk.ZPBSROW=r.Z_PK WHERE r.ZKUNDE=k.Z_PK AND ZJAHR=2012 AND ZMONAT>=1 AND ZMONAT<=6) as summeDBVJ,
(SELECT SUM(vk.ZDB_BASIS) FROM ZPBSROW r LEFT JOIN ZWARENGRUPPEVK vk ON vk.ZPBSROW=r.Z_PK WHERE r.ZKUNDE=k.Z_PK AND ZJAHR=2013 AND ZMONAT>=1 AND ZMONAT<=6) as summeDBJ
FROM ZKUNDE k
) t
WHERE summeJ>0
ORDER BY summeJ DESC
LIMIT 0,10

Related

mariadb alternative to outer apply or lateral?

What I wanted was to use CROSS APPLY, but I guess that doesn't exist in mysql. The alternative I've read is LATERAL. Well, I'm using mariadb 10.3 and I guess that doesn't exist either. The ticket table contains an id that's referenced by the ticket_id column in the note table. A ticket can have many notes, I'm trying to list all tickets with their most recent note date (post_date). How could I write the query below for mariadb?
SELECT t.*, n.post_date
FROM ticket t,
LATERAL (
SELECT note.post_date FROM note WHERE t.id = note.ticket_id ORDER BY note.post_date DESC LIMIT 1
) n;
Example table structure:
Ticket
id
subject
1
stuff
2
more
note
id
post_date
ticket_id
1
1
2
1
3
2
4
1
5
2
I did find an open jira ticket from people asking for mariadb to support lateral.
From what I read, LATERAL will not be supported in MariaDB until version 11. But we can just as easily use ROW_NUMBER here, which is supported:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY ticket_id ORDER BY post_date DESC) rn
FROM note
)
SELECT t.*, n.post_date
FROM ticket t
INNER JOIN cte n
ON n.ticket_id = t.id
WHERE n.rn = 1;
If you wanted a close translation of your current lateral join, then use:
SELECT t.*,
(SELECT n.post_date
FROM note n
WHERE t.id = note.ticket_id
ORDER BY n.post_date DESC
LIMIT 1)
FROM ticket t;

Error in using WITH clause and INTERSECT in SQLite

I have two SQL queries;
First one is:
with b as (select person_id from people where name='Ward Bond' and born=1903)
select title_id from b natural join crew;
Which is producing correct results and is OK.
Another one is:
with c as (select person_id from people where name='John Wayne' and born=1907)
select title_id from c natural join crew;
Which is also totally OK and producing correct results. As soon as I try to find the intersection of these two queries using the following query:
with b as (select person_id from people where name='Ward Bond' and born=1903) select title_id from b natural join crew
intersect
with c as (select person_id from people where name='John Wayne' and born=1907) select title_id from c natural join crew;
I get the error Error: near "with": syntax error
I'm using SQLite3. Can you please help me to find the problem? The thing I'm trying to get is straightforward; I want to have the intersection of these two temporary tables.
This is the correct syntax for SQLite:
select * from (
with b as (
select person_id
from people
where name='Ward Bond' and born=1903
)
select title_id from b natural join crew
)
intersect
select * from (
with c as (
select person_id
from people
where name='John Wayne' and born=1907
)
select title_id from c natural join crew
);
Another way to get the intersected rows:
with cte(name, born) as (values ('Ward Bond', 1903), ('John Wayne', 1907))
select c.title_id
from crew c natural join people p
where (p.name, p.born) in cte
group by c.title_id
having count(distinct p.person_id) = 2;
That's how I did it using view:
create view a as select person_id from people where name='Ward Bond' and born=1903;
create view b as select person_id from people where name='John Wayne' and born=1907;
with c as
(select title_id from a natural join crew
intersect
select title_id from b natural join crew)
select primary_title from c natural join titles;

Teradata 16 Issues

We recently upgraded our Teradata version to 16 and since then we are facing few spool space issues. Before the query was consuming less then 200GB and now it consumes more than 1.5TB. We then started optimizing the query and we modified a part of the query
LEFT JOIN (SELECT A.XX, C.YY FROM TABLE1 A
INNER JOIN TABLE2 B ON A.DD = B.EE
INNER JOIN TABLE3 C ON C.FF = B.GG
GROUP BY 1,2)
to
LEFT JOIN (SELECT A.XX, C.YY FROM TABLE1 A
INNER JOIN TABLE2 B ON A.DD = B.EE
INNER JOIN (SELECT FF, YY FROM TABLE3 GROUP BY 1,2) C
ON C.FF = B.GG
GROUP BY 1,2)
After modification the query runs within 200GB. Could someone please explain as why we face such issues after upgrade? Thanks in advance.

Merge 2 Tables Data in SQL

I have 3 Data Table Claim, Part and Labor.
In this Claim is parent table and Part and Labor is mapping tables of Claim and they have Part and Labor has the ClaimId as a Foreign Key.
Claim has data like:
Part has data Like
Labor table has data Like
Target Output would be:
Can anyone help me to achieve this in SQL server.
I have tried to solve with the Union/CTE but it did not gives the result as I want.
I got the same output (for your updated output screen) for this specific case. I don't know if any other data will work for you.
SELECT TMP.ClaimId
, CASE WHEN TMP.RowNum = 1 THEN TMP.Name ELSE NULL END AS ClaimName
, CASE WHEN TMP.RowNum = 1 THEN TMP.Note ELSE NULL END AS Note
, TMP.PartId
, TMP.PartNumber
, TMP.PartCost
, JOIN_L.LaborId
, JOIN_L.LaborCost
FROM (
SELECT C.ClaimId, C.Name, C.Note, P.PartId, P.PartNumber, P.PartCost
, ROW_NUMBER() OVER(PARTITION BY C.ClaimId ORDER BY P.PartId) AS RowNum
FROM Claim AS C
LEFT JOIN Part AS P ON C.ClaimId = P.ClaimId
)AS TMP
LEFT JOIN (
SELECT *
, ROW_NUMBER() OVER(PARTITION BY L.ClaimId ORDER BY L.ClaimId) AS RowNum
FROM Labor AS L
) AS JOIN_L ON (TMP.ClaimId = JOIN_L.ClaimId AND TMP.RowNum = JOIN_L.RowNum)
ORDER BY TMP.ClaimId
Not sure why you tried CTE here
Select C.ClaimId,C.name,C.Note,P.PartId,P.PartNumber,P.PartCost,L.LabourId,L.LabourCost
From Claim C
Left Outer Join Part P On P.ClaimId = C.ClaimId
Left Outer Join Labor L On L.ClaimId=C.ClaimId

Inner Join & Count, Microsoft SQL

I am trying to do a count inside a nested statement with inner join
select a.app_id, a.first_name, a.last_name, d.svd_id
from wwhs_app a inner join
wwhs_svc d on a.app_id = d.app_id
where a.app_id in(
select top 50 app_id
from wwhs_app
Where app_create_dt > '2012-07-23 00:00:00')
I need a count of svd_id as well, but I keep getting errors every way I try. Suggestions?
You need to count for svd_id but it's not in the query.
Did you mean 'app_id'?
Try this...
SELECT a.app_id, a.first_name, a.last_name, d.svd_id
FROM wwhs_app a
INNER JOIN wwhs_svc d on a.app_id = d.app_id
WHERE a.app_id in (
SELECT TOP 50 app_id, COUNT(*) as id_count
FROM wwhs_app
WHERE app_create_dt > '2012-07-23 00:00:00'
GROUP BY app_id ORDER BY id_count)

Resources