Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9233079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:28:01+00:00 2026-06-18T06:28:01+00:00

This code now displays information from multiple tables, that are pivoted Ok, so this

  • 0

This code now displays information from multiple tables, that are pivoted
Ok, so this code now works almost as I want it to (working currently on something with it, no further questions atm):

if (exists (select * from tempdb.INFORMATION_SCHEMA.TABLES where TABLE_NAME = '##tempz'))
begin
drop table ##tempz
end
else
DECLARE @startDate datetime
DECLARE @enddate datetime
DECLARE @registernum int
DECLARE @storename varchar(20)
DECLARE @cashiername varchar(20)
SET @startDate = '1/1/2011'
SET @enddate = '1/1/2013'
SET @registernum = 01
SET @storename = '01'
SET @cashiername = 'admin'

DECLARE @cols AS NVARCHAR(MAX),@colsNull AS NVARCHAR(MAX),@query  AS NVARCHAR(MAX)
SELECT @cols = STUFF((SELECT DISTINCT ',' + QUOTENAME(c.CurrencyDesc)
FROM rpPay p LEFT JOIN RPTrs r ON p.ReceiptNo = r.ReceiptNo LEFT JOIN Currencies c ON c.POSCurrency = LEFT(p.paytype,1)
WHERE r.trsdate >= @startDate AND r.trsdate <= @enddate
FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)'),1,1,'')

SELECT @colsNull = STUFF((SELECT DISTINCT ', IsNull(' + QUOTENAME(c.CurrencyDesc) +', 0) as '+ QUOTENAME(c.CurrencyDesc)
FROM rpPay p LEFT JOIN RPTrs r ON p.ReceiptNo = r.ReceiptNo LEFT JOIN Currencies c ON LEFT(p.paytype,1) = c.POSCurrency
WHERE r.trsdate >= @startDate AND r.trsdate <= @enddate
FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)'),1,1,'')

--select @cols, @colsnull

SET @query = 'select date, cashregister, storeid, cashier, '+@colsNull+' into ##tempz
FROM
(SELECT cast(r.trsdate AS DATE) date,c.CurrencyDesc,p.amount,r.cashregister,r.storeid,r.cashier
FROM rpPay p LEFT JOIN RPTrs r ON p.ReceiptNo = r.receiptno LEFT JOIN Currencies c ON LEFT(p.paytype,1) = c.POSCurrency
WHERE r.trsdate >= '''+ convert(varchar(10), @startDate, 101) +''' AND r.trsdate <= '''+ convert(varchar(10), @endDate, 101) +''' 
) p
pivot
(sum(amount) FOR CurrencyDesc in('+@cols+')) piv'
execute(@query)

select * from ##tempz
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:28:02+00:00Added an answer on June 18, 2026 at 6:28 am

    You did not specify what RDBMS you are using but I am assuming SQL Server based on your previous questions. Based on the limited details, you can PIVOT the data.

    If you know the values ahead of time, then you can hard-code the values using a static pivot:

    select *
    from
    (
        select 
            cast(r.trsdate as DATE) date,
            c.CurrencyDesc,
            p.amount
        from rpPay p
        left join RPTrs r
            on p.id = r.id
        left join Currencies c
            on p.PayType = c.PayType
        where r.trsdate >= @startDate 
            and r.trsdate <= @enddate
    ) src
    pivot
    (
        sum(amount)
        for CurrencyDesc in (Amex, Cash, Visa, Check, [Gift Card])
    ) piv
    

    If the values are unknown, then you can use dynamic sql:

    declare @startDate datetime
    declare @enddate datetime
      set @startDate = '1/1/12'
     set @enddate = '1/1/13'
    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT DISTINCT ',' + QUOTENAME(c.CurrencyDesc) 
                        from rpPay p
                        left join RPTrs r
                            on p.id = r.id
                        left join Currencies c
                            on p.PayType = c.PayType
                        where r.trsdate >= @startDate 
                            and r.trsdate <= @enddate
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query 
        = 'select date, '+@cols+'
           from
           (
             select 
                cast(r.trsdate as DATE) date,
                c.CurrencyDesc,
                p.amount
            from rpPay p
            left join RPTrs r
                on p.id = r.id
            left join Currencies c
                on p.PayType = c.PayType
            where r.trsdate >= '''+ convert(varchar(10), @startDate, 101) +'''
                and r.trsdate <= '''+ convert(varchar(10), @endDate, 101) +'''
           ) p
           pivot
           (
              sum(amount)
              for CurrencyDesc in('+@cols+')
           ) piv'
    
    execute(@query)
    

    Edit #1, based on your edited question you can use:

    declare @startDate datetime
    declare @enddate datetime
    declare @registernum int
    declare @storename int
    declare @cashiername varchar(20)
    set @startDate = '1/1/2012'
    set @enddate = '1/1/2013'
    set @registernum = 01
    set @storename = '01'
    set @cashiername = 'admin'
    
    DECLARE @cols AS NVARCHAR(MAX),
        @colsNull AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT DISTINCT ',' + QUOTENAME(c.CurrencyDesc) 
                            from rpPay p 
                            left join RPTrs r 
                                on p.ReceiptNo = r.ReceiptNo 
                            left join Currencies c 
                                on p.PayType = c.POSCurrency
                            where r.trsdate >= @startDate and r.trsdate <= @enddate
                                and cashregister = @registernum
                                and r.storeid = @storename
                                and cashier = @cashiername
                            FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)'),1,1,'')
    
    select @colsNull = STUFF((SELECT DISTINCT ', IsNull(' + QUOTENAME(c.CurrencyDesc) +', 0) as '+ QUOTENAME(c.CurrencyDesc)
                            from rpPay p 
                            left join RPTrs r 
                                on p.ReceiptNo = r.ReceiptNo 
                            left join Currencies c 
                                on p.PayType = c.POSCurrency
                            where r.trsdate >= @startDate and r.trsdate <= @enddate
                                and cashregister = @registernum
                                and r.storeid = @storename
                                and cashier = @cashiername
                            FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)'),1,1,'')
    
    --select @cols, @colsnull
    
    set @query = 'select date, cashregister, storeid, cashier, '+@colsNull+'
                    from
                    (
                        select cast(r.trsdate as DATE) date,
                            c.CurrencyDesc,
                            p.amount,
                            r.cashregister,
                            r.storeid,
                            r.cashier 
                        from rpPay p 
                        left join RPTrs r 
                            on p.ReceiptNo = r.receiptno 
                        left join Currencies c 
                            on p.PayType = c.POSCurrency
                        where r.trsdate >= '''+ convert(varchar(10), @startDate, 101) +''' 
                            and r.trsdate <= '''+ convert(varchar(10), @endDate, 101) +''' 
                            and cashregister = '''+cast(@registernum as varchar(10))+''' 
                            and r.storeid = '''+cast(@storename as varchar(50))+''' 
                            and r.cashier = '''+@cashiername+''' 
                    ) p
                    pivot
                    (
                        sum(amount) 
                        for CurrencyDesc in('+@cols+')
                    ) piv'
    
    execute(@query)
    

    See SQL Fiddle with Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: This code now works correctly, I only left it in case someone finds
I have this code right now to get the row value from jquery grid..
this is my code for now: SELECT id, number FROM Media WHERE user =
I have been working on a script that pulls information from a certain website.
I have the following ColdFusion code that is getting information from a database and
In Python I have this code: now = datetime.now().isoformat() if . not in now:
I'm using this code now echo $form->input('username'); How do I make sure the label
I have this code ,now can anyone reply s what happens to the lock
This code used to return my local ip address as 192.xxx.x.xxx but now it
Now i'm using this code: User has_one User_extra User => :username, :email, :crypted_password, :salt,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.