Solved

SQl Query help for reports

Posted on 2011-03-09
8
182 Views
Last Modified: 2012-05-11

 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

0
Comment
Question by:sqlcurious
  • 4
  • 4
8 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 35088489
Try like this to retrieve the data from database when the user selects a customer_name
select *
  from tbl_customer
 where CustomerName = case when @customer_name = 'ALL' then CustomerName else @customer_name end

Open in new window

To display the Customer names in the drop down, you can try like this.
select distinct customername from tbl_customer union all select 'all'

Open in new window

0
 

Author Comment

by:sqlcurious
ID: 35088586


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
0
 
LVL 41

Expert Comment

by:Sharath
ID: 35088604
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.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:sqlcurious
ID: 35088617
following is the stored proc


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 CU.CustomerName IN (SELECT Item FROM dbo.fneBid_SplitValue(CASE WHEN @CustomerName = 'all' Then CU.CustomerName Else @CustomerName END, ','))
0
 
LVL 41

Expert Comment

by:Sharath
ID: 35088727
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

Open in new window

0
 

Author Comment

by:sqlcurious
ID: 35088892
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
0
 
LVL 41

Accepted Solution

by:
Sharath earned 500 total points
ID: 35088955
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, 
           case when @CustomerName = 'all' then 'All Customers' else CU.CustomerName end 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

Open in new window

0
 

Author Comment

by:sqlcurious
ID: 35089347

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'
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question