SELECT a.invoice_id,
invoice_no,
a.cs_no,
identity,
name,
model,
body_color,
lot_no,
key_no,
serial_no,
location,
buyoff_date,
received_date,
tagged_date,
invoice_date,
a.pullout_date,
vin_no,
engine_no,
order_no,
b.approve,
COUNT(a.cs_no) AS buyoff_count,
SUM(CASE WHEN received_date IS NULL THEN 0 ELSE 1 END) AS received_count,
SUM(CASE WHEN tagged_date IS NULL THEN 0 ELSE 1 END) AS tagged_count,
SUM(CASE WHEN invoice_date IS NULL THEN 0 ELSE 1 END) AS invoice_count,
SUM(CASE WHEN a.pullout_date IS NULL THEN 0 ELSE 1 END) AS pullout_count
FROM buy_off a
LEFT JOIN pull_out b
ON a.invoice_id = b.invoice_id
AND a.cs_no = b.cs_no
WHERE tagged_date IS NOT NULL
AND invoice_date IS NULL
GROUP BY identity, tagged_date, invoice_id WITH ROLLUP
My above code have output like this:
invoice_id identity tagged_date
1 a 2/5/2015
total = 1
2 a 2/6/2015
3 a 2/6/2015
total = 2
4 b 2/5/2015
total = 1
5 b 2/6/2015
total = 1
grand total = 5
But this is what i want:
invoice_id identity tagged_date
1 a 2/5/2015
2 a 2/6/2015
3 a 2/6/2015
total = 3
4 b 2/5/2015
5 b 2/6/2015
total = 2
grand total = 5
Can you guys please help to achieve the above output.....Thanks in advance. :D
Related
im trying to make a column for month 1, 2 and 3 and put the invoice's total in that column. I have made that successfully, but then below there are more columns with zeroes for every column, aka it shows me the column for every invoice, but I want to get ONLY the invoices that have been made in month 1,2 and 3 and don't show the rest. How do I do that?
what I get:
invoice_id | total | firstMonth | secondMonth | thirdMonth |
1 33.4 0 0 33.4
2 3434.5 0 0 0 <=====
what I want:
invoice_id | total | firstMonth | secondMonth | thirdMonth |
1 33.4 0 0 33.4
Please learn about types. Single quotes should be used for string and date constants -- and not for column identifiers or numbers.
Then, I think you just want to filter the results correctly:
SELECT i.id AS invoice_ID , i.total AS TOTAL,
(CASE WHEN month(i.datetime) = 1 THEN ABS(i.total) ELSE 0 END) AS firstMonth,
(CASE WHEN month(i.datetime) = 2 THEN ABS(i.total) ELSE 0 END) AS secondMonth,
(CASE WHEN month(i.datetime) = 3 THEN ABS(i.total) ELSE 0 END) AS thirdMonth
FROM invoice i JOIN
store s
ON i.store_id = s.id JOIN
company c
ON s.company_id = c.id
WHERE c.name = 'RubberDuck' AND
i.datetime >= '2019-01-01' AND
i.date_time < '2019-04-01'
Unlike SQL: Count each row value in a column and return count into multiple column
My rows are also different.
I want vote count result like this.
CONTENT_NO CONTENT_TEXT one two three four(ANSWER_SCORE)
1 How often? 0 1 0 1
2 How fast? 0 1 0 1
3 How long? 0 0 2 0
4 How much? 1 1 0 0
I have 2 tables
question_content
CONTENT_NO CONTENT_TEXT TEST_PAPER_NO
1 How often? 1
2 How fast? 1
3 How long? 1
4 How much? 1
question_content_answer
ANSWER_NO CONTENT_NO TEST_PAPER_NO ANSWER_SCORE PERSON_NO
1 1 1 4 1
2 2 1 4 1
3 3 1 3 1
4 4 1 2 1
5 1 1 2 2
6 2 1 2 2
7 3 1 3 2
8 4 1 1 2
I have tried
SELECT question_content.CONTENT_NO,
question_content.CONTENT_TEXT,
(SELECT COUNT(ANSWER_SCORE)
FROM (SELECT ANSWER_SCORE
FROM question_content_answer
WHERE TEST_PAPER_NO= '1'
) as qa
WHERE ANSWER_SCORE = '1'
AND CONTENT_NO = qa.CONTENT_NO
GROUP BY CONTENT_NO
) as one,
(SELECT COUNT(ANSWER_SCORE)
FROM (SELECT ANSWER_SCORE
FROM question_content_answer
WHERE TEST_PAPER_NO= '1'
) as qa
WHERE ANSWER_SCORE = '2'
AND CONTENT_NO = qa.CONTENT_NO
GROUP BY CONTENT_NO
) as two,
(SELECT COUNT(ANSWER_SCORE)
FROM (SELECT ANSWER_SCORE
FROM question_content_answer
WHERE TEST_PAPER_NO= '1'
) as qa
WHERE ANSWER_SCORE = '3'
AND CONTENT_NO = qa.CONTENT_NO
GROUP BY CONTENT_NO
) as three
FROM question_content_answer
INNER JOIN question_content
USING (CONTENT_NO)
WHERE question_content.TEST_PAPER_NO= '1'
but this counts all the questions in every row.
How to count each row?
Just like SQL: Count each row value in a column and return count into multiple column
This code works fine.
SELECT question_content.CONTENT_NO,
question_content.CONTENT_TEXT,
COUNT(CASE WHEN ANSWER_SCORE = '1' THEN 1 END) AS one,
COUNT(CASE WHEN ANSWER_SCORE = '2' THEN 1 END) AS two,
COUNT(CASE WHEN ANSWER_SCORE = '3' THEN 1 END) AS three,
COUNT(CASE WHEN ANSWER_SCORE = '4' THEN 1 END) AS four
FROM question_content_answer
INNER JOIN question_content
USING (CONTENT_NO)
WHERE question_content.TEST_PAPER_NO = '1'
GROUP BY question_content.CONTENT_NO
I have one table with product details in each row where each item is unique.Second table is tax basis which have multiple tax for any single item. I want result which should have single row for each item and all tax related to that item should be in that row with other details
item
item_id name type
1. bike vehicle
tax
id tax_name tax_rate item_id
1. tax1 12 1
2. tax2 13 1
3. tax3 14 1
Result should be:
id name type tax_name_1 tax_rate_1 tax_name_2 tax_rate_2 tax_name_3 tax_rate_3
1 bike vehicle tax1 12 tax2 13 tax3 14
This is a typical table pivot issue, try this:
select
item.*,
max(case when tax.id = 1 then tax_name end) as tax_name_1,
max(case when tax.id = 1 then tax_rate end) as tax_rate_1,
max(case when tax.id = 2 then tax_name end) as tax_name_2,
max(case when tax.id = 2 then tax_rate end) as tax_rate_2,
max(case when tax.id = 3 then tax_name end) as tax_name_3,
max(case when tax.id = 3 then tax_rate end) as tax_rate_3
from item
join tax
group by item.id
I think simple inner join will do this task for you
SELECT item.*, tax.taxname,tax.taxrate
FROM table1
INNER JOIN tax
ON item.id=tax.id;
We have the table to track what product page a user visited.
product_tracking
____________________
id user_id product
1 1 A
2 1 B
3 2 A
4 1 A
I know we can use group by user_id, and group_by product but I need both.
Expected result :
result_table
____________________________
user_id A B C
1 2 1 0
2 1 0 0
any idea how to merge the 2 group by ?
You could do this:
SELECT
product_tracking.user_id,
SUM(CASE WHEN product_tracking.product='A' THEN 1 ELSE 0 END) AS A,
SUM(CASE WHEN product_tracking.product='B' THEN 1 ELSE 0 END) AS B,
SUM(CASE WHEN product_tracking.product='C' THEN 1 ELSE 0 END) AS C
FROM
product_tracking
GROUP BY
product_tracking.user_id
An alternative to using sum(...) is using count(...):
SELECT
product_tracking.user_id,
COUNT(CASE WHEN product_tracking.product='A' THEN 1 END) AS A,
COUNT(CASE WHEN product_tracking.product='B' THEN 1 END) AS B,
COUNT(CASE WHEN product_tracking.product='C' THEN 1 END) AS C
FROM
product_tracking
GROUP BY
product_tracking.user_id
I have 2 tables, orders and order_lineitems.
orders contains the order status info (sold date, invoice no, type of sale, etc)
order_lineitems contains the item(s) for each order in a one to many relationship.
Since we provide shipping info by line item, ship_date is in the order_lineitems table, null if not shipped, a date if it is shipped.
I am trying to pull the orders where all items have shipped by comparing the number of line item rows against the line item rows that have a ship date. While I have successfully pulled all that info, I am unable to make the last step, limiting the result set to include only the completely shipped orders (number of rows = number of rows where ship_date is not null).
I know I am missing something simple, but just don't see it..
select sum(custom.lineitems) as totalitems, sum(custom.shipped) as totalshipped,
custom.invoice, z.shipregion
from (
select a.invoice, count(a.invoice) as lineitems, 0 as shipped
from order_lineitem a
group by a.invoice
UNION ALL
select b.invoice, 0 as notshipped, count(b.ship_date) as shipped
from order_lineitem b
where b.ship_date is not null
group by b.invoice
) as custom
left join orders z on custom.invoice = z.invoice
where z.result = 0
and z.respmsg like 'Approved%'
and z.shipregion <> 'PENDING'
and z.cancelorder = 0
group by custom.invoice;
This returns a result set like so (one row for each invoice in the DB)
totalitems totalshipped invoice shipregion
4 2 1000 REGION08
1 1 10001 REGION07
1 1 10004 REGION05
3 1 10006 REGION05
2 2 10007 REGION04
1 1 10008 REGION08
7 7 10009 REGION01
1 1 1001 REGION08
What I am looking for is a result set like this - only where totalitems = totalshipped
totalitems totalshipped invoice shipregion
1 1 10001 REGION07
1 1 10004 REGION05
2 2 10007 REGION04
1 1 10008 REGION08
7 7 10009 REGION01
1 1 1001 REGION08
Use HAVING clause
SELECT a.invoice, z.shipregion, COUNT(a.invoice) AS lineitems,
SUM(CASE WHEN a.ship_date IS NOT NULL THEN 1 ELSE 0 END) AS shipped
FROM order_lineitem a
LEFT JOIN orders z ON a.invoice = z.invoice AND z.result = 0 AND z.cancelorder = 0 AND
z.respmsg LIKE 'Approved%' AND z.shipregion <> 'PENDING'
GROUP BY a.invoice HAVING lineitems = shipped
OR
SELECT a.invoice, a.shipregion, a.lineitems, a.shipped
FROM (SELECT a.invoice, z.shipregion, COUNT(a.invoice) AS lineitems,
SUM(CASE WHEN a.ship_date IS NOT NULL THEN 1 ELSE 0 END) AS shipped
FROM order_lineitem a
LEFT JOIN orders z ON a.invoice = z.invoice AND z.result = 0 AND z.cancelorder = 0 AND
z.respmsg LIKE 'Approved%' AND z.shipregion <> 'PENDING'
GROUP BY a.invoice
) AS a WHERE a.lineitems = a.shipped
One more outer query needed.
select * from
(
\\Your whole query here
) as Result
where Result.totalitems = Result.totalshipped