473,416 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Left join with conditions

Hi.

I have three tables KUNDR, ORDRA, ARTIK.
I want to have sum of orders, grouped by costumers (KUNDR), and showing all costumers even if no orders that period.
My script here works, but its gave me all artikels (artik.grupp) if when I try to set criteria on artik
(select grupp, nr from artik where artik.grupp = 5)

How do I solve this so I can get all costumers every time even if I have critera on artik.grupp?

Thanks in advance /pellepels@gmail.com

Expand|Select|Wrap|Line Numbers
  1. SELECT KUNDR.NR AS Nr, SUM(ORDRA.TOT_LEV_ANTAL) AS Summa
  2. FROM KUNDR LEFT JOIN ORDRA ON KUNDR.NR = ORDRA.KUND_NR AND ORDRA.LEV_DATUM >= '2010-09-13' AND ORDRA.LEV_DATUM <= '2010-09-17' 
  3. LEFT  JOIN (select grupp, nr from artik where artik.grupp = 5) artikA ON artikA.NR = ORDRA.ART_NR
  4. WHERE KUNDR.NR IN(SELECT NR FROM KUNDR WHERE SPARR_KOD = 'N') 
  5. GROUP BY KUNDR.NR
  6.  
Sep 30 '10 #1
2 1463
:-)
I solved it, by trial end error, searching google, and subqueries.

Maybe not best solution but it works.
Pelle Pels

Expand|Select|Wrap|Line Numbers
  1. SELECT KUNDR.NR, sum(subQ.summa) FROM KUNDR
  2. LEFT JOIN (select ORDRA.KUND_NR, SUM(ORDRA.TOT_LEV_ANTAL) as summa
  3. from ORDRA LEFT JOIN ARTIK ON ARTIK.NR = ORDRA.ART_NR
  4. WHERE ORDRA.LEV_DATUM >= '2010-08-01' AND ORDRA.LEV_DATUM <= '2010-08-31' AND ARTIK.GRUPP IN ('1','5','6')
  5. group by ORDRA.KUND_NR) subQ on KUNDR.NR = subQ.kund_nr
  6. Where KUNDR.NR IN(SELECT NR FROM KUNDR WHERE SPARR_KOD = 'N') 
  7. group by KUNDR.NR order by nr
  8.  
Sep 30 '10 #2
Better : Removed double sum, no need for that.

Expand|Select|Wrap|Line Numbers
  1. SELECT KUNDR.NR, KUNDR.NAMN, ISNULL(subQ.subSumma,0) as summa FROM KUNDR
  2. LEFT JOIN (
  3. select ORDRA.KUND_NR, SUM(ORDRA.TOT_LEV_ANTAL) as subSumma
  4. from ORDRA LEFT JOIN ARTIK ON ARTIK.NR = ORDRA.ART_NR
  5. WHERE ORDRA.LEV_DATUM >= '$DATE_FROM$' AND ORDRA.LEV_DATUM <= '$DATE_TO$' AND ARTIK.GRUPP IN ($GRUPPNR$)
  6. group by ORDRA.KUND_NR) as subQ on KUNDR.NR = subQ.kund_nr
  7. WHERE KUNDR.NR IN(SELECT NR FROM KUNDR WHERE SPARR_KOD = 'N') 
  8. order by NR
  9.  
Oct 1 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Marek Lewczuk | last post by:
Hello, I have a strange problem, maybe some of you will be able to explain me something. I use LEFT JOIN as a substitute for subselects. It's true that many subselects can be rewriten using LEFT...
0
by: Soefara | last post by:
Dear Sirs, I am experiencing strange results when trying to optimize a LEFT JOIN on 3 tables using MySQL. Given 3 tables A, B, C such as the following: create table A ( uniqueId int not...
1
by: Paul Bramscher | last post by:
Here's one for pathological SQL programmers. I've got a table of things called elements. They're components, sort of like amino acids, which come together to form complex web pages -- as nodes...
2
by: Ryan | last post by:
I'm going daft. I have what should be a simple query and it seems that the left side of the join is being ignored. The query and DDL are below. Basically, my RDOData_Extract_Lines table (where...
1
by: mamapossible | last post by:
Hi, I've spent hours trying to optimize this simple query: SELECT count(sites_jobs.id) as jobCount, sites_jobs_categories.frn_site_id, sites_jobs_categories.id, sites_jobs_categories.name,...
2
by: tricard | last post by:
Good day all, I have a large outer joined query that I want to have some criteria. The select query is gathering all part numbers from tblPartNumbers, left joining to tblPartNumberVendor (since...
2
by: David F | last post by:
Hello, I have what seems like a simple left join query, but Access returns results like it's an inner join. I've tried numerous combinations without success. I have a table (ProjectList)...
1
by: naveenchhibber | last post by:
Hi all pls tell me that the following statment is valid in oracle 9i or 10g.. update ws set received_by_facility = coalesce(rbf_ouk.organizational_unit_id, 0), ...
4
by: markcarroll | last post by:
Right now I have the following SQL query inside of an access database: It works, but it runs VERY slowly. I figure I could speed it up if I could inculde the WHERE conditions as part of of the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.