I am working on a database of a large retail store.
I have to query data from multiple tables to get numbers such as revenue, raw proceeds and compare different time periods.
Most of it is quite easy but I was struggling to work out a way of joining multiple CTEs.
I made a fiddle so you know what I am talking about.
I simplified the structure a lot and left out quite a few columns in the subqueries because they do not matter in this case.
As you can see every row in every table has country and brand in it.
The final query has to be grouped by those.
What I first tried was to FULL JOIN all the tables, but that didn't work in some cases as you can see here: SQLfiddle #1. Note the two last rows which did not group correctly.
Select Coalesce(incoming.country, revenue.country, revcompare.country,
openord.country) As country,
Coalesce(incoming.brand, revenue.brand, revcompare.brand,
openord.brand) As brand,
incoming.OrdersNet,
openord.OpenOrdersNet,
revenue.Revenue,
revenue.RawProceeds,
revcompare.RevenueCompare,
revcompare.RawProceedsCompare
From incoming
Full Join openord On openord.country = incoming.country And
openord.brand = incoming.brand
Full Join revenue On revenue.country = incoming.country And
revenue.brand = incoming.brand
Full Join revcompare On revcompare.country = incoming.country And
revcompare.brand = incoming.brand
Group By incoming.OrdersNet,
openord.OpenOrdersNet,
revenue.Revenue,
revenue.RawProceeds,
revcompare.RevenueCompare,
revcompare.RawProceedsCompare,
incoming.country,
revenue.country,
openord.country,
revcompare.country,
incoming.brand,
revenue.brand,
revcompare.brand,
openord.brand
Order By country,
brand
I then rewrote the query keeping all the CTEs. I added another CTE (basis) which UNIONs all the possible country and brand combinations and left joined on that one.
Now it works fine (check it out here -> SQLfiddle #2) but it just seems so complicated. Isn't there an easier way to achieve this? The only thing I probably won't be able to change are the CTEs as in real life they are way more complex.
WITH basis AS (
SELECT Country, Brand FROM incoming
UNION
SELECT Country, Brand FROM openord
UNION
SELECT Country, Brand FROM revenue
UNION
SELECT Country, Brand FROM revcompare
)
SELECT
basis.Country,
basis.Brand,
incoming.OrdersNet,
openord.OpenOrdersNet,
revenue.Revenue,
revenue.RawProceeds,
revcompare.RevenueCompare,
revcompare.RawProceedsCompare
FROM basis
LEFT JOIN incoming On incoming.Country = basis.Country AND incoming.Brand = basis.Brand
LEFT JOIN openord On openord.Country = basis.Country AND openord.Brand = basis.Brand
LEFT JOIN revenue On revenue.Country = basis.Country AND revenue.Brand = basis.Brand
LEFT JOIN revcompare On revcompare.Country = basis.Country AND revcompare.Brand = basis.Brand
Thank you all for your help!
Since you only work with two tables, orders and rev, consider conditional aggregation by moving WHERE conditions to CASE logic for single aggregate query. Also, consider only one CTE for all possible country/brand pairs for LEFT JOIN on the two tables.
WITH cb AS (
SELECT Country, Brand FROM orders
UNION
SELECT Country, Brand FROM rev
)
SELECT cb.Country
, cb.Brand
, SUM(o.netprice) AS OrdersNet
, SUM(CASE
WHEN o.isopen = 1
THEN o.netprice
END) AS OpenOrdersNet
, SUM(CASE
WHEN r.bdate BETWEEN '2020-12-01' AND '2020-12-31'
THEN r.netprice
END) AS Revenue
, SUM(CASE
WHEN r.bdate BETWEEN '2020-12-01' AND '2020-12-31'
THEN r.rpro
END) AS RawProceeds
, SUM(CASE
WHEN r.bdate BETWEEN '2020-11-01' AND '2020-11-30'
THEN r.netprice
END) AS RevenueCompare
, SUM(CASE
WHEN r.bdate BETWEEN '2020-11-01' AND '2020-11-30'
THEN r.rpro
END) AS RawProceedsCompare
FROM cb
LEFT JOIN orders o
ON cb.Country = o.Country
AND cb.Brand = o.Brand
LEFT JOIN rev r
ON cb.Country = r.Country
AND cb.Brand = r.Brand
GROUP BY cb.Country
, cb.Brand
SQL Fiddle
Related
Here's my query:
select
cast(ar.AudienceCreationDate as date) as AudienceDate,
Count(*) as [Count],
count(case when ar.Source = 'Contact' then ar.Id end) as PatientCount,
count(case when ar.Source = 'PatientContact' then ar.Id end) as PatientContactCount,
(
select
count(*)
from
_SMSMessageTracking sms
inner join
[CTT Preferences] pref on pref.ContactId = sms.SubscriberKey
where
sms.Name <> 'ky_ctt_join' and pref.Source = 'Patient'
) as PatientSMS,
(
select
count(*)
from
_SMSMessageTracking sms
inner join
[CTT Preferences] pref on pref.ContactId = sms.SubscriberKey
where
sms.Name <> 'ky_ctt_join' and pref.Source = 'PatientContact'
) as PatientContactSMS
from
Daily_Symptom_Check_Audience_Archive ar
group by
cast(ar.AudienceCreationDate as date)
And here's the result set it creates:
The issue I'm having is that the values in the rightmost two columns are the same across the board, for all records. This number represents the TOTAL, and not aggregated by day, as the other values indicate. I realize that I'm doing something wrong - what can I do to modify my query to effectively have a proper "grouping" on these last two columns just like all the other data in this table?
in the where clause of the nested query add sms.AudienceDate = ar.AudienceDate
Cound you please support with question below, i trying to fill a new colmn in my SQL query based in a conditional value.
pseudo code:
IF ap_id exists in visit_table and visit_type = 'first'
then firs_tvisit_id = visit_table.visit_id and first_visit_user = visit_table.Username
The same logic for second and third visit.
This are visit_table.
And this are ap_table (where are the key values):
And below is the expected table.
SQL is able to make these kind of data manipulation, if there are how can i get this.
I alrady tryed left joins, inner joins, and full outher join. however i were not able to create a new column and fill this based in a conditional.
One method is multiple joins:
select a.*,
v1.visit_id, v1.user_name,
v2.visit_id, v2.user_name,
v3.visit_id, v3.user_name
from ap_table a left join
visit_table v1
on v1.ap_id = a.ap_id and v1.type = 'first' left join
visit_table v2
on v2.ap_id = a.ap_id and v2.type = 'second' left join
visit_table v3
on v3.ap_id = a.ap_id and v3.type = 'third'
You can join and do conditional aggregation:
select
a.ap_id,
a.location,
max(case when visit_type = 'first' then visit_id end) first_visit_id,
max(case when visit_type = 'second' then visit_id end) second_visit_id,
...
max(case when visit_type = 'first' then username end) first_visit_username,
max(case when visit_type = 'second' then username end) second_visit_username,
...
from ap_table a
inner join visit_table v on v.ap_id = a.ap_id
group by a.ap_id, a.location
It looks like you have some potential data issues here that must be answered for a proper solution. Most specifically, what should happen if the visit_table has multiple records for the same ap_id and visit_type? Or should we assume that there is a unique key on those two columns?
The best answer to this question as posted is actually likely to be dependent on the flavor of SQL you are using and the nuance of what you are really trying to do. For example, PostgreSQL has the ability to "SELECT DISTINCT ON" columns without requiring grouping. Oracle, SQL Server, and others have the ability to simplify complex queries using the with clause. And if other methods fail you can always write nested queries to try and grab everything in one monster query.
If you are able to use a temp table or table variable you may be better off selecting information into a constructed table as needed. If you need more than 3 visits, you may have to write dynamic sql. And if this is information you want to have readily available for other queries to access you might consider writing it as a view instead.
One quick note about simply selecting min(visit_id) and min(username) for your query - if you have multiple grouped rows you are likely to get the id from one visit incorrectly paired with the username from another visit.
Here is an attempt at creating a monster query for just the 3 visit types included:
SELECT a.ap_id,
a.location,
v1.visit_id AS first_visit_id,
v1.username AS first_visit_username,
v2.visit_id AS second_visit_id,
v2.username AS second_visit_username,
v3.visit_id AS third_visit_id,
v3.username AS third_visit_username
FROM (
SELECT ai.ap_id,
(SELECT MIN(v1i.visit_id) FROM visit_table v1i ON v1i.ap_id = ai.ap_id AND v1i.type = 'first') AS v1_id,
(SELECT MIN(v2i.visit_id) FROM visit_table v2i ON v2i.ap_id = ai.ap_id AND v2i.type = 'second') AS v2_id,
(SELECT MIN(v3i.visit_id) FROM visit_table v3i ON v3i.ap_id = ai.ap_id AND v3i.type = 'third') AS v3_id
FROM ap_table ai
) x
JOIN ap_table a ON x.ap_id = a.ap_id
LEFT JOIN visit_table v1 ON x.v1_id = v1.visit_id
LEFT JOIN visit_table v3 ON x.v2_id = v2.visit_id
LEFT JOIN visit_table v3 ON x.v3_id = v3.visit_id
WHERE
x.v1_id IS NOT NULL
OR x.v2_id IS NOT NULL
OR x.v3_id IS NOT NULL
I have a cs cart database and I am trying to select all the attributes for all the products, the problem is that for each separate attribute for a product, my query creates a new row, I want to to have a single row for each products that has all the attributes into columns.
This is my query right now:
SELECT a.product_id, b.variant, c.description, d.product_code
FROM cscart_product_features_values a
LEFT JOIN cscart_product_feature_variant_descriptions b ON a.variant_id = b.variant_id
LEFT JOIN cscart_product_features_descriptions c ON a.feature_id = c.feature_id
LEFT JOIN cscart_products d ON a.product_id = d.product_id
After I run the query, I get the following result:
product_id;"variant";"description";"product_code"
38;"1st";"Grade Level";"750"
38;"Math";"Subject Area";"750"
38;"Evan-Moor";"Publisher";"750"
etc next product
What I want is this:
product_id;"product_code";"Grade Level";"Subject Area";"Publisher"
38;"750";"1st";"Math";"Evan-Moor"
etc next product
We only have 3 type of attributes: Grade Level, Subject Area and Publisher.
Any ideas how to improve my query and achieve this? I would be happy even with concatenating all 3 attributes in one column, delimited by ",".
This is a generic SQL solution using GROUP BY and MAX(case expression) to achieve the transformation of 3 rows into a single row with the 3 columns.
SELECT
v.product_id
, p.product_code
, MAX(CASE WHEN fd.description = 'Grade Level' THEN vd.variant END) AS GradeLevel
, MAX(CASE WHEN fd.description = 'Subject Area' THEN vd.variant END) AS SubjectArea
, MAX(CASE WHEN fd.description = 'Publisher' THEN vd.variant END) AS Publisher
FROM cscart_products p
LEFT JOIN cscart_product_features_values v ON p.product_id = v.product_id
LEFT JOIN cscart_product_feature_variant_descriptions vd ON v.variant_id = vd.variant_id
LEFT JOIN cscart_product_features_descriptions fd ON v.feature_id = fd.feature_id
GROUP BY
v.product_id
, p.product_code
This approach should work on just about any SQL database.
Note also that I have changed the order of tables because I presume there has to be a row in cscart_products, but there might not be related rows in the other tables.
I have also changed the aliases, personally I do not care for aliaes based on the order of use in a query (e.g. I just changed the order so I had to change all references). I have use 'p' = product, 'v' = variant, 'vd' = variant description & 'fd' = feature description' - with such a convention for aliases I can re-arrange the query without changing every reference.
I'm trying to get a count of all speakers who are active regarding that item as well as the total of speakers who correlate to a certain item. The first LEFT JOIN for the total speakers works, but the other for ONLY the active speakers regarding that item doesn't, any help is appreciated. The SQLFiddle is here
http://sqlfiddle.com/#!3/b579d/1
But when I try to add in the portion where you would get the number of active speakers
(LEFT JOIN (SELECT COUNT (tbl_SpeakerCard_Log.SpeakerName)
WHERE tbl_Speaker_Log.Spoken = 0)
ON tbl_AgendaList.AID = tbl_SpeakerCard_Log.AID)
under the previous LEFT JOIN I get an error. I'm 100% sure the query is wrong in some form, but I'm not sure how to approach it.
*NOTE: Spoken/Active are interchangeable, I just use different wording to clarify what I'm looking for.
EDIT: This is the desired output
http://imgur.com/yP1FKxg
You can use conditional aggregation to do this:
SELECT
AgendaList.AID,
AgendaList.Item,
COUNT(SpeakerList.SPID) as SpeakerTotal,
SUM(CASE WHEN SpeakerList.Spoken = 0 THEN 1 ELSE 0 END) as ActiveSpeakers
FROM AgendaList
LEFT JOIN SpeakerLIST ON AgendaList.AID = SpeakerList.AID
GROUP BY AgendaList.AID, AgendaList.Item;
Sample SQL Fiddle
Or you could use count instead of sum (which might be clearer):
COUNT(CASE WHEN Spoken = 0 THEN Spoken END) as ActiveSpeakers
SQL FIDDLE
WITH sTotal AS (
SELECT AgendaList.AID, AgendaList.Item, COUNT( SpeakerList.SPID) as SpeakerTotal
FROM AgendaList
LEFT JOIN SpeakerLIST ON AgendaList.AID = SpeakerList.AID
GROUP BY AgendaList.AID, AgendaList.Item
),
sActive AS (
SELECT AgendaList.AID, AgendaList.Item, COUNT( SpeakerList.SPID) as SpeakerActive
FROM AgendaList
LEFT JOIN SpeakerLIST ON AgendaList.AID = SpeakerList.AID
WHERE SpeakerLIST.Spoken = 0
GROUP BY AgendaList.AID, AgendaList.Item
)
SELECT sTotal.*, sActive.SpeakerActive
FROM sTotal left join
sActive on sTotal.AID = sActive.AID
I have a script when I'm trying to select locations in an inventory where quantity of said location is <= 5. The query is complete, now I'm trying to do some fine tuning, and what I'm running into now is when I use the distinct clause I am still receiving duplicate records in the same column. I do know the column next to the first are unique, but I thought distinguishing distinct and one column would roll over to next related to said column.
Here is my code:
select DISTINCT bin.scannable_id as bin,
bi.bin_id as case1,
pallet.scannable_id as pallet,
-- bi.isbn as fcsku,
nvl(fs.asin,bi.isbn) as asin,
sum(bi.quantity) as quantity,
pallet.creation_date as received_date
from containers bin
join containers pallet on pallet.containing_container_id = bin.container_id
join containers case on case.containing_container_id = pallet.container_id
join bin_items bi on bi.container_id = case.container_id
left join fcskus fs on fs.fcsku = bi.isbn
where bin.scannable_id like 'R-1-T%'
and bi.quantity <= '5'
group by bin.scannable_id, pallet.scannable_id, bi.bin_id, bi.owner,bi.isbn,nvl(fs.asin,bi.isbn), pallet.creation_date
order by sum(bi.quantity);
My output, which is obviously showing duplicate records in the scannable_id column:
Correct Formatting Thanks to conrad.
select DISTINCT bin.scannable_id as bin,
pallet.scannable_id as pallet,
nvl(fs.asin,bi.isbn) as asin,
sum(bi.quantity) as quantity
from containers bin
join containers pallet on pallet.containing_container_id = bin.container_id
join containers case on case.containing_container_id = pallet.container_id
join bin_items bi on bi.container_id = case.container_id
left join fcskus fs on fs.fcsku = bi.isbn
where bin.scannable_id like 'R-1-T%'
having sum(bi.quantity) <= '5'
group by bin.scannable_id, pallet.scannable_id, nvl(fs.asin,bi.isbn), bi.quantity
order by sum(bi.quantity);
As said on the comments you dont need a DISTINCT if you have the group by statement. And format your date field because depending on your oracle client configuration it will not show you the entire date format (e.g. date time). So try with this:
select bin.scannable_id as bin,
bi.bin_id as case1,
pallet.scannable_id as pallet,
nvl(fs.asin,bi.isbn) as asin,
to_char(pallet.creation_date, 'yyyy-mm-dd') as received_date
sum(bi.quantity) as quantity,
from containers bin
join containers pallet on pallet.containing_container_id = bin.container_id
join containers case on case.containing_container_id = pallet.container_id
join bin_items bi on bi.container_id = case.container_id
left join fcskus fs on fs.fcsku = bi.isbn
where bin.scannable_id like 'R-1-T%'
and bi.quantity <= '5'
group by bin.scannable_id,
pallet.scannable_id,
bi.bin_id,
bi.owner,
bi.isbn,
nvl(fs.asin,bi.isbn),
to_char(pallet.creation_date, 'yyyy-mm-dd')
order by sum(bi.quantity);
bi.bin_id is different for each row, so you do only have distinct results in your resultset.
distinct is applied to the final visible resultset (once the to_char etc. functions are processed)
distinct is redundant if you already use a group by expression
Solution: skipp the bi.bin_id column from your select expression.
Your logic is also confusing. You want to know the SUM of all the bi.* elements. To do so you cannot group by bi.bin_id nor any field from the bi table. This is the reason why your quantity result is always 1.