我有一个包含以下列的表:
ProductName copmanyname arianame bno mrp exp_date Date qty
DANZEN DS HELIX PHARMA CITY 1 J026 215 01-Feb-16 30-Oct-19 41
DANZEN DS HELIX PHARMA CITY 2 J026 215 01-Feb-16 30-Aug-19 2
HIPRO HELIX PHARMA CITY 1 J035 225 01-Feb-16 30-Nov-18 20
NOGARD HELIX PHARMA CITY 1 J010 135 01-Feb-16 30-Nov-20 2
NOGARD HELIX PHARMA CITY 2 J010 135 01-Feb-16 30-Nov-20 8
NOGARD HELIX PHARMA TANK J004 135 01-Feb-16 30-May-20 1
ALINAMIN F HELIX PHARMA CITY 1 I002 195 02-Feb-16 30-Sep-19 2
ALINAMIN F HELIX PHARMA CITY 2 H003 195 02-Feb-16 30-Nov-18 1
我想在特定日期显示记录,哪个记录具有相同的产品和公司以及bno和mrp,然后是这些记录的数量总和。例如在上表中:
ProductName copmanyname arianame bno mrp exp_date Date qty
NOGARD HELIX PHARMA CITY 1 J010 135 01-Feb-16 30-Nov-20 30
我尝试了以下声明,但它没有总结数量,显示所有记录。
SELECT
ProductName, CopmanyName, AriaName, bno, mrp, exp_date,
Sum(quantity) AS qty
FROM
q_saledetail
GROUP BY
ProductName, CopmanyName, AriaName, bno, mrp, exp_date,date
WHERE
date = any date
答案 0 :(得分:0)
使其他评论明确:
ProductName copmanyname arianame bno mrp exp_date Date qty
DANZEN DS HELIX PHARMA CITY 1 J026 215 01-Feb-16 30-Oct-19 41
DANZEN DS HELIX PHARMA CITY 2 J026 215 01-Feb-16 30-Aug-19 2
^ can you see the different arianame? ^^^ date??
HIPRO HELIX PHARMA CITY 1 J035 225 01-Feb-16 30-Nov-18 20
NOGARD HELIX PHARMA CITY 1 J010 135 01-Feb-16 30-Nov-20 2
NOGARD HELIX PHARMA CITY 2 J010 135 01-Feb-16 30-Nov-20 8
NOGARD HELIX PHARMA TANK J004 135 01-Feb-16 30-May-20 1
^ again ^^^^
ALINAMIN F HELIX PHARMA CITY 1 I002 195 02-Feb-16 30-Sep-19 2
ALINAMIN F HELIX PHARMA CITY 2 H003 195 02-Feb-16 30-Nov-18 1
^ and ^^^^ ^^^^^^^^^
如果你看看NOGARD:你有3个不同的行 - 它们不能分组。如果你省略了arianame和bno - 那么你可以将NOGARD分组到10 ...
答案 1 :(得分:0)
您没有告诉我们您使用的数据库(我希望MySQL);
然后你的查询中有很多错误,也可能在你的算法中。
这是我认为您可以照顾的:
MySQL 5.6架构设置:
CREATE TABLE q_saledetail
(`ProductName` varchar(10), `copmanyname` varchar(12), `arianame` varchar(6), `bno` varchar(4), `mrp` int, `exp_date` varchar(9), `Date` varchar(9), `qty` int)
;
INSERT INTO q_saledetail
(`ProductName`, `copmanyname`, `arianame`, `bno`, `mrp`, `exp_date`, `Date`, `qty`)
VALUES
('DANZEN DS', 'HELIX PHARMA', 'CITY 1', 'J026', 215, '01-Feb-16', '30-Oct-19', 41),
('DANZEN DS', 'HELIX PHARMA', 'CITY 2', 'J026', 215, '01-Feb-16', '30-Aug-19', 2),
('HIPRO', 'HELIX PHARMA', 'CITY 1', 'J035', 225, '01-Feb-16', '30-Nov-18', 20),
('NOGARD', 'HELIX PHARMA', 'CITY 1', 'J010', 135, '01-Feb-16', '30-Nov-20', 2),
('NOGARD', 'HELIX PHARMA', 'CITY 2', 'J010', 135, '01-Feb-16', '30-Nov-20', 8),
('NOGARD', 'HELIX PHARMA', 'TANK', 'J004', 135, '01-Feb-16', '30-May-20', 1),
('ALINAMIN F', 'HELIX PHARMA', 'CITY 1', 'I002', 195, '02-Feb-16', '30-Sep-19', 2),
('ALINAMIN F', 'HELIX PHARMA', 'CITY 2', 'H003', 195, '02-Feb-16', '30-Nov-18', 1)
;
查询1 :
SELECT
ProductName, copmanyname, arianame, bno, mrp, exp_date,`Date`,
Sum(qty) AS quantity
FROM
q_saledetail
WHERE
`Date` = '30-Nov-20'
GROUP BY
ProductName, copmanyname, arianame, bno, mrp, exp_date,`Date`
<强> Results 强>:
| ProductName | copmanyname | arianame | bno | mrp | exp_date | Date | quantity |
|-------------|--------------|----------|------|-----|-----------|-----------|----------|
| NOGARD | HELIX PHARMA | CITY 1 | J010 | 135 | 01-Feb-16 | 30-Nov-20 | 2 |
| NOGARD | HELIX PHARMA | CITY 2 | J010 | 135 | 01-Feb-16 | 30-Nov-20 | 8 |
查询2 :
-- or if you want to group by `arianame` too
SELECT
ProductName, copmanyname, GROUP_CONCAT(arianame), bno, mrp, exp_date,`Date`,
Sum(qty) AS quantity
FROM
q_saledetail
WHERE
`Date` = '30-Nov-20'
GROUP BY
ProductName, copmanyname, bno, mrp, exp_date,`Date`
<强> Results 强>:
| ProductName | copmanyname | GROUP_CONCAT(arianame) | bno | mrp | exp_date | Date | quantity |
|-------------|--------------|------------------------|------|-----|-----------|-----------|----------|
| NOGARD | HELIX PHARMA | CITY 1,CITY 2 | J010 | 135 | 01-Feb-16 | 30-Nov-20 | 10 |
答案 2 :(得分:0)
您问题中的查询与您要求的结果大不相同。此外,您所需的输出似乎已经过量了。
如果你想要数量的总和,其中Productname,companyname,bno和mrp是相同的,那么按exp_date和date分组是错误的。
如果您查看此SQL Fiddle,我会按随机日期过滤,然后按您提到的4列进行分组。
SELECT
ProductName, companyName, bno, mrp, SUM(qty) as Quantity
FROM
FiddleTable
WHERE [date] = '30-Nov-20'
GROUP BY
ProductName, companyName, bno, mrp