How to merge Two queries into one and result in one row - sql

SELECT lp.lead_bucket_no ,
case when p.product = 'S-400' then qty end as 'S400' ,
case when p.product = 'Dish Antenna' then qty end as 'DishAntenna'
FROM lead_products lp INNER JOIN products p ON p.product_id = lp.product_id WHERE type = 'stock' GROUP BY lead_bucket_no ORDER BY lp.lead_bucket_no
and
SELECT lp.lead_bucket_no ,
case when p.product = 'S-400' then qty end as 'S400' ,
case when p.product = 'Dish Antenna' then qty end as 'DishAntenna'
FROM lead_products lp INNER JOIN products p ON p.product_id = lp.product_id WHERE type = 'order' GROUP BY lead_bucket_no ORDER BY lp.lead_bucket_no
how to merge these both query and and got record in single row. only type are different in both queries.
enter image description here

I think you just want OR (or IN) in the WHERE clause:
SELECT lp.lead_bucket_no,
SUM(case when p.product = 'S-400' then qty end) as S400,
SUM(case when p.product = 'Dish Antenna' then qty end) as DishAntenna
FROM lead_products lp INNER JOIN
products p
ON p.product_id = lp.product_id
WHERE type IN ('stock', 'order')
GROUP BY lead_bucket_no
ORDER BY lp.lead_bucket_no;
You also need some sort of aggregation for the expressions that are not in the GROUP BY clause. You may also want to aggregate by TYPE. It is unclear what you want for the final output.

Related

Aggregate rows inside new columns

I would like to join the two first tables (Product and ProductProperties) to get the result in the bottom.
How do I do this?
There are penalties for EAV. I'm not saying EAV is evil, but should be deployed carefully and with great forethought.
Here are two examples. The first is a PIVOT, and the second is a conditional aggregation.
I tend to lean towards the conditional aggregation, it offers more flexibility and often a performance bump
Untested for you did not supply sample data and desired results as text
Select *
From (
Select A.product_id
,A.product_name
,B.product_property
,B.product_property_value
From Product A
Join ProductProperties B on A.product_id=B.product_di
) src
Pivot (max( product_property_value ) for product_property in ([Price],[Category],[Status] ) ) pvt
Select A.product_id
,A.product_name
,Price = max( case when product_propery='Price' then product_propery_value end)
,Category = max( case when product_propery='Category' then product_propery_value end)
,Status = max( case when product_propery='Status' then product_propery_value end)
From Product A
Join ProductProperties B on A.product_id=B.product_di
Group By A.product_id,A.product_name
SELECT
b.product_id
,b.product_name
,Price = MAX(IIF(p.product_property = 'Price',p.product_property_value,NULL))
,Category = MAX(IIF(p.product_property = 'Category',p.product_property_value,NULL))
,Status = MAX(IIF(p.product_property = 'Status',p.product_property_value,NULL))
FROM books b (nolock)
JOIN prodprop p (nolock)
ON b.product_id = p.product_id
GROUP BY b.product_id,b.product_name

Avoid NULL value in CASE SQL SERVER

How to avoid NULL values in CASE and SUM by P.Id. Problem is that i have more than one DPB.ProductTypeId in DPB table
SELECT P.[Id],
CASE
WHEN DPB.ProductTypeId = 1 THEN SUM(DPB.BonusAmount)
END AS [CasinoBonus]
FROM Player P
JOIN PlayerBonus DPB ON P.[Id] = DPB.[PlayerId]
group by P.[Id],DPB.ProductTypeId
use case when inside sum
SELECT P.[Id],
sum(CASE
WHEN DPB.ProductTypeId = 1 THEN DPB.BonusAmount
else 0
END) AS [CasinoBonus]
FROM Player P
JOIN PlayerBonus DPB ON P.[Id] = DPB.[PlayerId]
where P.[Id] is not null and DPB.[PlayerId] is not null
group by P.[Id],DPB.ProductTypeId
The case should be the argument to the sum(). You query should look like this:
SELECT P.[Id],
SUM(CASE WHEN DPB.ProductTypeId = 1 THEN DPB.BonusAmount
END) AS [CasinoBonus]
FROM Player P JOIN
PlayerBonus DPB
ON P.[Id] = DPB.[PlayerId]
GROUP BY P.[Id];
Note that you don't want DPB.ProductTypeId in the GROUP BY.
That said, you may simply want:
SELECT P.[Id],
SUM(DPB.BonusAmount) AS [CasinoBonus]
FROM Player P LEFT JOIN
PlayerBonus DPB
ON P.[Id] = DPB.[PlayerId] AND
DPB.ProductTypeId = 1
GROUP BY P.[Id];
Moving the condition to the WHERE clause removes the need for the CASE entirely. The LEFT JOIN keeps all players, even those that don't have that product type.

SQL Select Query into another query

I am a beginner with SQL so apologise in advance if my terminology / coding is a little off, or maybe way off.
I have two queries which I would like to join into one. The first creating a list of productids which contain two specific processes.
I then want to use these productids in the second query.
Also is below correct?
group by products.productid having (sum(case when processid like 'pick%' then 1 else 0 end) + sum(case when processid like 'pack%' then 1 else 0 end) = 2)
Any help would be much appreciated, hope this makes sense.
SELECT
Products.ProductID
FROM Products
INNER JOIN Categories ON Products.Category = Categories.Category
INNER JOIN Boms ON Products.Product = Boms.Product AND Boms.BomVersion = Products.BomVersion
LEFT OUTER JOIN Products AS Comp ON Boms.Component = Comp.Product
LEFT OUTER JOIN Processes ON Boms.Process = Processes.Process
WHERE
products.active = 1
and Categorys.Categoryid in ('5','20')
group by products.productid
having (sum(case when processid like 'pick%' then 1 else 0 end) + sum(case when processid like 'pack%' then 1 else 0 end) = 2)
order by products.productid
SELECT
Products.ProductID,
Products.productdescription,
Boms.Type As Type,
Comp.ProductId as Component,
Comp.productdescription,
Boms.Quantity,
BomVersions.BomVersionID,
Processes.processid,
Processes.ProcessDescription
FROM Products
INNER JOIN Categories ON Products.Category = Categories.Category
INNER JOIN Boms ON Products.Product = Boms.Product AND Boms.BomVersion = Products.BomVersion
LEFT OUTER JOIN Products AS Comp ON Boms.Component = Comp.Product
LEFT OUTER JOIN Processes ON Boms.Process = Processes.Process
INNER JOIN BomVersions ON Products.BomVersion = BomVersions.BomVersion
WHERE
products.active = 1
order by products.productid, products.type,comp.productid
To combine them you could do the following.
SELECT b.*
FROM
(SELECT Products.ProductID FROM Products INNER JOIN ...) AS a
INNER JOIN
(SELECT Products.ProductID, Products.productdescription, Boms.Type As Type, ...) AS b
ON a.ProductID = b.ProductID

AND isHidden = 0 is not taking into consideration when running this sql server query

I have this sql server query - that works almost - but for some reason it also returns results where ishidden is not 0 even though I have stated in the query that it should only return results where ishidden = 0. Can someone help me fix this ? Here is the sql query:
SELECT p.productId, p.colorId, p.creationDate, p.productName,
p.description, p.oldprice, p.price, p.isPriority, p.isHidden, p.isOffer, p.isInStock,
p.sortorder, p.colorId, pc.catid, c.picurl, c.isfrontpic, c.picid, pc.catname FROM ProductAndCat pac INNER JOIN Product
as p on pac.productid = p.productid INNER JOIN ProductCat as pc on pac.catid = pc.catid LEFT JOIN
(SELECT productid, picurl,isfrontpic, picid, ROW_NUMBER() OVER (PARTITION BY productid ORDER BY isfrontpic DESC) rn
FROM productpic) c ON c.rn = 1 AND p.ishidden = 0 AND p.productId = c.productId order by productid
You've added the p.ishidden = 0 to the join condition for the left join of the derived table c. Try moving it to a proper WHERE p.ishidden = 0 after to joins.
The end of the query should look something like this:
FROM productpic )c
ON c.rn = 1
AND p.productId = c.productId
WHERE p.ishidden = 0
ORDER BY productid;

A column in the predicate of the SQL

How can I select custType in the predicate section of the query
I can't do so now because
Error code -1, SQL state 42X04: Column 'CUSTTYPE' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE statement then 'CUSTTYPE' is not a column in the target table.
select p.SKU AS GiftID, p.ProductName AS GiftName,
case when sa.Sales >= v.LevelUpAmount then 1 else 2 end AS custType
from products p, campaign ca, SubCategory sc,
VIPLevelUpParam v,
ActiveParam a, customer c,
(select c.CustomerCode, sum(od.NetSales) AS Sales from customer c
INNER JOIN orders o ON (c.CustomerCode = o.CustomerCode)
INNER JOIN order_details od ON (o.OrderCode = od.OrderCode)
group by c.CustomerCode ) sa
where ca.CUSTOMERTYPE = custType AND
c.CustomerCode = 'CUS000001-2013-11-06' AND
p.SubCategoryID = sc.SubCategoryCode AND
p.SKU = ca.GiftID AND
sc.SubCategoryName = 'Gift'AND
v.LevelUpID = a.ActiveID AND
a.TableName = 'VIPLevelUpParam'
group by p.SKU, p.ProductName, sa.Sales, v.LevelUpAmount, custType;
Any one shed some light on this, it would be greatly appreciated!
As per logical query processing where clause is evaluated before select statement that why you're getting CustType column won't be available in where clause.
http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/
select * from
(
select p.SKU AS GiftID, p.ProductName AS GiftName,
case when sa.Sales >= v.LevelUpAmount then 1 else 2 end AS custType
from products p, campaign ca, SubCategory sc,
VIPLevelUpParam v,
ActiveParam a, customer c,
(select c.CustomerCode, sum(od.NetSales) AS Sales from customer c
INNER JOIN orders o ON (c.CustomerCode = o.CustomerCode)
INNER JOIN order_details od ON (o.OrderCode = od.OrderCode)
group by c.CustomerCode ) sa
) DT
where DT.CUSTOMERTYPE = DT.custType AND
DT.CustomerCode = 'CUS000001-2013-11-06' AND
DT.SubCategoryID = DT.SubCategoryCode AND
DT.SKU = DT.GiftID AND
DT.SubCategoryName = 'Gift' AND
DT.LevelUpID = DT.ActiveID AND
DT.TableName = 'VIPLevelUpParam'
group by DT.SKU, DT.ProductName, DT.Sales, DT.LevelUpAmount, DT.custType;