Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Query (Sybase): Subtract 1 from the sum of one column depending on the value of another
    primarykey
    data
    text
    <p>I have 2 SQL queries that I need to combine into one due to the sheer size of the database (most of the retail store locations, it is fine, but some have much much more transactions)</p> <p>Basically, I need the number of transactions per employee for a given day. To do this, I am counting the distinct number of transaction numbers per employee (each item in a transaction gets its own row with the same transaction number, thus it needs to be distinct). However, there is a column "txnvoidmod". Basically the goal is that if the value of this column is 0, I simply count the transactions normally, but if the value is 1, I need to subtract off one because the transaction was actually voided. </p> <p>Here is some sample data</p> <pre><code>+------ -+------------+-------------+ |transnum| txnvoidmod | salesprsnid | +--------+------------+-------------+ | 115568 | 0 | 1339 | | 115568 | 0 | 1339 | | 114566 | 0 | 1339 | | 114566 | 0 | 1339 | | 115504 | 0 | 2555 | | 105551 | 0 | 0452 | | 105551 | 0 | 0452 | | 105551 | 0 | 0452 | | 105552 | 1 | 0452 | | 105552 | 1 | 0452 | | 105552 | 1 | 0452 | +--------+------------+-------------+ </code></pre> <p>I am omitting some fields that are unnecessary for this example.</p> <p>Here is the query that I am using</p> <pre><code> select txn_pos_transactions.cashiernum as salesprsnid, (count(distinct MS.transnum))as transcnt from Txn_Merchandise_Sale MS INNER JOIN txn_pos_transactions ON MS.transnum=txn_pos_transactions.transnum Where MS.modtime like '2013-06-01 %' AND MS.itemdatetime like '2013-06-01 %' and (txnvoidmod=0 or txnvoidmod=1) Group by txn_pos_transactions.cashiernum order by salesprsnid </code></pre> <p>I am leaving out the fact that this query also is querying for netsales and units sold, so I must take that into account. Some sample results for this query (including the omitted netsales and units portion which is quite verbose)</p> <pre><code>+------ -+------------+-------------+-------------+ |transcnt| unitssold | salesprsnid | netsalesamt | +--------+------------+-------------+-------------+ | 2 | 5 | 1339 | 98.50 | | 1 | 2 | 2555 | 35.20 | | 2 | 1 | 0452 | 24.00 | +--------+------------+-------------+-------------+ </code></pre> <p>From above, the employee with id "0452" has 2 transaction in the results (transcnt), however, I need to build my query so that it actually reads as 0 transactions because their 1 transaction has a txnvoidmod equal to 1 and is actually a void of the previous transaction where txnvoidmod=0. If I only said "txnvoidmod=0" in the where clause, then "0452" would have 1 transaction, but I still need to subtract off the transaction where the txnvoidmod=1</p> <p>I've tried various things such as conditionals and subtracting off the value of the txnvoidmod, but to no avail as it trys to group by the txnvoidmod which gives me unnecessary rows. I need there to be only one row per salesprsnid.</p> <p>Any suggestions would be great, thanks. </p> <p>By the way, I am using a SAP Sybase database.. it seems like for the most part that the queries are the same with the exception of a few items they are missing.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload