sqlcurious
asked on
SQl Query help for reports
i have a drop down list of customers in the report from where i can select one customer or all customers.If i select one
customer then the report should be populated for that customer only.If i seelct all from the drop down list then the
report should be poulated for all the customers
Like it should be as follows
Customer name : harry id:1---------For one customer
Customer Name:All Customers-------------For all customers
For this i created a dataset called customer name and wrote the following queries for that data set
select c.customername
from tbl_customer c
union
select 'all'
where all= select customername from tbl_customer-------showed error here
ASKER
yes i tried that one too but i am designing the report based on that .I have a dataset called customer name and wrote the same query as
select distinct customername from tbl_customer union all select 'all----to get customer from dropdown list
' it is not selecting all customers but only giving me the report based on random one customer
however my stored proc is showing data for multiple customers
Can you post the SP code? Want to see how are you handling the 'ALL' parameter. If you have logic something like which I have mentioned, that should work.
ASKER
following is the stored proc
ALTER PROCEDURE [dbo].[DBSP_Report_GetLett ingDate1]
(
@LettingDateFrom datetime,
@LettingDateTo datetime,
@ProjectTxDot int,
@CustomerName varchar(50)='',
@DivisionName varchar(250)=''
)
AS
BEGIN
--declare @SQL varchar(8000)
--DECLARE @country varchar(255)
SELECT CU.CustomerID,
CU.CustomerName,
P.ProjectID ,
P.Projectname,
p.ProjectNumber,
P.ProjectCountyID,
convert(varchar(10),P.Proj ectStartDa te,110) AS ProjectStartDate,
P.projectTxdot,
D.DivisionID,
D.Divisionname As DivisionName,
--P.projectCountyID As CountyID,
C.CountyName As CountyName,
--CU.CustomerName As CustomerName,
--CU.customerID,
QI.QuoteItemQuantity as Quantity,
QI.QuoteItem_ItemNumber AS Item#,
QI.QuoteItemDescription As ItemDescription,
QI.QuoteItemQuantity As ItemQuantity,
QI.QuoteItemUnit As ItemUnit,
QI.QuoteItemUnitprice As UnitCost,
QI.QuoteItem_ItemTotalPric e As UnitPrice,
(QI.QuoteItemQuantity * QI.QuoteItemUnitPrice) AS TotalCost,
(QI.QuoteItemQuantity*QI.Q uoteItem_I temTotalPr ice) AS TotalPrice,
(QI.QuoteItemWeight*QI.Quo teItemQuan tity) AS TotalWeight,
QI.QuoteItemWeight AS ItemWeight,
QI.QuoteItemMarkupPercent AS MarkupPercent,
Q.QuoteFreightRate as FreightRate,
Q.QuoteNumberOfTrucks,
Q.QuoteSurcharge1,
Q.QuoteSurcharge2,
Q.QuoteSurcharge3,
((Q.QuoteFreightRate * Q.QuoteNumberOfTrucks) +(Q.QuoteSurcharge1+Q.Quot eSurcharge 2+Q.QuoteS urcharge3) ) AS TotalFreightforQuote,
(((QI.QuoteItemQuantity*QI .QuoteItem _ItemTotal Price)-(QI .QuoteItem Quantity * QI.QuoteItemUnitPrice)-((Q .QuoteFrei ghtRate * Q.QuoteNumberOfTrucks) +(Q.QuoteSurcharge1+Q.Quot eSurcharge 2+Q.QuoteS urcharge3) ))/((QI.Qu oteItemQua ntity*QI.Q uoteItem_I temTotalPr ice))*100) AS MARGIN
into #temptable
FROM TBL_Project P INNER JOIN TBL_Quotes1 Q
ON P.ProjectID = Q.ProjectID inner JOIN TBL_County C
ON C.CountyID = P.ProjectCountyID inner JOIN TBL_QuoteItems QI
ON Q.QuoteID = QI.QuoteID INNER JOIN TBL_Division D
ON D.Createdby = Q.QuoteCreatedBy --INNER JOIN TBL_CustomerQuotes CU
-- ON CU.QuoteID = Q.QuoteID
inner join tbl_customer cu on cu.customercreatedby=Q.Quo teCreatedb y
WHERE p.projectstartdate >= ISNULL(@LettingDateFrom,p. projectsta rtdate)
and p.projectstartdate <= ISNULL(@LettingDateTo,p.pr ojectstart date)
and D.DivisionName IN (SELECT Item FROM dbo.fneBid_SplitValue(CASE WHEN @DivisionName = '' OR @DivisionName IS NULL Then D.DivisionName Else @DivisionName END, ','))
and p.ProjectTxDot = CASE WHEN @projectTxDot = '' Then p.ProjectTxDot Else @projectTxDot END
and CU.CustomerName IN (SELECT Item FROM dbo.fneBid_SplitValue(CASE WHEN @CustomerName = 'all' Then CU.CustomerName Else @CustomerName END, ','))
ALTER PROCEDURE [dbo].[DBSP_Report_GetLett
(
@LettingDateFrom datetime,
@LettingDateTo datetime,
@ProjectTxDot int,
@CustomerName varchar(50)='',
@DivisionName varchar(250)=''
)
AS
BEGIN
--declare @SQL varchar(8000)
--DECLARE @country varchar(255)
SELECT CU.CustomerID,
CU.CustomerName,
P.ProjectID ,
P.Projectname,
p.ProjectNumber,
P.ProjectCountyID,
convert(varchar(10),P.Proj
P.projectTxdot,
D.DivisionID,
D.Divisionname As DivisionName,
--P.projectCountyID As CountyID,
C.CountyName As CountyName,
--CU.CustomerName As CustomerName,
--CU.customerID,
QI.QuoteItemQuantity as Quantity,
QI.QuoteItem_ItemNumber AS Item#,
QI.QuoteItemDescription As ItemDescription,
QI.QuoteItemQuantity As ItemQuantity,
QI.QuoteItemUnit As ItemUnit,
QI.QuoteItemUnitprice As UnitCost,
QI.QuoteItem_ItemTotalPric
(QI.QuoteItemQuantity * QI.QuoteItemUnitPrice) AS TotalCost,
(QI.QuoteItemQuantity*QI.Q
(QI.QuoteItemWeight*QI.Quo
QI.QuoteItemWeight AS ItemWeight,
QI.QuoteItemMarkupPercent AS MarkupPercent,
Q.QuoteFreightRate as FreightRate,
Q.QuoteNumberOfTrucks,
Q.QuoteSurcharge1,
Q.QuoteSurcharge2,
Q.QuoteSurcharge3,
((Q.QuoteFreightRate * Q.QuoteNumberOfTrucks) +(Q.QuoteSurcharge1+Q.Quot
(((QI.QuoteItemQuantity*QI
into #temptable
FROM TBL_Project P INNER JOIN TBL_Quotes1 Q
ON P.ProjectID = Q.ProjectID inner JOIN TBL_County C
ON C.CountyID = P.ProjectCountyID inner JOIN TBL_QuoteItems QI
ON Q.QuoteID = QI.QuoteID INNER JOIN TBL_Division D
ON D.Createdby = Q.QuoteCreatedBy --INNER JOIN TBL_CustomerQuotes CU
-- ON CU.QuoteID = Q.QuoteID
inner join tbl_customer cu on cu.customercreatedby=Q.Quo
WHERE p.projectstartdate >= ISNULL(@LettingDateFrom,p.
and p.projectstartdate <= ISNULL(@LettingDateTo,p.pr
and D.DivisionName IN (SELECT Item FROM dbo.fneBid_SplitValue(CASE
and p.ProjectTxDot = CASE WHEN @projectTxDot = '' Then p.ProjectTxDot Else @projectTxDot END
and CU.CustomerName IN (SELECT Item FROM dbo.fneBid_SplitValue(CASE
Can you check this?
ALTER PROCEDURE [dbo].[DBSP_REPORT_GETLETTINGDATE1](
@LettingDateFrom DATETIME,
@LettingDateTo DATETIME,
@ProjectTxDot INT,
@CustomerName VARCHAR(50) = '',
@DivisionName VARCHAR(250) = '')
AS
BEGIN
--declare @SQL varchar(8000)
--DECLARE @country varchar(255)
SELECT CU.CustomerID,
CU.CustomerName,
P.ProjectID,
P.Projectname,
p.ProjectNumber,
P.ProjectCountyID,
CONVERT(VARCHAR(10),P.ProjectStartDate,110) AS ProjectStartDate,
P.projectTxdot,
D.DivisionID,
D.Divisionname AS DivisionName,
--P.projectCountyID As CountyID,
C.CountyName AS CountyName,
--CU.CustomerName As CustomerName,
--CU.customerID,
QI.QuoteItemQuantity AS Quantity,
QI.QuoteItem_ItemNumber AS Item#,
QI.QuoteItemDescription AS ItemDescription,
QI.QuoteItemQuantity AS ItemQuantity,
QI.QuoteItemUnit AS ItemUnit,
QI.QuoteItemUnitprice AS UnitCost,
QI.QuoteItem_ItemTotalPrice AS UnitPrice,
(QI.QuoteItemQuantity * QI.QuoteItemUnitPrice) AS TotalCost,
(QI.QuoteItemQuantity * QI.QuoteItem_ItemTotalPrice) AS TotalPrice,
(QI.QuoteItemWeight * QI.QuoteItemQuantity) AS TotalWeight,
QI.QuoteItemWeight AS ItemWeight,
QI.QuoteItemMarkupPercent AS MarkupPercent,
Q.QuoteFreightRate AS FreightRate,
Q.QuoteNumberOfTrucks,
Q.QuoteSurcharge1,
Q.QuoteSurcharge2,
Q.QuoteSurcharge3,
((Q.QuoteFreightRate * Q.QuoteNumberOfTrucks) + (Q.QuoteSurcharge1 + Q.QuoteSurcharge2 + Q.QuoteSurcharge3)) AS TotalFreightforQuote,
(((QI.QuoteItemQuantity * QI.QuoteItem_ItemTotalPrice) - (QI.QuoteItemQuantity * QI.QuoteItemUnitPrice) - ((Q.QuoteFreightRate * Q.QuoteNumberOfTrucks) + (Q.QuoteSurcharge1 + Q.QuoteSurcharge2 + Q.QuoteSurcharge3))) / ((QI.QuoteItemQuantity * QI.QuoteItem_ItemTotalPrice)) * 100) AS MARGIN
INTO #temptable
FROM TBL_Project P
INNER JOIN TBL_Quotes1 Q
ON P.ProjectID = Q.ProjectID
INNER JOIN TBL_County C
ON C.CountyID = P.ProjectCountyID
INNER JOIN TBL_QuoteItems QI
ON Q.QuoteID = QI.QuoteID
INNER JOIN TBL_Division D
ON D.Createdby = Q.QuoteCreatedBy --INNER JOIN TBL_CustomerQuotes CU
-- ON CU.QuoteID = Q.QuoteID
INNER JOIN tbl_customer cu
ON cu.customercreatedby = Q.QuoteCreatedby
WHERE p.projectstartdate >= ISNULL(@LettingDateFrom,p.projectstartdate)
AND p.projectstartdate <= ISNULL(@LettingDateTo,p.projectstartdate)
AND D.DivisionName IN (SELECT Item
FROM dbo.FNEBID_SPLITVALUE(CASE
WHEN @DivisionName = ''
OR @DivisionName IS NULL THEN D.DivisionName
ELSE @DivisionName
END,','))
AND p.ProjectTxDot = CASE
WHEN @projectTxDot = '' THEN p.ProjectTxDot
ELSE @projectTxDot
END
AND ((@CustomerName <> 'all'
AND CU.CustomerName IN (SELECT Item
FROM dbo.FNEBID_SPLITVALUE(@CustomerName,',')))
OR (@CustomerName = 'all'))
END
ASKER
yes i seems working
but i have a small issue in the report.If i select all the report should display for all customers but it showing the data in report by one customer name.However the calculation are made by selecting all customers .I have attatched the screenshot
Output1.docx
but i have a small issue in the report.If i select all the report should display for all customers but it showing the data in report by one customer name.However the calculation are made by selecting all customers .I have attatched the screenshot
Output1.docx
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
actually i want to display customer name :all in the report output if i select all from the drop down list now its taking one particular customer but showing data for all customers.
Please check the screenshot .I think some changes has to be made in ssrs
i used the following query in the dataset called customer.Do i have to chnage anything there?please let me know
SELECT distinct CustomerName
FROM TBL_Customer
UNION all
SELECT 'all'
Open in new window
To display the Customer names in the drop down, you can try like this.Open in new window